test.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "Ext2FileSystem.h"
  2. #include "FileBackedDiskDevice.h"
  3. #include "VirtualFileSystem.h"
  4. #include "FileDescriptor.h"
  5. #include "SyntheticFileSystem.h"
  6. #include "ZeroDevice.h"
  7. #include "NullDevice.h"
  8. #include "FullDevice.h"
  9. #include "RandomDevice.h"
  10. #include <cstring>
  11. #include <AK/FileSystemPath.h>
  12. #include <AK/SimpleMalloc.h>
  13. #include <AK/StdLib.h>
  14. #include <AK/kmalloc.h>
  15. #include <AK/ktime.h>
  16. static RetainPtr<FS> makeFileSystem(const char* imagePath);
  17. int main(int c, char** v)
  18. {
  19. const char* filename = "small.fs";
  20. if (c >= 2)
  21. filename = v[1];
  22. VFS::initialize_globals();
  23. VFS vfs;
  24. auto zero = make<ZeroDevice>();
  25. vfs.register_character_device(*zero);
  26. auto null = make<NullDevice>();
  27. vfs.register_character_device(*null);
  28. auto full = make<FullDevice>();
  29. vfs.register_character_device(*full);
  30. auto random = make<RandomDevice>();
  31. vfs.register_character_device(*random);
  32. if (!vfs.mount_root(makeFileSystem(filename))) {
  33. printf("Failed to mount root :(\n");
  34. return 1;
  35. }
  36. #if 0
  37. auto newFile = vfs.create("/empty");
  38. printf("vfs.create: %p\n", newFile.ptr());
  39. #endif
  40. #if 1
  41. auto newDir = vfs.mkdir("/mydir");
  42. printf("vfs.mkdir: %p\n", newDir.ptr());
  43. #endif
  44. //return 0;
  45. if (!strcmp(v[0], "./vcat")) {
  46. int error;
  47. auto descriptor = vfs.open(v[2], error);
  48. if (!descriptor) {
  49. printf("failed to open %s inside fs image\n", v[2]);
  50. return 1;
  51. }
  52. auto contents = descriptor->read_entire_file();
  53. FILE* fout = fopen(v[3], "w");
  54. if (!fout) {
  55. printf("failed to open %s for output\n", v[3]);
  56. return 1;
  57. }
  58. fwrite(contents.pointer(), sizeof(char), contents.size(), fout);
  59. fclose(fout);
  60. return 0;
  61. }
  62. auto synthfs = SynthFS::create();
  63. bool success = static_cast<FS&>(*synthfs).initialize();
  64. printf("synth->initialize(): returned %u\n", success);
  65. vfs.mount(std::move(synthfs), "/syn");
  66. vfs.listDirectory(".", vfs.root()->inode);
  67. printf("list /syn:\n");
  68. vfs.listDirectory("/syn", vfs.root()->inode);
  69. #if 0
  70. auto descriptor = vfs.open("/home/andreas/../../home/./andreas/./file2");
  71. printf("descriptor = %p\n", handle.ptr());
  72. ASSERT(descriptor);
  73. auto contents = descriptor->readEntireFile();
  74. ASSERT(contents);
  75. printf("contents: '%s'\n", contents->pointer());
  76. #endif
  77. String currentDirectory = "/";
  78. auto cwd = vfs.root()->inode;
  79. while (true) {
  80. char cmdbuf[256];
  81. printf("::>");
  82. fflush(stdout);
  83. fgets(cmdbuf, sizeof(cmdbuf), stdin);
  84. if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
  85. cmdbuf[strlen(cmdbuf) - 1] = '\0';
  86. String command = cmdbuf;
  87. auto parts = command.split(' ');
  88. if (parts.is_empty())
  89. continue;
  90. String cmd = parts[0];
  91. if (cmd == "q")
  92. break;
  93. if (cmd == "pwd") {
  94. printf("%s\n", currentDirectory.characters());
  95. continue;
  96. }
  97. if (cmd == "ls") {
  98. vfs.listDirectory(".", cwd);
  99. continue;
  100. }
  101. if (cmd == "lr") {
  102. vfs.listDirectoryRecursively(".", cwd);
  103. continue;
  104. }
  105. if (cmd == "cd" && parts.size() > 1) {
  106. char buf[4096];
  107. sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  108. FileSystemPath new_path(buf);
  109. if (new_path.string() == "/") {
  110. cwd = vfs.root()->inode;
  111. continue;
  112. }
  113. int error;
  114. auto new_cwd = vfs.open(new_path.string(), error, 0, cwd);
  115. if (new_cwd && new_cwd->is_directory()) {
  116. currentDirectory = new_path.string();
  117. cwd = new_cwd->metadata().inode;
  118. } else {
  119. printf("No such directory: %s\n", parts[1].characters());
  120. }
  121. continue;
  122. }
  123. if (cmd == "mt" && parts.size() > 1) {
  124. char buf[1024];
  125. sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  126. vfs.touch(buf);
  127. continue;
  128. }
  129. if (cmd == "stat" && parts.size() > 1) {
  130. char buf[1024];
  131. sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  132. int error;
  133. auto descriptor = vfs.open(buf, error);
  134. if (!descriptor) {
  135. printf("Can't open '%s' :(\n", buf);
  136. continue;
  137. }
  138. Unix::stat st;
  139. int rc = descriptor->stat(&st);
  140. if (rc < 0) {
  141. printf("stat failed: %d\n", rc);
  142. continue;
  143. }
  144. printf("st_dev: %u\n", st.st_dev);
  145. printf("st_ino: %u\n", st.st_ino);
  146. printf("st_mode: %o\n", st.st_mode);
  147. printf("st_nlink: %u\n", st.st_nlink);
  148. printf("st_uid: %u\n", st.st_uid);
  149. printf("st_gid: %u\n", st.st_gid);
  150. printf("st_rdev: %u\n", st.st_rdev);
  151. printf("st_size: %lld\n", st.st_size);
  152. printf("st_blksize: %u\n", st.st_blksize);
  153. printf("st_blocks: %u\n", st.st_blocks);
  154. printf("st_atime: %u - %s", st.st_atime, ctime(&st.st_atime));
  155. printf("st_mtime: %u - %s", st.st_mtime, ctime(&st.st_mtime));
  156. printf("st_ctime: %u - %s", st.st_ctime, ctime(&st.st_ctime));
  157. continue;
  158. }
  159. if (cmd == "cat" && parts.size() > 1) {
  160. char pathbuf[1024];
  161. sprintf(pathbuf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  162. int error;
  163. auto descriptor = vfs.open(pathbuf, error);
  164. if (!descriptor) {
  165. printf("failed to open %s\n", pathbuf);
  166. continue;
  167. }
  168. auto contents = descriptor->read_entire_file();
  169. fwrite(contents.pointer(), sizeof(char), contents.size(), stdout);
  170. continue;
  171. }
  172. if (cmd == "kat" && parts.size() > 1) {
  173. char pathbuf[1024];
  174. sprintf(pathbuf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  175. int error;
  176. auto descriptor = vfs.open(pathbuf, error);
  177. if (!descriptor) {
  178. printf("failed to open %s\n", pathbuf);
  179. continue;
  180. }
  181. ssize_t nread;
  182. byte buffer[512];
  183. for (;;) {
  184. nread = descriptor->read(buffer, sizeof(buffer));
  185. if (nread <= 0)
  186. break;
  187. fwrite(buffer, 1, nread, stdout);
  188. }
  189. if (nread < 0)
  190. printf("ERROR: %d\n", nread);
  191. continue;
  192. }
  193. if (cmd == "ma") {
  194. SimpleMalloc::dump();
  195. continue;
  196. }
  197. }
  198. return 0;
  199. }
  200. RetainPtr<FS> makeFileSystem(const char* imagePath)
  201. {
  202. auto fsImage = FileBackedDiskDevice::create(imagePath, 512);
  203. if (!fsImage->is_valid()) {
  204. fprintf(stderr, "Failed to open fs image file '%s'\n", imagePath);
  205. exit(1);
  206. }
  207. auto ext2 = Ext2FS::create(std::move(fsImage));
  208. bool success = static_cast<FS&>(*ext2).initialize();
  209. printf("ext2->initialize(): returned %u\n", success);
  210. return ext2;
  211. }