Skip to content

Commit

Permalink
filesystem nonsense
Browse files Browse the repository at this point in the history
  • Loading branch information
tmathmeyer committed Feb 1, 2018
1 parent b933038 commit 4667ddb
Show file tree
Hide file tree
Showing 16 changed files with 944 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ISO := $(BUILD)/os.iso
LINKER_SCRIPT := src/asm/$(arch)/linker.ld
GRUB_CFG := src/grub/grub.cfg
SFSDISK := $(BUILD)/sfsdisk
SOURCEDIR := ./src

C_SRC := $(shell find $(SOURCEDIR) -name '*.c')
C_OBJ := $(patsubst ./src/%, $(BUILD)/%, $(C_SRC:%.c=%.o))
Expand Down
62 changes: 62 additions & 0 deletions include/fs/fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef fs_h
#define fs_h

#include <std/int.h>


typedef
enum {
NO_ERROR = 0,
FILE_NOT_FOUND = 1,
ARGUMENT_ERROR = 2,
} F_err;

typedef
struct {
uint8_t __open__;
uint64_t __position__;
void *__data__;
struct filesystem_s *fs;
} F;

typedef
struct _directory {
uint64_t __position__;
} D;

typedef
struct _stat {
uint64_t __position__;
} S;

typedef
struct filesystem_s {
/* File operations */
F_err (*f_open)(F *file, char *name, uint16_t mode);
F_err (*f_close)(F *file);
F_err (*f_read)(F *file, void *dest, uint64_t to_read, uint64_t *actually_read);
F_err (*f_write)(F *file, void *src, uint64_t to_write, uint64_t *actually_wrote);
F_err (*f_lseek)(F *file, uint64_t seek_to);
F_err (*f_truncate)(F *file);
F_err (*f_sync)(F *file);
F_err (*f_tell)(F *file, uint64_t *buffer_position);
F_err (*f_eof)(F *file);
F_err (*f_size)(F *file, uint64_t *file_size);

/* Directory operations */
F_err (*d_open)(D *dir, char *name, uint16_t mode);
F_err (*d_close)(D *dir);
F_err (*d_next)(D *dir, char **name);
F_err (*d_rewind)(D *dir);
F_err (*d_mkdir)(char *name);

/* Status operations */
F_err (*i_stat)(S *stat, char *path);
F_err (*i_unlink)(char *path);
F_err (*i_rename)(char *old_path, char *new_path);

void *__internal__;
} filesystem_t;


#endif
11 changes: 11 additions & 0 deletions include/fs/kernel_fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef kernel_fs_h
#define kernel_fs_h

#include <fs/fs.h>

filesystem_t *kernel_fs_init();




#endif
33 changes: 33 additions & 0 deletions include/fs/mfs/map_fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef mapfs_h
#define mapfs_h

#include <fs/fs.h>
#include <std/int.h>
#include <std/map.h>

typedef
enum {
FILE,
DIR
} TYPE;

typedef
struct {
union {
map_t dir;
char *data;
};
uint64_t size;
TYPE type;
} d_f;


F_err mfs_open(F *file, char *name, uint16_t mode);
F_err mfs_close(F *file);
F_err mfs_read(F *file, void *dest, uint64_t to_read, uint64_t *actually_read);
F_err mfs_write(F *file, void *src, uint64_t to_write, uint64_t *actually_wrote);

filesystem_t *mfs_new_fs();


#endif
16 changes: 16 additions & 0 deletions include/fs/root.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef root_h
#define root_h

#include <fs/fs.h>

void root_init();
int mount(char *path, filesystem_t *fs);


int open(char *, uint16_t);
int close(int);
int read(int, void *, uint64_t);
int write(int, void *, uint64_t);
void stat(int);

#endif
1 change: 1 addition & 0 deletions include/mem/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

void *kmalloc_init(void);
void *kmalloc(uint64_t);
void *kcmalloc(uint64_t, uint64_t);
void kfree(void *_ptr);

