No empty .Rs/.Re
[netbsd-mini2440.git] / sbin / fsck_msdos / check.c
blob4a7830ccbc6732c6dea5f5f1ea9101a207fbc195
1 /* $NetBSD: check.c,v 1.17 2008/06/13 20:46:09 martin Exp $ */
3 /*
4 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
5 * Copyright (c) 1995 Martin Husemann
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.
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.
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 */
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <fcntl.h>
40 #include "ext.h"
41 #include "fsutil.h"
42 #include "exitvalues.h"
44 int
45 checkfilesys(const char *filename)
47 int dosfs;
48 struct bootblock boot;
49 struct fatEntry *fat = NULL;
50 int finish_dosdirsection=0;
51 u_int i;
52 int mod = 0;
53 int ret = FSCK_EXIT_CHECK_FAILED;
55 rdonly = alwaysno;
56 if (!preen)
57 printf("** %s", filename);
59 dosfs = open(filename, rdonly ? O_RDONLY : O_RDWR, 0);
60 if (dosfs < 0 && !rdonly) {
61 dosfs = open(filename, O_RDONLY, 0);
62 if (dosfs >= 0)
63 pwarn(" (NO WRITE)\n");
64 else if (!preen)
65 printf("\n");
66 rdonly = 1;
67 } else if (!preen)
68 printf("\n");
70 if (dosfs < 0) {
71 perr("Can't open `%s'", filename);
72 return FSCK_EXIT_CHECK_FAILED;
75 if (readboot(dosfs, &boot) != FSOK) {
76 close(dosfs);
77 printf("\n");
78 return FSCK_EXIT_CHECK_FAILED;
81 if (!preen) {
82 if (boot.ValidFat < 0)
83 printf("** Phase 1 - Read and Compare FATs\n");
84 else
85 printf("** Phase 1 - Read FAT\n");
88 mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
89 if (mod & FSFATAL) {
90 close(dosfs);
91 return FSCK_EXIT_CHECK_FAILED;
94 if (boot.ValidFat < 0)
95 for (i = 1; i < boot.FATs; i++) {
96 struct fatEntry *currentFat;
98 mod |= readfat(dosfs, &boot, i, &currentFat);
100 if (mod & FSFATAL)
101 goto out;
103 mod |= comparefat(&boot, fat, currentFat, i);
104 free(currentFat);
105 if (mod & FSFATAL)
106 goto out;
109 if (!preen)
110 printf("** Phase 2 - Check Cluster Chains\n");
112 mod |= checkfat(&boot, fat);
113 if (mod & FSFATAL)
114 goto out;
115 /* delay writing FATs */
117 if (!preen)
118 printf("** Phase 3 - Checking Directories\n");
120 mod |= resetDosDirSection(&boot, fat);
121 finish_dosdirsection = 1;
122 if (mod & FSFATAL)
123 goto out;
124 /* delay writing FATs */
126 mod |= handleDirTree(dosfs, &boot, fat);
127 if (mod & FSFATAL)
128 goto out;
130 if (!preen)
131 printf("** Phase 4 - Checking for Lost Files\n");
133 mod |= checklost(dosfs, &boot, fat);
134 if (mod & FSFATAL)
135 goto out;
137 /* now write the FATs */
138 if (mod & FSFATMOD) {
139 if (ask(1, "Update FATs")) {
140 mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
141 if (mod & FSFATAL)
142 goto out;
143 } else
144 mod |= FSERROR;
147 if (boot.NumBad)
148 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
149 boot.NumFiles,
150 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
151 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
152 else
153 pwarn("%d files, %d free (%d clusters)\n",
154 boot.NumFiles,
155 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
157 if (mod && (mod & FSERROR) == 0) {
158 if (mod & FSDIRTY) {
159 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
160 mod &= ~FSDIRTY;
162 if (mod & FSDIRTY) {
163 pwarn("MARKING FILE SYSTEM CLEAN\n");
164 mod |= writefat(dosfs, &boot, fat, 1);
165 } else {
166 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
167 mod |= FSERROR; /* file system not clean */
172 if (mod & (FSFATAL | FSERROR))
173 goto out;
175 ret = FSCK_EXIT_OK;
177 out:
178 if (finish_dosdirsection)
179 finishDosDirSection();
180 free(fat);
181 close(dosfs);
183 if (mod & (FSFATMOD|FSDIRMOD))
184 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
186 return ret;