check.c revision 1.1 1 /* $NetBSD: check.c,v 1.1 1996/05/14 17:39:29 ws Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank
5 * Copyright (c) 1995 Martin Husemann
6 *
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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Martin Husemann
18 * and Wolfgang Solfrank.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35
36 #ifndef lint
37 static char rcsid[] = "$NetBSD: check.c,v 1.1 1996/05/14 17:39:29 ws Exp $";
38 #endif /* not lint */
39
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46
47 #include "ext.h"
48
49 int
50 checkfilesys(fname)
51 const char *fname;
52 {
53 int dosfs;
54 struct dosDirEntry *rootDir;
55 struct bootblock boot;
56 struct fatEntry * fat = NULL;
57 int i;
58 int mod = 0;
59
60 rdonly = alwaysno;
61 if (!preen)
62 printf("** %s", fname);
63
64 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
65 if (dosfs < 0 && !rdonly) {
66 dosfs = open(fname, O_RDONLY, 0);
67 if (dosfs >= 0)
68 pwarn(" (NO WRITE)\n");
69 else if (!preen)
70 printf("\n");
71 rdonly = 1;
72 } else if (!preen)
73 printf("\n");
74
75 if (dosfs < 0) {
76 perror("Can't open");
77 return 8;
78 }
79
80 if (readboot(dosfs, &boot) != FSOK) {
81 close(dosfs);
82 return 8;
83 }
84
85 if (!preen)
86 printf("** Phase 1 - Read and Compare FATs\n");
87
88 for (i = 0; i < boot.FATs; i++) {
89 struct fatEntry *currentFat;
90
91 mod |= readfat(dosfs, &boot, i, ¤tFat);
92
93 if (mod&FSFATAL) {
94 close(dosfs);
95 return 8;
96 }
97
98 if (fat == NULL)
99 fat = currentFat;
100 else {
101 mod |= comparefat(&boot, fat, currentFat, i + 1);
102 if (mod&FSFATAL) {
103 close(dosfs);
104 return 8;
105 }
106 }
107 }
108
109 if (!preen)
110 printf("** Phase 2 - Check Cluster Chains\n");
111
112 mod |= checkfat(&boot, fat);
113 if (mod&FSFATAL) {
114 close(dosfs);
115 return 8;
116 }
117
118 if (mod&FSFATMOD)
119 mod |= writefat(dosfs, &boot, fat); /* delay writing fats? XXX */
120 if (mod&FSFATAL) {
121 close(dosfs);
122 return 8;
123 }
124
125 if (!preen)
126 printf("** Phase 3 - Checking Directories\n");
127
128 rootDir = malloc(sizeof(struct dosDirEntry));
129 memset(rootDir, 0, sizeof(struct dosDirEntry));
130 rootDir->fullpath = strdup("/");
131 if (resetDosDirSection(&boot)&FSFATAL) {
132 close(dosfs);
133 return 8;
134 }
135
136 mod = readDosDirSection(dosfs, &boot, fat, rootDir);
137 if (mod&FSFATAL) {
138 close(dosfs);
139 return 8;
140 }
141
142 if (mod&FSFATMOD)
143 mod |= writefat(dosfs, &boot, fat); /* delay writing fats? XXX */
144 if (mod&FSFATAL) {
145 close(dosfs);
146 return 8;
147 }
148
149 /*
150 * process the directory todo list
151 */
152 while (pendingDirectories) {
153 struct dosDirEntry *dir = pendingDirectories->dir;
154 struct dirTodoNode *n = pendingDirectories->next;
155
156 /*
157 * remove TODO entry now, the list might change during
158 * directory reads
159 */
160 free(pendingDirectories);
161 pendingDirectories = n;
162
163 /*
164 * handle subdirectory
165 */
166 mod |= readDosDirSection(dosfs, &boot, fat, dir);
167 if (mod&FSFATAL) {
168 close(dosfs);
169 return 8;
170 }
171 if (mod&FSFATMOD)
172 mod |= writefat(dosfs, &boot, fat); /* delay writing fats? XXX */
173 if (mod&FSFATAL) {
174 close(dosfs);
175 return 8;
176 }
177 }
178 finishDosDirSection();
179
180 if (!preen)
181 printf("** Phase 4 - Checking for Lost Files\n");
182
183 mod |= checklost(dosfs, &boot, fat, rootDir);
184
185 close(dosfs);
186 pwarn("%d files, %d free (%d clusters)\n",
187 boot.NumFiles, boot.NumFree * boot.ClusterSize / 1024,
188 boot.NumFree);
189 if (mod&(FSFATAL|FSERROR))
190 return 8;
191 if (mod) {
192 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
193 return 4;
194 }
195 return 0;
196 }
197