-
Notifications
You must be signed in to change notification settings - Fork 3
darnit_filesystem.h
typedef struct DARNIT_DIR_LIST {
const char *fname;
unsigned int directory : 1;
unsigned int file : 1;
unsigned int writeable : 1;
unsigned int in_file_image : 1;
struct DARNIT_DIR_LIST *next;
} DARNIT_DIR_LIST;This struct is used for directory listings.
- fname - The files name
- directory - 1 if file is a directory, 0 if not
- file - 1 if it's a regular file, 0 if it's not
- writeable - 1 if the file/directory is in a location that should be writeable, 0 if not
- in_file_image - if the file/directory is inside a filesystem image
- next - next file in the listing. NULL if it's the end of the list.
typedef enum {
DARNIT_FILESYSTEM_TYPE_READ = 0x1,
DARNIT_FILESYSTEM_TYPE_WRITE = 0x2,
} DARNIT_FILESYSTEM_TYPE;- DARNIT_FILESYSTEM_TYPE_READ - If you want a list of readable files
- DARNIT_FILESYSTEM_TYPE_WRITE - If you want a list of files that you can write to
DARNIT_FILE is a file handle simlar to libc's FILE. DARNIT_FILE wraps filesystem functions in a platform independent way, and integrates file managing with mountable filesystem images "LDI".
- int d_fs_mount(const char *fname);
- void d_fs_unmount(const char *fname);
- int d_fs_mount_self();
- void d_fs_unmount_self();
- char *d_fs_exec_path();
- DARNIT_FILE *d_file_open(const char *fname, const char *mode);
- DARNIT_FILE *d_file_close(DARNIT_FILE *file);
- void d_directory_create(void *dir_name);
- size_t d_file_read_ints(void *buffer, size_t ints, DARNIT_FILE *file);
- size_t d_file_read(void *buffer, size_t bytes, DARNIT_FILE *file);
- size_t d_file_write_ints(void *buffer, size_t ints, DARNIT_FILE *file);
- size_t d_file_write(void *buffer, size_t bytes, DARNIT_FILE *file);
- size_t d_file_gets(void *buffer, size_t bytes, DARNIT_FILE *file);
- size_t d_file_getln(void *buffer, size_t bytes, DARNIT_FILE *file);
- void d_file_whitespace_skip(DARNIT_FILE *file);
- int d_file_read_compressed(DARNIT_FILE *file, void *data, int len);
- int d_file_write_compressed(DARNIT_FILE *file, void *data, int len);
- off_t d_file_tell(DARNIT_FILE *file);
- size_t d_file_seek(DARNIT_FILE *file, off_t offset, int mode);
- int d_file_eof(DARNIT_FILE *file);
- void d_file_size_set(DARNIT_FILE *file, off_t size);
- DARNIT_DIR_LIST *d_file_list(const char *path, DARNIT_FILESYSTEM_TYPE type, int *entries);
- DARNIT_DIR_LIST *d_file_list_free(DARNIT_DIR_LIST *list);
Can be used to write LDI files at run time. When calling any of these functions, expect the file descriptor you opened the file with to move in the file.
- DARNIT_LDI_WRITER *d_file_ldi_write(DARNIT_FILE *f, int files);
- int d_file_ldi_write_file(DARNIT_LDI_WRITER *h, const char *filename, void *data, unsigned int size);
- DARNIT_LDI_WRITER *d_file_ldi_write_end(DARNIT_LDI_WRITER *h);
int d_fs_mount(const char *fname);
Mounts a filesystem image with the path fname.
Arguments
- fname - name of the filesystem image to mount
Return value
Returns -1 on failure, 0 on success.
void d_fs_unmount(const char *fname);
Unmounts the filesystem image at path fname
Arguments
- fname - name of the filesystem image to unmount
Return value None
int d_fs_mount_self();
Mounts the LDI appended to the executable
Arguments None
Return value
-1 on failure, 0 on success.
void d_fs_unmount_self();
Unmounts the LDI appended to the executable.
Arguments None.
Return value None.
char *d_fs_exec_path();
Allocates a string and copies the absolute path to the executable to it. You have to free this string yourself when you're done with it.
Arguments None
Return value
Returns a string containing the absolute path to the executable (currently running.) You need to free this string yourself
DARNIT_FILE *d_file_open(const char *fname, const char *mode);
Opens a file and returns a handle to it. If a file is opened for writing, the file will only be looked for in writeable locations.
Arguments fname - Name of the file to open mode - What mode to open the file in. Works just like libc's fopen.
Return value
NULL on failure. Anything else is a valid handle for the file.
DARNIT_FILE *d_file_close(DARNIT_FILE *file); Closes a file and returns NULL.
Arguments
- file - The file handle to close
Return value
A NULL-pointer for compact pointer clearing.
void d_directory_create(const char *dir_name);
Creates a directory in the writable path.
Arguments
- dir_name - path and name of the directory to create
Return value None.
size_t d_file_read_ints(void *buffer, size_t ints, DARNIT_FILE *file);
Reads ints binary ints, converts them from big endian and stores in buffer.
Arguments
- buffer - The memory buffer to read ints to
- ints - Number of ints to read
- file - The file handle to read from
Return value
Returns the number of ints read.
size_t d_file_read(void *buffer, size_t ints, DARNIT_FILE *file);
Reads bytes bytes from file into buffer.
Arguments
- buffer - The memory buffer to read data into
- bytes - The number of bytes to read
- file - The file handle to read from
Return value
Returns the number of ints read.
size_t d_file_write_ints(void *buffer, size_t ints, DARNIT_FILE *file);
Converts ints ints from buffer to big endian and writes them to file.
Arguments
- buffer - The memory buffer containing the ints to write
- ints - The number of ints to write
- file - The file handle to write ints to
Return value
Returns the number of ints written
size_t d_file_write(void *buffer, size_t bytes, DARNIT_FILE *file);
Writes bytes bytes from buffer to file file.
Arguments
- buffer - Memory buffer to read bytes for writing from
- bytes - Number of bytes to write
- file - File handle to write to
Return value
The number of bytes written is returned.
size_t d_file_gets(void *buffer, size_t bytes, DARNIT_FILE *file);
Reads a string into buffer. Reads a maximum of bytes bytes including NULL-termination. Stops at newline and EOF. If it's stopped at newline, the newline is included into the buffer.
Arguments
- buffer - Memory buffer to read the string into
- bytes - Maximum number of bytes to read, including NULL-termination
- file - The file handle to read from
Return value
Returns the nubmer of bytes read from file.
size_t d_file_getln(void *buffer, size_t bytes, DARNIT_FILE *file);
Reads a maximum of bytes bytes from file. Stops at EOF and newlines. If a newline is encountered, it is read, but not included into the buffer. The string is NULL-terminated.
Arguments
- buffer - Memeory buffer to read the string into
- bytes - Maximum number of bytes to read into buffer, including NULL-termination
- file - File handle to read from
Return value
Returns the number of bytes read, including the newline if it was encountered.
void d_file_whitespace_skip(DARNIT_FILE *file);
Skips bytes in file until a non-whitespace (space and tab counts as whitespace) or EOF is found.
Arguments
- file - The file handle to skip whitespace in
Return value None
int d_file_read_compressed(DARNIT_FILE *file, void *data, int len);
Reads len bzip2 compressed bytes from file and extracts to data.
Arguments
- file - File handle to read compressed data from
- data - Memory buffer to extract data to. This buffer needs to be large enough to hold the entire uncompressed block
- len - Number of decompressed bytes to fill the buffer with
Return value
Returns the number of extracted bytes written to data.
int d_file_write_compressed(DARNIT_FILE *file, void *data, int len);
Compresses len bytes from data with bzip2 and writes them to file.
Arguments
- file - File handle to write compressed data to
- data - Memory buffer to read data from, which will be compressed
- len - Number of bytes to compress
Return value
Returns the number of bytes written to file (the size of the data compressed.)
off_t d_file_tell(DARNIT_FILE *file);
Returns the current cursor position in bytes in the file file. 0 is the start of the file. Works just like ftell from libc.
Arguments
- file - File handle to get current cursor position from
Return value
Returns the current cursor position in bytes from the beginning of the file
int d_file_seek(DARNIT_FILE *file, off_t offset, int mode);
Moves the cursor in file file. Works like fseek in libc.
Arguments
- file - File handle to seek in
- offset - The offset relative to mode to seek to
- mode - One of SEEK_SET, SEEK_CUR, SEEK_END from stdio.h
Return value
-1 on failure, 0 on success.
int d_file_eof(DARNIT_FILE *file);
Checks if the cursor is at the end of the file.
Arguments
- file - The file handle to check the cursor position in
Return value
Returns 1 if at EOF, 0 if not at EOF.
- void darnit_filesystem.h#d_file_size_set(DARNIT_FILE *file, off_t size);
Sets the size of file to size. If the file is smaller than requested size, it is padded out with undefined content. If it is bigger than the requested size, then that part is truncated and the data after the requested size is lost. Can ONLY be used on writeable files. The file descriptor will point to the end of the file after the call.
Arguments
- file - The file to set size for
- size - The file size to apply to the file
Return value None.
DARNIT_DIR_LIST *d_file_list(const char *path, DARNIT_FILESYSTEM_TYPE type, int *entries);
Returns a directory listing of the folder at path. If the directory exists in multiple locations, you'll get a directory listing of all of them that matches the type argument.
Arguments
- path - Path to the directory to list
- type - Select if you only want to list writeable directories, or if read-only ones are okay too
- entries - The int this pointer points to will be set to the number of entries in the listing
Return value
Returns NULL on failure. Returns a DARNIT_DIR_LIST with the entries found.
DARNIT_DIR_LIST *d_file_list_free(DARNIT_DIR_LIST *list);
Frees the linked list of entries from a directory listing and returns NULL for compact pointer clearing.
Arguments
- list - The directory listing to free
Return value
Returns NULL for compact pointer clearing.
DARNIT_LDI_WRITER *d_file_ldi_write(DARNIT_FILE *f, int files);
Initializes a LDI header in the supplied file. The file descriptor should be opened with mode set to "w+b".
Arguments
- f - The file descriptor to use
- files - The exact number of files you'll write. No more, no less.
Return value
Returns NULL on failure, anything else is a valid DARNIT_LDI_WRITER.
int d_file_ldi_write_file(DARNIT_LDI_WRITER *writer, const char *filename, void *data, unsigned int size);
Writes a file to the started LDI writer session.
Arguments
- writer - The LDI writer session to use
- filename - The filename of the file you want to use. This is where the file will appear when the LDI is mounted.
- data - The data of the file you want to write
- size - The size of the file data
Return value
Returns 1 on success, 0 on failure.
DARNIT_LDI_WRITER *d_file_ldi_write_end(DARNIT_LDI_WRITER *writer);
Frees memory used by the LDI writer session.
Arguments
- writer - The LDI writer session to end
Return value
Returns NULL for compact pointer clearing.