1/*
2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
4 *
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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27#include <sys/cdefs.h>
28#ifndef lint
29__RCSID("$NetBSD: check.c,v 1.10 2000/04/25 23:02:51 jdolecek Exp $");
30static const char rcsid[] =
31 "$FreeBSD: head/sbin/fsck_msdosfs/check.c 203874 2010-02-14 12:31:28Z kib $";
32#endif /* not lint */
33
34#include <stdlib.h>
35#include <string.h>
36#include <stdio.h>
37#include <unistd.h>
38#include <fcntl.h>
39
40#include "ext.h"
41#include "fsutil.h"
42
43int
44checkfilesys(const char *fname)
45{
46 int dosfs;
47 struct bootblock boot;
48 struct fatEntry *fat = NULL;
49 int finish_dosdirsection=0;
50 u_int i;
51 int mod = 0;
52 int ret = 8;
53
54 rdonly = alwaysno;
55 if (!preen)
56 printf("** %s", fname);
57
58 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
59 if (dosfs < 0 && !rdonly) {
60 dosfs = open(fname, O_RDONLY, 0);
61 if (dosfs >= 0)
62 pwarn(" (NO WRITE)\n");
63 else if (!preen)
64 printf("\n");
65 rdonly = 1;
66 } else if (!preen)
67 printf("\n");
68
69 if (dosfs < 0) {
70 perror("Can't open");
71 return 8;
72 }
73
74 if (readboot(dosfs, &boot) != FSOK) {
75 close(dosfs);
76 printf("\n");
77 return 8;
78 }
79
80 if (skipclean && preen && checkdirty(dosfs, &boot)) {
81 printf("%s: ", fname);
82 printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
83 ret = 0;
84 goto out;
85 }
86
87 if (!preen) {
88 if (boot.ValidFat < 0)
89 printf("** Phase 1 - Read and Compare FATs\n");
90 else
91 printf("** Phase 1 - Read FAT\n");
92 }
93
94 mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
95 if (mod & FSFATAL) {
96 close(dosfs);
97 return 8;
98 }
99
100 if (boot.ValidFat < 0)
101 for (i = 1; i < (int)boot.bpbFATs; i++) {
102 struct fatEntry *currentFat;
103
104 mod |= readfat(dosfs, &boot, i, ¤tFat);
105
106 if (mod & FSFATAL)
107 goto out;
108
109 mod |= comparefat(&boot, fat, currentFat, i);
110 free(currentFat);
111 if (mod & FSFATAL)
112 goto out;
113 }
114
115 if (!preen)
116 printf("** Phase 2 - Check Cluster Chains\n");
117
118 mod |= checkfat(&boot, fat);
119 if (mod & FSFATAL)
120 goto out;
121 /* delay writing FATs */
122
123 if (!preen)
124 printf("** Phase 3 - Checking Directories\n");
125
126 mod |= resetDosDirSection(&boot, fat);
127 finish_dosdirsection = 1;
128 if (mod & FSFATAL)
129 goto out;
130 /* delay writing FATs */
131
132 mod |= handleDirTree(dosfs, &boot, fat);
133 if (mod & FSFATAL)
134 goto out;
135
136 if (!preen)
137 printf("** Phase 4 - Checking for Lost Files\n");
138
139 mod |= checklost(dosfs, &boot, fat);
140 if (mod & FSFATAL)
141 goto out;
142
143 /* now write the FATs */
144 if (mod & FSFATMOD) {
145 if (ask(1, "Update FATs")) {
146 mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
147 if (mod & FSFATAL)
148 goto out;
149 } else
150 mod |= FSERROR;
151 }
152
153 if (boot.NumBad)
154 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
155 boot.NumFiles,
156 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
157 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
158 else
159 pwarn("%d files, %d free (%d clusters)\n",
160 boot.NumFiles,
161 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
162
163 if (mod && (mod & FSERROR) == 0) {
164 if (mod & FSDIRTY) {
165 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
166 mod &= ~FSDIRTY;
167
168 if (mod & FSDIRTY) {
169 pwarn("MARKING FILE SYSTEM CLEAN\n");
170 mod |= writefat(dosfs, &boot, fat, 1);
171 } else {
172 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
173 mod |= FSERROR; /* file system not clean */
174 }
175 }
176 }
177
178 if (mod & (FSFATAL | FSERROR))
179 goto out;
180
181 ret = 0;
182
183 out:
184 if (finish_dosdirsection)
185 finishDosDirSection();
186 free(fat);
187 close(dosfs);
188
189 if (mod & (FSFATMOD|FSDIRMOD))
190 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
191
192 return ret;
193}
2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
4 *
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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26
27#include <sys/cdefs.h>
28#ifndef lint
29__RCSID("$NetBSD: check.c,v 1.10 2000/04/25 23:02:51 jdolecek Exp $");
30static const char rcsid[] =
31 "$FreeBSD: head/sbin/fsck_msdosfs/check.c 203874 2010-02-14 12:31:28Z kib $";
32#endif /* not lint */
33
34#include <stdlib.h>
35#include <string.h>
36#include <stdio.h>
37#include <unistd.h>
38#include <fcntl.h>
39
40#include "ext.h"
41#include "fsutil.h"
42
43int
44checkfilesys(const char *fname)
45{
46 int dosfs;
47 struct bootblock boot;
48 struct fatEntry *fat = NULL;
49 int finish_dosdirsection=0;
50 u_int i;
51 int mod = 0;
52 int ret = 8;
53
54 rdonly = alwaysno;
55 if (!preen)
56 printf("** %s", fname);
57
58 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
59 if (dosfs < 0 && !rdonly) {
60 dosfs = open(fname, O_RDONLY, 0);
61 if (dosfs >= 0)
62 pwarn(" (NO WRITE)\n");
63 else if (!preen)
64 printf("\n");
65 rdonly = 1;
66 } else if (!preen)
67 printf("\n");
68
69 if (dosfs < 0) {
70 perror("Can't open");
71 return 8;
72 }
73
74 if (readboot(dosfs, &boot) != FSOK) {
75 close(dosfs);
76 printf("\n");
77 return 8;
78 }
79
80 if (skipclean && preen && checkdirty(dosfs, &boot)) {
81 printf("%s: ", fname);
82 printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
83 ret = 0;
84 goto out;
85 }
86
87 if (!preen) {
88 if (boot.ValidFat < 0)
89 printf("** Phase 1 - Read and Compare FATs\n");
90 else
91 printf("** Phase 1 - Read FAT\n");
92 }
93
94 mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
95 if (mod & FSFATAL) {
96 close(dosfs);
97 return 8;
98 }
99
100 if (boot.ValidFat < 0)
101 for (i = 1; i < (int)boot.bpbFATs; i++) {
102 struct fatEntry *currentFat;
103
104 mod |= readfat(dosfs, &boot, i, ¤tFat);
105
106 if (mod & FSFATAL)
107 goto out;
108
109 mod |= comparefat(&boot, fat, currentFat, i);
110 free(currentFat);
111 if (mod & FSFATAL)
112 goto out;
113 }
114
115 if (!preen)
116 printf("** Phase 2 - Check Cluster Chains\n");
117
118 mod |= checkfat(&boot, fat);
119 if (mod & FSFATAL)
120 goto out;
121 /* delay writing FATs */
122
123 if (!preen)
124 printf("** Phase 3 - Checking Directories\n");
125
126 mod |= resetDosDirSection(&boot, fat);
127 finish_dosdirsection = 1;
128 if (mod & FSFATAL)
129 goto out;
130 /* delay writing FATs */
131
132 mod |= handleDirTree(dosfs, &boot, fat);
133 if (mod & FSFATAL)
134 goto out;
135
136 if (!preen)
137 printf("** Phase 4 - Checking for Lost Files\n");
138
139 mod |= checklost(dosfs, &boot, fat);
140 if (mod & FSFATAL)
141 goto out;
142
143 /* now write the FATs */
144 if (mod & FSFATMOD) {
145 if (ask(1, "Update FATs")) {
146 mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
147 if (mod & FSFATAL)
148 goto out;
149 } else
150 mod |= FSERROR;
151 }
152
153 if (boot.NumBad)
154 pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
155 boot.NumFiles,
156 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
157 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
158 else
159 pwarn("%d files, %d free (%d clusters)\n",
160 boot.NumFiles,
161 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
162
163 if (mod && (mod & FSERROR) == 0) {
164 if (mod & FSDIRTY) {
165 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
166 mod &= ~FSDIRTY;
167
168 if (mod & FSDIRTY) {
169 pwarn("MARKING FILE SYSTEM CLEAN\n");
170 mod |= writefat(dosfs, &boot, fat, 1);
171 } else {
172 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
173 mod |= FSERROR; /* file system not clean */
174 }
175 }
176 }
177
178 if (mod & (FSFATAL | FSERROR))
179 goto out;
180
181 ret = 0;
182
183 out:
184 if (finish_dosdirsection)
185 finishDosDirSection();
186 free(fat);
187 close(dosfs);
188
189 if (mod & (FSFATMOD|FSDIRMOD))
190 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
191
192 return ret;
193}