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