dosfsck.c 5.4 KB

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