tape-kernel 1.0
a modular modern independent kernel
Loading...
Searching...
No Matches
fs.c File Reference
#include "fs.h"
#include "ide.h"
#include "../mem/heap.h"
#include "../lib/err.h"
#include "../lib/utils.h"
Include dependency graph for fs.c:

Go to the source code of this file.

Classes

struct  fs_entry_t
 the fs_entry_t type More...

Macros

#define FS_TABLE_LBA   1

Functions

void __fsinit (arena_t *heap)
 fsinit initializes the filesystem table from disk
void __fsadd (arena_t *heap, const char *name, uint32_t lba)
 fsadd adds a new file entry to the filesystem
uint32_t __fsfind (const char *name)
 fsfind searches the filesystem for a file by name
char * __fslist (void)
 fslist returns a newline-separated listing of all files
uint32_t __fsnextlba (void)
 fsnextlba finds the next unused lba for a new file
char * __fsread (const char *name)
 fsread reads a files contents from disk into memory
int __fswrite (arena_t *heap, const char *name, const char *content)
 fswrite writes text content to a file on disk
void __fsdelete (const char *name)
 fsdelete removes a file from the filesystem
int __fswriteb (arena_t *heap, const char *name, uint8_t *data, uint32_t size)
 fswriteb writes raw byte data to a file on disk
int __fsreadb (const char *name, uint8_t *buffer, uint32_t max_size)
 fsreadb reads raw byte data from a file off disk

Variables

static fs_entry_t files [32]
static int file_count = 0
static arena_tfs_heap = ((void*)0)
static uint32_t lba_map [32]

Macro Definition Documentation

◆ FS_TABLE_LBA

#define FS_TABLE_LBA   1

Definition at line 8 of file fs.c.

Referenced by __fsadd(), __fsdelete(), and __fsinit().

Function Documentation

◆ __fsadd()

void __fsadd ( arena_t * heap,
const char * name,
uint32_t lba )

fsadd adds a new file entry to the filesystem

fsadd is a function that registers a file name and lba into the in-memory file table and persists it to disk

Parameters
heap,thearena to allocate the filename from
name,thename of the file to add
lba,thelogical block address where the file lives

using fsadd is done with

#include "../fs/fs.h" //or just fs.h
fsadd(fs_arena, "myfile.txt", 100);
#define fsadd
Definition fs.h:10
arena_t * fs_arena
the filesystem child arena
Definition main.c:56
See also
fsinit(), fsfind(), fsdelete()

Definition at line 102 of file fs.c.

References alc, assert, file_count, files, FS_TABLE_LBA, iwrt, MAX_FILES, NULL, and strcpy().

◆ __fsdelete()

void __fsdelete ( const char * name)

fsdelete removes a file from the filesystem

fsdelete is a function that finds a file by name, zeros out its data sector on disk, marks its lba as free, and removes it from the in-memory file table

Parameters
name,thefilename to delete

using fsdelete is done with

#include "../fs/fs.h" //or just fs.h
fsdelete("oldfile.txt");
#define fsdelete
Definition fs.h:16
See also
fsadd(), fsfind(), fswrite()

Definition at line 316 of file fs.c.

References assert, file_count, files, FS_TABLE_LBA, iwrt, lba_map, MAX_FILES, and strcmp().

◆ __fsfind()

uint32_t __fsfind ( const char * name)

fsfind searches the filesystem for a file by name

fsfind is a function that looks up a file in the in-memory table and returns its lba if found

Parameters
name,thefilename to search for

using fsfind is done with

#include "../fs/fs.h" //or just fs.h
uint32_t lba = fsfind("myfile.txt");
if (lba == 0) {
//file doesnt exist
}
#define fsfind
Definition fs.h:11
unsigned int uint32_t
Definition types.h:30
See also
fslist(), fsread(), fs_entry_t

Definition at line 150 of file fs.c.

References file_count, files, and strcmp().

◆ __fsinit()

void __fsinit ( arena_t * heap)

fsinit initializes the filesystem table from disk

fsinit is a function that reads the filesystem table from the disk, parses all existing file entries, and rebuilds the lba map

Parameters
heap,thearena to allocate filename strings from

using fsinit is done with

#include "../fs/fs.h" //or just fs.h
#define fsinit
Definition fs.h:9
See also
ffsinit(), fsadd(), fslist()

Definition at line 44 of file fs.c.

References alc, file_count, files, fs_heap, FS_TABLE_LBA, irsec, lba_map, and MAX_FILES.

◆ __fslist()

char * __fslist ( void )

