diff options
| author | Miguel <m.i@gmx.at> | 2018-09-15 00:30:36 +0200 |
|---|---|---|
| committer | Miguel <m.i@gmx.at> | 2018-09-15 00:30:36 +0200 |
| commit | fe7d0332267ef1e62153b685d2b5574ce624a4bc (patch) | |
| tree | 0c5bb5af4275b55b141ff598d9daedd99eb12603 | |
| parent | c4b20a0ebbde1348e1e085e2ea3be35345d92b7c (diff) | |
reading ext2 files and using our abstractions
| -rw-r--r-- | fs/fd.c | 86 | ||||
| -rw-r--r-- | fs/fd.h (renamed from kernel/fd.h) | 8 | ||||
| -rw-r--r-- | kernel/fd.c | 33 | ||||
| -rw-r--r-- | kernel/kernel.c | 8 | ||||
| -rw-r--r-- | kernel/syscalls.c | 79 | ||||
| -rw-r--r-- | userspace/cat.c | 8 | ||||
| -rw-r--r-- | userspace/piper.c | 21 | ||||
| -rw-r--r-- | userspace/test.txt | 2 |
8 files changed, 164 insertions, 81 deletions
@@ -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; +} @@ -8,6 +8,11 @@ #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); @@ -15,6 +20,7 @@ typedef struct fd_struct bool (*has)(struct fd_struct*); bool (*close)(struct fd_struct*); + uint8_t type; void *data; // opaque data }fd; @@ -24,6 +30,6 @@ bool fd_write(fd*,uint8_t); bool fd_close(fd*); fd fd_from_fifo(fifo* f); -//fd fd_from_path(char *path); +fd fd_from_path(char *path); #endif diff --git a/kernel/fd.c b/kernel/fd.c deleted file mode 100644 index 524737e..0000000 --- a/kernel/fd.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "fd.h" -#include "fifo.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.data=fif; - f.read=fifo_get; - f.write=fifo_put; -// f.close=fd_close; - f.has=fifo_has; - return f; -} diff --git a/kernel/kernel.c b/kernel/kernel.c index 9088d00..5caed04 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -112,17 +112,17 @@ void kernel_main(uint32_t eax,uint32_t ebx) // -- STDIN/STDOUT -- // fixme("do not force order"); // now needed since ids are allocated 0,1,2... klog("stdin/stdout init ..."); - uint32_t sstderr = syscall_open("stderr",0,0); // stderr 2 + uint32_t sstderr = syscall_open("~stderr",0,0); // stderr 2 uint32_t sstdout; if(cfg_multiboot->framebuffer_type==2) // EGA-standard text mode { - sstdout = syscall_open("term",0,0); // stdout 1 + sstdout = syscall_open("~term",0,0); // stdout 1 } else { - sstdout = syscall_open("xterm",0,0); // stdout 1 + sstdout = syscall_open("~xterm",0,0); // stdout 1 } - uint32_t sstdin = syscall_open("stdin",0,0); // stdin 0 + uint32_t sstdin = syscall_open("~stdin",0,0); // stdin 0 klog("Keyboard init ..."); keyboard_init(0); diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 4d1c29d..57ebc54 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -15,15 +15,13 @@ #include "syscalls.h" #include "scheduler.h" #include "log.h" - -// TODO: use includes!!! -uint64_t timer_get_ms(); +#include "timer.h" static fd fds[MAX_FD]; static uint32_t next_fd=0; +// fifos for backing up some file descrpitors static fifo fifos[MAX_FIFOS]; -static uint32_t fifo_data_len[MAX_FIFOS]; static uint32_t next_fifo=0; // screen / terminal @@ -121,7 +119,6 @@ int syscall_write(int file, char *buf, int len) for(int i=0;i<len;i++) { fd_write(&fds[file],buf[i]); - fifo_data_len[file]++; } return len; } @@ -132,19 +129,9 @@ int syscall_write(int file, char *buf, int len) */ int syscall_read(int file, char *buf, int len) { - //file 0 = stdin , file 1 = stdout , file 2 = stderr - char c; - int l=0; - - c=fd_read(&fds[file]); - fifo_data_len[file]--; - *buf=c; - buf++; - l++; - - return l; - if(l==len)return l; - if(c=='\n')return l; + if(fds[file].type==FD_TYPE_EXT2_FILE && !fd_has(&fds[file]))return 0; + *buf=fd_read(&fds[file]); + return 1; } //TODO: replace with dirent! @@ -283,44 +270,53 @@ int get_max_fd() // TODO: allow opening existing files/named pipes int syscall_open(char *name, int flags, int mode) { - if( next_fifo>=MAX_FIFOS || next_fd>=MAX_FD)kpanic("we ran out of fd's or fifo's"); - if(!strcmp(name,"term")) + bool create_fifo=true; + if(name[0]!='~')create_fifo=false; + + if(create_fifo) { - screen.put_char=console_put_char; - screen.update_cursor=update_cursor; - tty1=terminal_init(&screen,NULL); + if(!strcmp(name,"~term")) + { + screen.put_char=console_put_char; + screen.update_cursor=update_cursor; - fifos[next_fifo].data=&tty1; - fifos[next_fifo].put=terminal_put; + tty1=terminal_init(&screen,NULL); - fds[next_fd]=fd_from_fifo(&fifos[next_fifo]); - } - else if(!strcmp(name,"xterm")) - { - screen.put_char=vesa_console_put_char; - screen.update_cursor=vesa_update_cursor; + fifos[next_fifo].data=&tty1; + fifos[next_fifo].put=terminal_put; + + fds[next_fd]=fd_from_fifo(&fifos[next_fifo]); + } + else if(!strcmp(name,"~xterm")) + { + screen.put_char=vesa_console_put_char; + screen.update_cursor=vesa_update_cursor; - tty1=terminal_init(&screen,NULL); + tty1=terminal_init(&screen,NULL); - fifos[next_fifo].data=&tty1; - fifos[next_fifo].put=terminal_put; + fifos[next_fifo].data=&tty1; + fifos[next_fifo].put=terminal_put; + fds[next_fd]=fd_from_fifo(&fifos[next_fifo]); + } + else + { + fifos[next_fifo]=fifo_create_buffered(1); fds[next_fd]=fd_from_fifo(&fifos[next_fifo]); + } + + next_fifo++; } else { - fifos[next_fifo]=fifo_create_buffered(1); - fds[next_fd]=fd_from_fifo(&fifos[next_fifo]); + + fds[next_fd]=fd_from_path(name); } - fifo_data_len[next_fifo]=0; - - next_fifo++; next_fd++; - return next_fd-1; } uint32_t syscall_fork(int pid) @@ -389,7 +385,8 @@ uint32_t syscall_generic_test(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, case SYSCALL_WAIT : return !task_runs(p1); case SYSCALL_READ : - return p3 <= fifo_data_len[p1]; + if(fds[p1].type==FD_TYPE_EXT2_FILE)return 1; + return fd_has(&fds[p1]); case SYSCALL_EXIT : return 1; } diff --git a/userspace/cat.c b/userspace/cat.c index 39d994f..5b419ca 100644 --- a/userspace/cat.c +++ b/userspace/cat.c @@ -3,7 +3,11 @@ int main(int argc, char **argv) { FILE *f; - if(argc>1)f=fopen(argv[1],"r"); + if(argc>1){ + char buf[256]; + sprintf(buf,"%s/%s",getenv("PWD"),argv[1]); + f=fopen(buf,"r"); + } else f=stdin; setvbuf(stdin,NULL,_IONBF,0); @@ -11,7 +15,7 @@ int main(int argc, char **argv) char c; - printf("-- read from file byte by byte --\n"); + printf("-- reading from file byte by byte --\n\n"); while(fread(&c,1,1,f)) { diff --git a/userspace/piper.c b/userspace/piper.c new file mode 100644 index 0000000..2272f96 --- /dev/null +++ b/userspace/piper.c @@ -0,0 +1,21 @@ +#include <stdio.h> +int main() +{ + int pid=_fork(); + FILE *f=fopen("~testpipe","rw"); + + setvbuf(f,NULL,_IONBF,0); + + if(pid==0) + { + char buf[2]; + fread(f,buf,1,1); + printf("[%c]\n",buf[0]); + } + else + { + char buf="666"; + fwrite(f,buf,1,1); + printf("written\n"); + } +} diff --git a/userspace/test.txt b/userspace/test.txt index d44e39c..7a22897 100644 --- a/userspace/test.txt +++ b/userspace/test.txt @@ -2,4 +2,6 @@ HELLO THIS IS A TEXTFILE USED FOR TESTING THE FOOL OS EXT2 RAM IMAGE TRY TO READ ME! + [41;37m [COLOR CODES ARE SUPPORTED TOO FROM FILES] [37;40m + BYE |
