main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. main.c (08.11.10)
  3. Prints detailed information about exFAT volume.
  4. Copyright (C) 2011-2013 Andrew Nayenko
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <inttypes.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <exfat.h>
  22. static void print_generic_info(const struct exfat_super_block* sb)
  23. {
  24. printf("Volume serial number 0x%08x\n",
  25. le32_to_cpu(sb->volume_serial));
  26. printf("FS version %hhu.%hhu\n",
  27. sb->version.major, sb->version.minor);
  28. printf("Sector size %10u\n",
  29. SECTOR_SIZE(*sb));
  30. printf("Cluster size %10u\n",
  31. CLUSTER_SIZE(*sb));
  32. }
  33. static void print_sector_info(const struct exfat_super_block* sb)
  34. {
  35. printf("Sectors count %10"PRIu64"\n",
  36. le64_to_cpu(sb->sector_count));
  37. }
  38. static void print_cluster_info(const struct exfat_super_block* sb)
  39. {
  40. printf("Clusters count %10u\n",
  41. le32_to_cpu(sb->cluster_count));
  42. }
  43. static void print_other_info(const struct exfat_super_block* sb)
  44. {
  45. printf("First sector %10"PRIu64"\n",
  46. le64_to_cpu(sb->sector_start));
  47. printf("FAT first sector %10u\n",
  48. le32_to_cpu(sb->fat_sector_start));
  49. printf("FAT sectors count %10u\n",
  50. le32_to_cpu(sb->fat_sector_count));
  51. printf("First cluster sector %10u\n",
  52. le32_to_cpu(sb->cluster_sector_start));
  53. printf("Root directory cluster %10u\n",
  54. le32_to_cpu(sb->rootdir_cluster));
  55. printf("Volume state 0x%04hx\n",
  56. le16_to_cpu(sb->volume_state));
  57. printf("FATs count %10hhu\n",
  58. sb->fat_count);
  59. printf("Drive number 0x%02hhx\n",
  60. sb->drive_no);
  61. printf("Allocated space %9hhu%%\n",
  62. sb->allocated_percent);
  63. }
  64. static int dump_sb(const char* spec)
  65. {
  66. struct exfat_dev* dev;
  67. struct exfat_super_block sb;
  68. dev = exfat_open(spec, EXFAT_MODE_RO);
  69. if (dev == NULL)
  70. return 1;
  71. if (exfat_read(dev, &sb, sizeof(struct exfat_super_block)) < 0)
  72. {
  73. exfat_close(dev);
  74. exfat_error("failed to read from `%s'", spec);
  75. return 1;
  76. }
  77. if (memcmp(sb.oem_name, "EXFAT ", sizeof(sb.oem_name)) != 0)
  78. {
  79. exfat_close(dev);
  80. exfat_error("exFAT file system is not found on `%s'", spec);
  81. return 1;
  82. }
  83. print_generic_info(&sb);
  84. print_sector_info(&sb);
  85. print_cluster_info(&sb);
  86. print_other_info(&sb);
  87. exfat_close(dev);
  88. return 0;
  89. }
  90. static void dump_sectors(struct exfat* ef)
  91. {
  92. off_t a = 0, b = 0;
  93. printf("Used sectors ");
  94. while (exfat_find_used_sectors(ef, &a, &b) == 0)
  95. printf(" %"PRIu64"-%"PRIu64, a, b);
  96. puts("");
  97. }
  98. static int dump_full(const char* spec, bool used_sectors)
  99. {
  100. struct exfat ef;
  101. uint32_t free_clusters;
  102. uint64_t free_sectors;
  103. if (exfat_mount(&ef, spec, "ro") != 0)
  104. return 1;
  105. free_clusters = exfat_count_free_clusters(&ef);
  106. free_sectors = (uint64_t) free_clusters << ef.sb->spc_bits;
  107. printf("Volume label %15s\n", exfat_get_label(&ef));
  108. print_generic_info(ef.sb);
  109. print_sector_info(ef.sb);
  110. printf("Free sectors %10"PRIu64"\n", free_sectors);
  111. print_cluster_info(ef.sb);
  112. printf("Free clusters %10u\n", free_clusters);
  113. print_other_info(ef.sb);
  114. if (used_sectors)
  115. dump_sectors(&ef);
  116. exfat_unmount(&ef);
  117. return 0;
  118. }
  119. static void usage(const char* prog)
  120. {
  121. fprintf(stderr, "Usage: %s [-s] [-u] [-v] <device>\n", prog);
  122. exit(1);
  123. }
  124. int main(int argc, char* argv[])
  125. {
  126. char** pp;
  127. const char* spec = NULL;
  128. bool sb_only = false;
  129. bool used_sectors = false;
  130. printf("dumpexfat %u.%u.%u\n",
  131. EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
  132. for (pp = argv + 1; *pp; pp++)
  133. {
  134. if (strcmp(*pp, "-s") == 0)
  135. sb_only = true;
  136. else if (strcmp(*pp, "-u") == 0)
  137. used_sectors = true;
  138. else if (strcmp(*pp, "-v") == 0)
  139. {
  140. puts("Copyright (C) 2011-2013 Andrew Nayenko");
  141. return 0;
  142. }
  143. else if (spec == NULL)
  144. spec = *pp;
  145. else
  146. usage(argv[0]);
  147. }
  148. if (spec == NULL)
  149. usage(argv[0]);
  150. if (sb_only)
  151. return dump_sb(spec);
  152. return dump_full(spec, used_sectors);
  153. }