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