The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
fs.h File Reference
#include "common/stdint.h"
#include "arch/arch.h"
#include "common/error.h"
#include "drivers/disk/block_device.h"
#include "fs/ext2/ext2.h"

Go to the source code of this file.

Data Structures

struct  fs_attr_t
 a change of a file's permissions or owner (setattr). More...
struct  fs_ops_t
struct  fs_dirent
 directory entry structure for reading directory contents. More...
struct  fs_mount
 filesystem mount structure representing a mounted filesystem. More...
struct  fs_stat
 filesystem metadata structure returned by stat. More...
struct  fs_file
 file handle structure representing an open file or directory. More...

Macros

#define FS_ATTR_MODE   0x1
#define FS_ATTR_OWNER   0x2
#define FS_ATTR_ATIME   0x4
#define FS_ATTR_MTIME   0x8

Typedefs

typedef struct fs_file fs_file_t
typedef struct fs_dirent fs_dirent_t
typedef struct fs_stat fs_stat_t

Enumerations

enum  {
  FS_TYPE_UNKNOWN = 0 , FS_TYPE_FILE = 1 , FS_TYPE_DIR = 2 , FS_TYPE_SYMLINK = 3 ,
  FS_TYPE_FIFO = 4 , FS_TYPE_CHAR = 5 , FS_TYPE_BLOCK = 6
}
 file system types More...

Functions

bool fs_mount (fs_mount_t *mount, block_device_t *device)
 mount a filesystem on a block device.
kerr_t fs_open (fs_mount_t *mount, const char *path, fs_file_t *file)
 open a file or directory by absolute path.
kerr_t fs_create (fs_mount_t *mount, const char *path)
 create a regular file at an absolute path.
kerr_t fs_mkdir (fs_mount_t *mount, const char *path)
 create a directory at an absolute path.
kerr_t fs_remove (fs_mount_t *mount, const char *path)
 remove a file at an absolute path.
kerr_t fs_rmdir (fs_mount_t *mount, const char *path)
 remove a directory at an absolute path.
kerr_t fs_rename (fs_mount_t *mount, const char *old_path, const char *new_path)
 rename a file or directory.
kerr_t fs_stat (fs_mount_t *mount, const char *path, fs_stat_t *stat_out)
 stat a file or directory by absolute path.
kerr_t fs_find_link (fs_mount_t *mount, const char *path, bool follow_last, char *target, uint32_t size, uint32_t *end_out)
 the link operations (see fs_ops_t).
kerr_t fs_readlink (fs_mount_t *mount, const char *path, char *target, uint32_t size)
kerr_t fs_symlink (fs_mount_t *mount, const char *path, const char *target)
kerr_t fs_link (fs_mount_t *mount, const char *old_path, const char *new_path)
kerr_t fs_mknod (fs_mount_t *mount, const char *path, uint32_t mode, uint32_t rdev)
uint32_t fs_read (fs_file_t *file, uint32_t byteCount, void *dataOut)
 read bytes from an open file into a buffer.
uint32_t fs_write (fs_file_t *file, uint32_t byteCount, const void *dataIn)
 write bytes to an open file.
kerr_t fs_truncate (fs_file_t *file, uint64_t length)
 cut or grow an open file to length bytes (growth reads as zeros).
kerr_t fs_fstat (fs_file_t *file, fs_stat_t *stat_out)
 stat an open file or directory.
kerr_t fs_setattr (fs_file_t *file, const fs_attr_t *attr)
 change an open file's permission bits and/or owner.
kerr_t fs_read_entry (fs_file_t *file, fs_dirent_t *entryOut)
 read a directory entry from an open directory file.
void fs_close (fs_file_t *file)
 close an open file or directory.
uint8_t fs_file_type (const fs_file_t *file)
 get the type of an open file or directory.
int32_t fs_ioctl (fs_file_t *file, uint32_t request, void *arg)
 issue a device-control (ioctl) request on an open special file.
