µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches

µOS++ POSIX I/O C++ functions. More...

int os::posix::mkdir (const char *path, mode_t mode)
 
int os::posix::rmdir (const char *path)
 
void os::posix::sync (void)
 
int os::posix::chmod (const char *path, mode_t mode)
 
int os::posix::stat (const char *path, struct stat *buf)
 
int os::posix::truncate (const char *path, off_t length)
 
int os::posix::rename (const char *existing, const char *_new)
 
int os::posix::unlink (const char *path)
 
int os::posix::utime (const char *path, const utimbuf *times)
 
int os::posix::statvfs (const char *path, struct statvfs *buf)
 
directoryos::posix::opendir (const char *dirname)
 Open directory.
 
ioos::posix::open (const char *path, int oflag,...)
 
ioos::posix::vopen (const char *path, int oflag, std::va_list args)
 
socketos::posix::socket (int domain, int type, int protocol)
 

Detailed Description

This page groups the functions providing support for standard POSIX I/O operations.x

TODO: add content

Function Documentation

◆ chmod()

int os::posix::chmod ( const char *  path,
mode_t  mode 
)

Definition at line 155 of file file-system.cpp.

156 {
157#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
158 trace::printf ("%s(\"%s\", %u)\n", __func__, path, mode);
159#endif
160
161 if (path == nullptr)
162 {
163 errno = EFAULT;
164 return -1;
165 }
166
167 if (*path == '\0')
168 {
169 errno = ENOENT;
170 return -1;
171 }
172
173 const char* adjusted_path = path;
174 auto* const fs = file_system::identify_mounted (&adjusted_path);
175
176 if (fs == nullptr)
177 {
178 errno = ENOENT;
179 return -1;
180 }
181
182 return fs->chmod (adjusted_path, mode);
183 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_chmod().

◆ mkdir()

int os::posix::mkdir ( const char *  path,
mode_t  mode 
)

Definition at line 62 of file file-system.cpp.

63 {
64#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
65 trace::printf ("%s(\"%s\", %u)\n", __func__, path, mode);
66#endif
67
68 if (path == nullptr)
69 {
70 errno = EFAULT;
71 return -1;
72 }
73
74 if (*path == '\0')
75 {
76 errno = ENOENT;
77 return -1;
78 }
79
80 auto adjusted_path = path;
81 auto* const fs = file_system::identify_mounted (&adjusted_path);
82
83 if (fs == nullptr)
84 {
85 errno = ENOENT;
86 return -1;
87 }
88
89 // Execute the implementation specific code.
90 return fs->mkdir (adjusted_path, mode);
91 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_mkdir().

◆ open()

io * os::posix::open ( const char *  path,
int  oflag,
  ... 
)

Definition at line 49 of file io.cpp.

50 {
51 // Forward to the variadic version of the function.
52 std::va_list args;
53 va_start (args, oflag);
54 io* const ret = vopen (path, oflag, args);
55 va_end (args);
56
57 return ret;
58 }
io * vopen(const char *path, int oflag, std::va_list args)
Definition io.cpp:66

References os::posix::vopen().

◆ opendir()

directory * os::posix::opendir ( const char *  dirname)
Parameters
dirname[in] Directory name.
Returns
Pointer to directory object.

Definition at line 380 of file file-system.cpp.

381 {
382#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
383 trace::printf ("%s(\"%s\")\n", __func__, dirpath);
384#endif
385
386 if (dirpath == nullptr)
387 {
388 errno = EFAULT;
389 return nullptr;
390 }
391
392 if (*dirpath == '\0')
393 {
394 errno = ENOENT;
395 return nullptr;
396 }
397
398 errno = 0;
399
401
402 while (true)
403 {
404 // Check if path is a device.
405 os::posix::io* io;
407 if (io != nullptr)
408 {
409 // Cannot list devices (for now).
410 return nullptr;
411 }
412
413 // Check if a regular folder.
414 auto adjusted_path = dirpath;
415 auto* const fs
417
418 // The manager will return null if there are no file systems
419 // registered, no need to check this condition separately.
420 if (fs == nullptr)
421 {
422 errno = EBADF;
423 return nullptr;
424 }
425
426 // Use the file system implementation to open the directory, using
427 // the adjusted path (mount point prefix removed).
428 dir = fs->opendir (adjusted_path);
429 if (dir == nullptr)
430 {
431 // Open failed.
432 return nullptr;
433 }
434
435 break;
436 }
437
438 // Return a valid pointer to an object derived from directory, or
439 // nullptr.
440
441#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
442 trace::printf ("%s(\"%s\")=%p\n", __func__, dirpath, dir);
443#endif
444 return dir;
445 }
static value_type * identify_device(const char *path)
Directory class.
Definition directory.h:75
static file_system * identify_mounted(const char **path1, const char **path2=nullptr)
Base I/O class.
Definition io.h:86

References os::posix::device_registry< T >::identify_device(), os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_opendir().

◆ rename()

int os::posix::rename ( const char *  existing,
const char *  _new 
)

Definition at line 254 of file file-system.cpp.

255 {
256#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
257 trace::printf ("%s(\"%s\",\"%s\")\n", __func__, existing, _new);
258#endif
259
260 if ((existing == nullptr) || (_new == nullptr))
261 {
262 errno = EFAULT;
263 return -1;
264 }
265
266 if ((*existing == '\0') || (*_new == '\0'))
267 {
268 errno = ENOENT;
269 return -1;
270 }
271
272 auto adjusted_existing = existing;
273 auto adjusted_new = _new;
274 auto* const fs
275 = file_system::identify_mounted (&adjusted_existing, &adjusted_new);
276
277 if (fs == nullptr)
278 {
279 errno = ENOENT;
280 return -1;
281 }
282
283 return fs->rename (adjusted_existing, adjusted_new);
284 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_rename().

◆ rmdir()

int os::posix::rmdir ( const char *  path)

Definition at line 94 of file file-system.cpp.

95 {
96#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
97 trace::printf ("%s(\"%s\")\n", __func__, path);
98#endif
99
100 if (path == nullptr)
101 {
102 errno = EFAULT;
103 return -1;
104 }
105
106 if (*path == '\0')
107 {
108 errno = ENOENT;
109 return -1;
110 }
111
112 auto adjusted_path = path;
113 auto* const fs = file_system::identify_mounted (&adjusted_path);
114
115 if (fs == nullptr)
116 {
117 errno = ENOENT;
118 return -1;
119 }
120
121 // Execute the implementation specific code.
122 return fs->rmdir (adjusted_path);
123 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_rmdir().

◆ socket()

class socket * os::posix::socket ( int  domain,
int  type,
int  protocol 
)

Definition at line 62 of file net-stack.cpp.

63 {
64 errno = 0;
65
66 // TODO: implement a way to identify the net stack.
67 net_stack* ns = nullptr;
68 class socket* sock = ns->socket (domain, type, protocol);
69 if (sock == nullptr)
70 {
71 return nullptr;
72 }
73 sock->alloc_file_descriptor ();
74 return sock;
75 }
int socket(int domain, int type, int protocol)

References os::posix::io::alloc_file_descriptor(), and os::posix::net_stack::socket().

◆ stat()

int os::posix::stat ( const char *  path,
struct stat *  buf 
)

Definition at line 186 of file file-system.cpp.

187 {
188#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
189 trace::printf ("%s(\"%s\", %p)\n", __func__, path, buf);
190#endif
191
192 if ((path == nullptr) || (buf == nullptr))
193 {
194 errno = EFAULT;
195 return -1;
196 }
197
198 if (*path == '\0')
199 {
200 errno = ENOENT;
201 return -1;
202 }
203
204 const char* adjusted_path = path;
205 auto* const fs = file_system::identify_mounted (&adjusted_path);
206
207 if (fs == nullptr)
208 {
209 errno = ENOENT;
210 return -1;
211 }
212
213 return fs->stat (adjusted_path, buf);
214 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_stat().

◆ statvfs()

int os::posix::statvfs ( const char *  path,
struct statvfs buf 
)

Definition at line 349 of file file-system.cpp.

350 {
351#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
352 trace::printf ("%s(\"%s\", %p)\n", __func__, path, buf);
353#endif
354
355 if ((path == nullptr) || (buf == nullptr))
356 {
357 errno = EFAULT;
358 return -1;
359 }
360
361 if (*path == '\0')
362 {
363 errno = ENOENT;
364 return -1;
365 }
366
367 auto adjusted_path = path;
368 auto* const fs = file_system::identify_mounted (&adjusted_path);
369
370 if (fs == nullptr)
371 {
372 errno = ENOENT;
373 return -1;
374 }
375
376 return fs->statvfs (buf);
377 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_statvfs().

◆ sync()

void os::posix::sync ( void  )

Definition at line 126 of file file-system.cpp.

127 {
128#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
129 trace::printf ("%s()\n", __func__);
130#endif
131
132 // Enumerate all mounted file systems and sync them.
133#pragma GCC diagnostic push
134#if defined(__clang__)
135#elif defined(__GNUC__)
136#pragma GCC diagnostic ignored "-Waggregate-return"
137#endif
138 for (auto&& fs : file_system::mounted_list__)
139#pragma GCC diagnostic pop
140 {
141 fs.sync ();
142 }
143
144 if (file_system::mounted_root__ != nullptr)
145 {
146 file_system::mounted_root__->sync ();
147 }
148 }

References os::trace::printf().

Referenced by __posix_sync().

◆ truncate()

int os::posix::truncate ( const char *  path,
off_t  length 
)

Definition at line 217 of file file-system.cpp.

218 {
219#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
220 trace::printf ("%s(\"%s\", %u)\n", __func__, path, length);
221#endif
222
223 if (path == nullptr)
224 {
225 errno = EFAULT;
226 return -1;
227 }
228
229 if (*path == '\0')
230 {
231 errno = ENOENT;
232 return -1;
233 }
234
235 const char* adjusted_path = path;
236 auto* const fs = file_system::identify_mounted (&adjusted_path);
237
238 if (fs == nullptr)
239 {
240 errno = ENOENT;
241 return -1;
242 }
243
244 if (length < 0)
245 {
246 errno = EINVAL;
247 return -1;
248 }
249
250 return fs->truncate (adjusted_path, length);
251 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_truncate().

◆ unlink()

int os::posix::unlink ( const char *  path)

Definition at line 287 of file file-system.cpp.

288 {
289#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
290 trace::printf ("%s(\"%s\")\n", __func__, path);
291#endif
292
293 if (path == nullptr)
294 {
295 errno = EFAULT;
296 return -1;
297 }
298
299 if (*path == '\0')
300 {
301 errno = ENOENT;
302 return -1;
303 }
304
305 auto adjusted_path = path;
306 auto* const fs = file_system::identify_mounted (&adjusted_path);
307
308 if (fs == nullptr)
309 {
310 errno = ENOENT;
311 return -1;
312 }
313
314 return fs->unlink (adjusted_path);
315 }

References os::posix::file_system::identify_mounted(), and os::trace::printf().

Referenced by __posix_unlink().

◆ utime()

int os::posix::utime ( const char *  path,
const utimbuf *  times 
)

Definition at line 318 of file file-system.cpp.

319 {
320#if defined(OS_TRACE_POSIX_IO_FILE_SYSTEM)
321 trace::printf ("%s(\"%s\", %p)\n", __func__, path, times);
322#endif
323
324 if ((path == nullptr) || (times == nullptr))
325 {
326 errno = EFAULT;
327 return -1;
328 }
329
330 if (*path == '\0')
331 {
332 errno = ENOENT;
333 return -1;
334 }
335
336 auto adjusted_path = path;
337 auto* const fs = file_system::identify_mounted (&adjusted_path);
338
339 if (fs == nullptr)
340 {
341 errno = ENOENT;
342 return -1;
343 }
344
345 return fs->utime (adjusted_path, times);
346 }
clock_t times(struct tms *buf)

References os::posix::file_system::identify_mounted(), os::trace::printf(), and times().

Referenced by __posix_utime().

◆ vopen()

io * os::posix::vopen ( const char *  path,
int  oflag,
std::va_list  args 
)

The actual open workhorse. Using the path, try to identify the io object, then call the implementation. If successful, allocate a new POSIX file descriptor, to be used by C functions.

Definition at line 66 of file io.cpp.

67 {
68#if defined(OS_TRACE_POSIX_IO_IO)
69 trace::printf ("io::%s(\"%s\")\n", __func__, path ? path : "");
70#endif
71
72 if (path == nullptr)
73 {
74 errno = EFAULT;
75 return nullptr;
76 }
77
78 if (*path == '\0')
79 {
80 errno = ENOENT;
81 return nullptr;
82 }
83
84 errno = 0;
85
86 os::posix::io* io;
87 while (true)
88 {
89 // Check if path is a device.
91 if (io != nullptr)
92 {
93 // If so, use the implementation to open the device.
94 int oret = static_cast<device*> (io)->vopen (path, oflag, args);
95 if (oret < 0)
96 {
97 // Open failed.
98 return nullptr;
99 }
100
101 // File descriptor already allocated by device.
102 break;
103 }
104
105 // Check if a regular file.
106 auto adjusted_path = path;
107 auto* const fs
109
110 // The manager will return null if there are no file systems
111 // registered, no need to check this condition separately.
112 if (fs == nullptr)
113 {
114 errno = EBADF;
115 return nullptr;
116 }
117
118 // Use the file system implementation to open the file, using
119 // the adjusted path (mount point prefix removed).
120 io = fs->vopen (adjusted_path, oflag, args);
121 if (io == nullptr)
122 {
123 // Open failed.
124 return nullptr;
125 }
126
127 break;
128 }
129
130 // Return a valid pointer to an object derived from io, or nullptr.
131
132#if defined(OS_TRACE_POSIX_IO_IO)
133 trace::printf ("io::%s(\"%s\")=%p fd=%d\n", __func__, path, io,
134 io->file_descriptor ());
135#endif
136 return io;
137 }
file_descriptor_t file_descriptor(void) const
Definition io.h:452

References os::posix::io::file_descriptor(), os::posix::device_registry< T >::identify_device(), os::posix::file_system::identify_mounted(), os::trace::printf(), and os::posix::vopen().

Referenced by __posix_open(), os::posix::open(), and os::posix::vopen().