init commit of lab4_challenge1

lab4_challenge1_relativepath
Zhiyuan Shao 2 years ago
parent 38183534a3
commit b047d4e7d8

@ -68,7 +68,7 @@ USER_CPPS := user/*.c
USER_CPPS := $(wildcard $(USER_CPPS)) USER_CPPS := $(wildcard $(USER_CPPS))
USER_OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(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------------------------ #------------------------targets------------------------
$(OBJ_DIR): $(OBJ_DIR):
@-mkdir -p $(OBJ_DIR) @-mkdir -p $(OBJ_DIR)

@ -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;
}

@ -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;
}

@ -167,3 +167,17 @@ int unlink_u(const char *fn){
int close(int fd) { int close(int fd) {
return do_user_call(SYS_user_close, fd, 0, 0, 0, 0, 0, 0); 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);
}

@ -33,5 +33,7 @@ int closedir_u(int fd);
int link_u(const char *fn1, const char *fn2); int link_u(const char *fn1, const char *fn2);
int unlink_u(const char *fn); int unlink_u(const char *fn);
int read_cwd(char *path);
int change_cwd(const char *path);
#endif #endif

Loading…
Cancel
Save