test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 == "cat" && parts.size() > 1) {
  107. char pathbuf[1024];
  108. sprintf(pathbuf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  109. auto handle = vfs.open(pathbuf);
  110. if (!handle) {
  111. printf("failed to open %s\n", pathbuf);
  112. continue;
  113. }
  114. auto contents = handle->readEntireFile();
  115. fwrite(contents.pointer(), sizeof(char), contents.size(), stdout);
  116. continue;
  117. }
  118. if (cmd == "kat" && parts.size() > 1) {
  119. char pathbuf[1024];
  120. sprintf(pathbuf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  121. auto handle = vfs.open(pathbuf);
  122. if (!handle) {
  123. printf("failed to open %s\n", pathbuf);
  124. continue;
  125. }
  126. ssize_t nread;
  127. byte buffer[512];
  128. for (;;) {
  129. nread = handle->read(buffer, sizeof(buffer));
  130. printf("read() returned %d\n", nread);
  131. if (nread <= 0)
  132. break;
  133. fwrite(buffer, 1, nread, stdout);
  134. }
  135. if (nread < 0)
  136. printf("ERROR: %d\n", nread);
  137. continue;
  138. }
  139. if (cmd == "ma") {
  140. SimpleMalloc::dump();
  141. continue;
  142. }
  143. }
  144. return 0;
  145. }
  146. RetainPtr<FileSystem> makeFileSystem(const char* imagePath)
  147. {
  148. auto fsImage = FileBackedBlockDevice::create(imagePath, 512);
  149. if (!fsImage->isValid()) {
  150. fprintf(stderr, "Failed to open fs image file '%s'\n", imagePath);
  151. exit(1);
  152. }
  153. auto ext2 = Ext2FileSystem::create(std::move(fsImage));
  154. bool success = static_cast<FileSystem&>(*ext2).initialize();
  155. printf("ext2->initialize(): returned %u\n", success);
  156. return ext2;
  157. }