main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. main.c (02.09.09)
  3. exFAT file system checker.
  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 <stdio.h>
  19. #include <string.h>
  20. #include <exfat.h>
  21. #include <exfatfs.h>
  22. #include <inttypes.h>
  23. #include <unistd.h>
  24. #define exfat_debug(format, ...)
  25. uint64_t files_count, directories_count;
  26. static int nodeck(struct exfat* ef, struct exfat_node* node)
  27. {
  28. const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
  29. cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
  30. cluster_t c = node->start_cluster;
  31. int rc = 0;
  32. while (clusters--)
  33. {
  34. if (CLUSTER_INVALID(c))
  35. {
  36. char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
  37. exfat_get_name(node, name, sizeof(name) - 1);
  38. exfat_error("file '%s' has invalid cluster 0x%x", name, c);
  39. rc = 1;
  40. break;
  41. }
  42. if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
  43. {
  44. char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
  45. exfat_get_name(node, name, sizeof(name) - 1);
  46. exfat_error("cluster 0x%x of file '%s' is not allocated", c, name);
  47. rc = 1;
  48. }
  49. c = exfat_next_cluster(ef, node, c);
  50. }
  51. return rc;
  52. }
  53. static void dirck(struct exfat* ef, const char* path)
  54. {
  55. struct exfat_node* parent;
  56. struct exfat_node* node;
  57. struct exfat_iterator it;
  58. int rc;
  59. size_t path_length;
  60. char* entry_path;
  61. if (exfat_lookup(ef, &parent, path) != 0)
  62. exfat_bug("directory '%s' is not found", path);
  63. if (!(parent->flags & EXFAT_ATTRIB_DIR))
  64. exfat_bug("'%s' is not a directory (0x%x)", path, parent->flags);
  65. if (nodeck(ef, parent) != 0)
  66. {
  67. exfat_put_node(ef, parent);
  68. return;
  69. }
  70. path_length = strlen(path);
  71. entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
  72. if (entry_path == NULL)
  73. {
  74. exfat_put_node(ef, parent);
  75. exfat_error("out of memory");
  76. return;
  77. }
  78. strcpy(entry_path, path);
  79. strcat(entry_path, "/");
  80. rc = exfat_opendir(ef, parent, &it);
  81. if (rc != 0)
  82. {
  83. free(entry_path);
  84. exfat_put_node(ef, parent);
  85. return;
  86. }
  87. while ((node = exfat_readdir(ef, &it)))
  88. {
  89. exfat_get_name(node, entry_path + path_length + 1,
  90. UTF8_BYTES(EXFAT_NAME_MAX));
  91. exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
  92. IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
  93. node->size, node->start_cluster);
  94. if (node->flags & EXFAT_ATTRIB_DIR)
  95. {
  96. directories_count++;
  97. dirck(ef, entry_path);
  98. }
  99. else
  100. {
  101. files_count++;
  102. nodeck(ef, node);
  103. }
  104. exfat_put_node(ef, node);
  105. }
  106. exfat_closedir(ef, &it);
  107. exfat_put_node(ef, parent);
  108. free(entry_path);
  109. }
  110. static void fsck(struct exfat* ef)
  111. {
  112. exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
  113. dirck(ef, "");
  114. }
  115. static void usage(const char* prog)
  116. {
  117. fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
  118. exit(1);
  119. }
  120. int main(int argc, char* argv[])
  121. {
  122. int opt;
  123. const char* spec = NULL;
  124. struct exfat ef;
  125. printf("exfatfsck %u.%u.%u\n",
  126. EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
  127. while ((opt = getopt(argc, argv, "V")) != -1)
  128. {
  129. switch (opt)
  130. {
  131. case 'V':
  132. puts("Copyright (C) 2011-2014 Andrew Nayenko");
  133. return 0;
  134. default:
  135. usage(argv[0]);
  136. break;
  137. }
  138. }
  139. if (argc - optind != 1)
  140. usage(argv[0]);
  141. spec = argv[optind];
  142. if (exfat_mount(&ef, spec, "ro") != 0)
  143. return 1;
  144. printf("Checking file system on %s.\n", spec);
  145. fsck(&ef);
  146. exfat_unmount(&ef);
  147. printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
  148. directories_count, files_count);
  149. fputs("File system checking finished. ", stdout);
  150. if (exfat_errors != 0)
  151. {
  152. printf("ERRORS FOUND: %d.\n", exfat_errors);
  153. return 1;
  154. }
  155. puts("No errors found.");
  156. return 0;
  157. }