add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / fsck_msdos / check.c
blobb6cd2e7ba1d7dac0c4044c2b4115e739ba44bc3d
1 /*
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
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.
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.
34 #include <sys/cdefs.h>
35 #ifndef lint
36 /* __RCSID("$NetBSD: check.c,v 1.10 2000/04/25 23:02:51 jdolecek Exp $");*/
37 static const char rcsid[] =
38 "$FreeBSD: src/sbin/fsck_msdosfs/check.c,v 1.10 2004/02/05 15:47:46 bde Exp $";
39 #endif /* not lint */
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <fcntl.h>
48 #include "ext.h"
49 #include "fsutil.h"
52 * If the FAT > this size then skip comparing, lest we risk
53 * OOMing the framework. in the future we need to just re-write
54 * this whole thing and optimize for less memory
56 #define FAT_COMPARE_MAX_KB 4096
58 int
59 checkfilesys(const char *fname)
61 int dosfs;
62 struct bootblock boot;
63 struct fatEntry *fat = NULL;
64 int i, finish_dosdirsection=0;
65 int mod = 0;
66 int ret = 8;
67 int quiet = 0;
68 int skip_fat_compare = 0;
70 rdonly = alwaysno;
71 if (!quiet)
72 printf("** %s", fname);
74 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
75 if (dosfs < 0 && !rdonly) {
76 dosfs = open(fname, O_RDONLY, 0);
77 if (dosfs >= 0)
78 pwarn(" (NO WRITE)\n");
79 else if (!quiet)
80 printf("\n");
81 rdonly = 1;
82 } else if (!quiet)
83 printf("\n");
85 if (dosfs < 0) {
86 perror("Can't open");
87 return 8;
90 if (readboot(dosfs, &boot) == FSFATAL) {
91 close(dosfs);
92 printf("\n");
93 return 8;
96 if (skipclean && preen && checkdirty(dosfs, &boot)) {
97 printf("%s: ", fname);
98 printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
99 ret = 0;
100 goto out;
103 if (((boot.FATsecs * boot.BytesPerSec) / 1024) > FAT_COMPARE_MAX_KB)
104 skip_fat_compare = 1;
106 if (!quiet) {
107 if (skip_fat_compare)
108 printf("** Phase 1 - Read FAT (compare skipped)\n");
109 else if (boot.ValidFat < 0)
110 printf("** Phase 1 - Read and Compare FATs\n");
111 else
112 printf("** Phase 1 - Read FAT\n");
115 mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
116 if (mod & FSFATAL) {
117 printf("Fatal error during readfat()\n");
118 close(dosfs);
119 return 8;
122 if (!skip_fat_compare && boot.ValidFat < 0)
123 for (i = 1; i < (int)boot.FATs; i++) {
124 struct fatEntry *currentFat;
126 mod |= readfat(dosfs, &boot, i, &currentFat);
128 if (mod & FSFATAL) {
129 printf("Fatal error during readfat() for comparison\n");
130 goto out;
133 mod |= comparefat(&boot, fat, currentFat, i);
134 free(currentFat);
135 if (mod & FSFATAL) {
136 printf("Fatal error during FAT comparison\n");
137 goto out;
141 if (!quiet)
142 printf("** Phase 2 - Check Cluster Chains\n");
144 mod |= checkfat(&boot, fat);
145 if (mod & FSFATAL) {
146 printf("Fatal error during FAT check\n");
147 goto out;
149 /* delay writing FATs */
151 if (!quiet)
152 printf("** Phase 3 - Checking Directories\n");
154 mod |= resetDosDirSection(&boot, fat);
155 finish_dosdirsection = 1;
156 if (mod & FSFATAL) {
157 printf("Fatal error during resetDosDirSection()\n");
158 goto out;
160 /* delay writing FATs */
162 mod |= handleDirTree(dosfs, &boot, fat);
163 if (mod & FSFATAL)
164 goto out;
166 if (!quiet)
167 printf("** Phase 4 - Checking for Lost Files\n");
169 mod |= checklost(dosfs, &boot, fat);
170 if (mod & FSFATAL)
171 goto out;
173 /* now write the FATs */
174 if (mod & FSFATMOD) {
175 if (ask(1, "Update FATs")) {
176 mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
177 if (mod & FSFATAL) {
178 printf("Fatal error during writefat()\n");
179 goto out;
181 } else
182 mod |= FSERROR;
185 if (boot.NumBad)
186 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
187 boot.NumFiles,
188 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
189 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
190 else
191 pwarn("%d files, %d free (%d clusters)\n",
192 boot.NumFiles,
193 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
195 if (mod && (mod & FSERROR) == 0) {
196 if (mod & FSDIRTY) {
197 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
198 mod &= ~FSDIRTY;
200 if (mod & FSDIRTY) {
201 pwarn("MARKING FILE SYSTEM CLEAN\n");
202 mod |= writefat(dosfs, &boot, fat, 1);
203 } else {
204 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
205 mod |= FSERROR; /* file system not clean */
210 if (mod & (FSFATAL | FSERROR))
211 goto out;
213 ret = 0;
215 out:
216 if (finish_dosdirsection)
217 finishDosDirSection();
218 free(fat);
219 close(dosfs);
221 if (mod & (FSFATMOD|FSDIRMOD)) {
222 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
223 return 4;
226 return ret;