int32_t fs_mmap (fs_file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
 map an open special file's memory into a process address space.
int64_t fs_seek (fs_file_t *file, int64_t offset, int whence)
 reposition an open file's cursor via the filesystem's seek op.
int64_t fs_seek_position (uint64_t current, uint64_t size, int64_t offset, int whence)
 the position a seek gives, as lseek computes it.

Macro Definition Documentation

◆ FS_ATTR_MODE

#define FS_ATTR_MODE   0x1

◆ FS_ATTR_OWNER

#define FS_ATTR_OWNER   0x2

◆ FS_ATTR_ATIME

#define FS_ATTR_ATIME   0x4

◆ FS_ATTR_MTIME

#define FS_ATTR_MTIME   0x8

Typedef Documentation

◆ fs_file_t

typedef struct fs_file fs_file_t

◆ fs_dirent_t

typedef struct fs_dirent fs_dirent_t

◆ fs_stat_t

typedef struct fs_stat fs_stat_t

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

file system types

Enumerator
FS_TYPE_UNKNOWN 
FS_TYPE_FILE 
FS_TYPE_DIR 
FS_TYPE_SYMLINK 
FS_TYPE_FIFO 
FS_TYPE_CHAR 
FS_TYPE_BLOCK 

Function Documentation

◆ fs_mount()

bool fs_mount ( fs_mount_t * mount,
block_device_t * device )

mount a filesystem on a block device.

Parameters
mountfilesystem mount object to initialize.
deviceinitialized block device backend.
Returns
true on success. false on failure.

◆ fs_open()

kerr_t fs_open ( fs_mount_t * mount,
const char * path,
fs_file_t * file )

open a file or directory by absolute path.

Parameters
mountinitialized filesystem mount.
pathabsolute path to the file or directory.
fileoutput file handle.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_create()

kerr_t fs_create ( fs_mount_t * mount,
const char * path )

create a regular file at an absolute path.

Parameters
mountinitialized filesystem mount.
pathabsolute path to the new file.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_mkdir()

kerr_t fs_mkdir ( fs_mount_t * mount,
const char * path )

create a directory at an absolute path.

Parameters
mountinitialized filesystem mount.
pathabsolute path to the new directory.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_remove()

kerr_t fs_remove ( fs_mount_t * mount,
const char * path )

remove a file at an absolute path.

Parameters
mountinitialized filesystem mount.
pathabsolute path to the file to remove.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_rmdir()

kerr_t fs_rmdir ( fs_mount_t * mount,
const char * path )

remove a directory at an absolute path.

Parameters
mountinitialized filesystem mount.
pathabsolute path to the directory to remove.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_rename()

kerr_t fs_rename ( fs_mount_t * mount,
const char * old_path,
const char * new_path )

rename a file or directory.

Parameters
mountinitialized filesystem mount.
old_pathabsolute path to the existing file or directory.
new_pathabsolute path to the new name for the file or directory.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_stat()

kerr_t fs_stat ( fs_mount_t * mount,
const char * path,
fs_stat_t * stat_out )

stat a file or directory by absolute path.

Parameters
mountinitialized filesystem mount.
pathabsolute path to the file or directory.
stat_outoutput stat structure.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_find_link()

kerr_t fs_find_link ( fs_mount_t * mount,
const char * path,
bool follow_last,
char * target,
uint32_t size,
uint32_t * end_out )

the link operations (see fs_ops_t).

with no such operation: find_link finds none, readlink is KERR_INVAL (not a link), symlink and link are KERR_PERM.

◆ fs_readlink()

kerr_t fs_readlink ( fs_mount_t * mount,
const char * path,
char * target,
uint32_t size )

◆ fs_symlink()

kerr_t fs_symlink ( fs_mount_t * mount,
const char * path,
const char * target )

◆ fs_link()

kerr_t fs_link ( fs_mount_t * mount,
const char * old_path,
const char * new_path )

◆ fs_mknod()

kerr_t fs_mknod ( fs_mount_t * mount,
const char * path,
uint32_t mode,
uint32_t rdev )

◆ fs_read()

uint32_t fs_read ( fs_file_t * file,
uint32_t byteCount,
void * dataOut )

read bytes from an open file into a buffer.

Parameters
fileopen file handle.
byteCountnumber of bytes to read.
dataOutdestination buffer.
Returns
number of bytes actually read.

◆ fs_write()

uint32_t fs_write ( fs_file_t * file,
uint32_t byteCount,
const void * dataIn )

write bytes to an open file.

Parameters
fileopen file handle.
byteCountnumber of bytes to write.
dataInsource buffer.
Returns
number of bytes actually written, or 0 on failure.

◆ fs_truncate()

kerr_t fs_truncate ( fs_file_t * file,
uint64_t length )

cut or grow an open file to length bytes (growth reads as zeros).

Returns
KERR_OK on success, or an error code on failure.

◆ fs_fstat()

kerr_t fs_fstat ( fs_file_t * file,
fs_stat_t * stat_out )

stat an open file or directory.

Parameters
fileopen file handle.
stat_outoutput stat structure.
Returns
KERR_OK, or KERR_INVAL if the filesystem cannot stat open files.

◆ fs_setattr()

kerr_t fs_setattr ( fs_file_t * file,
const fs_attr_t * attr )

change an open file's permission bits and/or owner.

Parameters
fileopen file handle.
attrwhat to change.
Returns
KERR_OK, or KERR_PERM if the filesystem keeps no owners.

◆ fs_read_entry()

kerr_t fs_read_entry ( fs_file_t * file,
fs_dirent_t * entryOut )

read a directory entry from an open directory file.

Parameters
fileopen directory file handle.
entryOutoutput directory entry.
Returns
KERR_OK on success, or an error code on failure.

◆ fs_close()

void fs_close ( fs_file_t * file)

close an open file or directory.

Parameters
fileopen file handle to close.
Returns
void

◆ fs_file_type()

uint8_t fs_file_type ( const fs_file_t * file)

get the type of an open file or directory.

Parameters
fileopen file handle.
Returns
file type (FS_TYPE_FILE, FS_TYPE_DIR, or FS_TYPE_UNKNOWN).

◆ fs_ioctl()

int32_t fs_ioctl ( fs_file_t * file,
uint32_t request,
void * arg )

issue a device-control (ioctl) request on an open special file.

dispatches through the filesystem's ioctl op; filesystems without one (ext2, etc.) return -ENOTTY.

Parameters
fileopen file handle.
requestdriver-defined request code.
argrequest-specific argument (typically a bounds-checked pointer).
Returns
0 on success, or a negative errno on failure.

◆ fs_mmap()

int32_t fs_mmap ( fs_file_t * file,
addr_space_t as,
uintptr_t virt,
size_t length,
int prot,
uint64_t offset )

map an open special file's memory into a process address space.

dispatches through the filesystem's mmap op; filesystems without one return -ENODEV. the frames are mapped eagerly at virt in page_directory.

Parameters
fileopen file handle.
page_directoryphysical address of the target process page directory.
virtpage-aligned destination virtual address.
lengthbytes to map (page-multiple).
protPROT_* bits.
offsetpage-aligned byte offset into the mapped object.
Returns
0 on success, or a negative errno on failure.

◆ fs_seek()

int64_t fs_seek ( fs_file_t * file,
int64_t offset,
int whence )

reposition an open file's cursor via the filesystem's seek op.

Returns
the new absolute position, or -ENOSYS if the filesystem has no seek op (the caller then handles seeking itself, e.g. via the ext2 cursor), or another negative errno on failure.

◆ fs_seek_position()

int64_t fs_seek_position ( uint64_t current,
uint64_t size,
int64_t offset,
int whence )

the position a seek gives, as lseek computes it.

Parameters
currentthe current position.
sizethe size of the file (for SEEK_END).
offsetthe offset relative to whence.
whence0 (SEEK_SET), 1 (SEEK_CUR) or 2 (SEEK_END).
Returns
the new position, or -EINVAL for a bad whence or a negative result, or -EOVERFLOW if the result does not fit in 63 bits.