1 /* $NetBSD: check.c,v 1.17 2008/06/13 20:46:09 martin Exp $ */ 2 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 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __RCSID("$NetBSD: check.c,v 1.17 2008/06/13 20:46:09 martin Exp $"); 32 #endif /* not lint */ 33 34 #include <stdlib.h> 35 #include <string.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 #include <fcntl.h> 39 40 #include "ext.h" 41 #include "fsutil.h" 42 #include "exitvalues.h" 43 44 int 45 checkfilesys(const char *filename) 46 { 47 int dosfs; 48 struct bootblock boot; 49 struct fatEntry *fat = NULL; 50 int i, finish_dosdirsection=0; 51 int mod = 0; 52 int ret = FSCK_EXIT_CHECK_FAILED; 53 54 rdonly = alwaysno; 55 if (!preen) 56 printf("** %s", filename); 57 58 dosfs = open(filename, rdonly ? O_RDONLY : O_RDWR, 0); 59 if (dosfs < 0 && !rdonly) { 60 dosfs = open(filename, O_RDONLY, 0); 61 if (dosfs >= 0) 62 pwarn(" (NO WRITE)\n"); 63 else if (!preen) 64 printf("\n"); 65 rdonly = 1; 66 } else if (!preen) 67 printf("\n"); 68 69 if (dosfs < 0) { 70 perr("Can't open `%s'", filename); 71 return FSCK_EXIT_CHECK_FAILED; 72 } 73 74 if (readboot(dosfs, &boot) != FSOK) { 75 close(dosfs); 76 printf("\n"); 77 return FSCK_EXIT_CHECK_FAILED; 78 } 79 80 if (!preen) { 81 if (boot.ValidFat < 0) 82 printf("** Phase 1 - Read and Compare FATs\n"); 83 else 84 printf("** Phase 1 - Read FAT\n"); 85 } 86 87 mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat); 88 if (mod & FSFATAL) { 89 close(dosfs); 90 return FSCK_EXIT_CHECK_FAILED; 91 } 92 93 if (boot.ValidFat < 0) 94 for (i = 1; i < boot.FATs; i++) { 95 struct fatEntry *currentFat; 96 97 mod |= readfat(dosfs, &boot, i, ¤tFat); 98 99 if (mod & FSFATAL) 100 goto out; 101 102 mod |= comparefat(&boot, fat, currentFat, i); 103 free(currentFat); 104 if (mod & FSFATAL) 105 goto out; 106 } 107 108 if (!preen) 109 printf("** Phase 2 - Check Cluster Chains\n"); 110 111 mod |= checkfat(&boot, fat); 112 if (mod & FSFATAL) 113 goto out; 114 /* delay writing FATs */ 115 116 if (!preen) 117 printf("** Phase 3 - Checking Directories\n"); 118 119 mod |= resetDosDirSection(&boot, fat); 120 finish_dosdirsection = 1; 121 if (mod & FSFATAL) 122 goto out; 123 /* delay writing FATs */ 124 125 mod |= handleDirTree(dosfs, &boot, fat); 126 if (mod & FSFATAL) 127 goto out; 128 129 if (!preen) 130 printf("** Phase 4 - Checking for Lost Files\n"); 131 132 mod |= checklost(dosfs, &boot, fat); 133 if (mod & FSFATAL) 134 goto out; 135 136 /* now write the FATs */ 137 if (mod & FSFATMOD) { 138 if (ask(1, "Update FATs")) { 139 mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT); 140 if (mod & FSFATAL) 141 goto out; 142 } else 143 mod |= FSERROR; 144 } 145 146 if (boot.NumBad) 147 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n", 148 boot.NumFiles, 149 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree, 150 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad); 151 else 152 pwarn("%d files, %d free (%d clusters)\n", 153 boot.NumFiles, 154 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree); 155 156 if (mod && (mod & FSERROR) == 0) { 157 if (mod & FSDIRTY) { 158 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0) 159 mod &= ~FSDIRTY; 160 161 if (mod & FSDIRTY) { 162 pwarn("MARKING FILE SYSTEM CLEAN\n"); 163 mod |= writefat(dosfs, &boot, fat, 1); 164 } else { 165 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n"); 166 mod |= FSERROR; /* file system not clean */ 167 } 168 } 169 } 170 171 if (mod & (FSFATAL | FSERROR)) 172 goto out; 173 174 ret = FSCK_EXIT_OK; 175 176 out: 177 if (finish_dosdirsection) 178 finishDosDirSection(); 179 free(fat); 180 close(dosfs); 181 182 if (mod & (FSFATMOD|FSDIRMOD)) 183 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 184 185 return ret; 186 } 187