1 // #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <sys/mount.h>
10
11 #define MAX_PATH_LENGTH 100
12 #define MAX_DIR_DEPTH 4
13
14
main(int argc,char const * argv[])15 int main(int argc, char const* argv[]) {
16
17 if (mkdir("/SOME", 0777) == -1) {
18 perror("Failed to create directory under /some");
19 return 1;
20 }
21
22 // Create a directory under /SOME/RAMFS
23 if (mkdir("/SOME/RAMFS", 0777) == -1) {
24 perror("Failed to create directory under /SOME/RAMFS");
25 return 1;
26 }
27
28 // Mount the first ramfs at /SOME/RAMFS
29 if (mount("", "/SOME/RAMFS", "ramfs", 0, NULL) == -1) {
30 perror("Failed to mount ramfs at /SOME/RAMFS");
31 return 1;
32 }
33
34 if (mkdir("/SOME/RAMFS/some", 0777) == -1) {
35 perror("Failed to create directory under /SOME/RAMFS/some");
36 return 1;
37 }
38
39 puts("Success mkdir /SOME/RAMFS/some");
40
41 // Create a directory under /SOME/RAMFS/some/another
42 if (mkdir("/SOME/RAMFS/some/another", 0777) == -1) {
43 perror("Failed to create directory under /SOME/RAMFS/some/another");
44 return 1;
45 }
46
47 puts("Success mkdir /SOME/RAMFS/some/another");
48
49 if (mount("", "/SOME/RAMFS/some/another", "ramfs", 0, NULL) == -1) {
50 perror("Failed to mount ramfs at /SOME/RAMFS/some/another");
51 return 1;
52 }
53
54 puts("Success mount on /SOME/RAMFS/some/another");
55
56 if (mkdir("/SOME/RAMFS/some/another/just_another", 0777) == -1) {
57 perror("Failed to create directory under /SOME/RAMFS/some/another");
58 return 1;
59 }
60
61 puts("Success mkdir /SOME/RAMFS/some/another/just_another");
62
63 if (mount("", "/SOME/RAMFS/some/another/just_another", "ramfs", 0, NULL) == -1) {
64 perror("Failed to mount ramfs at /SOME/RAMFS/some/another");
65 return 1;
66 }
67
68 puts("Success mount on /SOME/RAMFS/some/another/just_another");
69
70 // Write files under /SOME/RAMFS and /SOME/RAMFS/some/another
71 FILE* file1 = fopen("/SOME/RAMFS/file1.txt", "w");
72 if (file1 == NULL) {
73 perror("Failed to open /SOME/RAMFS/file1.txt");
74 return 1;
75 }
76 fprintf(file1, "This is file1.txt\n");
77 fclose(file1);
78
79 FILE* file2 = fopen("/SOME/RAMFS/some/another/file2.txt", "w");
80 if (file2 == NULL) {
81 perror("Failed to open /SOME/RAMFS/some/another/file2.txt");
82 return 1;
83 }
84 fprintf(file2, "This is file2.txt\n");
85 fclose(file2);
86
87 FILE* file3 = fopen("/SOME/RAMFS/some/another/just_another/file3.txt", "w+");
88 if (file3 == NULL) {
89 perror("Failed to open /SOME/RAMFS/some/another/just_another/file3.txt");
90 return 1;
91 }
92 fprintf(file3, "Multi mount behave well.\n");
93 // print file3.txt
94 char buffer[100];
95 fseek(file3, 0, SEEK_SET);
96 fread(buffer, 1, 100, file3);
97 printf("file3.txt content: %s\n", buffer);
98 fclose(file3);
99
100 // test umount with flags ( use umount2 )
101 if (umount("/SOME/RAMFS/some/another/just_another") == -1) {
102 perror("Failed to umount ramfs at /SOME/RAMFS/some/another/just_another");
103 return 1;
104 }
105
106 puts("Successful umount /SOME/RAMFS/some/another/just_another");
107
108 // delete just_another
109 if (rmdir("/SOME/RAMFS/some/another/just_another") == -1) {
110 perror("Failed to delete /SOME/RAMFS/some/another/just_another");
111 return 1;
112 }
113
114 return 0;
115 }