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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $");
30 static const char rcsid
[] =
44 checkfilesys(const char *fname
)
47 struct bootblock boot
;
48 struct fatEntry
*fat
= NULL
;
49 int finish_dosdirsection
=0;
56 printf("** %s", fname
);
58 dosfs
= open(fname
, rdonly
? O_RDONLY
: O_RDWR
, 0);
59 if (dosfs
< 0 && !rdonly
) {
60 dosfs
= open(fname
, O_RDONLY
, 0);
62 pwarn(" (NO WRITE)\n");
70 perr("Can't open `%s'", fname
);
75 if (readboot(dosfs
, &boot
) != FSOK
) {
81 if (skipclean
&& preen
&& checkdirty(dosfs
, &boot
)) {
82 printf("%s: ", fname
);
83 printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
89 if (boot
.ValidFat
< 0)
90 printf("** Phase 1 - Read and Compare FATs\n");
92 printf("** Phase 1 - Read FAT\n");
95 mod
|= readfat(dosfs
, &boot
, boot
.ValidFat
>= 0 ? boot
.ValidFat
: 0, &fat
);
101 if (boot
.ValidFat
< 0)
102 for (i
= 1; i
< boot
.bpbFATs
; i
++) {
103 struct fatEntry
*currentFat
;
105 mod
|= readfat(dosfs
, &boot
, i
, ¤tFat
);
110 mod
|= comparefat(&boot
, fat
, currentFat
, i
);
117 printf("** Phase 2 - Check Cluster Chains\n");
119 mod
|= checkfat(&boot
, fat
);
122 /* delay writing FATs */
125 printf("** Phase 3 - Checking Directories\n");
127 mod
|= resetDosDirSection(&boot
, fat
);
128 finish_dosdirsection
= 1;
131 /* delay writing FATs */
133 mod
|= handleDirTree(dosfs
, &boot
, fat
);
138 printf("** Phase 4 - Checking for Lost Files\n");
140 mod
|= checklost(dosfs
, &boot
, fat
);
144 /* now write the FATs */
145 if (mod
& (FSFATMOD
|FSFIXFAT
)) {
146 if (ask(1, "Update FATs")) {
147 mod
|= writefat(dosfs
, &boot
, fat
, mod
& FSFIXFAT
);
155 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
157 boot
.NumFree
* boot
.ClusterSize
/ 1024, boot
.NumFree
,
158 boot
.NumBad
* boot
.ClusterSize
/ 1024, boot
.NumBad
);
160 pwarn("%d files, %d free (%d clusters)\n",
162 boot
.NumFree
* boot
.ClusterSize
/ 1024, boot
.NumFree
);
164 if (mod
&& (mod
& FSERROR
) == 0) {
166 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
170 pwarn("MARKING FILE SYSTEM CLEAN\n");
171 mod
|= writefat(dosfs
, &boot
, fat
, 1);
173 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
174 mod
|= FSERROR
; /* file system not clean */
179 if (mod
& (FSFATAL
| FSERROR
))
185 if (finish_dosdirsection
)
186 finishDosDirSection();
190 if (mod
& (FSFATMOD
|FSDIRMOD
))
191 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");