2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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 * $DragonFly: src/sbin/fsck_msdosfs/check.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
38 #include <sys/cdefs.h>
55 struct bootblock boot;
56 struct fatEntry *fat = NULL;
57 int i, finish_dosdirsection=0;
63 printf("** %s", fname);
65 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
66 if (dosfs < 0 && !rdonly) {
67 dosfs = open(fname, O_RDONLY, 0);
69 pwarn(" (NO WRITE)\n");
81 if (readboot(dosfs, &boot) != FSOK) {
88 if (boot.ValidFat < 0)
89 printf("** Phase 1 - Read and Compare FATs\n");
91 printf("** Phase 1 - Read FAT\n");
94 mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
100 if (boot.ValidFat < 0)
101 for (i = 1; i < boot.FATs; i++) {
102 struct fatEntry *currentFat;
104 mod |= readfat(dosfs, &boot, i, ¤tFat);
109 mod |= comparefat(&boot, fat, currentFat, i);
116 printf("** Phase 2 - Check Cluster Chains\n");
118 mod |= checkfat(&boot, fat);
121 /* delay writing FATs */
124 printf("** Phase 3 - Checking Directories\n");
126 mod |= resetDosDirSection(&boot, fat);
127 finish_dosdirsection = 1;
130 /* delay writing FATs */
132 mod |= handleDirTree(dosfs, &boot, fat);
137 printf("** Phase 4 - Checking for Lost Files\n");
139 mod |= checklost(dosfs, &boot, fat);
143 /* now write the FATs */
144 if (mod & FSFATMOD) {
145 if (ask(1, "Update FATs")) {
146 mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
154 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
156 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
157 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
159 pwarn("%d files, %d free (%d clusters)\n",
161 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
163 if (mod && (mod & FSERROR) == 0) {
165 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
169 pwarn("MARKING FILE SYSTEM CLEAN\n");
170 mod |= writefat(dosfs, &boot, fat, 1);
172 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
173 mod |= FSERROR; /* file system not clean */
178 if (mod & (FSFATAL | FSERROR))
184 if (finish_dosdirsection)
185 finishDosDirSection();
189 if (mod & (FSFATMOD|FSDIRMOD))
190 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");