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