The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
ext2_file.c File Reference
#include "ext2_internal.h"

Macros

#define EXT2_REV0_FILE_MAX   0x7FFFFFFFull

Functions

uint64_t ext2_inode_size (const ext2_inode_t *inode)
 the size of an inode's file in bytes.
uint64_t ext2_max_file_size (const ext2_volume_t *volume)
 the largest size a regular file can have on a volume: what the direct, single, double and triple indirect blocks can map, and at most what i_blocks can count (2 TiB).
kerr_t ext2_open (ext2_volume_t *volume, const char *path, ext2_file_t *file)
 open a file or directory by absolute path.
uint32_t ext2_read (ext2_file_t *file, uint32_t byteCount, void *dataOut)
 read bytes from an open ext2 file handle.
uint32_t ext2_write (ext2_file_t *file, uint32_t byteCount, const void *dataIn)
 write bytes to an open ext2 file at the current cursor position.
kerr_t ext2_truncate (ext2_file_t *file, uint64_t length)
 cut or grow an open file to length bytes (growth reads as zeros).
kerr_t ext2_read_entry (ext2_file_t *file, ext2_directory_entry_t *entryOut)
 read one directory entry from an open directory handle.
void ext2_close (ext2_file_t *file)
 close an open ext2 file handle.
kerr_t ext2_read_file (ext2_volume_t *volume, uint32_t inode_number, uint64_t offset, uint32_t length, void *buffer)
 read bytes from a file inode into buffer.
kerr_t ext2_create_file (ext2_volume_t *volume, const char *path)
 create a regular file at an absolute path.
kerr_t ext2_create_dir (ext2_volume_t *volume, const char *path)
 create a directory at an absolute path.
kerr_t ext2_remove_file (ext2_volume_t *volume, const char *path)
 remove a regular file at an absolute path.
kerr_t ext2_remove_dir (ext2_volume_t *volume, const char *path)
 remove a directory at an absolute path.
kerr_t ext2_rename (ext2_volume_t *volume, const char *old_path, const char *new_path)
 rename a file or directory (both paths absolute and normalized).
kerr_t ext2_write_file (ext2_volume_t *volume, uint32_t inode_number, uint64_t offset, uint32_t length, const void *buffer)
 write bytes to a file inode.
kerr_t ext2_truncate_file (ext2_volume_t *volume, uint32_t inode_number, uint64_t length)
 cut or grow a file to length bytes.

Macro Definition Documentation

◆ EXT2_REV0_FILE_MAX

#define EXT2_REV0_FILE_MAX   0x7FFFFFFFull

Function Documentation

◆ ext2_inode_size()

uint64_t ext2_inode_size ( const ext2_inode_t * inode)

the size of an inode's file in bytes.

a regular file keeps the high 32 bits in i_dir_acl.

Parameters
inodethe inode.
Returns
the size.

◆ ext2_max_file_size()

uint64_t ext2_max_file_size ( const ext2_volume_t * volume)

the largest size a regular file can have on a volume: what the direct, single, double and triple indirect blocks can map, and at most what i_blocks can count (2 TiB).

on a revision 0 volume (no feature flags), 2 GiB - 1.

Parameters
volumethe volume.
Returns
the size in bytes.

◆ ext2_open()

kerr_t ext2_open ( ext2_volume_t * volume,
const char * path,
ext2_file_t * file )

open a file or directory by absolute path.

Parameters
volumeinitialized ext2 volume.
pathabsolute path.
fileoutput file handle.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_read()

uint32_t ext2_read ( ext2_file_t * file,
uint32_t byteCount,
void * dataOut )

read bytes from an open ext2 file handle.

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

◆ ext2_write()

uint32_t ext2_write ( ext2_file_t * file,
uint32_t byteCount,
const void * dataIn )

write bytes to an open ext2 file at the current cursor position.

allocates blocks as needed. advances the cursor.

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

◆ ext2_truncate()

kerr_t ext2_truncate ( ext2_file_t * file,
uint64_t length )

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

the cursor stays.

Returns
KERR_OK, or KERR_IO.

◆ ext2_read_entry()

kerr_t ext2_read_entry ( ext2_file_t * file,
ext2_directory_entry_t * entryOut )

read one directory entry from an open directory handle.

Parameters
fileopen directory handle.
entryOutoutput directory entry.
Returns
KERR_OK when an entry is read. KERR_NOENT when done or on failure.

◆ ext2_close()

void ext2_close ( ext2_file_t * file)

close an open ext2 file handle.

Parameters
filefile handle to close.

◆ ext2_read_file()

kerr_t ext2_read_file ( ext2_volume_t * volume,
uint32_t inode_number,
uint64_t offset,
uint32_t length,
void * buffer )

read bytes from a file inode into buffer.

Parameters
volumeinitialized ext2 volume.
inode_numberfile inode number.
offsetbyte offset in file.
lengthbytes to read.
bufferdestination buffer.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_create_file()

kerr_t ext2_create_file ( ext2_volume_t * volume,
const char * path )

create a regular file at an absolute path.

◆ ext2_create_dir()

kerr_t ext2_create_dir ( ext2_volume_t * volume,
const char * path )

create a directory at an absolute path.

◆ ext2_remove_file()

kerr_t ext2_remove_file ( ext2_volume_t * volume,
const char * path )

remove a regular file at an absolute path.

◆ ext2_remove_dir()

kerr_t ext2_remove_dir ( ext2_volume_t * volume,
const char * path )

remove a directory at an absolute path.

◆ ext2_rename()

kerr_t ext2_rename ( ext2_volume_t * volume,
const char * old_path,
const char * new_path )

rename a file or directory (both paths absolute and normalized).

an existing new_path is replaced first, as POSIX rename does: a file by a file, an empty directory by a directory. this is not atomic: if the rename then fails, the replaced entry is gone.

Returns
KERR_OK, KERR_INVAL (a directory into itself), KERR_ISDIR, KERR_NOTDIR, KERR_NOTEMPTY, KERR_NOTFOUND, KERR_NOSPC or KERR_IO.

◆ ext2_write_file()

kerr_t ext2_write_file ( ext2_volume_t * volume,
uint32_t inode_number,
uint64_t offset,
uint32_t length,
const void * buffer )

write bytes to a file inode.

allocates blocks as needed (direct blocks only, up to 48KB with 4K blocks).

Parameters
volumeinitialized ext2 volume.
inode_numberfile inode number.
offsetbyte offset in file to start writing.
lengthbytes to write.
buffersource data.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_truncate_file()

kerr_t ext2_truncate_file ( ext2_volume_t * volume,
uint32_t inode_number,
uint64_t length )

cut or grow a file to length bytes.

the blocks past a shorter end are freed; a longer file gets a hole, which reads as zeros.

Parameters
volumeinitialized ext2 volume.
inode_numberfile inode number.
lengththe new size.
Returns
KERR_OK on success, or an error code on failure.