bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | #include "version.h" |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <unistd.h> |
| 33 | #include <getopt.h> |
| 34 | |
| 35 | #include "common.h" |
| 36 | #include "dosfsck.h" |
| 37 | #include "io.h" |
| 38 | #include "boot.h" |
| 39 | #include "fat.h" |
| 40 | #include "file.h" |
| 41 | #include "check.h" |
| 42 | |
| 43 | int interactive = 0, rw = 0, list = 0, test = 0, verbose = 0, write_immed = 0; |
| 44 | int atari_format = 0; |
| 45 | unsigned n_files = 0; |
| 46 | void *mem_queue = NULL; |
| 47 | |
| 48 | #ifdef USE_ANDROID_RETVALS |
| 49 | unsigned retandroid = 1; |
| 50 | #else |
| 51 | unsigned retandroid = 0; |
| 52 | #endif |
| 53 | |
| 54 | static void usage(char *name) |
| 55 | { |
| 56 | fprintf(stderr, "usage: %s [-aAflrtvVwy] [-d path -d ...] " |
| 57 | "[-u path -u ...]\n%15sdevice\n", name, ""); |
| 58 | fprintf(stderr, " -a automatically repair the file system\n"); |
| 59 | fprintf(stderr, " -A toggle Atari file system format\n"); |
| 60 | fprintf(stderr, " -d path drop that file\n"); |
| 61 | fprintf(stderr, " -f ignored\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 file system\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 | if (retandroid) { |
| 74 | exit(1); |
| 75 | } else { |
| 76 | exit(2); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant |
| 82 | * of MS-DOS filesystem by default. |
| 83 | */ |
| 84 | static void check_atari(void) |
| 85 | { |
| 86 | #ifdef __mc68000__ |
| 87 | FILE *f; |
| 88 | char line[128], *p; |
| 89 | |
| 90 | if (!(f = fopen("/proc/hardware", "r"))) { |
| 91 | perror("/proc/hardware"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | while (fgets(line, sizeof(line), f)) { |
| 96 | if (strncmp(line, "Model:", 6) == 0) { |
| 97 | p = line + 6; |
| 98 | p += strspn(p, " \t"); |
| 99 | if (strncmp(p, "Atari ", 6) == 0) |
| 100 | atari_format = 1; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | fclose(f); |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | int main(int argc, char **argv) |
| 109 | { |
| 110 | DOS_FS fs; |
| 111 | int salvage_files, verify, c; |
| 112 | unsigned n_files_check = 0, n_files_verify = 0; |
| 113 | unsigned long free_clusters; |
| 114 | |
| 115 | memset(&fs, 0, sizeof(fs)); |
| 116 | rw = salvage_files = verify = 0; |
| 117 | interactive = 1; |
| 118 | check_atari(); |
| 119 | |
| 120 | while ((c = getopt(argc, argv, "Aad:flnprtu:vVwy")) != EOF) |
| 121 | switch (c) { |
| 122 | case 'A': /* toggle Atari format */ |
| 123 | atari_format = !atari_format; |
| 124 | break; |
| 125 | case 'a': |
| 126 | case 'p': |
| 127 | case 'y': |
| 128 | rw = 1; |
| 129 | interactive = 0; |
| 130 | salvage_files = 1; |
| 131 | break; |
| 132 | case 'd': |
| 133 | file_add(optarg, fdt_drop); |
| 134 | break; |
| 135 | case 'f': |
| 136 | salvage_files = 1; |
| 137 | break; |
| 138 | case 'l': |
| 139 | list = 1; |
| 140 | break; |
| 141 | case 'n': |
| 142 | rw = 0; |
| 143 | interactive = 0; |
| 144 | break; |
| 145 | case 'r': |
| 146 | rw = 1; |
| 147 | interactive = 1; |
| 148 | break; |
| 149 | case 't': |
| 150 | test = 1; |
| 151 | break; |
| 152 | case 'u': |
| 153 | file_add(optarg, fdt_undelete); |
| 154 | break; |
| 155 | case 'v': |
| 156 | verbose = 1; |
| 157 | printf("dosfsck " VERSION " (" VERSION_DATE ")\n"); |
| 158 | break; |
| 159 | case 'V': |
| 160 | verify = 1; |
| 161 | break; |
| 162 | case 'w': |
| 163 | write_immed = 1; |
| 164 | break; |
| 165 | default: |
| 166 | usage(argv[0]); |
| 167 | } |
| 168 | if ((test || write_immed) && !rw) { |
| 169 | fprintf(stderr, "-t and -w require -a or -r\n"); |
| 170 | if (retandroid) { |
| 171 | exit(1); |
| 172 | } else { |
| 173 | exit(2); |
| 174 | } |
| 175 | } |
| 176 | if (optind != argc - 1) |
| 177 | usage(argv[0]); |
| 178 | |
| 179 | printf("dosfsck " VERSION ", " VERSION_DATE ", FAT32, LFN\n"); |
| 180 | fs_open(argv[optind], rw); |
| 181 | read_boot(&fs); |
| 182 | if (verify) |
| 183 | printf("Starting check/repair pass.\n"); |
| 184 | while (read_fat(&fs), scan_root(&fs)) |
| 185 | qfree(&mem_queue); |
| 186 | if (test) |
| 187 | fix_bad(&fs); |
| 188 | if (salvage_files && 0) |
| 189 | reclaim_file(&fs); |
| 190 | else |
| 191 | reclaim_free(&fs); |
| 192 | free_clusters = update_free(&fs); |
| 193 | file_unused(); |
| 194 | qfree(&mem_queue); |
| 195 | n_files_check = n_files; |
| 196 | if (verify) { |
| 197 | n_files = 0; |
| 198 | printf("Starting verification pass.\n"); |
| 199 | read_fat(&fs); |
| 200 | scan_root(&fs); |
| 201 | reclaim_free(&fs); |
| 202 | qfree(&mem_queue); |
| 203 | n_files_verify = n_files; |
| 204 | } |
| 205 | |
| 206 | if (fs_changed()) { |
| 207 | if (rw) { |
| 208 | if (interactive) |
| 209 | rw = get_key("yn", "Perform changes ? (y/n)") == 'y'; |
| 210 | else |
| 211 | printf("Performing changes.\n"); |
| 212 | } else |
| 213 | printf("Leaving file system unchanged.\n"); |
| 214 | } |
| 215 | |
| 216 | printf("%s: %u files, %lu/%lu clusters\n", argv[optind], |
| 217 | n_files, fs.clusters - free_clusters, fs.clusters); |
| 218 | |
| 219 | if (retandroid) { |
| 220 | return fs_close(rw) ? 4 : 0; |
| 221 | } else { |
| 222 | return fs_close(rw) ? 1 : 0; |
| 223 | } |
| 224 | } |