Skip to content

darnit_filesystem.h

slaeshjag edited this page Mar 30, 2013 · 22 revisions

DARNIT_DIR_LIST

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.

DARNIT_FILESYSTEM_TYPE

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

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".

d_fs_mount

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.

d_fs_unmount

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

d_fs_mount_self

int d_fs_mount_self();

Mounts the LDI appended to the executable

Arguments None

Return value

-1 on failure, 0 on success.

d_fs_unmount_self

void d_fs_unmount_self();

Unmounts the LDI appended to the executable.

Arguments None.

Return value None.

d_fs_exec_path

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

d_file_open

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.

d_file_close

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.

d_directory_create

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.

d_file_read_ints

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.

d_file_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.

d_file_write_ints

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

d_file_write

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.

d_file_gets

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.

d_file_getln

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.

d_file_whitespace_skip

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

d_file_read_compressed

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 compressed bytes to read

Return value

Returns the number of extracted bytes written to data.

d_file_write_compressed

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.)

d_file_tell

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

d_file_seek

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.

d_file_eof

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.

d_file_list

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.

d_file_list_free

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.

Clone this wiki locally