main.c 3.3 KB

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