File System APIs

File System Functions

int fs_open(ZFILE *zfp, const char *file_name)

File open.

Opens an existing file or create a new one and associates a stream with it.

Parameters
  • zfp: Pointer to file object
  • file_name: The name of file to open
Return Value
  • 0: Success
  • -ERRNO: errno code if error

int fs_close(ZFILE *zfp)

File close.

Flushes the associated stream and closes the file.

Parameters
  • zfp: Pointer to the file object
Return Value
  • 0: Success
  • -ERRNO: errno code if error

int fs_unlink(const char *path)

File unlink.

Deletes the specified file or directory

Parameters
  • path: Path to the file or directory to delete
Return Value
  • 0: Success
  • -ERRNO: errno code if error

ssize_t fs_read(ZFILE *zfp, void *ptr, size_t size)

File read.

Reads items of data of size bytes long.

Return
Number of bytes read. On success, it will be equal to number of items requested to be read. Returns less than number of bytes requested if there are not enough bytes available in file. Will return -ERRNO code on error.
Parameters
  • zfp: Pointer to the file object
  • ptr: Pointer to the data buffer
  • size: Number of bytes to be read

ssize_t fs_write(ZFILE *zfp, const void *ptr, size_t size)

File write.

Writes items of data of size bytes long.

Return
Number of bytes written. On success, it will be equal to the number of bytes requested to be written. Any other value, indicates an error. Will return -ERRNO code on error. In the case where -ERRNO is returned, the file pointer will not be advanced because it couldn’t start the operation. In the case where it is able to write, but is not able to complete writing all of the requested number of bytes, then it is because the disk got full. In that case, it returns less number of bytes written than requested, but not a negative -ERRNO value as in regular error case.
Parameters
  • zfp: Pointer to the file object
  • ptr: Pointer to the data buffer
  • size: Number of bytes to be write

int fs_seek(ZFILE *zfp, off_t offset, int whence)

File seek.

Moves the file position to a new location in the file. The offset is added to file position based on the ‘whence’ parameter.

Parameters
  • zfp: Pointer to the file object
  • offset: Relative location to move the file pointer to
  • whence: Relative location from where offset is to be calculated.
    • SEEK_SET = from beginning of file
    • SEEK_CUR = from current position,
    • SEEK_END = from end of file.
Return Value
  • 0: Success
  • -ERRNO: errno code if error.

off_t fs_tell(ZFILE *zfp)

Get current file position.

Retrieves the current position in the file.

Parameters
  • zfp: Pointer to the file object
Return Value
  • position: Current position in file Current revision does not validate the file object.

int fs_mkdir(const char *path)

Directory create.

Creates a new directory using specified path.

Parameters
  • path: Path to the directory to create
Return Value
  • 0: Success
  • -ERRNO: errno code if error

int fs_opendir(ZDIR *zdp, const char *path)

Directory open.

Opens an existing directory specified by the path.

Parameters
  • zdp: Pointer to the directory object
  • path: Path to the directory to open
Return Value
  • 0: Success
  • -ERRNO: errno code if error

int fs_readdir(ZDIR *zdp, struct zfs_dirent *entry)

Directory read entry.

Reads directory entries of a open directory

Return
In end-of-dir condition, this will return 0 and set entry->name[0] = 0
Parameters
  • zdp: Pointer to the directory object
  • entry: Pointer to zfs_dirent structure to read the entry into
Return Value
  • 0: Success
  • -ERRNO: errno code if error

int fs_closedir(ZDIR *zdp)

Directory close.

Closes an open directory.

Parameters
  • zdp: Pointer to the directory object
Return Value
  • 0: Success
  • -ERRNO: errno code if error

int fs_stat(const char *path, struct zfs_dirent *entry)

File or directory status.

Checks the status of a file or directory specified by the path

Parameters
  • path: Path to the file or directory
  • entry: Pointer to zfs_dirent structure to fill if file or directory exists.
Return Value
  • 0: Success
  • -ERRNO: errno code if error

File System Data Structures

typedef ZFILE

File object representing an open file.

typedef ZDIR

Directory object representing an open directory.

struct zfs_dirent
#include <fs.h>

Structure to receive file or directory information.

Used in functions that reads the directory entries to get file or directory information.

Parameters
  • dir_entry_type: Whether file or directory
    • DIR_ENTRY_FILE
    • DIR_ENTRY_DIR
  • name: Name of directory or file
  • size: Size of file. 0 if directory