dosfsck.c 5.3 KB

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