1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank 5 * Copyright (c) 1995 Martin Husemann 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <stdlib.h> 29 #include <string.h> 30 #include <stdio.h> 31 #include <unistd.h> 32 #include <fcntl.h> 33 34 #include "ext.h" 35 #include "fsutil.h" 36 37 int 38 checkfilesys(const char *fname) 39 { 40 int dosfs; 41 struct bootblock boot; 42 struct fatEntry *fat = NULL; 43 int finish_dosdirsection=0; 44 u_int i; 45 int mod = 0; 46 int ret = 8; 47 48 rdonly = alwaysno; 49 if (!preen) 50 printf("** %s", fname); 51 52 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0); 53 if (dosfs < 0 && !rdonly) { 54 dosfs = open(fname, O_RDONLY, 0); 55 if (dosfs >= 0) 56 pwarn(" (NO WRITE)\n"); 57 else if (!preen) 58 printf("\n"); 59 rdonly = 1; 60 } else if (!preen) 61 printf("\n"); 62 63 if (dosfs < 0) { 64 perr("Can't open `%s'", fname); 65 printf("\n"); 66 return 8; 67 } 68 69 if (readboot(dosfs, &boot) == FSFATAL) { 70 close(dosfs); 71 printf("\n"); 72 return 8; 73 } 74 75 if (!preen) { 76 if (boot.ValidFat < 0) 77 printf("** Phase 1 - Read and Compare FATs\n"); 78 else 79 printf("** Phase 1 - Read FAT\n"); 80 } 81 82 mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat); 83 if (mod & FSFATAL) { 84 close(dosfs); 85 return 8; 86 } 87 88 if (boot.ValidFat < 0) 89 for (i = 1; i < boot.bpbFATs; i++) { 90 struct fatEntry *currentFat; 91 92 mod |= readfat(dosfs, &boot, i, ¤tFat); 93 94 if (mod & FSFATAL) 95 goto out; 96 97 mod |= comparefat(&boot, fat, currentFat, i); 98 free(currentFat); 99 if (mod & FSFATAL) 100 goto out; 101 } 102 103 if (!preen) 104 printf("** Phase 2 - Check Cluster Chains\n"); 105 106 mod |= checkfat(&boot, fat); 107 if (mod & FSFATAL) 108 goto out; 109 /* delay writing FATs */ 110 111 if (!preen) 112 printf("** Phase 3 - Checking Directories\n"); 113 114 mod |= resetDosDirSection(&boot, fat); 115 finish_dosdirsection = 1; 116 if (mod & FSFATAL) 117 goto out; 118 /* delay writing FATs */ 119 120 mod |= handleDirTree(dosfs, &boot, fat); 121 if (mod & FSFATAL) 122 goto out; 123 124 if (!preen) 125 printf("** Phase 4 - Checking for Lost Files\n"); 126 127 mod |= checklost(dosfs, &boot, fat); 128 if (mod & FSFATAL) 129 goto out; 130 131 /* now write the FATs */ 132 if (mod & (FSFATMOD|FSFIXFAT)) { 133 if (ask(1, "Update FATs")) { 134 mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT); 135 if (mod & FSFATAL) 136 goto out; 137 } else 138 mod |= FSERROR; 139 } 140 141 if (boot.NumBad) 142 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n", 143 boot.NumFiles, 144 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree, 145 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad); 146 else 147 pwarn("%d files, %d free (%d clusters)\n", 148 boot.NumFiles, 149 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree); 150 151 if (mod && (mod & FSERROR) == 0) { 152 if (mod & FSDIRTY) { 153 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0) 154 mod &= ~FSDIRTY; 155 156 if (mod & FSDIRTY) { 157 pwarn("MARKING FILE SYSTEM CLEAN\n"); 158 mod |= writefat(dosfs, &boot, fat, 1); 159 } else { 160 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n"); 161 mod |= FSERROR; /* file system not clean */ 162 } 163 } 164 } 165 166 if (mod & (FSFATAL | FSERROR)) 167 goto out; 168 169 ret = 0; 170 171 out: 172 if (finish_dosdirsection) 173 finishDosDirSection(); 174 free(fat); 175 close(dosfs); 176 177 if (mod & (FSFATMOD|FSDIRMOD)) 178 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 179 180 return ret; 181 } 182