fslist returns a newline-separated listing of all files

fslist is a function that builds a string containing all filenames in the in-memory filesystem table separated by newlines

using fslist is done with

#include "../fs/fs.h" //or just fs.h
char *listing = fslist();
prt(0, 0, listing, 0x0F);
#define fslist
Definition fs.h:12
#define prt
Definition vga.h:6
See also
fsfind(), fsread()

Definition at line 174 of file fs.c.

References file_count, and files.

◆ __fsnextlba()

uint32_t __fsnextlba ( void )

fsnextlba finds the next unused lba for a new file

fsnextlba is a function that scans the lba map for a free slot and returns an unused lba starting from sector 100

using fsnextlba is done with

#include "../fs/fs.h" //or just fs.h
uint32_t new_lba = fsnextlba();
#define fsnextlba
Definition fs.h:13
See also
fswrite(), fswriteb()

Definition at line 208 of file fs.c.

References assert, lba_map, and MAX_FILES.

◆ __fsread()

char * __fsread ( const char * name)

fsread reads a files contents from disk into memory

fsread is a function that looks up a file by name, reads its sector from disk, and returns the contents as a string

Parameters
name,thefilename to read

using fsread is done with

#include "../fs/fs.h" //or just fs.h
char *content = fsread("myfile.txt");
if (content != NULL) {
prt(0, 0, content, 0x0F);
}
#define fsread
Definition fs.h:14
#define NULL
Definition types.h:38
See also
fswrite(), fsfind(), irsec()

Definition at line 240 of file fs.c.

References fsfind, irsec, and NULL.

◆ __fsreadb()

int __fsreadb ( const char * name,
uint8_t * buffer,
uint32_t max_size )

fsreadb reads raw byte data from a file off disk

fsreadb is a function that reads raw bytes from a file sector into a provided buffer

Parameters
name,thefilename to read
buffer,thebuffer to store the raw bytes in
max_size,themaximum number of bytes to read

using fsreadb is done with

#include "../fs/fs.h" //or just fs.h
uint8_t buffer[512];
int bytes_read = fsreadb("data.bin", buffer, 512);
#define fsreadb
Definition fs.h:18
unsigned char uint8_t
Definition types.h:28
See also
fswriteb(), fsread(), irsec()

Definition at line 433 of file fs.c.

References fsfind, and irsec.

◆ __fswrite()

int __fswrite ( arena_t * heap,
const char * name,
const char * content )

fswrite writes text content to a file on disk

fswrite is a function that writes a text string to a file, creating the file entry if it doesnt exist yet

Parameters
heap,thearena to allocate from if a new file entry is created
name,thefilename to write to
content,thetext string to write

using fswrite is done with

#include "../fs/fs.h" //or just fs.h
fswrite(fs_arena, "hello.txt", "hello, world!");
#define fswrite
Definition fs.h:15
See also
fsread(), fswriteb(), iwrt()

Definition at line 276 of file fs.c.

References fsadd, fsfind, fsnextlba, and iwrt.

◆ __fswriteb()

int __fswriteb ( arena_t * heap,
const char * name,
uint8_t * data,
uint32_t size )

fswriteb writes raw byte data to a file on disk

fswriteb is a function that writes raw bytes to a file sector, limited to 511 bytes per sector, creating the file entry if needed

Parameters
heap,thearena to allocate from if a new file entry is created
name,thefilename to write to
data,theraw byte buffer to write
size,thenumber of bytes to write

using fswriteb is done with

#include "../fs/fs.h" //or just fs.h
uint8_t mydata[256];
fswriteb(fs_arena, "data.bin", mydata, 256);
#define fswriteb
Definition fs.h:17
See also
fsreadb(), fswrite(), iwrt()

Definition at line 390 of file fs.c.

References fsadd, fsfind, fsnextlba, and iwrt.

Variable Documentation

◆ file_count

int file_count = 0
static

Definition at line 25 of file fs.c.

Referenced by __fsadd(), __fsdelete(), __fsfind(), __fsinit(), and __fslist().

◆ files

fs_entry_t files[32]
static

Definition at line 24 of file fs.c.

Referenced by __fsadd(), __fsdelete(), __fsfind(), __fsinit(), and __fslist().

◆ fs_heap

arena_t* fs_heap = ((void*)0)
static

Definition at line 26 of file fs.c.

Referenced by __fsinit().

◆ lba_map

uint32_t lba_map[32]
static

Definition at line 27 of file fs.c.

Referenced by __fsdelete(), __fsinit(), and __fsnextlba().