Update chapter6_filesystem.md

pull/16/head
Zhiyuan Shao 2 years ago
parent 3b5efb7b28
commit 820f42afa3

@ -1719,7 +1719,7 @@ $ git commit -a -m "my work on lab4_2 is done."
#### **给定应用** #### **给定应用**
- user/app_hardlinks.c - user/app_hardlink.c
```c ```c
@ -2058,62 +2058,73 @@ $ git commit -a -m "my work on lab4_3 is done."
- user/app_relativepath.c - user/app_relativepath.c
```c ```c
01 #include "user_lib.h" 1 #include "user_lib.h"
02 #include "util/string.h" 2 #include "util/string.h"
03 #include "util/types.h" 3 #include "util/types.h"
04 4
05 int main(int argc, char *argv[]) { 5 void pwd() {
06 int fd; 6 char path[30];
07 int MAXBUF = 512; 7 read_cwd(path);
08 char buf[MAXBUF]; 8 printu("cwd:%s\n", path);
09 char str[] = "hello world"; 9 }
10 int fd1, fd2; 10
11 11 void cd(const char *path) {
12 printu("\n======== Test 1: change current directory ========\n"); 12 if (change_cwd(path) != 0)
13 13 printu("cd failed\n");
14 pwd(); 14 }
15 cd("./RAMDISK0"); 15
16 printu("change current directory to ./RAMDISK0\n"); 16 int main(int argc, char *argv[]) {
17 pwd(); 17 int fd;
18 18 int MAXBUF = 512;
19 printu("\n======== Test 2: write/read file by relative path ========\n"); 19 char buf[MAXBUF];
20 printu("write: ./ramfile\n"); 20 char str[] = "hello world";
21 21 int fd1, fd2;
22 fd = open("./ramfile", O_RDWR | O_CREAT); 22
23 printu("file descriptor fd: %d\n", fd); 23 printu("\n======== Test 1: change current directory ========\n");
24 24
25 write_u(fd, str, strlen(str)); 25 pwd();
26 printu("write content: \n%s\n", str); 26 cd("./RAMDISK0");
27 close(fd); 27 printu("change current directory to ./RAMDISK0\n");
28 28 pwd();
29 fd = open("./ramfile", O_RDWR); 29
30 printu("read: ./ramfile\n"); 30 printu("\n======== Test 2: write/read file by relative path ========\n");
31 31 printu("write: ./ramfile\n");
32 read_u(fd, buf, MAXBUF); 32
33 printu("read content: \n%s\n", buf); 33 fd = open("./ramfile", O_RDWR | O_CREAT);
34 close(fd); 34 printu("file descriptor fd: %d\n", fd);
35 35
36 printu("\n======== Test 3: Go to parent directory ========\n"); 36 write_u(fd, str, strlen(str));
37 37 printu("write content: \n%s\n", str);
38 pwd(); 38 close(fd);
39 cd(".."); 39
40 printu("change current directory to ..\n"); 40 fd = open("./ramfile", O_RDWR);
41 pwd(); 41 printu("read: ./ramfile\n");
42 42
43 printu("read: ./hostfile.txt\n"); 43 read_u(fd, buf, MAXBUF);
44 44 printu("read content: \n%s\n", buf);
45 fd = open("./hostfile.txt", O_RDONLY); 45 close(fd);
46 printu("file descriptor fd: %d\n", fd); 46
47 47 printu("\n======== Test 3: Go to parent directory ========\n");
48 read_u(fd, buf, MAXBUF); 48
49 printu("read content: \n%s\n", buf); 49 pwd();
50 50 cd("..");
51 close(fd); 51 printu("change current directory to ..\n");
52 52 pwd();
53 printu("\nAll tests passed!\n\n"); 53
54 exit(0); 54 printu("read: ./hostfile.txt\n");
55 return 0; 55
56 } 56 fd = open("./hostfile.txt", O_RDONLY);
57 printu("file descriptor fd: %d\n", fd);
58
59 read_u(fd, buf, MAXBUF);
60 printu("read content: \n%s\n", buf);
61
62 close(fd);
63
64 printu("\nAll tests passed!\n\n");
65 exit(0);
66 return 0;
67 }
``` ```
相对路径即形如:“`./file`”、“`./dir/file`”以及“`../dir/file`”的路径形式。与绝对路径总是从根目录开始逐级指定文件或目录在目录树中的位置不同,相对路径从进程的“当前工作目录“开始,对一个文件或目录在目录树中的位置进行描述。 相对路径即形如:“`./file`”、“`./dir/file`”以及“`../dir/file`”的路径形式。与绝对路径总是从根目录开始逐级指定文件或目录在目录树中的位置不同,相对路径从进程的“当前工作目录“开始,对一个文件或目录在目录树中的位置进行描述。

Loading…
Cancel
Save