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