test.cpp 6.7 KB

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