Skip to content

darnit_filesystem.h

slaeshjag edited this page Mar 24, 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 file's 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".

  • 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);

  • 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);

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_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_fs_file_open

DARNIT_FILE *d_fs_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_fs_file_close

DARNIT_FILE *d_fs_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_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

Clone this wiki locally