test.cpp 6.5 KB

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