From 4b3384e45d74c7e6a5614fed2f0e9665a1bf1dc3 Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 7 Apr 2019 15:14:46 -0500 Subject: [PATCH] Work on ext2 directory support --- fs/ext2.c | 44 +++++++++++++++++++++++++++++++++++--------- kernel/kernel.c | 22 +++++++++++----------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index c3dc5a9..d8e775c 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -116,6 +116,7 @@ char** get_dir_listing(uint32_t inode_num,FILE* f,int num) { names[num_entries_used]=malloc(current_entry->name_len+1); strcpy(names[num_entries_used],current_entry->file_name); names[num_entries_used][(int)current_entry->name_len]='\0'; + klog("INFO","Found entry with name %s",names[num_entries_used]); num_entries_used++; tot_size+=current_entry->rec_len; current_entry=(dir_entry*)(((uint32_t)current_entry)+current_entry->rec_len); @@ -125,6 +126,7 @@ char** get_dir_listing(uint32_t inode_num,FILE* f,int num) { names=realloc(names,sizeof(char*)*max_len); } names[num_entries_used]=NULL; + klog("INFO","Parsed directory"); return names; } @@ -233,16 +235,40 @@ static char drv(fs_op op,FILE* stream,void* data1,void* data2) { } if (data) { FILE* f=fopen(devs[data->num],"r"); - char got_inode; - uint32_t inode_num=inode_for_fname(2,stream->path,&got_inode,f,data->num); - if (got_inode) { - data->inode=read_inode(inode_num,f,data->num); - fclose(f); - return 1; - } else { - fclose(f); - return 0; + uint32_t inode_num=2; + for (char* tok=strtok(stream->path,"/");tok!=NULL;tok=strtok(NULL,"/")) { + char got_inode; + inode_num=inode_for_fname(inode_num,tok,&got_inode,f,data->num); + if (got_inode) { + inode inode=read_inode(inode_num,f,data->num); + if ((inode.i_mode&EXT2_S_IFDIR)==0) { + char* next_tok=strtok(NULL,"/"); + if (tok) { + klog("INFO","%s: Not a directory",tok); + fclose(f); + return 0; + } else { + break; + } + } + } else { + klog("INFO","%s: No such file or directory",tok); + fclose(f); + return 0; + } } + klog("INFO","File inode:%d",inode_num); + return 0; + // char got_inode; + // uint32_t inode_num=inode_for_fname(2,stream->path,&got_inode,f,data->num); + // if (got_inode) { + // data->inode=read_inode(inode_num,f,data->num); + // fclose(f); + // return 1; + // } else { + // fclose(f); + // return 0; + // } } else { return 0; } diff --git a/kernel/kernel.c b/kernel/kernel.c index ee6ede2..1db7739 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -91,10 +91,10 @@ static void init() { uint32_t word=port_long_in(0xCFC); port_long_out(0xCF8,(1<<31)|0x4); if (word!=port_long_in(0xCFC)) { - pci_init(); + // pci_init(); } // Detect and initailize serial ports - serial_init(); + //serial_init(); FILE* file=fopen("/initrd/prog.elf","r"); elf_header header; fread(&header,sizeof(elf_header),1,file); @@ -116,15 +116,15 @@ static void init() { ide_init(); init_ext2(); mount("/","/dev/hda","ext2"); - FILE* f=fopen("/file","r"); - char str[256]; - fgets(str,256,f); - str[strlen(str)-1]='\0'; - klog("INFO","Got string %s",str); - fgets(str,256,f); - str[strlen(str)-1]='\0'; - klog("INFO","Got string %s",str); - fclose(f); + FILE* f=fopen("/mydir/myfile","r"); + // char str[256]; + // fgets(str,256,f); + // str[strlen(str)-1]='\0'; + // klog("INFO","Got string %s",str); + // fgets(str,256,f); + // str[strlen(str)-1]='\0'; + // klog("INFO","Got string %s",str); + // fclose(f); // ext2_parse(); // char** names=get_dir_listing(2); // for(int i=0;names[i]!=NULL;i++) {