1 /* $NetBSD: check.c,v 1.6 1997/01/03 14:32:48 ws Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Martin Husemann 18 * and Wolfgang Solfrank. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 36 #ifndef lint 37 static char rcsid[] = "$NetBSD: check.c,v 1.6 1997/01/03 14:32:48 ws Exp $"; 38 #endif /* not lint */ 39 40 #include <stdlib.h> 41 #include <string.h> 42 #include <ctype.h> 43 #include <stdio.h> 44 #include <unistd.h> 45 #include <fcntl.h> 46 47 #include "ext.h" 48 #include "fsutil.h" 49 50 int 51 checkfilesys(fname) 52 const char *fname; 53 { 54 int dosfs; 55 struct bootblock boot; 56 struct fatEntry * fat = NULL; 57 int i; 58 int mod = 0; 59 60 rdonly = alwaysno; 61 if (!preen) 62 printf("** %s", fname); 63 64 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0); 65 if (dosfs < 0 && !rdonly) { 66 dosfs = open(fname, O_RDONLY, 0); 67 if (dosfs >= 0) 68 pwarn(" (NO WRITE)\n"); 69 else if (!preen) 70 printf("\n"); 71 rdonly = 1; 72 } else if (!preen) 73 printf("\n"); 74 75 if (dosfs < 0) { 76 perror("Can't open"); 77 return 8; 78 } 79 80 if (readboot(dosfs, &boot) != FSOK) { 81 close(dosfs); 82 return 8; 83 } 84 85 if (!preen) 86 printf("** Phase 1 - Read and Compare FATs\n"); 87 88 for (i = 0; i < boot.FATs; i++) { 89 struct fatEntry *currentFat; 90 91 mod |= readfat(dosfs, &boot, i, ¤tFat); 92 93 if (mod & FSFATAL) { 94 if (fat) 95 free(fat); 96 close(dosfs); 97 return 8; 98 } 99 100 if (fat == NULL) 101 fat = currentFat; 102 else { 103 mod |= comparefat(&boot, fat, currentFat, i + 1); 104 free(currentFat); 105 if (mod & FSFATAL) { 106 free(fat); 107 close(dosfs); 108 return 8; 109 } 110 } 111 } 112 113 if (!preen) 114 printf("** Phase 2 - Check Cluster Chains\n"); 115 116 mod |= checkfat(&boot, fat); 117 if (mod & FSFATAL) { 118 free(fat); 119 close(dosfs); 120 return 8; 121 } 122 123 if (mod & FSFATMOD) 124 mod |= writefat(dosfs, &boot, fat); /* delay writing fats? XXX */ 125 if (mod & FSFATAL) { 126 free(fat); 127 close(dosfs); 128 return 8; 129 } 130 131 if (!preen) 132 printf("** Phase 3 - Checking Directories\n"); 133 134 if (resetDosDirSection(&boot) & FSFATAL) { 135 free(fat); 136 close(dosfs); 137 return 8; 138 } 139 140 mod |= handleDirTree(dosfs, &boot, fat); 141 if (mod & FSFATAL) { 142 finishDosDirSection(); 143 free(fat); 144 close(dosfs); 145 return 8; 146 } 147 148 if (!preen) 149 printf("** Phase 4 - Checking for Lost Files\n"); 150 151 mod |= checklost(dosfs, &boot, fat); 152 153 finishDosDirSection(); 154 free(fat); 155 close(dosfs); 156 157 if (boot.NumBad) 158 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n", 159 boot.NumFiles, 160 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree, 161 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad); 162 else 163 pwarn("%d files, %d free (%d clusters)\n", 164 boot.NumFiles, 165 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree); 166 if (mod & (FSFATAL | FSERROR)) 167 return 8; 168 if (mod) { 169 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 170 return 4; 171 } 172 return 0; 173 } 174