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