KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
fs.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 kos/fs.h
4 Copyright (C) 2000, 2001, 2002, 2003 Megan Potter
5 Copyright (C) 2012, 2013, 2014, 2016 Lawrence Sebald
6
7*/
8
9/** \file kos/fs.h
10 \brief Virtual filesystem support.
11 \ingroup vfs_generic
12
13 This file contains the interface to the virtual filesystem (VFS) of KOS. The
14 functions defined in this file make up the base of the filesystem operations
15 that can be performed by programs. The functions in here are abstracted by
16 various other layers in libc, and shouldn't be necessarily used (for
17 portability reasons). However, if you want only to interact with KOS in your
18 programs, feel free to use them to your heart's content!
19
20 \author Megan Potter
21 \author Lawrence Sebald
22*/
23
24#ifndef __KOS_FS_H
25#define __KOS_FS_H
26
27#include <sys/cdefs.h>
28__BEGIN_DECLS
29
30#include <sys/types.h>
31#include <kos/limits.h>
32#include <kos/opts.h>
33#include <time.h>
34#include <sys/queue.h>
35#include <stdarg.h>
36#include <sys/stat.h>
37
38#include <kos/nmmgr.h>
39
40/** \defgroup vfs_generic Generic
41 \brief Generic, uniform access to a virtual filesystems
42 \ingroup vfs
43 @{
44*/
45
46/** \brief Directory entry.
47
48 All VFS handlers must conform to this interface in their directory entries.
49
50 \headerfile kos/fs.h
51*/
52typedef struct kos_dirent {
53 int size; /**< \brief Size of the file in bytes. */
54 char name[NAME_MAX]; /**< \brief Name of the file. */
55 time_t time; /**< \brief Last access/mod/change time (depends on VFS) */
56 uint32 attr; /**< \brief Attributes of the file. */
57} dirent_t;
58
59/* Forward declaration */
60struct vfs_handler;
61
62/* stat_t.unique */
63/** \brief stat_t.unique: Constant to use denoting file has no unique ID */
64#define STAT_UNIQUE_NONE 0
65
66/* stat_t.type */
67/** \brief stat_t.type: Unknown / undefined / not relevant */
68#define STAT_TYPE_NONE 0
69
70/** \brief stat_t.type: Standard file */
71#define STAT_TYPE_FILE 1
72
73/** \brief stat_t.type: Standard directory */
74#define STAT_TYPE_DIR 2
75
76/** \brief stat_t.type: A virtual device of some sort (pipe, socket, etc) */
77#define STAT_TYPE_PIPE 3
78
79/** \brief stat_t.type: Meta data */
80#define STAT_TYPE_META 4
81
82/** \brief stat_t.type: Symbolic link */
83#define STAT_TYPE_SYMLINK 5
84
85/* stat_t.attr */
86#define STAT_ATTR_NONE 0x00 /**< \brief stat_t.attr: No attributes */
87#define STAT_ATTR_R 0x01 /**< \brief stat_t.attr: Read-capable */
88#define STAT_ATTR_W 0x02 /**< \brief stat_t.attr: Write-capable */
89
90/** \brief stat_t.attr: Read/Write capable */
91#define STAT_ATTR_RW (STAT_ATTR_R | STAT_ATTR_W)
92
93/** \brief File descriptor type */
94typedef int file_t;
95
96/** \brief Invalid file handle constant (for open failure, etc) */
97#define FILEHND_INVALID ((file_t)-1)
98
99/** \brief VFS handler interface.
100
101 All VFS handlers must implement this interface.
102
103 \headerfile kos/fs.h
104*/
105typedef struct vfs_handler {
106 /** \brief Name manager handler header */
108
109 /* Some VFS-specific pieces */
110 /** \brief Allow VFS caching; 0=no, 1=yes */
111 int cache;
112 /** \brief Pointer to private data for the handler */
113 void *privdata;
114
115 /** \brief Open a file on the given VFS; return a unique identifier */
116 void *(*open)(struct vfs_handler *vfs, const char *fn, int mode);
117
118 /** \brief Close a previously opened file */
119 int (*close)(void *hnd);
120
121 /** \brief Read from a previously opened file */
122 ssize_t (*read)(void *hnd, void *buffer, size_t cnt);
123
124 /** \brief Write to a previously opened file */
125 ssize_t (*write)(void *hnd, const void *buffer, size_t cnt);
126
127 /** \brief Seek in a previously opened file */
128 off_t (*seek)(void *hnd, off_t offset, int whence);
129
130 /** \brief Return the current position in a previously opened file */
131 off_t (*tell)(void *hnd);
132
133 /** \brief Return the total size of a previously opened file */
134 size_t (*total)(void *hnd);
135
136 /** \brief Read the next directory entry in a directory opened with O_DIR */
137 dirent_t *(*readdir)(void *hnd);
138
139 /** \brief Execute a device-specific call on a previously opened file */
140 int (*ioctl)(void *hnd, int cmd, va_list ap);
141
142 /** \brief Rename/move a file on the given VFS */
143 int (*rename)(struct vfs_handler *vfs, const char *fn1, const char *fn2);
144
145 /** \brief Delete a file from the given VFS */
146 int (*unlink)(struct vfs_handler *vfs, const char *fn);
147
148 /** \brief "Memory map" a previously opened file */
149 void *(*mmap)(void *fd);
150
151 /** \brief Perform an I/O completion (async I/O) for a previously opened
152 file */
153 int (*complete)(void *fd, ssize_t *rv);
154
155 /** \brief Get status information on a file on the given VFS
156 \note path will not be passed through realpath() before calling the
157 filesystem-level function. It is also important to not call
158 realpath() in any implementation of this function as it is
159 possible that realpath() will call this function. */
160 int (*stat)(struct vfs_handler *vfs, const char *path, struct stat *buf,
161 int flag);
162
163 /** \brief Make a directory on the given VFS */
164 int (*mkdir)(struct vfs_handler *vfs, const char *fn);
165
166 /** \brief Remove a directory from the given VFS */
167 int (*rmdir)(struct vfs_handler *vfs, const char *fn);
168
169 /** \brief Manipulate file control flags on the given file */
170 int (*fcntl)(void *fd, int cmd, va_list ap);
171
172 /** \brief Check if an event is pending on the given file */
173 short (*poll)(void *fd, short events);
174
175 /** \brief Create a hard link */
176 int (*link)(struct vfs_handler *vfs, const char *path1, const char *path2);
177
178 /** \brief Create a symbolic link */
179 int (*symlink)(struct vfs_handler *vfs, const char *path1,
180 const char *path2);
181
182 /* 64-bit file access functions. Generally, you should only define one of
183 the 64-bit or 32-bit versions of these functions. */
184
185 /** \brief Seek in a previously opened file (64-bit offsets) */
186 _off64_t (*seek64)(void *hnd, _off64_t offset, int whence);
187
188 /** \brief Return the current position in an opened file (64-bit offset) */
189 _off64_t (*tell64)(void *hnd);
190
191 /** \brief Return the size of an opened file as a 64-bit integer */
192 uint64 (*total64)(void *hnd);
193
194 /** \brief Read the value of a symbolic link
195 \note path will not be passed through realpath() before calling the
196 filesystem-level function. It is also important to not call
197 realpath() in any implementation of this function as it is
198 possible that realpath() will call this function. */
199 ssize_t (*readlink)(struct vfs_handler *vfs, const char *path, char *buf,
200 size_t bufsize);
201
202 /** \brief Rewind a directory stream to the start */
203 int (*rewinddir)(void *hnd);
204
205 /** \brief Get status information on an already opened file. */
206 int (*fstat)(void *hnd, struct stat *st);
208
209/** \cond */
210/* This is the private struct that will be used as raw file handles
211 underlying descriptors. */
212struct fs_hnd;
213
214/* The kernel-wide file descriptor table. These will reference to open files. */
215extern struct fs_hnd *fd_table[FD_SETSIZE];
216/** \endcond */
217
218/* Open modes */
219#include <sys/fcntl.h>
220
221/** \anchor vfs_fopen_modes
222 \name File Open Modes
223 @{
224*/
225#define O_MODE_MASK 0x0f /**< \brief Mask for mode numbers */
226//#define O_TRUNC 0x0100 /* Truncate */
227#define O_ASYNC 0x0200 /**< \brief Open for asynchronous I/O */
228//#define O_NONBLOCK 0x0400 /* Open for non-blocking I/O */
229#define O_DIR 0x1000 /**< \brief Open as directory */
230#define O_META 0x2000 /**< \brief Open as metadata */
231/** @} */
232
233/** \anchor vfs_seek_modes
234 \name Seek Modes
235
236 These are the values you can pass for the whence parameter to fs_seek().
237
238 @{
239*/
240#define SEEK_SET 0 /**< \brief Set position to offset. */
241#define SEEK_CUR 1 /**< \brief Seek from current position. */
242#define SEEK_END 2 /**< \brief Seek from end of file. */
243/** @} */
244
245/* Standard file descriptor functions */
246/** \brief Open a file on the VFS.
247
248 This function opens the specified file, returning a new file descriptor to
249 access the file.
250
251 \param fn The path to open.
252 \param mode The mode to use with opening the file. This may
253 include the standard open modes (O_RDONLY, O_WRONLY,
254 etc), as well as values from the \ref vfs_fopen_modes
255 "File Open Modes" list. Multiple values can be ORed
256 together.
257
258 \return The new file descriptor on success, -1 on error.
259*/
260file_t fs_open(const char *fn, int mode);
261
262/** \brief Close an opened file.
263
264 This function closes the specified file descriptor, releasing all resources
265 associated with the descriptor.
266
267 \param hnd The file descriptor to close.
268
269 \return 0 for success, -1 for error
270*/
272
273/** \brief Read from an opened file.
274
275 This function reads into the specified buffer from the file at its current
276 file pointer.
277
278 \param hnd The file descriptor to read from.
279 \param buffer The buffer to read into.
280 \param cnt The size of the buffer (or the number of bytes
281 requested).
282
283 \return The number of bytes read, or -1 on error. Note that
284 this may not be the full number of bytes requested.
285*/
286ssize_t fs_read(file_t hnd, void *buffer, size_t cnt);
287
288/** \brief Write to an opened file.
289
290 This function writes the specified buffer into the file at the current file
291 pointer.
292
293 \param hnd The file descriptor to write into.
294 \param buffer The data to write into the file.
295 \param cnt The size of the buffer, in bytes.
296
297 \return The number of bytes written, or -1 on failure. Note
298 that the number of bytes written may be less than
299 what was requested.
300*/
301ssize_t fs_write(file_t hnd, const void *buffer, size_t cnt);
302
303/** \brief Seek to a new position within a file.
304
305 This function moves the file pointer to the specified position within the
306 file (the base of this position is determined by the whence parameter).
307
308 \param hnd The file descriptor to move the pointer for.
309 \param offset The offset in bytes from the specified base.
310 \param whence The base of the pointer move. This should be one of
311 the \ref vfs_seek_modes "Seek Modes" values.
312
313 \return The new position of the file pointer.
314*/
315off_t fs_seek(file_t hnd, off_t offset, int whence);
316
317/** \brief Seek to a new position within a file (64-bit offsets).
318
319 This function moves the file pointer to the specified position within the
320 file (the base of this position is determined by the whence parameter).
321
322 \param hnd The file descriptor to move the pointer for.
323 \param offset The offset in bytes from the specified base.
324 \param whence The base of the pointer move. This should be one of
325 the \ref vfs_seek_modes "Seek Modes" values.
326
327 \return The new position of the file pointer.
328*/
329_off64_t fs_seek64(file_t hnd, _off64_t offset, int whence);
330
331/** \brief Retrieve the position of the pointer within a file.
332
333 This function retrieves the current location of the file pointer within an
334 opened file. This is an offset in bytes from the start of the file.
335
336 \param hnd The file descriptor to retrieve the pointer from.
337
338 \return The offset within the file for the pointer.
339*/
340off_t fs_tell(file_t hnd);
341
342/** \brief Retrieve the position of the 64-bit pointer within a file.
343
344 This function retrieves the current location of the file pointer within an
345 opened file. This is an offset in bytes from the start of the file.
346
347 \param hnd The file descriptor to retrieve the pointer from.
348
349 \return The offset within the file for the pointer.
350*/
352
353/** \brief Retrieve the length of an opened file.
354
355 This file retrieves the length of the file associated with the given file
356 descriptor.
357
358 \note size_t is unsigned, so the error return value is not
359 less than 0.
360
361 \param hnd The file descriptor to retrieve the size from.
362
363 \return The length of the file on success, -1 on failure.
364*/
365size_t fs_total(file_t hnd);
366
367/** \brief Retrieve the length of an opened file as a 64-bit integer.
368
369 This file retrieves the length of the file associated with the given file
370 descriptor.
371
372 \note uint64 is unsigned, so the error return value is not
373 less than 0.
374
375 \param hnd The file descriptor to retrieve the size from.
376
377 \return The length of the file on success, -1 on failure.
378*/
380
381
382/** \brief Read an entry from an opened directory.
383
384 This function reads the next entry from the directory specified by the given
385 file descriptor.
386
387 \param hnd The opened directory's file descriptor.
388
389 \return The next entry, or NULL on failure.
390*/
392
393/** \brief Execute a device-specific command on a file descriptor.
394
395 The types and formats of the commands are device/filesystem specific, and
396 are not documented here. Each filesystem may define any commands that are
397 specific to it with its implementation of this function.
398
399 \param hnd The file descriptor to use.
400 \param cmd The command to run.
401 \param ... Arguments for the command specified.
402
403 \return -1 on error.
404*/
405int fs_ioctl(file_t hnd, int cmd, ...);
406
407/** \brief Rename the specified file to the given filename.
408
409 This function renames the file specified by the first argument to the second
410 argument. The two paths should be on the same filesystem.
411
412 \param fn1 The existing file to rename.
413 \param fn2 The new filename to rename to.
414
415 \return 0 on success, -1 on failure.
416*/
417int fs_rename(const char *fn1, const char *fn2);
418
419/** \brief Delete the specified file.
420
421 This function deletes the specified file from the filesystem. This should
422 only be used for files, not for directories. For directories, use fs_rmdir()
423 instead of this function.
424
425 \param fn The path to remove.
426
427 \return 0 on success, -1 on failure.
428*/
429int fs_unlink(const char *fn);
430
431/** \brief Change the current working directory of the current thread.
432
433 This function changes the current working directory for the current thread.
434 Any relative paths passed into file-related functions will be relative to
435 the path that is changed to.
436
437 \param fn The path to set as the current working directory.
438
439 \return 0 on success, -1 on failure.
440*/
441int fs_chdir(const char *fn);
442
443/** \brief Memory-map a previously opened file.
444
445 This file "maps" the opened file into memory, reading the whole file into a
446 buffer, and returning that buffer. The returned buffer should not be freed,
447 as it will be freed when the file is closed. Bytes written into the buffer,
448 up to the original length of the file, will be written back to the file when
449 it is closed, assuming that the file is opened for writing.
450
451 \note Some of the filesystems in KallistiOS do not support
452 this operation. If you attempt to use this function
453 on a filesystem that does not support it, the
454 function will return NULL and set errno to EINVAL.
455
456 \param hnd The descriptor to memory map.
457
458 \return The memory mapped buffer, or NULL on failure.
459*/
460void *fs_mmap(file_t hnd);
461
462/** \brief Perform an I/O completion on the given file descriptor.
463
464 This function is used with asynchronous I/O to perform an I/O completion on
465 the given file descriptor.
466
467 \note Most of the filesystems in KallistiOS do not support
468 this operation. If you attempt to use this function
469 on a filesystem that does not support it, the
470 function will return -1 and set errno to EINVAL.
471
472 \param fd The descriptor to complete I/O on.
473 \param rv A buffer to store the size of the I/O in.
474
475 \return 0 on success, -1 on failure.
476*/
477int fs_complete(file_t fd, ssize_t *rv);
478
479/** \brief Create a directory.
480
481 This function creates the specified directory, if possible.
482
483 \param fn The path of the directory to create.
484 \return 0 on success, -1 on failure.
485*/
486int fs_mkdir(const char *fn);
487
488/** \brief Remove a directory by name.
489
490 This function removes the specified directory. The directory shall only be
491 removed if it is empty.
492
493 \param fn The path of the directory to remove.
494
495 \return 0 on success, -1 on failure.
496*/
497int fs_rmdir(const char *fn);
498
499/** \brief Manipulate file control flags.
500
501 This function implements the standard C fcntl function.
502
503 \param fd The file descriptor to use.
504 \param cmd The command to run.
505 \param ... Arguments for the command specified.
506
507 \return -1 on error (generally).
508*/
509int fs_fcntl(file_t fd, int cmd, ...);
510
511/** \brief Create a hard link.
512
513 This function implements the POSIX function link(), which creates a hard
514 link for an existing file.
515
516 \note Most filesystems in KallistiOS do not support hard
517 links. If you call this function on a filesystem
518 that does not support hard links, the function will
519 return -1 and set errno to EMLINK.
520
521 \param path1 An existing file to create a new link to.
522 \param path2 The pathname of the new link to be created.
523
524 \return 0 on success, -1 on failure.
525*/
526int fs_link(const char *path1, const char *path2);
527
528/** \brief Create a symbolic link.
529
530 This function implements the POSIX function symlink(), which creates a
531 symbolic link on the filesystem. Symbolic links are not required to point to
532 an existing file (per POSIX) and may result in circular links if care is not
533 taken. For now, symbolic links cannot cross filesystem boundaries in KOS.
534
535 \note Most filesystems in KallistiOS do not support
536 symbolic links. Filesystems that do not support
537 symlinks will simply set errno to ENOSYS and return
538 -1.
539
540 \param path1 The content of the link (i.e, what to point at).
541 \param path2 The pathname of the new link to be created.
542
543 \return 0 on success, -1 on failure.
544*/
545int fs_symlink(const char *path1, const char *path2);
546
547/** \brief Read the value of a symbolic link.
548
549 This function implements the POSIX function readlink(), which simply reads
550 the value of the symbolic link at the end of a path. This does not resolve
551 any internal links and it does not canonicalize the path either.
552
553 \note Most filesystems in KallistiOS do not support
554 symbolic links. Filesystems that do not support
555 symlinks will simply set errno to ENOSYS and return
556 -1.
557
558 \param path The symbolic link to read.
559 \param buf The buffer to place the link's contents in.
560 \param bufsize The number of bytes allocated to buf.
561
562 \return -1 on failure, the number of bytes placed into buf
563 on success. If the return value is equal to bufsize,
564 you may not have the whole link -- provide a larger
565 buffer and try again.
566*/
567ssize_t fs_readlink(const char *path, char *buf, size_t bufsize);
568
569/** \brief Retrieve information about the specified path.
570
571 This function retrieves status information on the given path. This function
572 now returns the normal POSIX-style struct stat, rather than the old KOS
573 stat_t structure. In addition, you can specify whether or not this function
574 should resolve symbolic links on filesystems that support symlinks.
575
576 \param path The path to retrieve information about.
577 \param buf The buffer to store stat information in.
578 \param flag Specifies whether or not to resolve a symbolic link.
579 If you don't want to resolve any symbolic links at
580 the end of the path, pass AT_SYMLINK_NOFOLLOW,
581 otherwise pass 0.
582
583 \return 0 on success, -1 on failure.
584*/
585int fs_stat(const char *path, struct stat *buf, int flag);
586
587/** \brief Rewind a directory to the start.
588
589 This function rewinds the position of a directory stream to the beginning of
590 the directory.
591
592 \note Some filesystems may not support this function. If a
593 filesystem doesn't support it, errno will be set to
594 ENOSYS and -1 will be returned.
595
596 \param hnd The opened directory's file descriptor.
597
598 \return 0 on success, -1 on failure.
599*/
601
602/** \brief Retrieve information about an opened file.
603
604 This function retrieves status information on the given file descriptor,
605 which must correspond to an already opened file.
606
607 \note Some filesystems may not support this function. If a
608 filesystem doesn't support it, errno will be set to
609 ENOSYS and -1 will be returned.
610
611 \param hnd The file descriptor to retrieve information about.
612 \param buf The buffer to store stat information in.
613
614 \return 0 on success, -1 on failure.
615*/
616int fs_fstat(file_t hnd, struct stat *buf);
617
618/** \brief Duplicate a file descriptor.
619
620 This function duplicates the specified file descriptor, returning a new file
621 descriptor that can be used to access the file. This is equivalent to the
622 standard POSIX function dup().
623
624 \param oldfd The old file descriptor to duplicate.
625
626 \return The new file descriptor on success, -1 on failure.
627*/
629
630/** \brief Duplicate a file descriptor onto the specified descriptor.
631
632 This function duplicates the specified file descriptor onto the other file
633 descriptor provided. If the newfd parameter represents an open file, that
634 file will be closed before the old descriptor is duplicated onto it. This is
635 equivalent to the standard POSIX function dup2().
636
637 \param oldfd The old file descriptor to duplicate.
638 \param newfd The descriptor to copy into.
639
640 \return The new file descriptor on success, -1 on failure.
641*/
643
644/** \brief Create a "transient" file descriptor.
645
646 This function creates and opens a new file descriptor that isn't associated
647 directly with a file on the filesystem. This is used internally to actually
648 open files, and should (in general) not be called by user code. Effectively,
649 if you're trying to implement your own filesystem handler in your code, you
650 may need this function, otherwise you should just ignore it.
651
652 \param vfs The VFS handler structure to use for the file.
653 \param hnd Internal handle data for the file.
654
655 \return The opened descriptor on success, -1 on failure.
656*/
658
659/** \brief Retrieve the VFS Handler for a file descriptor.
660
661 This function retrieves the Handler structure for the VFS of the specified
662 file descriptor. There is generally no reason to call this function in user
663 code, as it is meant for use internally.
664
665 \param fd The file descriptor to retrieve the handler for.
666
667 \return The VFS' handler structure.
668*/
670
671/** \brief Retrieve the internal handle for a file descriptor.
672
673 This function retrieves the internal file handle data of the specified file
674 descriptor. There is generally no reason to call this function in user code,
675 as it is meant for use internally.
676
677 \param fd The file descriptor to retrieve the handler for.
678
679 \return The internal handle for the file descriptor.
680*/
682
683/** \brief Get the current working directory of the running thread.
684
685 \return The current working directory.
686*/
687const char *fs_getwd(void);
688
689/* Couple of util functions */
690
691/** \brief Copy a file.
692
693 This function copies the file at src to dst on the filesystem.
694
695 \param src The filename to copy from.
696 \param dst The filename to copy to.
697
698 \return The number of bytes copied successfully.
699*/
700ssize_t fs_copy(const char *src, const char *dst);
701
702/** \brief Open and read a whole file into RAM.
703
704 This function opens the specified file, reads it into memory (allocating the
705 necessary space with malloc), and closes the file. The caller is responsible
706 for freeing the memory when they are done with it.
707
708 \param src The filename to open and read.
709 \param out_ptr A pointer to the buffer on success, NULL otherwise.
710
711 \return The size of the file on success, -1 otherwise.
712*/
713ssize_t fs_load(const char *src, void **out_ptr);
714
715/** \brief Append a path component to a string.
716
717 This function acts mostly like the function strncat(), with a few slight
718 differences. First, if the destination string doesn't end in a '/'
719 character, this function will add it. Second, it returns the length of the
720 resulting string, including the NUL terminator. Finally, no modification of
721 the destination string will occur if there isn't enough space left in the
722 string to do so.
723
724 \param dst The string to modify.
725 \param src The path component to append.
726 \param len The length allocated for dst.
727
728 \return The length of the new string (including the NUL
729 terminator) on success, -1 otherwise.
730
731 \par Error Conditions:
732 \em EFAULT - src or dst is a NULL pointer \n
733 \em EINVAL - len is zero \n
734 \em ENAMETOOLONG - the resulting path would be longer than len bytes \n
735*/
736ssize_t fs_path_append(char *dst, const char *src, size_t len);
737
738/** \brief Normalize the specified path.
739 This function acts mostly like the function realpath() but it only simplifies
740 a path by resolving . and .. components and removing redundant slashes. It
741 doesn't check if the path exists or resolve symbolic links.
742 \param path The path to normalize.
743 \param resolved The buffer to store resolved normalized path. It has
744 to be PATH_MAX bytes in size.
745
746 \return A pointer to the normalized path on success,
747 or NULL on failure, in which case the path which
748 caused trouble is left in resolved.
749 \par Error Conditions:
750 \em EINVAL - path or resolved is a NULL pointer \n
751 \em ENAMETOOLONG - the resulting path would be longer than PATH_MAX bytes \n
752*/
753char *fs_normalize_path(const char *__RESTRICT path, char *__RESTRICT resolved);
754
755/** \brief Initialize the virtual filesystem.
756
757 This is normally done for you by default when KOS starts. In general, there
758 should be no reason for you to call this function.
759
760 \retval 0 On success.
761*/
762int fs_init(void);
763
764/** \brief Shut down the virtual filesystem.
765
766 This is done for you by the normal shutdown procedure of KOS. There should
767 not really be any reason for you to call this function yourself.
768*/
769void fs_shutdown(void);
770
771/** @} */
772
773__END_DECLS
774
775#endif /* __KOS_FS_H */
#define FD_SETSIZE
The number of distinct file descriptors, including files and network sockets, that can be in use at a...
Definition opts.h:136
#define __RESTRICT
Definition cdefs.h:176
#define NAME_MAX
Max filename length.
Definition limits.h:25
unsigned long long uint64
64-bit unsigned integer
Definition types.h:32
long long _off64_t
64-bit file offset type.
Definition _types.h:46
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
int poll(struct pollfd fds[], nfds_t nfds, int timeout)
Poll a group of file descriptors for activity.
ssize_t fs_write(file_t hnd, const void *buffer, size_t cnt)
Write to an opened file.
int fs_rmdir(const char *fn)
Remove a directory by name.
file_t fs_open_handle(vfs_handler_t *vfs, void *hnd)
Create a "transient" file descriptor.
int fs_init(void)
Initialize the virtual filesystem.
size_t fs_total(file_t hnd)
Retrieve the length of an opened file.
int fs_rewinddir(file_t hnd)
Rewind a directory to the start.
file_t fs_open(const char *fn, int mode)
Open a file on the VFS.
dirent_t * fs_readdir(file_t hnd)
Read an entry from an opened directory.
int fs_chdir(const char *fn)
Change the current working directory of the current thread.
off_t fs_tell(file_t hnd)
Retrieve the position of the pointer within a file.
const char * fs_getwd(void)
Get the current working directory of the running thread.
_off64_t fs_tell64(file_t hnd)
Retrieve the position of the 64-bit pointer within a file.
vfs_handler_t * fs_get_handler(file_t fd)
Retrieve the VFS Handler for a file descriptor.
ssize_t fs_load(const char *src, void **out_ptr)
Open and read a whole file into RAM.
void * fs_mmap(file_t hnd)
Memory-map a previously opened file.
int fs_link(const char *path1, const char *path2)
Create a hard link.
int fs_close(file_t hnd)
Close an opened file.
void * fs_get_handle(file_t fd)
Retrieve the internal handle for a file descriptor.
int fs_fstat(file_t hnd, struct stat *buf)
Retrieve information about an opened file.
int fs_rename(const char *fn1, const char *fn2)
Rename the specified file to the given filename.
ssize_t fs_readlink(const char *path, char *buf, size_t bufsize)
Read the value of a symbolic link.
void fs_shutdown(void)
Shut down the virtual filesystem.
int fs_symlink(const char *path1, const char *path2)
Create a symbolic link.
int fs_fcntl(file_t fd, int cmd,...)
Manipulate file control flags.
int fs_ioctl(file_t hnd, int cmd,...)
Execute a device-specific command on a file descriptor.
int fs_unlink(const char *fn)
Delete the specified file.
file_t fs_dup(file_t oldfd)
Duplicate a file descriptor.
off_t fs_seek(file_t hnd, off_t offset, int whence)
Seek to a new position within a file.
ssize_t fs_read(file_t hnd, void *buffer, size_t cnt)
Read from an opened file.
_off64_t fs_seek64(file_t hnd, _off64_t offset, int whence)
Seek to a new position within a file (64-bit offsets).
ssize_t fs_copy(const char *src, const char *dst)
Copy a file.
uint64 fs_total64(file_t hnd)
Retrieve the length of an opened file as a 64-bit integer.
int file_t
File descriptor type.
Definition fs.h:94
int fs_stat(const char *path, struct stat *buf, int flag)
Retrieve information about the specified path.
ssize_t fs_path_append(char *dst, const char *src, size_t len)
Append a path component to a string.
file_t fs_dup2(file_t oldfd, file_t newfd)
Duplicate a file descriptor onto the specified descriptor.
int fs_complete(file_t fd, ssize_t *rv)
Perform an I/O completion on the given file descriptor.
int fs_mkdir(const char *fn)
Create a directory.
char * fs_normalize_path(const char *__RESTRICT path, char *__RESTRICT resolved)
Normalize the specified path.
void rewinddir(DIR *dir)
Rewind a directory stream to the start of the directory.
#define ioctl
Definition ioctl.h:39
Limits.
Name manager.
Compile-time options regarding debugging and other topics.
Directory entry.
Definition fs.h:52
uint32 attr
Attributes of the file.
Definition fs.h:56
time_t time
Last access/mod/change time (depends on VFS)
Definition fs.h:55
int size
Size of the file in bytes.
Definition fs.h:53
Name handler interface.
Definition nmmgr.h:61
VFS handler interface.
Definition fs.h:105
void * privdata
Pointer to private data for the handler.
Definition fs.h:113
nmmgr_handler_t nmmgr
Name manager handler header.
Definition fs.h:107
int cache
Allow VFS caching; 0=no, 1=yes.
Definition fs.h:111
KOS-implementation of select C11 and POSIX extensions.