From 29ec949e91d65ee1d8ebe4bb3e1eedea0aec87fd Mon Sep 17 00:00:00 2001 From: Smrithi Panuganti Date: Mon, 22 Sep 2025 15:12:17 -0700 Subject: [PATCH 01/11] created and added basic find files --- Makefile | 1 + kernel/syscall.c | 2 ++ kernel/syscall.h | 1 + kernel/sysproc.c | 7 +++++++ user/find.c | 12 ++++++++++++ user/user.h | 1 + user/usys.pl | 1 + 7 files changed, 25 insertions(+) create mode 100644 user/find.c diff --git a/Makefile b/Makefile index cf031b0..91c5f1f 100644 --- a/Makefile +++ b/Makefile @@ -142,6 +142,7 @@ UPROGS=\ $U/_logstress\ $U/_forphan\ $U/_dorphan\ + $U/_find\ fs.img: mkfs/mkfs README.md $(UPROGS) mkfs/mkfs fs.img README.md $(UPROGS) diff --git a/kernel/syscall.c b/kernel/syscall.c index 076d965..6e0b144 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -101,6 +101,7 @@ extern uint64 sys_unlink(void); extern uint64 sys_link(void); extern uint64 sys_mkdir(void); extern uint64 sys_close(void); +extern uint64 sys_find(void); // An array mapping syscall numbers from syscall.h // to the function that handles the system call. @@ -126,6 +127,7 @@ static uint64 (*syscalls[])(void) = { [SYS_link] sys_link, [SYS_mkdir] sys_mkdir, [SYS_close] sys_close, +[SYS_find] sys_find, }; void diff --git a/kernel/syscall.h b/kernel/syscall.h index 3dd926d..9d7bc1b 100644 --- a/kernel/syscall.h +++ b/kernel/syscall.h @@ -20,3 +20,4 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_find 22 diff --git a/kernel/sysproc.c b/kernel/sysproc.c index 3044d00..4fea17e 100644 --- a/kernel/sysproc.c +++ b/kernel/sysproc.c @@ -105,3 +105,10 @@ sys_uptime(void) release(&tickslock); return xticks; } + + +uint64 +sys_find(void) +{ + return 0; +} diff --git a/user/find.c b/user/find.c new file mode 100644 index 0000000..b988c06 --- /dev/null +++ b/user/find.c @@ -0,0 +1,12 @@ +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" + +int +main(int argc, char *argv[]) +{ + printf("In the find.c file\n"); + find("/home/user","document.pdf"); + return 0; + +} diff --git a/user/user.h b/user/user.h index ac84de9..01d4545 100644 --- a/user/user.h +++ b/user/user.h @@ -24,6 +24,7 @@ int getpid(void); char* sys_sbrk(int,int); int pause(int); int uptime(void); +int find(const char* /*str*/ path, const char* /*str*/ expression); // ulib.c int stat(const char*, struct stat*); diff --git a/user/usys.pl b/user/usys.pl index c5d4c3a..8d844c2 100755 --- a/user/usys.pl +++ b/user/usys.pl @@ -42,3 +42,4 @@ sub entry { entry("sbrk"); entry("pause"); entry("uptime"); +entry("find"); From 108ec56104b31ab1999fd8dc71dce0b00bec57a9 Mon Sep 17 00:00:00 2001 From: Smrithi Panuganti Date: Wed, 24 Sep 2025 12:37:40 -0700 Subject: [PATCH 02/11] find has grep logic right now will change later --- user/find.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/user/find.c b/user/find.c index b988c06..c6ec822 100644 --- a/user/find.c +++ b/user/find.c @@ -1,12 +1,106 @@ #include "kernel/types.h" #include "kernel/stat.h" +#include "kernel/fcntl.h" #include "user/user.h" +char buf[1024]; +int match(char*, char*); + +void +find(char *path, char *expression) +{ + int n, m; + char *p, *q; + + m = 0; + while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){ + m += n; + buf[m] = '\0'; + p = buf; + while((q = strchr(p, '\n')) != 0){ + *q = 0; + if(match(pattern, p)){ + *q = '\n'; + write(1, p, q+1 - p); + } + p = q+1; + } + if(m > 0){ + m -= p - buf; + memmove(buf, p, m); + } + } +} int main(int argc, char *argv[]) { - printf("In the find.c file\n"); - find("/home/user","document.pdf"); + int fd, i; + char *path; + char *expression; + + printf("in user space find.c file\n"); + + if(argc <= 1){ + fprintf(2, "usage: find pattern [file ...]\n"); + exit(1); + } + path = argv[1]; + expression = argv[2]; + + if(argc <= 2){ + find(path, ); + fprintf("find: unable to find due to lack of arguments"); + exit(0); + } + + for(i = 2; i < argc; i++){ + if((fd = open(argv[i], O_RDONLY)) < 0){ + printf("find: cannot open %s\n", argv[i]); + exit(1); + } + find(pattern, fd); + close(fd); + } + exit(0); +} +// Regexp matcher from Kernighan & Pike, +// The Practice of Programming, Chapter 9, or +// https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html + +int matchhere(char*, char*); +int matchstar(int, char*, char*); + +int +match(char *re, char *text) +{ + if(re[0] == '^') + return matchhere(re+1, text); + do{ // must look at empty string + if(matchhere(re, text)) + return 1; + }while(*text++ != '\0'); + return 0; +} +// matchhere: search for re at beginning of text +int matchhere(char *re, char *text) +{ + if(re[0] == '\0') + return 1; + if(re[1] == '*') + return matchstar(re[0], re+2, text); + if(re[0] == '$' && re[1] == '\0') + return *text == '\0'; + if(*text!='\0' && (re[0]=='.' || re[0]==*text)) + return matchhere(re+1, text+1); + return 0; +} + +// matchstar: search for c*re at beginning of text +int matchstar(int c, char *re, char *text) +{ + do{ // a * matches zero or more instances + if(matchhere(re, text)) + return 1; + }while(*text!='\0' && (*text++==c || c=='.')); return 0; - } From 176c07137432276a403f81856e467bee5798de3a Mon Sep 17 00:00:00 2001 From: Smrithi Panuganti Date: Wed, 24 Sep 2025 12:58:02 -0700 Subject: [PATCH 03/11] compiles properly after renaming find function in find.c --- kernel/sysproc.c | 1 + user/find.c | 13 ++++++++----- user/user.h | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/sysproc.c b/kernel/sysproc.c index 4fea17e..d01522e 100644 --- a/kernel/sysproc.c +++ b/kernel/sysproc.c @@ -112,3 +112,4 @@ sys_find(void) { return 0; } + diff --git a/user/find.c b/user/find.c index c6ec822..fc26905 100644 --- a/user/find.c +++ b/user/find.c @@ -7,10 +7,13 @@ char buf[1024]; int match(char*, char*); void -find(char *path, char *expression) +findit(char *path, char *expression) { int n, m; char *p, *q; + int fd = 0; + printf("Expression: %s\n", expression); + printf("Path: %s\n", path); m = 0; while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){ @@ -19,7 +22,7 @@ find(char *path, char *expression) p = buf; while((q = strchr(p, '\n')) != 0){ *q = 0; - if(match(pattern, p)){ + if(match(path, p)){ *q = '\n'; write(1, p, q+1 - p); } @@ -48,8 +51,8 @@ main(int argc, char *argv[]) expression = argv[2]; if(argc <= 2){ - find(path, ); - fprintf("find: unable to find due to lack of arguments"); + findit(path, expression); + printf("find: unable to find due to lack of arguments"); exit(0); } @@ -58,7 +61,7 @@ main(int argc, char *argv[]) printf("find: cannot open %s\n", argv[i]); exit(1); } - find(pattern, fd); + findit(path, expression); close(fd); } exit(0); diff --git a/user/user.h b/user/user.h index 01d4545..5db0a0c 100644 --- a/user/user.h +++ b/user/user.h @@ -24,7 +24,7 @@ int getpid(void); char* sys_sbrk(int,int); int pause(int); int uptime(void); -int find(const char* /*str*/ path, const char* /*str*/ expression); +void find(char* /*str*/ path, char* /*str*/ expression); // ulib.c int stat(const char*, struct stat*); From d49892a1c14028b351ccc95377fdd13548a1e4a3 Mon Sep 17 00:00:00 2001 From: Arya Srivastava Date: Wed, 24 Sep 2025 13:43:40 -0700 Subject: [PATCH 04/11] Added ls.c func to find.c to see which parts to alter to actually be find --- user/find.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/user/find.c b/user/find.c index fc26905..b54615f 100644 --- a/user/find.c +++ b/user/find.c @@ -6,6 +6,61 @@ char buf[1024]; int match(char*, char*); +// ls implementation code +void +find2(char *path, char *expr) +{ + char buf[512], *p; + int fd; + struct dirent de; + struct stat st; + + if((fd = open(path, O_RDONLY)) < 0){ + fprintf(2, "find: cannot open %s\n", path); + return; + } + + if(fstat(fd, &st) < 0){ + fprintf(2, "find: cannot stat %s\n", path); + close(fd); + return; + } + + switch(st.type){ + case T_DEVICE: + case T_FILE: + printf("%s %d %d %d\n", fmtname(path), st.type, st.ino, (int) st.size); + break; + + case T_DIR: + if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ + printf("find: path too long\n"); + break; + } + /* + - recursion would happen when we're searching within directories to find the file name given in expr + - stringcmp(TFILE == expr), else + - base case would be the tree for all files + */ + strcpy(buf, path); + p = buf+strlen(buf); + *p++ = '/'; + while(read(fd, &de, sizeof(de)) == sizeof(de)){ + if(de.inum == 0) + continue; + memmove(p, de.name, DIRSIZ); + p[DIRSIZ] = 0; + if(stat(buf, &st) < 0){ + printf("find: cannot stat %s\n", buf); + continue; + } + printf("%s %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); + } + break; + } + close(fd); +} + void findit(char *path, char *expression) { From 4fd73f4cf197a72ad7045650085539a4aff81ba5 Mon Sep 17 00:00:00 2001 From: Smrithi Panuganti Date: Wed, 24 Sep 2025 13:47:22 -0700 Subject: [PATCH 05/11] adding testing directory with empty txt files --- tests/itestdir/itest1.txt | 0 tests/itestdir/itest2.txt | 0 tests/test1.txt | 0 tests/test2.txt | 0 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/itestdir/itest1.txt create mode 100644 tests/itestdir/itest2.txt create mode 100644 tests/test1.txt create mode 100644 tests/test2.txt diff --git a/tests/itestdir/itest1.txt b/tests/itestdir/itest1.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/itestdir/itest2.txt b/tests/itestdir/itest2.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/test1.txt b/tests/test1.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/test2.txt b/tests/test2.txt new file mode 100644 index 0000000..e69de29 From 6a5a1137eb84699b4f29b154ae6897521ba2396b Mon Sep 17 00:00:00 2001 From: Arya Srivastava Date: Wed, 24 Sep 2025 14:00:50 -0700 Subject: [PATCH 06/11] Starting to move around pieces of the ls func to be in line with find's recursive properties --- user/find.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/user/find.c b/user/find.c index b54615f..095c065 100644 --- a/user/find.c +++ b/user/find.c @@ -2,6 +2,7 @@ #include "kernel/stat.h" #include "kernel/fcntl.h" #include "user/user.h" +#include "kernel/fs.h" char buf[1024]; int match(char*, char*); @@ -26,6 +27,40 @@ find2(char *path, char *expr) return; } + if (st.type == T_FILE) { + if (match(expr, fmtname(path))) { + printf("path: %s\n", path); + } + } else if (T_DIR) { + if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ + printf("find: path too long\n"); + close(fd); + break; + } + strcpy(buf, path); + p = buf+strlen(buf); + *p++ = '/'; + + + while(read(fd, &de, sizeof(de)) == sizeof(de)){ + if(de.inum == 0) + continue; + if (strcmp(de.name, '.') == 0 || strcmp(de.name, '..') == 0) { + continue; + } + + memmove(p, de.name, DIRSIZ); + p[DIRSIZ] = 0; + if(stat(buf, &st) < 0){ + printf("find: cannot stat %s\n", buf); + continue; + } + printf("%d %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); + } + } else { + printf("Error found while traversing dirs \n"); + } + switch(st.type){ case T_DEVICE: case T_FILE: @@ -45,6 +80,8 @@ find2(char *path, char *expr) strcpy(buf, path); p = buf+strlen(buf); *p++ = '/'; + + while(read(fd, &de, sizeof(de)) == sizeof(de)){ if(de.inum == 0) continue; From 56b3256cadf716c195cfe95d1c34a3f5bf104b0d Mon Sep 17 00:00:00 2001 From: Arya Srivastava Date: Wed, 24 Sep 2025 14:19:29 -0700 Subject: [PATCH 07/11] Moved around ls func pieces and attempted to make a recursive statement within existing while loop. Pardon our (commented out) dust --- user/find.c | 65 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/user/find.c b/user/find.c index 095c065..ef6c7c2 100644 --- a/user/find.c +++ b/user/find.c @@ -7,9 +7,27 @@ char buf[1024]; int match(char*, char*); +char* +fmtname(char *path) +{ + static char buf[DIRSIZ+1]; + char *p; + + // Find first character after last slash. + for(p=path+strlen(path); p >= path && *p != '/'; p--) + ; + p++; + + // Return blank-padded name. + if(strlen(p) >= DIRSIZ) + return p; + memmove(buf, p, strlen(p)); + memset(buf+strlen(p), ' ', DIRSIZ-strlen(p)); + return buf; +} // ls implementation code void -find2(char *path, char *expr) +findit(char *path, char *expr) { char buf[512], *p; int fd; @@ -35,7 +53,7 @@ find2(char *path, char *expr) if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ printf("find: path too long\n"); close(fd); - break; + return; } strcpy(buf, path); p = buf+strlen(buf); @@ -43,28 +61,36 @@ find2(char *path, char *expr) while(read(fd, &de, sizeof(de)) == sizeof(de)){ - if(de.inum == 0) + if(de.inum == 0){ continue; - if (strcmp(de.name, '.') == 0 || strcmp(de.name, '..') == 0) { + } + if (strcmp(de.name, ".") == 0 || strcmp(de.name, "..") == 0) { continue; } memmove(p, de.name, DIRSIZ); p[DIRSIZ] = 0; + if(stat(buf, &st) < 0){ printf("find: cannot stat %s\n", buf); continue; } - printf("%d %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); + //printf("%d %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); + if(st.type == T_DIR){ + findit(buf, expr); + } else if (match(expr,de.name)) { + printf("path: %s\n", buf); + } } - } else { + close(fd); + } /*else { printf("Error found while traversing dirs \n"); - } - + }*/ + /* switch(st.type){ case T_DEVICE: case T_FILE: - printf("%s %d %d %d\n", fmtname(path), st.type, st.ino, (int) st.size); + printf("%d %d %d %d\n", fmtname(path), st.type, st.ino, (int) st.size); break; case T_DIR: @@ -72,11 +98,11 @@ find2(char *path, char *expr) printf("find: path too long\n"); break; } - /* + - recursion would happen when we're searching within directories to find the file name given in expr - stringcmp(TFILE == expr), else - base case would be the tree for all files - */ + strcpy(buf, path); p = buf+strlen(buf); *p++ = '/'; @@ -91,13 +117,13 @@ find2(char *path, char *expr) printf("find: cannot stat %s\n", buf); continue; } - printf("%s %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); + printf("%d %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); } break; } - close(fd); + close(fd); */ } - +/* void findit(char *path, char *expression) { @@ -105,7 +131,7 @@ findit(char *path, char *expression) char *p, *q; int fd = 0; printf("Expression: %s\n", expression); - printf("Path: %s\n", path); + printf("Path: %s \n", path); m = 0; while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){ @@ -125,10 +151,16 @@ findit(char *path, char *expression) memmove(buf, p, m); } } -} +}*/ int main(int argc, char *argv[]) { + if(argc < 3){ + fprintf(2, "usage: find path expression\n"); + exit(1); + } + findit(argv[1], argv[2]); +/* int fd, i; char *path; char *expression; @@ -156,6 +188,7 @@ main(int argc, char *argv[]) findit(path, expression); close(fd); } + */ exit(0); } // Regexp matcher from Kernighan & Pike, From 23012929d9f71de65d0a1d6d858a0e02e66c5d2b Mon Sep 17 00:00:00 2001 From: Arya Srivastava Date: Wed, 24 Sep 2025 14:35:12 -0700 Subject: [PATCH 08/11] Implementation works but we need to use makefs to create directory inside of fog os --- user/find.c | 100 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 98 deletions(-) diff --git a/user/find.c b/user/find.c index ef6c7c2..b9b5b02 100644 --- a/user/find.c +++ b/user/find.c @@ -33,7 +33,7 @@ findit(char *path, char *expr) int fd; struct dirent de; struct stat st; - + printf("Path: %s, expr: %s\n", path, expr); if((fd = open(path, O_RDONLY)) < 0){ fprintf(2, "find: cannot open %s\n", path); return; @@ -75,7 +75,6 @@ findit(char *path, char *expr) printf("find: cannot stat %s\n", buf); continue; } - //printf("%d %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); if(st.type == T_DIR){ findit(buf, expr); } else if (match(expr,de.name)) { @@ -83,75 +82,9 @@ findit(char *path, char *expr) } } close(fd); - } /*else { - printf("Error found while traversing dirs \n"); - }*/ - /* - switch(st.type){ - case T_DEVICE: - case T_FILE: - printf("%d %d %d %d\n", fmtname(path), st.type, st.ino, (int) st.size); - break; - - case T_DIR: - if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ - printf("find: path too long\n"); - break; - } - - - recursion would happen when we're searching within directories to find the file name given in expr - - stringcmp(TFILE == expr), else - - base case would be the tree for all files - - strcpy(buf, path); - p = buf+strlen(buf); - *p++ = '/'; - - - while(read(fd, &de, sizeof(de)) == sizeof(de)){ - if(de.inum == 0) - continue; - memmove(p, de.name, DIRSIZ); - p[DIRSIZ] = 0; - if(stat(buf, &st) < 0){ - printf("find: cannot stat %s\n", buf); - continue; - } - printf("%d %d %d %d\n", fmtname(buf), st.type, st.ino, (int) st.size); - } - break; - } - close(fd); */ + } } -/* -void -findit(char *path, char *expression) -{ - int n, m; - char *p, *q; - int fd = 0; - printf("Expression: %s\n", expression); - printf("Path: %s \n", path); - m = 0; - while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){ - m += n; - buf[m] = '\0'; - p = buf; - while((q = strchr(p, '\n')) != 0){ - *q = 0; - if(match(path, p)){ - *q = '\n'; - write(1, p, q+1 - p); - } - p = q+1; - } - if(m > 0){ - m -= p - buf; - memmove(buf, p, m); - } - } -}*/ int main(int argc, char *argv[]) { @@ -160,35 +93,6 @@ main(int argc, char *argv[]) exit(1); } findit(argv[1], argv[2]); -/* - int fd, i; - char *path; - char *expression; - - printf("in user space find.c file\n"); - - if(argc <= 1){ - fprintf(2, "usage: find pattern [file ...]\n"); - exit(1); - } - path = argv[1]; - expression = argv[2]; - - if(argc <= 2){ - findit(path, expression); - printf("find: unable to find due to lack of arguments"); - exit(0); - } - - for(i = 2; i < argc; i++){ - if((fd = open(argv[i], O_RDONLY)) < 0){ - printf("find: cannot open %s\n", argv[i]); - exit(1); - } - findit(path, expression); - close(fd); - } - */ exit(0); } // Regexp matcher from Kernighan & Pike, From 042d7d42dd3cd25f93eafc646b452216facfcc20 Mon Sep 17 00:00:00 2001 From: Smrithi Panuganti Date: Wed, 24 Sep 2025 15:14:52 -0700 Subject: [PATCH 09/11] altered Makefile --- Makefile | 4 +- mkfs/mkfs.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 91c5f1f..14409f0 100644 --- a/Makefile +++ b/Makefile @@ -144,8 +144,8 @@ UPROGS=\ $U/_dorphan\ $U/_find\ -fs.img: mkfs/mkfs README.md $(UPROGS) - mkfs/mkfs fs.img README.md $(UPROGS) +fs.img: mkfs/mkfs README.md $(UPROGS) tests + mkfs/mkfs fs.img README.md $(UPROGS) tests -include kernel/*.d user/*.d diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index 5f080bc..9bbc090 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -4,12 +4,18 @@ #include #include #include +#include +#include +#include +#define dirent xv6_dirent #define stat xv6_stat // avoid clash with host struct stat #include "kernel/types.h" #include "kernel/fs.h" #include "kernel/stat.h" #include "kernel/param.h" +#undef dirent +#undef stat #ifndef static_assert #define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0) @@ -20,9 +26,9 @@ // Disk layout: // [ boot block | sb block | log | inode blocks | free bit map | data blocks ] -int nbitmap = FSSIZE/BPB + 1; +int nbitmap = FSSIZE/(BSIZE*8) + 1; int ninodeblocks = NINODES / IPB + 1; -int nlog = LOGBLOCKS+1; // Header followed by LOGBLOCKS data blocks. +int nlog = LOGSIZE; int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap) int nblocks; // Number of data blocks @@ -65,12 +71,88 @@ xint(uint x) return y; } +void +add_file(int parent_fd, uint parentino, char *filename) +{ + uint inum; + int fd; + char buf[BSIZE]; + struct xv6_dirent de; + + if((fd = openat(parent_fd, filename, 0)) < 0) + die(filename); + + inum = ialloc(T_FILE); + + bzero(&de, sizeof(de)); + de.inum = xshort(inum); + strncpy(de.name, filename, DIRSIZ); + iappend(parentino, &de, sizeof(de)); + + ssize_t cc; + while((cc = read(fd, buf, sizeof(buf))) > 0) + iappend(inum, buf, cc); + + close(fd); +} + +uint +add_dir(int level, int parent_fd, uint parentino, char *dirname) +{ + struct xv6_dirent de; + uint dino = ialloc(T_DIR); + bzero(&de, sizeof(de)); + de.inum = xshort(dino); + strcpy(de.name, dirname); + iappend(parentino, &de, sizeof(de)); + + bzero(&de, sizeof(de)); + de.inum = xshort(dino); + strcpy(de.name, "."); + iappend(dino, &de, sizeof(de)); + + bzero(&de, sizeof(de)); + de.inum = xshort(parentino); + strcpy(de.name, ".."); + iappend(dino, &de, sizeof(de)); + + int dir_fd = -1; + if ((dir_fd = openat(parent_fd, dirname, O_RDONLY)) == -1) { + perror("open"); + return dino; + } + + DIR *d = fdopendir(dir_fd); + if (d == NULL) { + perror("fdopendir"); + return dino; + } + + struct dirent *e; + while ((e = readdir(d)) != NULL) { + if (e->d_name[0] == '.') { + continue; + } + + if (e->d_type == DT_REG) { + printf("%*s+ %s\n", level * 2, "", e->d_name); + add_file(dir_fd, dino, e->d_name); + } else if (e->d_type == DT_DIR) { + printf("%*s+ /%s\n", level * 2, "", e->d_name); + add_dir(level + 1, dir_fd, dino, e->d_name); + } + } + close(dir_fd); + + return dino; +} + int main(int argc, char *argv[]) { int i, cc, fd; uint rootino, inum, off; - struct dirent de; + struct xv6_dirent de; char buf[BSIZE]; struct dinode din; @@ -83,7 +165,7 @@ main(int argc, char *argv[]) } assert((BSIZE % sizeof(struct dinode)) == 0); - assert((BSIZE % sizeof(struct dirent)) == 0); + assert((BSIZE % sizeof(struct xv6_dirent)) == 0); fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fsfd < 0) @@ -102,7 +184,7 @@ main(int argc, char *argv[]) sb.inodestart = xint(2+nlog); sb.bmapstart = xint(2+nlog+ninodeblocks); - printf("nmeta %d (boot, super, log blocks %u, inode blocks %u, bitmap blocks %u) blocks %d total %d\n", + printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n", nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE); freeblock = nmeta; // the first free block that we can allocate @@ -128,6 +210,13 @@ main(int argc, char *argv[]) iappend(rootino, &de, sizeof(de)); for(i = 2; i < argc; i++){ + struct stat sb; + stat(argv[i], &sb); + if (S_ISDIR(sb.st_mode)) { + add_dir(0, AT_FDCWD, rootino, argv[i]); + continue; + } + // get rid of "user/" char *shortname; if(strncmp(argv[i], "user/", 5) == 0) @@ -147,8 +236,6 @@ main(int argc, char *argv[]) if(shortname[0] == '_') shortname += 1; - assert(strlen(shortname) <= DIRSIZ); - inum = ialloc(T_FILE); bzero(&de, sizeof(de)); @@ -240,7 +327,7 @@ balloc(int used) int i; printf("balloc: first %d blocks have been allocated\n", used); - assert(used < BPB); + assert(used < BSIZE*8); bzero(buf, BSIZE); for(i = 0; i < used; i++){ buf[i/8] = buf[i/8] | (0x1 << (i%8)); From ca8aa69191691160570df0bd93452c7817fde0c3 Mon Sep 17 00:00:00 2001 From: Arya Srivastava Date: Wed, 24 Sep 2025 15:20:09 -0700 Subject: [PATCH 10/11] Changed mkfs to new mkfs --- mkfs/mkfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index 9bbc090..5e3873e 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -28,7 +28,7 @@ int nbitmap = FSSIZE/(BSIZE*8) + 1; int ninodeblocks = NINODES / IPB + 1; -int nlog = LOGSIZE; +int nlog = LOGBLOCKS; int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap) int nblocks; // Number of data blocks From 2288d25a8f3bd90475cc5872aed8cf8417f24813 Mon Sep 17 00:00:00 2001 From: Smrithi Panuganti Date: Mon, 29 Sep 2025 14:36:22 -0700 Subject: [PATCH 11/11] added additional message as well as documentation in find.md --- docs/find.md | 23 +++++++++++++++++++++++ user/find.c | 5 ++--- 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 docs/find.md diff --git a/docs/find.md b/docs/find.md new file mode 100644 index 0000000..0d3fb04 --- /dev/null +++ b/docs/find.md @@ -0,0 +1,23 @@ +FogOS - Find Implementation + +Build +- Build the OS as usual (make qemu) + +Run +- In the command line type: find +- If "." is given as path, then it will look through current directory +- If 0 args are given it will return an error statement +- If expression/file does not exist in path, then it will return an error statement +- The output will be printed in the following format if successful + - Path: /tests/itests test1.txt + +Test +- Created local testing directory (tests) containing two text files and another directory (itests) with a text file + +Test Input 1: find . test1.txt +Test Output 1: Path: /tests/itest/test1.txt test1.txt /tests/test1.txt test1.txt + +Test Input 2: find /tests test2.txt +Test Output 2: Path: /tests/test2.txt test2.txt + + diff --git a/user/find.c b/user/find.c index b9b5b02..8b7ff9f 100644 --- a/user/find.c +++ b/user/find.c @@ -33,7 +33,6 @@ findit(char *path, char *expr) int fd; struct dirent de; struct stat st; - printf("Path: %s, expr: %s\n", path, expr); if((fd = open(path, O_RDONLY)) < 0){ fprintf(2, "find: cannot open %s\n", path); return; @@ -47,7 +46,7 @@ findit(char *path, char *expr) if (st.type == T_FILE) { if (match(expr, fmtname(path))) { - printf("path: %s\n", path); + printf("Path: %s\n", path); } } else if (T_DIR) { if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){ @@ -78,7 +77,7 @@ findit(char *path, char *expr) if(st.type == T_DIR){ findit(buf, expr); } else if (match(expr,de.name)) { - printf("path: %s\n", buf); + printf("Path: %s \nFound!\n", buf); } } close(fd);