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.3 2003/09/28 14:39:17 hmp Exp $
38 #include <sys/cdefs.h>
51 checkfilesys(const char *fname
)
54 struct bootblock boot
;
55 struct fatEntry
*fat
= NULL
;
56 int i
, finish_dosdirsection
=0;
62 printf("** %s", fname
);
64 dosfs
= open(fname
, rdonly
? O_RDONLY
: O_RDWR
, 0);
65 if (dosfs
< 0 && !rdonly
) {
66 dosfs
= open(fname
, O_RDONLY
, 0);
68 pwarn(" (NO WRITE)\n");
80 if (readboot(dosfs
, &boot
) != FSOK
) {
87 if (boot
.ValidFat
< 0)
88 printf("** Phase 1 - Read and Compare FATs\n");
90 printf("** Phase 1 - Read FAT\n");
93 mod
|= readfat(dosfs
, &boot
, boot
.ValidFat
>= 0 ? boot
.ValidFat
: 0, &fat
);
99 if (boot
.ValidFat
< 0)
100 for (i
= 1; i
< boot
.FATs
; i
++) {
101 struct fatEntry
*currentFat
;
103 mod
|= readfat(dosfs
, &boot
, i
, ¤tFat
);
108 mod
|= comparefat(&boot
, fat
, currentFat
, i
);
115 printf("** Phase 2 - Check Cluster Chains\n");
117 mod
|= checkfat(&boot
, fat
);
120 /* delay writing FATs */
123 printf("** Phase 3 - Checking Directories\n");
125 mod
|= resetDosDirSection(&boot
, fat
);
126 finish_dosdirsection
= 1;
129 /* delay writing FATs */
131 mod
|= handleDirTree(dosfs
, &boot
, fat
);
136 printf("** Phase 4 - Checking for Lost Files\n");
138 mod
|= checklost(dosfs
, &boot
, fat
);
142 /* now write the FATs */
143 if (mod
& FSFATMOD
) {
144 if (ask(1, "Update FATs")) {
145 mod
|= writefat(dosfs
, &boot
, fat
, mod
& FSFIXFAT
);
153 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
155 boot
.NumFree
* boot
.ClusterSize
/ 1024, boot
.NumFree
,
156 boot
.NumBad
* boot
.ClusterSize
/ 1024, boot
.NumBad
);
158 pwarn("%d files, %d free (%d clusters)\n",
160 boot
.NumFree
* boot
.ClusterSize
/ 1024, boot
.NumFree
);
162 if (mod
&& (mod
& FSERROR
) == 0) {
164 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
168 pwarn("MARKING FILE SYSTEM CLEAN\n");
169 mod
|= writefat(dosfs
, &boot
, fat
, 1);
171 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
172 mod
|= FSERROR
; /* file system not clean */
177 if (mod
& (FSFATAL
| FSERROR
))
183 if (finish_dosdirsection
)
184 finishDosDirSection();
188 if (mod
& (FSFATMOD
|FSDIRMOD
))
189 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");