test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <cstring>
  9. #include <AK/SimpleMalloc.h>
  10. #include <AK/kmalloc.h>
  11. static RetainPtr<FileSystem> makeFileSystem(const char* imagePath);
  12. int main(int c, char** v)
  13. {
  14. const char* filename = "small.fs";
  15. if (c >= 2)
  16. filename = v[1];
  17. VirtualFileSystem vfs;
  18. auto zero = make<ZeroDevice>();
  19. vfs.registerCharacterDevice(1, 5, *zero);
  20. auto null = make<NullDevice>();
  21. vfs.registerCharacterDevice(1, 3, *null);
  22. if (!vfs.mountRoot(makeFileSystem(filename))) {
  23. printf("Failed to mount root :(\n");
  24. return 1;
  25. }
  26. auto newFile = vfs.create("/empty");
  27. printf("vfs.create: %p\n", newFile.ptr());
  28. //return 0;
  29. if (!strcmp(v[0], "./vcat")) {
  30. auto handle = vfs.open(v[2]);
  31. if (!handle) {
  32. printf("failed to open %s inside fs image\n", v[2]);
  33. return 1;
  34. }
  35. auto contents = handle->read();
  36. FILE* fout = fopen(v[3], "w");
  37. if (!fout) {
  38. printf("failed to open %s for output\n", v[3]);
  39. return 1;
  40. }
  41. fwrite(contents.pointer(), sizeof(char), contents.size(), fout);
  42. fclose(fout);
  43. return 0;
  44. }
  45. auto synthfs = SyntheticFileSystem::create();
  46. bool success = static_cast<FileSystem&>(*synthfs).initialize();
  47. printf("synth->initialize(): returned %u\n", success);
  48. vfs.mount(std::move(synthfs), "/syn");
  49. vfs.listDirectory("/");
  50. printf("list /syn:\n");
  51. vfs.listDirectory("/syn");
  52. #if 0
  53. auto handle = vfs.open("/home/andreas/../../home/./andreas/./file2");
  54. printf("handle = %p\n", handle.ptr());
  55. ASSERT(handle);
  56. auto contents = handle->read();
  57. ASSERT(contents);
  58. printf("contents: '%s'\n", contents->pointer());
  59. #endif
  60. String currentDirectory = "/";
  61. while (true) {
  62. char cmdbuf[256];
  63. printf("::>");
  64. fflush(stdout);
  65. fgets(cmdbuf, sizeof(cmdbuf), stdin);
  66. if (cmdbuf[strlen(cmdbuf) - 1] == '\n')
  67. cmdbuf[strlen(cmdbuf) - 1] = '\0';
  68. String command = cmdbuf;
  69. auto parts = command.split(' ');
  70. if (parts.isEmpty())
  71. continue;
  72. String cmd = parts[0];
  73. if (cmd == "q")
  74. break;
  75. if (cmd == "pwd") {
  76. printf("%s\n", currentDirectory.characters());
  77. continue;
  78. }
  79. if (cmd == "ls") {
  80. vfs.listDirectory(currentDirectory);
  81. continue;
  82. }
  83. if (cmd == "lr") {
  84. vfs.listDirectoryRecursively(currentDirectory);
  85. continue;
  86. }
  87. if (cmd == "cd" && parts.size() > 1) {
  88. char buf[1024];
  89. sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  90. if (vfs.isDirectory(buf)) {
  91. currentDirectory = buf;
  92. } else {
  93. printf("No such directory: %s\n", buf);
  94. }
  95. continue;
  96. }
  97. if (cmd == "mt" && parts.size() > 1) {
  98. char buf[1024];
  99. sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  100. vfs.touch(buf);
  101. continue;
  102. }
  103. if (cmd == "cat" && parts.size() > 1) {
  104. char pathbuf[1024];
  105. sprintf(pathbuf, "%s/%s", currentDirectory.characters(), parts[1].characters());
  106. auto handle = vfs.open(pathbuf);
  107. if (!handle) {
  108. printf("failed to open %s\n", pathbuf);
  109. continue;
  110. }
  111. auto contents = handle->read();
  112. fwrite(contents.pointer(), sizeof(char), contents.size(), stdout);
  113. continue;
  114. }
  115. if (cmd == "ma") {
  116. SimpleMalloc::dump();
  117. continue;
  118. }
  119. }
  120. return 0;
  121. }
  122. RetainPtr<FileSystem> makeFileSystem(const char* imagePath)
  123. {
  124. auto fsImage = FileBackedBlockDevice::create(imagePath, 512);
  125. if (!fsImage->isValid()) {
  126. fprintf(stderr, "Failed to open fs image file '%s'\n", imagePath);
  127. exit(1);
  128. }
  129. auto ext2 = Ext2FileSystem::create(std::move(fsImage));
  130. bool success = static_cast<FileSystem&>(*ext2).initialize();
  131. printf("ext2->initialize(): returned %u\n", success);
  132. return ext2;
  133. }