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

Macros

#define EXT2_FALLBACK_TIME   1700000000u

Functions

uint32_t ext2_now (void)
 the time for inode times: ext2_clock, or a fixed time without one.
bool find_directory_entry (ext2_volume_t *volume, uint32_t dir_inode_number, const char *name, uint32_t name_len, uint32_t *block_index_out, uint32_t *offset_out, ext2_dir_entry_t *entry_out)
 searches for a directory entry with a specific name within a given directory inode, returning the block index, offset, and entry details if found.
bool append_or_replace_directory_entry (ext2_volume_t *volume, uint32_t parent_inode_number, ext2_inode_t *parent_inode, const char *name, uint32_t name_len, uint32_t child_inode_number, uint8_t file_type)
 appends a new directory entry to a parent directory inode or replaces an existing entry with the same name, ensuring proper space allocation and alignment within the directory's data blocks.
bool update_directory_entry_name (ext2_volume_t *volume, uint32_t dir_inode_number, const char *old_name, uint32_t old_name_len, const char *new_name, uint32_t new_name_len)
 updates the name of an existing directory entry within a specified directory inode, ensuring that the new name fits within the allocated space for the entry.
bool directory_is_empty (ext2_volume_t *volume, uint32_t inode_number)
 checks if a directory represented by a given inode number is empty, meaning it contains no entries other than the standard '.
bool update_directory_parent_link (ext2_volume_t *volume, uint32_t dir_inode_number, uint32_t parent_inode_number)
 updates the '..' entry in a directory to point to a new parent inode number, effectively changing the parent directory reference for that directory.
bool remove_directory_entry (ext2_volume_t *volume, uint32_t dir_inode_number, const char *name, uint32_t name_len)
 removes a directory entry with a specific name from a given directory inode, effectively deleting the entry from the directory's data blocks.
bool setup_new_file_inode (ext2_inode_t *inode)
 initializes a new inode structure for a regular file, setting its mode, link count, and clearing other fields to prepare it for use in the filesystem.
bool setup_new_dir_inode (ext2_inode_t *inode, ext2_volume_t *volume)
 initializes a new inode structure for a directory, setting its mode, link count, size, and block count to prepare it for use in the filesystem.
bool initialize_directory_block (ext2_volume_t *volume, uint32_t inode_number, uint32_t parent_inode_number, uint32_t block_number)
 initializes a new directory block with the standard '.
bool lookup_parent_and_name (ext2_volume_t *volume, const char *path, uint32_t *parent_inode_out, char *name_out, uint32_t name_out_size, char *parent_path, uint32_t parent_path_size)
 looks up the parent directory inode and the name of the final component in a given path, splitting the path into its parent and child components and verifying that the parent is a valid directory.
bool lookup_child_type (ext2_volume_t *volume, uint32_t parent_inode_number, const char *name, uint32_t name_len, uint32_t *child_inode_out, uint8_t *child_type_out)
 looks up a child directory entry by name within a specified parent directory inode, returning the child's inode number and file type if found.
bool free_inode_and_blocks (ext2_volume_t *volume, uint32_t inode_number, ext2_inode_t *inode)
 frees the blocks associated with a given inode and then frees the inode itself, effectively removing the file or directory represented by that inode from the filesystem.

Variables

uint32_t(*) ext2_clock (void)
 where the driver gets the time for inode times, in seconds since 1970.

Macro Definition Documentation

◆ EXT2_FALLBACK_TIME

#define EXT2_FALLBACK_TIME   1700000000u

Function Documentation

◆ ext2_now()

uint32_t ext2_now ( void )

the time for inode times: ext2_clock, or a fixed time without one.

◆ find_directory_entry()

bool find_directory_entry ( ext2_volume_t * volume,
uint32_t dir_inode_number,
const char * name,
uint32_t name_len,
uint32_t * block_index_out,
uint32_t * offset_out,
ext2_dir_entry_t * entry_out )

