check.c revision 1.13 1 1.13 xtraeme /* $NetBSD: check.c,v 1.13 2005/01/19 20:00:45 xtraeme Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.8 ws * Copyright (C) 1995, 1996, 1997 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.7 lukem #include <sys/cdefs.h>
37 1.1 ws #ifndef lint
38 1.13 xtraeme __RCSID("$NetBSD: check.c,v 1.13 2005/01/19 20:00:45 xtraeme Exp $");
39 1.1 ws #endif /* not lint */
40 1.1 ws
41 1.1 ws #include <stdlib.h>
42 1.1 ws #include <string.h>
43 1.1 ws #include <ctype.h>
44 1.1 ws #include <stdio.h>
45 1.1 ws #include <unistd.h>
46 1.1 ws #include <fcntl.h>
47 1.1 ws
48 1.1 ws #include "ext.h"
49 1.5 christos #include "fsutil.h"
50 1.1 ws
51 1.1 ws int
52 1.13 xtraeme checkfilesys(const char *filename)
53 1.1 ws {
54 1.1 ws int dosfs;
55 1.1 ws struct bootblock boot;
56 1.8 ws struct fatEntry *fat = NULL;
57 1.10 jdolecek int i, finish_dosdirsection=0;
58 1.1 ws int mod = 0;
59 1.10 jdolecek int ret = 8;
60 1.1 ws
61 1.1 ws rdonly = alwaysno;
62 1.1 ws if (!preen)
63 1.12 wiz printf("** %s", filename);
64 1.1 ws
65 1.12 wiz dosfs = open(filename, rdonly ? O_RDONLY : O_RDWR, 0);
66 1.1 ws if (dosfs < 0 && !rdonly) {
67 1.12 wiz dosfs = open(filename, O_RDONLY, 0);
68 1.1 ws if (dosfs >= 0)
69 1.1 ws pwarn(" (NO WRITE)\n");
70 1.1 ws else if (!preen)
71 1.1 ws printf("\n");
72 1.1 ws rdonly = 1;
73 1.1 ws } else if (!preen)
74 1.1 ws printf("\n");
75 1.8 ws
76 1.1 ws if (dosfs < 0) {
77 1.1 ws perror("Can't open");
78 1.1 ws return 8;
79 1.1 ws }
80 1.1 ws
81 1.1 ws if (readboot(dosfs, &boot) != FSOK) {
82 1.1 ws close(dosfs);
83 1.11 abs printf("\n");
84 1.1 ws return 8;
85 1.1 ws }
86 1.1 ws
87 1.9 ross if (!preen) {
88 1.8 ws if (boot.ValidFat < 0)
89 1.8 ws printf("** Phase 1 - Read and Compare FATs\n");
90 1.8 ws else
91 1.8 ws printf("** Phase 1 - Read FAT\n");
92 1.9 ross }
93 1.8 ws
94 1.8 ws mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
95 1.8 ws if (mod & FSFATAL) {
96 1.8 ws close(dosfs);
97 1.8 ws return 8;
98 1.8 ws }
99 1.8 ws
100 1.8 ws if (boot.ValidFat < 0)
101 1.8 ws for (i = 1; i < boot.FATs; i++) {
102 1.8 ws struct fatEntry *currentFat;
103 1.1 ws
104 1.8 ws mod |= readfat(dosfs, &boot, i, ¤tFat);
105 1.1 ws
106 1.10 jdolecek if (mod & FSFATAL)
107 1.10 jdolecek goto out;
108 1.8 ws
109 1.8 ws mod |= comparefat(&boot, fat, currentFat, i);
110 1.3 ws free(currentFat);
111 1.10 jdolecek if (mod & FSFATAL)
112 1.10 jdolecek goto out;
113 1.1 ws }
114 1.1 ws
115 1.1 ws if (!preen)
116 1.1 ws printf("** Phase 2 - Check Cluster Chains\n");
117 1.8 ws
118 1.1 ws mod |= checkfat(&boot, fat);
119 1.10 jdolecek if (mod & FSFATAL)
120 1.10 jdolecek goto out;
121 1.10 jdolecek /* delay writing FATs */
122 1.1 ws
123 1.1 ws if (!preen)
124 1.1 ws printf("** Phase 3 - Checking Directories\n");
125 1.3 ws
126 1.8 ws mod |= resetDosDirSection(&boot, fat);
127 1.10 jdolecek finish_dosdirsection = 1;
128 1.10 jdolecek if (mod & FSFATAL)
129 1.10 jdolecek goto out;
130 1.10 jdolecek /* delay writing FATs */
131 1.3 ws
132 1.3 ws mod |= handleDirTree(dosfs, &boot, fat);
133 1.10 jdolecek if (mod & FSFATAL)
134 1.10 jdolecek goto out;
135 1.8 ws
136 1.1 ws if (!preen)
137 1.1 ws printf("** Phase 4 - Checking for Lost Files\n");
138 1.1 ws
139 1.3 ws mod |= checklost(dosfs, &boot, fat);
140 1.10 jdolecek if (mod & FSFATAL)
141 1.10 jdolecek goto out;
142 1.10 jdolecek
143 1.10 jdolecek /* now write the FATs */
144 1.10 jdolecek if (mod & FSFATMOD) {
145 1.10 jdolecek if (ask(1, "Update FATs")) {
146 1.10 jdolecek mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
147 1.10 jdolecek if (mod & FSFATAL)
148 1.10 jdolecek goto out;
149 1.10 jdolecek } else
150 1.10 jdolecek mod |= FSERROR;
151 1.8 ws }
152 1.8 ws
153 1.6 ws if (boot.NumBad)
154 1.6 ws pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
155 1.6 ws boot.NumFiles,
156 1.6 ws boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
157 1.6 ws boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
158 1.6 ws else
159 1.6 ws pwarn("%d files, %d free (%d clusters)\n",
160 1.6 ws boot.NumFiles,
161 1.6 ws boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
162 1.8 ws
163 1.10 jdolecek if (mod && (mod & FSERROR) == 0) {
164 1.10 jdolecek if (mod & FSDIRTY) {
165 1.10 jdolecek if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
166 1.10 jdolecek mod &= ~FSDIRTY;
167 1.10 jdolecek
168 1.10 jdolecek if (mod & FSDIRTY) {
169 1.10 jdolecek pwarn("MARKING FILE SYSTEM CLEAN\n");
170 1.10 jdolecek mod |= writefat(dosfs, &boot, fat, 1);
171 1.10 jdolecek } else {
172 1.10 jdolecek pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
173 1.10 jdolecek mod |= FSERROR; /* file system not clean */
174 1.10 jdolecek }
175 1.10 jdolecek }
176 1.10 jdolecek }
177 1.10 jdolecek
178 1.3 ws if (mod & (FSFATAL | FSERROR))
179 1.10 jdolecek goto out;
180 1.10 jdolecek
181 1.10 jdolecek ret = 0;
182 1.10 jdolecek
183 1.10 jdolecek out:
184 1.10 jdolecek if (finish_dosdirsection)
185 1.10 jdolecek finishDosDirSection();
186 1.10 jdolecek free(fat);
187 1.10 jdolecek close(dosfs);
188 1.10 jdolecek
189 1.10 jdolecek if (mod & (FSFATMOD|FSDIRMOD))
190 1.1 ws pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
191 1.10 jdolecek
192 1.10 jdolecek return ret;
193 1.1 ws }
194