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