void *memset(void *p, int b, size_t n);
Expand Down
49 changes: 49 additions & 0 deletions include/std/map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef map_h
#define map_h

#include <std/int.h>

#define MAP_MISSING -3 /* No such element */
#define MAP_FULL -2 /* Hashmap is full */
#define MAP_OMEM -1 /* Out of Memory */
#define MAP_OK 0 /* OK */

// You don't get to know what a map_t is!
typedef void *map_t;

// For iterating through hashmaps as a simple list
typedef struct {
char *key;
void *data;
} hashmap_iterator_element;

typedef struct {
int position;
int maximum;
hashmap_iterator_element *data;
} hashmap_iterator;

int hashmap_put(map_t in, char *key, void *value);

int hashmap_get(map_t in, char *key, void **arg);

int hashmap_remove(map_t in, char *key);

void hashmap_free(map_t in);

int hashmap_size(map_t in);

map_t hashmap_new();

hashmap_iterator *map_iterator(map_t in);
int hashmap_iterator_has_next(hashmap_iterator *itr);
void hashmap_iterator_done(hashmap_iterator *itr);
hashmap_iterator_element *hashmap_iterator_next(hashmap_iterator *itr);

#define hashmap_iterate(m, k, v) \
for(void *___i=map_iterator(m),*___m=NULL; \
hashmap_iterator_has_next(___i)?(___m=hashmap_iterator_next(___i), \
v=((hashmap_iterator_element *)___m)->data, \
k=((hashmap_iterator_element *)___m)->key):0;)

#endif
12 changes: 12 additions & 0 deletions include/std/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ uint64_t strlen(char *);
void *memcpy(void *, void *, size_t);
void *memset(void *, int b, size_t);
char hexr(char);
char *strdup(char *);

typedef struct {
uint64_t size;
char *__underlying__[0];
} split_t;

char **split(char *, char);
void split_free(char **);

#define splitlen(strs) \
(((split_t *)(strs))[-1].size)

#endif
9 changes: 9 additions & 0 deletions src/alloc/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,13 @@ void kfree(void *_ptr) {
head_page->allocations[i].pages = 0;
}
}
}

void *kcmalloc(uint64_t size, uint64_t nmemb) {
size *= nmemb;
void *res = kmalloc(size);
if (res) {
memset(res, 0, size);
}
return res;
}
42 changes: 42 additions & 0 deletions src/fs/kernelf_fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <fs/kernel_fs.h>
#include <fs/fs.h>
#include <fs/mfs/map_fs.h>


/*
typedef
struct filesystem_s {
F_err (*f_open)(F *file, char *name, uint16_t mode);
F_err (*f_close)(F *file);
F_err (*f_read)(F *file, void *dest, uint64_t to_read, uint64_t *actually_read);
F_err (*f_write)(F *file, void *src, uint64_t to_write, uint64_t *actually_wrote);
F_err (*f_lseek)(F *file, uint64_t seek_to);
F_err (*f_truncate)(F *file);
F_err (*f_sync)(F *file);
F_err (*f_tell)(F *file, uint64_t *buffer_position);
F_err (*f_eof)(F *file);
F_err (*f_size)(F *file, uint64_t *file_size);
F_err (*d_open)(D *dir, char *name, uint16_t mode);
F_err (*d_close)(D *dir);
F_err (*d_next)(D *dir, char **name);
F_err (*d_rewind)(D *dir);
F_err (*d_mkdir)(char *name);
F_err (*i_stat)(S *stat, char *path);
F_err (*i_unlink)(char *path);
F_err (*i_rename)(char *old_path, char *new_path);
} filesystem_t;
*/

filesystem_t *kernel_fs_init() {
filesystem_t *result = mfs_new_fs();

result->f_open = mfs_open;
result->f_close = mfs_close;
result->f_read = mfs_read;
result->f_write = mfs_write;

return result;
}
Loading

0 comments on commit 4667ddb

Please sign in to comment.