main.c 4.0 KB

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