dosfsck.c 5.3 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. 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,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," -n no-op, check non-interactively without changing\n");
  47. fprintf(stderr," -p same as -a, for compat with other *fsck\n");
  48. fprintf(stderr," -r interactively repair the file system\n");
  49. fprintf(stderr," -t test for bad clusters\n");
  50. fprintf(stderr," -u path try to undelete that (non-directory) file\n");
  51. fprintf(stderr," -v verbose mode\n");
  52. fprintf(stderr," -V perform a verification pass\n");
  53. fprintf(stderr," -w write changes to disk immediately\n");
  54. fprintf(stderr," -y same as -a, for compat with other *fsck\n");
  55. exit(2);
  56. }
  57. /*
  58. * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
  59. * of MS-DOS filesystem by default.
  60. */
  61. static void check_atari( void )
  62. {
  63. #ifdef __mc68000__
  64. FILE *f;
  65. char line[128], *p;
  66. if (!(f = fopen( "/proc/hardware", "r" ))) {
  67. perror( "/proc/hardware" );
  68. return;
  69. }
  70. while( fgets( line, sizeof(line), f ) ) {
  71. if (strncmp( line, "Model:", 6 ) == 0) {
  72. p = line + 6;
  73. p += strspn( p, " \t" );
  74. if (strncmp( p, "Atari ", 6 ) == 0)
  75. atari_format = 1;
  76. break;
  77. }
  78. }
  79. fclose( f );
  80. #endif
  81. }
  82. int main(int argc,char **argv)
  83. {
  84. DOS_FS fs;
  85. int rw,salvage_files,verify,c;
  86. unsigned n_files_check=0, n_files_verify=0;
  87. unsigned long free_clusters;
  88. rw = salvage_files = verify = 0;
  89. interactive = 1;
  90. check_atari();
  91. while ((c = getopt(argc,argv,"Aad:flnprtu:vVwy")) != EOF)
  92. switch (c) {
  93. case 'A': /* toggle Atari format */
  94. atari_format = !atari_format;
  95. break;
  96. case 'a':
  97. case 'p':
  98. case 'y':
  99. rw = 1;
  100. interactive = 0;
  101. salvage_files = 1;
  102. break;
  103. case 'd':
  104. file_add(optarg,fdt_drop);
  105. break;
  106. case 'f':
  107. salvage_files = 1;
  108. break;
  109. case 'l':
  110. list = 1;
  111. break;
  112. case 'n':
  113. rw = 0;
  114. interactive = 0;
  115. break;
  116. case 'r':
  117. rw = 1;
  118. interactive = 1;
  119. break;
  120. case 't':
  121. test = 1;
  122. break;
  123. case 'u':
  124. file_add(optarg,fdt_undelete);
  125. break;
  126. case 'v':
  127. verbose = 1;
  128. printf("dosfsck " VERSION " (" VERSION_DATE ")\n");
  129. break;
  130. case 'V':
  131. verify = 1;
  132. break;
  133. case 'w':
  134. write_immed = 1;
  135. break;
  136. default:
  137. usage(argv[0]);
  138. }
  139. if ((test || write_immed) && !rw) {
  140. fprintf(stderr,"-t and -w require -a or -r\n");
  141. exit(2);
  142. }
  143. if (optind != argc-1) usage(argv[0]);
  144. printf( "dosfsck " VERSION ", " VERSION_DATE ", FAT32, LFN\n" );
  145. fs_open(argv[optind],rw);
  146. read_boot(&fs);
  147. if (verify) printf("Starting check/repair pass.\n");
  148. while (read_fat(&fs), scan_root(&fs)) qfree(&mem_queue);
  149. if (test) fix_bad(&fs);
  150. if (salvage_files) reclaim_file(&fs);
  151. else reclaim_free(&fs);
  152. free_clusters = update_free(&fs);
  153. file_unused();
  154. qfree(&mem_queue);
  155. n_files_check = n_files;
  156. if (verify) {
  157. n_files = 0;
  158. printf("Starting verification pass.\n");
  159. read_fat(&fs);
  160. scan_root(&fs);
  161. reclaim_free(&fs);
  162. qfree(&mem_queue);
  163. n_files_verify = n_files;
  164. }
  165. if (fs_changed()) {
  166. if (rw) {
  167. if (interactive)
  168. rw = get_key("yn","Perform changes ? (y/n)") == 'y';
  169. else printf("Performing changes.\n");
  170. }
  171. else
  172. printf("Leaving file system unchanged.\n");
  173. }
  174. printf( "%s: %u files, %lu/%lu clusters\n", argv[optind],
  175. n_files, fs.clusters - free_clusters, fs.clusters );
  176. return fs_close(rw) ? 1 : 0;
  177. }
  178. /* Local Variables: */
  179. /* tab-width: 8 */
  180. /* End: */