dumpcfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of CFS. */
  3. /*
  4. CFS is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. CFS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with CFS. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <sys/fcntl.h>
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #define __CFS
  28. #include "cfs.h"
  29. void dump_sb(int);
  30. void dump_inode(struct inode *);
  31. void dump_dir(int fd, struct inode *);
  32. __bn_t bmap(int, __inode_t, __bn_t);
  33. int main(int argc, const char **argv) {
  34. char *fname = (char *) argv[1];
  35. int fd;
  36. if (!fname) {
  37. printf("fatal: excepted one argument 'file name', but %d given. \n", argc - 1);
  38. printf("usage: %s device\n", argv[0]);
  39. return 1;
  40. }
  41. fd = open(fname, O_RDWR);
  42. if (fd < 0) {
  43. printf("fatal: failed when try to open %s: %s\n", fname, strerror(errno));
  44. return 1;
  45. }
  46. dump_sb(fd);
  47. close(fd);
  48. return 0;
  49. }
  50. void dump_sb(int fd) {
  51. lseek(fd, 512, SEEK_SET);
  52. read(fd, &sb, sizeof(struct superblock));
  53. if (sb.magic != MAGIC) {
  54. printf("fatal: bad magic number. \n");
  55. close(fd);
  56. exit(1);
  57. }
  58. printf("super block: \n");
  59. printf("\tmagic number: \t\t0x%08x (ok)\n", sb.magic);
  60. printf("\ttotal blocks: \t\t%10d\t0x%08x\n", sb.nblocks, sb.nblocks);
  61. printf("\ttotal inodes: \t\t%10d\t0x%08x\n", sb.ninodes, sb.ninodes);
  62. printf("\tbbm blocks count: \t%10d\t0x%08x\n", sb.nb_bbm, sb.nb_bbm);
  63. printf("\tibm blocks count: \t%10d\t0x%08x\n", sb.nb_ibm, sb.nb_ibm);
  64. printf("\tbim blocks count: \t%10d\t0x%08x\n", sb.nb_bim, sb.nb_bim);
  65. printf("\tinode blocks count: \t%10d\t0x%08x\n", sb.nb_inodes, sb.nb_inodes);
  66. printf("\tstart block of bbm: \t%10d\t0x%08x\n", sb.bbm_bn, sb.bbm_bn);
  67. printf("\tstart block of ibm: \t%10d\t0x%08x\n", sb.ibm_bn, sb.ibm_bn);
  68. printf("\tstart block of bim: \t%10d\t0x%08x\n", sb.bim_bn, sb.bim_bn);
  69. printf("\tstart block of inodes: \t%10d\t0x%08x\n", sb.inodes_bn, sb.inodes_bn);
  70. printf("\tfirst data block: \t%10d\t0x%08x\n", sb.first_data_block, sb.first_data_block);
  71. printf("\tnext free inode number: %10d\t0x%08x\n", sb.next_inode, sb.next_inode);
  72. dump_inode(&sb.root_ino);
  73. dump_dir(fd, &sb.root_ino);
  74. return;
  75. }
  76. void read_inode(int fd, __inode_t ino, struct inode *inode) {
  77. lseek(fd, (sb.inodes_bn << BLOCK_SHIFT) + ino * sizeof(struct inode), SEEK_SET);
  78. read(fd, inode, sizeof(struct inode));
  79. return;
  80. }
  81. void dump_inode(struct inode *inode) {
  82. printf("\ninode %10u: ", inode->inode);
  83. if (inode->inode == ROOT_INO)
  84. printf("(root)\n");
  85. else
  86. putc('\n', stdout);
  87. printf("\tparent inode number: \t%10u ", inode->pinode);
  88. if (inode->pinode == ROOT_INO)
  89. printf("(root)\n");
  90. else
  91. putc('\n', stdout);
  92. printf("\taccess: \t\t%10d\t0x%08x\n", inode->access, inode->access);
  93. /* for access */
  94. if (inode->access & A_ROOTRD)
  95. printf("\t\t\t\troot readable\t");
  96. else
  97. printf("\t\t\t\troot unreadable\t");
  98. if (inode->access & A_ROOTWR)
  99. printf("root writable\t");
  100. else
  101. printf("root unwritable\t");
  102. putc('\n', stdout);
  103. if (inode->access & A_USERRD)
  104. printf("\t\t\t\tuser readable\t");
  105. else
  106. printf("\t\t\t\tuser unreadable\t");
  107. if (inode->access & A_USERWR)
  108. printf("user writable\t");
  109. else
  110. printf("user unwritable\t");
  111. putc('\n', stdout);
  112. if (inode->access & A_REGFILE)
  113. printf("\t\t\t\tregular file\n");
  114. if (inode->access & A_DIR)
  115. printf("\t\t\t\tdirectory\n");
  116. if (inode->access & A_LINK)
  117. printf("\t\t\t\thard/soft link\n");
  118. if (inode->access & A_EXEC)
  119. printf("\t\t\t\texecutable\n");
  120. printf("\ttotal blocks: \t\t%10u\t0x%08x\n", inode->blocks_nr, inode->blocks_nr);
  121. printf("\tup size: \t\t%10u\t0x%08x\n", inode->upsize, inode->upsize);
  122. printf("\ttotal file size: \t%10u\t", (inode->blocks_nr << BLOCK_SHIFT) + inode->upsize);
  123. printf("%d KB %d bytes\n", fsize(inode) >> 10, fsize(inode) % 1024);
  124. printf("\tcreate time: \t\t%10u\n", inode->ctime);
  125. printf("\tlast write time: \t%10u\n", inode->wtime);
  126. printf("\tuid: \t\t\t%10d\n", inode->uid);
  127. printf("\tgid: \t\t\t%10d\n", inode->gid);
  128. printf("\textend access: \t\t%10d\t0x%08x\n", inode->eaccess, inode->eaccess);
  129. return;
  130. }
  131. void dump_dir(int fd, struct inode *dir) {
  132. int i;
  133. struct dentry *entries = (struct dentry *) malloc(BLOCK_SIZE);
  134. printf("directory: \n");
  135. for (i = 0; i < dir->blocks_nr; i++) {
  136. lseek(fd, bmap(fd, dir->inode, i) << BLOCK_SHIFT, SEEK_SET);
  137. read(fd, entries, BLOCK_SIZE);
  138. int j;
  139. for (j = 0; j < BLOCK_SIZE / sizeof(struct dentry) && entries[j].ino; j++) {
  140. printf("\tinode %u: %s\n", entries[j].ino, entries[j].name);
  141. struct inode ino;
  142. read_inode(fd, entries[j].ino, &ino);
  143. dump_inode(&ino);
  144. if (ino.access & A_DIR)
  145. dump_dir(fd, &ino);
  146. }
  147. }
  148. return;
  149. }
  150. __bn_t bmap(int fd, __inode_t ino, __bn_t bn) {
  151. __bn_t i;
  152. struct bim_entry *buf = (struct bim_entry *) malloc(BLOCK_SIZE);
  153. lseek(fd, sb.bim_bn << BLOCK_SHIFT, SEEK_SET);
  154. read(fd, buf, BLOCK_SIZE);
  155. /* search in buf */
  156. for (i = 0; i < BLOCK_SIZE / sizeof(struct bim_entry); i++) {
  157. if (buf[i].inode == ino && buf[i].th_block == bn)
  158. return i + sb.first_data_block;
  159. }
  160. return 0;
  161. }