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