From b047d4e7d8bd4728b7adb06ad0901e306eaef593 Mon Sep 17 00:00:00 2001 From: Zhiyuan Shao Date: Mon, 8 May 2023 15:42:40 +0800 Subject: [PATCH] init commit of lab4_challenge1 --- Makefile | 2 +- user/app_hardlink.c | 94 ----------------------------------------- user/app_relativepath.c | 67 +++++++++++++++++++++++++++++ user/user_lib.c | 14 ++++++ user/user_lib.h | 2 + 5 files changed, 84 insertions(+), 95 deletions(-) delete mode 100644 user/app_hardlink.c create mode 100644 user/app_relativepath.c diff --git a/Makefile b/Makefile index 980e035..6167717 100644 --- a/Makefile +++ b/Makefile @@ -68,7 +68,7 @@ USER_CPPS := user/*.c USER_CPPS := $(wildcard $(USER_CPPS)) USER_OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(USER_CPPS))) -USER_TARGET := $(OBJ_DIR)/app_hardlink +USER_TARGET := $(OBJ_DIR)/app_relativepath #------------------------targets------------------------ $(OBJ_DIR): @-mkdir -p $(OBJ_DIR) diff --git a/user/app_hardlink.c b/user/app_hardlink.c deleted file mode 100644 index 7e209e5..0000000 --- a/user/app_hardlink.c +++ /dev/null @@ -1,94 +0,0 @@ -#include "user_lib.h" -#include "util/string.h" -#include "util/types.h" - -void ls(char *path) { - int dir_fd = opendir_u(path); - printu("------------------------------\n"); - printu("ls \"%s\":\n", path); - printu("[name] [inode_num]\n"); - struct dir dir; - int width = 20; - while(readdir_u(dir_fd, &dir) == 0) { - // we do not have %ms :( - char name[width + 1]; - memset(name, ' ', width + 1); - name[width] = '\0'; - if (strlen(dir.name) < width) { - strcpy(name, dir.name); - name[strlen(dir.name)] = ' '; - printu("%s %d\n", name, dir.inum); - } - else - printu("%s %d\n", dir.name, dir.inum); - } - printu("------------------------------\n"); - closedir_u(dir_fd); -} - -int main(int argc, char *argv[]) { - int MAXBUF = 512; - char str[] = "hello world"; - char buf[MAXBUF]; - int fd1, fd2; - - printu("\n======== establish the file ========\n"); - - fd1 = open("/RAMDISK0/ramfile", O_RDWR | O_CREAT); - printu("create file: /RAMDISK0/ramfile\n"); - close(fd1); - - printu("\n======== Test 1: hard link ========\n"); - - link_u("/RAMDISK0/ramfile", "/RAMDISK0/ramfile2"); - printu("create hard link: /RAMDISK0/ramfile2 -> /RAMDISK0/ramfile\n"); - - fd1 = open("/RAMDISK0/ramfile", O_RDWR); - fd2 = open("/RAMDISK0/ramfile2", O_RDWR); - - printu("file descriptor fd1 (ramfile): %d\n", fd1); - printu("file descriptor fd2 (ramfile2): %d\n", fd2); - - // check the number of hard links to ramfile on disk - struct istat st; - disk_stat_u(fd1, &st); - printu("ramfile hard links: %d\n", st.st_nlinks); - if (st.st_nlinks != 2) { - printu("ERROR: the number of hard links to ramfile should be 2, but it is %d\n", - st.st_nlinks); - exit(-1); - } - - write_u(fd1, str, strlen(str)); - printu("/RAMDISK0/ramfile write content: \n%s\n", str); - - read_u(fd2, buf, MAXBUF); - printu("/RAMDISK0/ramfile2 read content: \n%s\n", buf); - - close(fd1); - close(fd2); - - printu("\n======== Test 2: unlink ========\n"); - - ls("/RAMDISK0"); - - unlink_u("/RAMDISK0/ramfile"); - printu("unlink: /RAMDISK0/ramfile\n"); - - ls("/RAMDISK0"); - - // check the number of hard links to ramfile2 on disk - fd2 = open("/RAMDISK0/ramfile2", O_RDWR); - disk_stat_u(fd2, &st); - printu("ramfile2 hard links: %d\n", st.st_nlinks); - if (st.st_nlinks != 1) { - printu("ERROR: the number of hard links to ramfile should be 1, but it is %d\n", - st.st_nlinks); - exit(-1); - } - close(fd2); - - printu("\nAll tests passed!\n\n"); - exit(0); - return 0; -} diff --git a/user/app_relativepath.c b/user/app_relativepath.c new file mode 100644 index 0000000..0787c4c --- /dev/null +++ b/user/app_relativepath.c @@ -0,0 +1,67 @@ +#include "user_lib.h" +#include "util/string.h" +#include "util/types.h" + +void pwd() { + char path[30]; + read_cwd(path); + printu("cwd:%s\n", path); +} + +void cd(const char *path) { + if (change_cwd(path) != 0) + printu("cd failed\n"); +} + +int main(int argc, char *argv[]) { + int fd; + int MAXBUF = 512; + char buf[MAXBUF]; + char str[] = "hello world"; + int fd1, fd2; + + printu("\n======== Test 1: change current directory ========\n"); + + pwd(); + cd("./RAMDISK0"); + printu("change current directory to ./RAMDISK0\n"); + pwd(); + + printu("\n======== Test 2: write/read file by relative path ========\n"); + printu("write: ./ramfile\n"); + + fd = open("./ramfile", O_RDWR | O_CREAT); + printu("file descriptor fd: %d\n", fd); + + write_u(fd, str, strlen(str)); + printu("write content: \n%s\n", str); + close(fd); + + fd = open("./ramfile", O_RDWR); + printu("read: ./ramfile\n"); + + read_u(fd, buf, MAXBUF); + printu("read content: \n%s\n", buf); + close(fd); + + printu("\n======== Test 3: Go to parent directory ========\n"); + + pwd(); + cd(".."); + printu("change current directory to ..\n"); + pwd(); + + printu("read: ./hostfile.txt\n"); + + fd = open("./hostfile.txt", O_RDONLY); + printu("file descriptor fd: %d\n", fd); + + read_u(fd, buf, MAXBUF); + printu("read content: \n%s\n", buf); + + close(fd); + + printu("\nAll tests passed!\n\n"); + exit(0); + return 0; +} diff --git a/user/user_lib.c b/user/user_lib.c index 1c4f6d1..8eb160a 100644 --- a/user/user_lib.c +++ b/user/user_lib.c @@ -167,3 +167,17 @@ int unlink_u(const char *fn){ int close(int fd) { return do_user_call(SYS_user_close, fd, 0, 0, 0, 0, 0, 0); } + +// +// lib call to read present working directory (pwd) +// +int read_cwd(char *path) { + return do_user_call(SYS_user_rcwd, (uint64)path, 0, 0, 0, 0, 0, 0); +} + +// +// lib call to change pwd +// +int change_cwd(const char *path) { + return do_user_call(SYS_user_ccwd, (uint64)path, 0, 0, 0, 0, 0, 0); +} diff --git a/user/user_lib.h b/user/user_lib.h index 95a686d..aa678e7 100644 --- a/user/user_lib.h +++ b/user/user_lib.h @@ -33,5 +33,7 @@ int closedir_u(int fd); int link_u(const char *fn1, const char *fn2); int unlink_u(const char *fn); +int read_cwd(char *path); +int change_cwd(const char *path); #endif