searches for a directory entry with a specific name within a given directory inode, returning the block index, offset, and entry details if found.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
dir_inode_numberthe inode number of the directory to search within.
namethe name of the directory entry to search for.
name_lenthe length of the name to search for.
block_index_outpointer to a variable where the block index of the found entry will be stored.
offset_outpointer to a variable where the byte offset of the found entry within the block will be stored.
entry_outpointer to an ext2_dir_entry_t structure where the details of the found entry will be stored.
Returns
true if the directory entry was found and its details are stored in the output parameters, false if the entry was not found or an error occurred during the search.

◆ append_or_replace_directory_entry()

bool append_or_replace_directory_entry ( ext2_volume_t * volume,
uint32_t parent_inode_number,
ext2_inode_t * parent_inode,
const char * name,
uint32_t name_len,
uint32_t child_inode_number,
uint8_t file_type )

appends a new directory entry to a parent directory inode or replaces an existing entry with the same name, ensuring proper space allocation and alignment within the directory's data blocks.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
parent_inode_numberthe inode number of the parent directory where the entry will be added or replaced.
parent_inodepointer to the ext2_inode_t structure representing the parent directory inode.
namethe name of the directory entry to add or replace.
name_lenthe length of the name of the directory entry.
child_inode_numberthe inode number of the child entry to be added or replaced.
file_typethe type of the file (e.g., regular file, directory) for the new directory entry.
Returns
true if the directory entry was successfully appended or replaced,

◆ update_directory_entry_name()

bool update_directory_entry_name ( ext2_volume_t * volume,
uint32_t dir_inode_number,
const char * old_name,
uint32_t old_name_len,
const char * new_name,
uint32_t new_name_len )

updates the name of an existing directory entry within a specified directory inode, ensuring that the new name fits within the allocated space for the entry.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
dir_inode_numberthe inode number of the directory containing the entry to be renamed.
old_namethe current name of the directory entry to be renamed.
old_name_lenthe length of the current name of the directory entry.
new_namethe new name to assign to the directory entry.
new_name_lenthe length of the new name to assign to the directory entry.
Returns
true if the directory entry name was successfully updated, false if the entry was not found, the new name does not fit, or an error occurred during the operation.

◆ directory_is_empty()

bool directory_is_empty ( ext2_volume_t * volume,
uint32_t inode_number )

checks if a directory represented by a given inode number is empty, meaning it contains no entries other than the standard '.

' and '..' entries.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
inode_numberthe inode number of the directory to check.
Returns
true if the directory is empty (only contains '.' and '..'), false if it contains other entries or if an error occurred during the check.

◆ update_directory_parent_link()

bool update_directory_parent_link ( ext2_volume_t * volume,
uint32_t dir_inode_number,
uint32_t parent_inode_number )

updates the '..' entry in a directory to point to a new parent inode number, effectively changing the parent directory reference for that directory.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
dir_inode_numberthe inode number of the directory whose parent link is to be updated.
parent_inode_numberthe inode number of the new parent directory to which the '..' entry should point.
Returns
true if the parent link was successfully updated, false if the directory inode could not be read, the directory is invalid, or an error occurred during the update process.

◆ remove_directory_entry()

bool remove_directory_entry ( ext2_volume_t * volume,
uint32_t dir_inode_number,
const char * name,
uint32_t name_len )

removes a directory entry with a specific name from a given directory inode, effectively deleting the entry from the directory's data blocks.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
dir_inode_numberthe inode number of the directory from which the entry will be removed.
namethe name of the directory entry to be removed.
name_lenthe length of the name of the directory entry to be removed.
Returns
true if the directory entry was successfully removed, false if the entry was not found, the directory inode could not be read, or an error occurred during the removal process.

◆ setup_new_file_inode()

bool setup_new_file_inode ( ext2_inode_t * inode)

initializes a new inode structure for a regular file, setting its mode, link count, and clearing other fields to prepare it for use in the filesystem.

