main.c 4.6 KB

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