From dd7bb7a71f908cad9170b3154f39dc86ea16ac3a Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Thu, 26 Sep 2024 18:35:02 -0700 Subject: [PATCH 1/8] added find file in user and updated Makefile --- Makefile | 1 + user/find.c | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 user/find.c diff --git a/Makefile b/Makefile index 2584e4a..e82af68 100644 --- a/Makefile +++ b/Makefile @@ -116,6 +116,7 @@ mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h .PRECIOUS: %.o UPROGS=\ + $U/_find\ $U/_cat\ $U/_echo\ $U/_forktest\ diff --git a/user/find.c b/user/find.c new file mode 100644 index 0000000..b56448b --- /dev/null +++ b/user/find.c @@ -0,0 +1,6 @@ +#include "kernel/types.h" +#include "kernel/stat.h" +#include "user/user.h" +#include "kernel/fcntl.h" +#include "stdbool.h" + From 41040f68447e68f996277c7aefa1217a5f8ea28c Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Sat, 28 Sep 2024 17:28:50 -0700 Subject: [PATCH 2/8] esha's changes to the find command --- user/find.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/user/find.c b/user/find.c index b56448b..24dd340 100644 --- a/user/find.c +++ b/user/find.c @@ -3,4 +3,73 @@ #include "user/user.h" #include "kernel/fcntl.h" #include "stdbool.h" +#include "kernel/fs.h" +void find(const char *path, const char *target) { + int fd; + struct stat st; + struct dirent de; + char buf[512], *p; + + if ((fd = open(path, O_RDONLY)) < 0) { + printf("find: cannot open %s\n", path); + return; + } + + if (fstat(fd, &st) < 0) { + printf("find: cannot stat %s\n", path); + close(fd); + return; + } + + // if it's a directory + if (st.type == T_DIR) { + if (strlen(path) + 1 + DIRSIZ + 1 > sizeof(buf)) { + printf("find: path too long\n"); + return; + } + + strcpy(buf, path); + p = buf + strlen(buf); + *p++ = '/'; + + while (read(fd, &de, sizeof(de)) == sizeof(de)) { + if (de.inum == 0) // Skip invalid args + continue; + + memmove(p, de.name, DIRSIZ); + p[DIRSIZ] = 0; + + // skip "." and ".." + if (strcmp(de.name, ".") == 0 || strcmp(de.name, "..") == 0) + continue; + + if (stat(buf, &st) < 0) { + printf("find: cannot stat %s\n", buf); + continue; + } + + // if the file/directory name matches the target + if (strcmp(de.name, target) == 0) { + printf("%s\n", buf); // print if it matches + } + + // recurse into it if it's a directory + if (st.type == T_DIR) { + find(buf, target); + } + } + } + + close(fd); +} + +int main(int argc, char *argv[]) { + if (argc < 3) { + printf("Usage: find \n"); + exit(1); + } + + find(argv[1], argv[2]); + exit(0); +} From f0d687d9a1d9ce4cc3a6260945d9c82e86588c23 Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Sat, 28 Sep 2024 19:48:04 -0700 Subject: [PATCH 3/8] added testing files --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e82af68..1ce939c 100644 --- a/Makefile +++ b/Makefile @@ -134,8 +134,8 @@ UPROGS=\ $U/_wc\ $U/_zombie\ -fs.img: mkfs/mkfs README.md $(UPROGS) - mkfs/mkfs fs.img README.md $(UPROGS) +fs.img: mkfs/mkfs README.md $(UPROGS) tester.txt + mkfs/mkfs fs.img README.md $(UPROGS) tester.txt -include kernel/*.d user/*.d From 623156ec362c99fd544448ffff13b85f2ea74793 Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Sun, 29 Sep 2024 16:07:22 -0700 Subject: [PATCH 4/8] added -type flag for both files and directories --- user/find.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/user/find.c b/user/find.c index 24dd340..4e2de8d 100644 --- a/user/find.c +++ b/user/find.c @@ -5,7 +5,9 @@ #include "stdbool.h" #include "kernel/fs.h" -void find(const char *path, const char *target) { +//goal: to recursively search thru directories to find a file + +void find(const char *path, const char *flag, const char *target) { int fd; struct stat st; struct dirent de; @@ -22,7 +24,7 @@ void find(const char *path, const char *target) { return; } - // if it's a directory + // if it's a directory, traverse it if (st.type == T_DIR) { if (strlen(path) + 1 + DIRSIZ + 1 > sizeof(buf)) { printf("find: path too long\n"); @@ -49,6 +51,14 @@ void find(const char *path, const char *target) { continue; } + if ((strcmp(target, "d") == 0) && (strcmp(flag, "-type") == 0) && (st.type == T_DIR)) { + printf("%s\n", buf); + } + + if ((strcmp(target, "f") == 0) && (strcmp(flag, "-type") == 0) && (st.type == T_FILE)) { + printf("%s\n", buf); + } + // if the file/directory name matches the target if (strcmp(de.name, target) == 0) { printf("%s\n", buf); // print if it matches @@ -56,7 +66,7 @@ void find(const char *path, const char *target) { // recurse into it if it's a directory if (st.type == T_DIR) { - find(buf, target); + find(buf, flag, target); } } } @@ -70,6 +80,6 @@ int main(int argc, char *argv[]) { exit(1); } - find(argv[1], argv[2]); + find(argv[1], argv[2], argv[3]); exit(0); } From 80bf1a4891b044aede487a823d0e6bb2ad8d1474 Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Sun, 29 Sep 2024 19:41:54 -0700 Subject: [PATCH 5/8] created a match function for -name type but not working --- user/find.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/user/find.c b/user/find.c index 4e2de8d..1a35307 100644 --- a/user/find.c +++ b/user/find.c @@ -7,6 +7,33 @@ //goal: to recursively search thru directories to find a file + +bool matches(const char *str, const char *pattern) { + const char *tmp_pattern = pattern; + const char *tmp_str = str; + const char *star; + int last_match = -1; + + for (int i = 0; *tmp_str; i++) { + if (*tmp_pattern == tmp_str[i] || *tmp_pattern == '?') { + tmp_pattern++; + } else if (*tmp_pattern == '*') { + star = tmp_pattern++; + last_match = i; + } else if (star) { + i = last_match; + } else { + return false; + } + } + + while (*tmp_pattern == '*') { + tmp_pattern++; + } + + return *tmp_pattern == '\0'; +} + void find(const char *path, const char *flag, const char *target) { int fd; struct stat st; @@ -51,17 +78,20 @@ void find(const char *path, const char *flag, const char *target) { continue; } + // prints all directory if argument is like "find path -type d" if ((strcmp(target, "d") == 0) && (strcmp(flag, "-type") == 0) && (st.type == T_DIR)) { printf("%s\n", buf); } + // prints all files if argument is like "find path -type f" if ((strcmp(target, "f") == 0) && (strcmp(flag, "-type") == 0) && (st.type == T_FILE)) { printf("%s\n", buf); } // if the file/directory name matches the target - if (strcmp(de.name, target) == 0) { - printf("%s\n", buf); // print if it matches + // see if target includes any symbols like * or ? + if (strcmp(flag, "-name") == 0 && matches(de.name, target)) { + printf("%s\n", buf); // print if it matches } // recurse into it if it's a directory From 65b79ba69722c758ac192af544301ea02dfb57ae Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Sun, 29 Sep 2024 19:52:45 -0700 Subject: [PATCH 6/8] compiles but not tested --- user/find.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/user/find.c b/user/find.c index 1a35307..6917bea 100644 --- a/user/find.c +++ b/user/find.c @@ -11,20 +11,23 @@ bool matches(const char *str, const char *pattern) { const char *tmp_pattern = pattern; const char *tmp_str = str; - const char *star; + int star = -1; int last_match = -1; for (int i = 0; *tmp_str; i++) { - if (*tmp_pattern == tmp_str[i] || *tmp_pattern == '?') { + if (*tmp_pattern == *tmp_str || *tmp_pattern == '?') { tmp_pattern++; } else if (*tmp_pattern == '*') { - star = tmp_pattern++; + star = i; + tmp_pattern++; last_match = i; - } else if (star) { + } else if (star != -1) { i = last_match; + tmp_pattern = pattern + (tmp_pattern - pattern); } else { return false; } + tmp_str++; } while (*tmp_pattern == '*') { From 361fc3ea408fd55ae6b7aa120a05ddbdf6dca443 Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Sun, 29 Sep 2024 21:20:25 -0700 Subject: [PATCH 7/8] find works and tested, javadoc added --- user/find.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/user/find.c b/user/find.c index 6917bea..461c731 100644 --- a/user/find.c +++ b/user/find.c @@ -6,8 +6,16 @@ #include "kernel/fs.h" //goal: to recursively search thru directories to find a file - - +// cite/research: https://www.geeksforgeeks.org/find-command-in-linux-with-examples/ + +/** + * checks if pattern (file name or other symbols) matches target string + * patterns include: * or ? + * + * @param str file/directory name + * @param pattern target provided + * @return true if file/directory matches name or pattern + */ bool matches(const char *str, const char *pattern) { const char *tmp_pattern = pattern; const char *tmp_str = str; @@ -15,28 +23,42 @@ bool matches(const char *str, const char *pattern) { int last_match = -1; for (int i = 0; *tmp_str; i++) { + // move pattern pointer to next if a match or contains ? character if (*tmp_pattern == *tmp_str || *tmp_pattern == '?') { tmp_pattern++; + // save position of * when found and save index of last match if string contains * character } else if (*tmp_pattern == '*') { + // star position star = i; tmp_pattern++; last_match = i; + // check if theres a mismatch after * character and revisit to last matched char } else if (star != -1) { i = last_match; tmp_pattern = pattern + (tmp_pattern - pattern); + // no match found } else { return false; } tmp_str++; } + // condition added to skip other chars after * in the pattern while (*tmp_pattern == '*') { tmp_pattern++; } + // return if pattern matches strings return *tmp_pattern == '\0'; } +/** + * finds path towards specific files or directories (some restrictions include) + * + * @param path starting directory for search + * @param flag settings or conditions + * @param target pattern or file/directory name + */ void find(const char *path, const char *flag, const char *target) { int fd; struct stat st; @@ -107,6 +129,13 @@ void find(const char *path, const char *flag, const char *target) { close(fd); } +/** + * main method used for testing + * + * @param argc number of arguments + * @param argv array of arguments + * @return exit code + */ int main(int argc, char *argv[]) { if (argc < 3) { printf("Usage: find \n"); From 14356f4d2bdd6320091f0c06b78e39169b8261db Mon Sep 17 00:00:00 2001 From: Johanna Lazaro Date: Sun, 29 Sep 2024 21:47:10 -0700 Subject: [PATCH 8/8] forgot to add test file --- tester.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tester.txt diff --git a/tester.txt b/tester.txt new file mode 100644 index 0000000..9473cb1 --- /dev/null +++ b/tester.txt @@ -0,0 +1,17 @@ + ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡤⠤⠤⠤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡤⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠉⠛⢦⣤⠶⠦⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠀⠀⠀⠀⠀⠀⢀⣴⠞⢋⡽⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠃⠀⠀⠙⢶⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠀⠀⠀⠀⠀⣰⠟⠁⠀⠘⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡀⠀⠀⠉⠓⠦⣤⣤⣤⣤⣤⣤⣄⣀⠀⠀⠀ +⠀⠀⠀⠀⣠⠞⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⣷⡄⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣆⠀ +⠀⠀⣠⠞⠁⠀⠀⣀⣠⣏⡀⠀⢠⣶⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⠿⡃⠀⠀⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⡆ +⢀⡞⠁⠀⣠⠶⠛⠉⠉⠉⠙⢦⡸⣿⡿⠀⠀⠀⡄⢀⣀⣀⡶⠀⠀⠀⢀⡄⣀⠀⣢⠟⢦⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⠃ +⡞⠀⠀⠸⠁⠀⠀⠀⠀⠀⠀⢳⢀⣠⠀⠀⠀⠉⠉⠀⠀⣀⠀⠀⠀⢀⣠⡴⠞⠁⠀⠀⠈⠓⠦⣄⣀⠀⠀⠀⠀⣀⣤⠞⠁⠀ +⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠁⠀⢀⣀⣀⡴⠋⢻⡉⠙⠾⡟⢿⣅⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠙⠛⠉⠉⠀⠀⠀⠀ +⠘⣦⡀⠀⠀⠀⠀⠀⠀⣀⣤⠞⢉⣹⣯⣍⣿⠉⠟⠀⠀⣸⠳⣄⡀⠀⠀⠙⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠈⠙⠒⠒⠒⠒⠚⠋⠁⠀⡴⠋⢀⡀⢠⡇⠀⠀⠀⠀⠃⠀⠀⠀⠀⠀⢀⡾⠋⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⢸⡀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⢠⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣇⠀⠀⠉⠋⠻⣄⠀⠀⠀⠀⠀⣀⣠⣴⠞⠋⠳⠶⠞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠳⠦⢤⠤⠶⠋⠙⠳⣆⣀⣈⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ +⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ + +⠀Hi I'm testing the find command right now!