From fe7d0332267ef1e62153b685d2b5574ce624a4bc Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 15 Sep 2018 00:30:36 +0200 Subject: reading ext2 files and using our abstractions --- fs/fd.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/fd.h | 35 +++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 fs/fd.c create mode 100644 fs/fd.h (limited to 'fs') diff --git a/fs/fd.c b/fs/fd.c new file mode 100644 index 0000000..0ec134b --- /dev/null +++ b/fs/fd.c @@ -0,0 +1,86 @@ +#include "kernel.h" +#include "fd.h" +#include "fifo.h" +#include "kmalloc.h" +#include "ext2.h" +#include "log.h" + +bool fd_write(fd* f,uint8_t c) +{ + return f->write(f->data,c); +} + +uint8_t fd_read(fd* f) +{ + return f->read(f->data); +} + +bool fd_has(fd* f) +{ + return f->has(f->data); +} + +bool fd_close(fd* f) +{ + return f->close(f->data); +} + +fd fd_from_fifo(fifo* fif) +{ + fd f; + f.type=FD_TYPE_FIFO_BUFFERED; + f.data=fif; + f.read=fifo_get; + f.write=fifo_put; +// f.close=fd_close; + f.has=fifo_has; + return f; +} + +//---- +void* ext2_init(char *path) +{ + uint32_t *data=kballoc(1); + uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,path); + data[0]=inode;// pos + data[1]=0; //pos + + return data; +} + +void ext2_write(void* x) +{ + kpanic("not impl"); +} + +uint8_t ext2_read(uint32_t* data) +{ + char c; + ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1); + return c; +} + +bool ext2_has(uint32_t* data) +{ + uint32_t save=data[1]; + uint32_t c=ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1); + data[1]=save; + return (c>0); + +} +void ext2_close(void* x) +{ + kbfree(x); +} + +fd fd_from_path(char* path) +{ + fd f; + f.type=FD_TYPE_EXT2_FILE; + f.data=ext2_init(path); + f.read=ext2_read; + f.write=ext2_write; + f.close=ext2_close; + f.has=ext2_has; + return f; +} diff --git a/fs/fd.h b/fs/fd.h new file mode 100644 index 0000000..a3bdb5a --- /dev/null +++ b/fs/fd.h @@ -0,0 +1,35 @@ +// SIMPLE FILE DESCRIPTOR // + +#ifndef FD_H +#define FD_H + +#include +#include + +#include "fifo.h" + +enum FD_TYPE{ + FD_TYPE_FIFO_BUFFERED=1, + FD_TYPE_EXT2_FILE=2 +}; + +typedef struct fd_struct +{ + bool (*write)(struct fd_struct*,uint8_t); + uint8_t (*read)(struct fd_struct*); + bool (*has)(struct fd_struct*); + bool (*close)(struct fd_struct*); + + uint8_t type; + void *data; // opaque data +}fd; + +uint8_t fd_read(fd*); +bool fd_has(fd*); +bool fd_write(fd*,uint8_t); +bool fd_close(fd*); + +fd fd_from_fifo(fifo* f); +fd fd_from_path(char *path); + +#endif -- cgit v1.2.3