dosfsck.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* dosfsck.c - User interface
  2. Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
  3. Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  4. Copyright (C) 2008-2013 Daniel Baumann <mail@daniel-baumann.ch>
  5. Copyright (C) 2013 Manoel Trapier <godzil@godzil.net>
  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 3 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
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. On Debian systems, the complete text of the GNU General Public License
  17. can be found in /usr/share/common-licenses/GPL-3 file.
  18. */
  19. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  20. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  21. #include "version.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <getopt.h>
  28. #include "common.h"
  29. #include "dosfsck.h"
  30. #include "io.h"
  31. #include "boot.h"
  32. #include "fat.h"
  33. #include "file.h"
  34. #include "check.h"
  35. #include "ui.h"
  36. int interactive = 0, rw = 0, list = 0, test = 0, verbose = 0, write_immed = 0;
  37. extern int output_ui_fd, output_ui;
  38. int atari_format = 0, boot_only = 0;
  39. unsigned n_files = 0;
  40. void *mem_queue = NULL;
  41. static void usage(char *name)
  42. {
  43. fprintf(stderr, "usage: %s [-aAbflrtvVwy] [-d path -d ...] "
  44. "[-u path -u ...]\n%15sdevice\n", name, "");
  45. fprintf(stderr, " -a automatically repair the file system\n");
  46. fprintf(stderr, " -A toggle Atari file system format\n");
  47. fprintf(stderr, " -b make read-only boot sector check\n");
  48. fprintf(stderr, " -d path drop that file\n");
  49. fprintf(stderr, " -f salvage unused chains to files\n");
  50. fprintf(stderr, " -l list path names\n");
  51. fprintf(stderr, " -C [fd] Display completion to fd file descriptor\n");
  52. fprintf(stderr,
  53. " -n no-op, check non-interactively without changing\n");
  54. fprintf(stderr, " -p same as -a, for compat with other *fsck\n");
  55. fprintf(stderr, " -r interactively repair the file system\n");
  56. fprintf(stderr, " -t test for bad clusters\n");
  57. fprintf(stderr, " -u path try to undelete that (non-directory) file\n");
  58. fprintf(stderr, " -v verbose mode\n");
  59. fprintf(stderr, " -V perform a verification pass\n");
  60. fprintf(stderr, " -w write changes to disk immediately\n");
  61. fprintf(stderr, " -y same as -a, for compat with other *fsck\n");
  62. exit(2);
  63. }
  64. /*
  65. * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
  66. * of MS-DOS filesystem by default.
  67. */
  68. static void check_atari(void)
  69. {
  70. #ifdef __mc68000__
  71. FILE *f;
  72. char line[128], *p;
  73. if (!(f = fopen("/proc/hardware", "r"))) {
  74. perror("/proc/hardware");
  75. return;
  76. }
  77. while (fgets(line, sizeof(line), f)) {
  78. if (strncmp(line, "Model:", 6) == 0) {
  79. p = line + 6;
  80. p += strspn(p, " \t");
  81. if (strncmp(p, "Atari ", 6) == 0)
  82. atari_format = 1;
  83. break;
  84. }
  85. }
  86. fclose(f);
  87. #endif
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. DOS_FS fs;
  92. int salvage_files, verify, c;
  93. unsigned n_files_check = 0, n_files_verify = 0;
  94. unsigned long free_clusters;
  95. memset(&fs, 0, sizeof(fs));
  96. rw = salvage_files = verify = 0;
  97. interactive = 1;
  98. check_atari();
  99. while ((c = getopt(argc, argv, "Aad:bC:flnprtu:vVwy")) != EOF)
  100. switch (c) {
  101. case 'A': /* toggle Atari format */
  102. atari_format = !atari_format;
  103. break;
  104. case 'a':
  105. case 'p':
  106. case 'y':
  107. rw = 1;
  108. interactive = 0;
  109. salvage_files = 1;
  110. break;
  111. case 'b':
  112. rw = 0;
  113. interactive = 0;
  114. boot_only = 1;
  115. break;
  116. case 'd':
  117. file_add(optarg, fdt_drop);
  118. break;
  119. case 'f':
  120. salvage_files = 1;
  121. break;
  122. case 'l':
  123. list = 1;
  124. break;
  125. case 'n':
  126. rw = 0;
  127. interactive = 0;
  128. break;
  129. case 'r':
  130. rw = 1;
  131. interactive = 1;
  132. break;
  133. case 't':
  134. test = 1;
  135. break;
  136. case 'u':
  137. file_add(optarg, fdt_undelete);
  138. break;
  139. case 'v':
  140. verbose = 1;
  141. printf("dosfsck " VERSION " (" VERSION_DATE ")\n");
  142. break;
  143. case 'V':
  144. verify = 1;
  145. break;
  146. case 'w':
  147. write_immed = 1;
  148. break;
  149. case 'C':
  150. output_ui_fd = atoi(optarg);
  151. output_ui = 1;
  152. break;
  153. default:
  154. usage(argv[0]);
  155. }
  156. if ((test || write_immed) && !rw) {
  157. fprintf(stderr, "-t and -w require -a or -r\n");
  158. exit(2);
  159. }
  160. if (optind != argc - 1)
  161. usage(argv[0]);
  162. printf("dosfsck " VERSION ", " VERSION_DATE ", FAT32, LFN\n");
  163. /* Now open FileSystem, and do all the checks.. */
  164. fs_open(argv[optind], rw);
  165. ui_set_device(argv[optind]);
  166. ui_print_new_pass("Checking boot sector...");
  167. read_boot(&fs);
  168. if (boot_only)
  169. goto exit;
  170. if (verify)
  171. printf("Starting check/repair pass.\n");
  172. ui_print_new_pass("Scanning FAT...");
  173. while (read_fat(&fs), scan_root(&fs))
  174. qfree(&mem_queue);
  175. if (test) {
  176. ui_print_new_pass("Fix bad FAT...");
  177. fix_bad(&fs);
  178. }
  179. if (salvage_files) {
  180. ui_print_new_pass("Reclaim bad file...");
  181. reclaim_file(&fs);
  182. }
  183. else {
  184. ui_print_new_pass("Reclaim free space...");
  185. reclaim_free(&fs);
  186. }
  187. ui_print_new_pass("Verify free clusters...");
  188. free_clusters = update_free(&fs);
  189. file_unused();
  190. qfree(&mem_queue);
  191. n_files_check = n_files;
  192. if (verify) {
  193. n_files = 0;
  194. printf("Starting verification pass.\n");
  195. ui_print_new_pass("Starting verification pass...");
  196. read_fat(&fs);
  197. scan_root(&fs);
  198. reclaim_free(&fs);
  199. qfree(&mem_queue);
  200. n_files_verify = n_files;
  201. }
  202. exit:
  203. if (fs_changed()) {
  204. if (rw) {
  205. if (interactive)
  206. rw = get_key("yn", "Perform changes ? (y/n)") == 'y';
  207. else {
  208. ui_print_new_pass("Performing changes...");
  209. printf("Performing changes.\n");
  210. }
  211. } else
  212. printf("Leaving file system unchanged.\n");
  213. }
  214. printf("%s: %u files, %lu/%lu clusters\n", argv[optind],
  215. n_files, fs.clusters - free_clusters, fs.clusters);
  216. return fs_close(rw) ? 1 : 0;
  217. }