Parameters
inodepointer to the ext2_inode_t structure to be initialized.
Returns
true if the inode was successfully initialized, false if the inode pointer was null.

◆ setup_new_dir_inode()

bool setup_new_dir_inode ( ext2_inode_t * inode,
ext2_volume_t * volume )

initializes a new inode structure for a directory, setting its mode, link count, size, and block count to prepare it for use in the filesystem.

Parameters
inodepointer to the ext2_inode_t structure to be initialized.
volumepointer to the ext2_volume_t structure representing the filesystem volume, used to determine the block size and sectors per block for the directory inode.
Returns
true if the inode was successfully initialized, false if either the inode or volume pointers were null.

◆ initialize_directory_block()

bool initialize_directory_block ( ext2_volume_t * volume,
uint32_t inode_number,
uint32_t parent_inode_number,
uint32_t block_number )

initializes a new directory block with the standard '.

' and '..' entries, setting their inode numbers and ensuring proper formatting for the directory structure.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
inode_numberthe inode number of the directory being initialized (for the '.' entry).
parent_inode_numberthe inode number of the parent directory (for the '..' entry).
block_numberthe block number where the new directory block will be written.
Returns
true if the directory block was successfully initialized and written to the specified block number, false if an error occurred during the initialization or writing process, or if the volume pointer was null or the block size was insufficient to hold the directory entries.

◆ lookup_parent_and_name()

bool lookup_parent_and_name ( ext2_volume_t * volume,
const char * path,
uint32_t * parent_inode_out,
char * name_out,
uint32_t name_out_size,
char * parent_path,
uint32_t parent_path_size )

looks up the parent directory inode and the name of the final component in a given path, splitting the path into its parent and child components and verifying that the parent is a valid directory.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
paththe input path string to be looked up (must start with '/').
parent_inode_outpointer to a variable where the inode number of the parent directory will be stored.
name_outbuffer to store the name of the final component (file or directory) in the path.
name_out_sizethe size of the name_out buffer in bytes.
parent_pathbuffer to store the parent directory path.
parent_path_sizethe size of the parent_path buffer in bytes.
Returns
true if the parent inode and name were successfully looked up and stored in the output parameters, false if the path was invalid, the parent directory could not be found, or the parent inode is not a valid directory.

◆ lookup_child_type()

bool lookup_child_type ( ext2_volume_t * volume,
uint32_t parent_inode_number,
const char * name,
uint32_t name_len,
uint32_t * child_inode_out,
uint8_t * child_type_out )

looks up a child directory entry by name within a specified parent directory inode, returning the child's inode number and file type if found.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
parent_inode_numberthe inode number of the parent directory to search within.
namethe name of the child directory entry to look up.
name_lenthe length of the name of the child directory entry.
child_inode_outpointer to a variable where the inode number of the found child entry will be stored.
child_type_outpointer to a variable where the file type of the found child entry will be stored.
Returns
true if the child directory entry was found and its details are stored in the output parameters, false if the entry was not found or an error occurred during the lookup process.

◆ free_inode_and_blocks()

bool free_inode_and_blocks ( ext2_volume_t * volume,
uint32_t inode_number,
ext2_inode_t * inode )

frees the blocks associated with a given inode and then frees the inode itself, effectively removing the file or directory represented by that inode from the filesystem.

Parameters
volumepointer to the ext2_volume_t structure representing the filesystem volume.
inode_numberthe inode number of the file or directory to be freed.
inodepointer to the ext2_inode_t structure representing the inode to be freed. If null, the function will read the inode from the filesystem.
Returns
true if the blocks and inode were successfully freed, false if an error occurred during the process or if the volume pointer was null.

Variable Documentation

◆ ext2_clock

uint32_t(*) ext2_clock(void) ( void )

where the driver gets the time for inode times, in seconds since 1970.

the kernel sets it at boot; while it is NULL (the bootloader) the driver uses a fixed time.