xref: /openbsd/sbin/fsck_msdos/check.c (revision 7b36286a)
1 /*	$OpenBSD: check.c,v 1.11 2006/05/27 22:30:09 thib 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.11 2006/05/27 22:30:09 thib 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(const char *fname)
52 {
53 	int dosfs;
54 	struct bootblock boot;
55 	struct fatEntry *fat = NULL;
56 	int i;
57 	int mod = 0;
58 
59 	rdonly = alwaysno;
60 	if (!preen)
61 		printf("** %s", fname);
62 
63 	dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
64 	if (dosfs < 0 && !rdonly) {
65 		dosfs = open(fname, O_RDONLY, 0);
66 		if (dosfs >= 0)
67 			pwarn(" (NO WRITE)\n");
68 		else if (!preen)
69 			printf("\n");
70 		rdonly = 1;
71 	} else if (!preen)
72 		printf("\n");
73 
74 	if (dosfs < 0) {
75 		xperror("Can't open");
76 		return (8);
77 	}
78 
79 	if (readboot(dosfs, &boot) != FSOK) {
80 		(void)close(dosfs);
81 		return (8);
82 	}
83 
84 	if (!preen) {
85 		if (boot.ValidFat < 0)
86 			printf("** Phase 1 - Read and Compare FATs\n");
87 		else
88 			printf("** Phase 1 - Read FAT\n");
89 	}
90 
91 	mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
92 	if (mod & FSFATAL) {
93 		(void)close(dosfs);
94 		return 8;
95 	}
96 
97 	if (boot.ValidFat < 0)
98 		for (i = 1; i < boot.FATs; i++) {
99 			struct fatEntry *currentFat;
100 			mod |= readfat(dosfs, &boot, i, &currentFat);
101 
102 			if (mod & FSFATAL) {
103 				free(fat);
104 				(void)close(dosfs);
105 				return 8;
106 			}
107 
108 			mod |= comparefat(&boot, fat, currentFat, i);
109 			free(currentFat);
110 			if (mod & FSFATAL) {
111 				free(fat);
112 				(void)close(dosfs);
113 				return (8);
114 			}
115 		}
116 
117 	if (!preen)
118 		printf("** Phase 2 - Check Cluster Chains\n");
119 
120 	mod |= checkfat(&boot, fat);
121 	if (mod & FSFATAL) {
122 		free(fat);
123 		(void)close(dosfs);
124 		return (8);
125 	}
126 
127 	if (mod & FSFATMOD)
128 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
129 	if (mod & FSFATAL) {
130 		free(fat);
131 		(void)close(dosfs);
132 		return (8);
133 	}
134 
135 	if (!preen)
136 		printf("** Phase 3 - Check Directories\n");
137 
138 	mod |= resetDosDirSection(&boot, fat);
139 	if (mod & FSFATAL) {
140 		free(fat);
141 		close(dosfs);
142 		return 8;
143 	}
144 
145 	if (mod & FSFATMOD)
146 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
147 	if (mod & FSFATAL) {
148 		finishDosDirSection();
149 		free(fat);
150 		(void)close(dosfs);
151 		return (8);
152 	}
153 
154 	mod |= handleDirTree(dosfs, &boot, fat);
155 	if (mod & FSFATAL) {
156 		finishDosDirSection();
157 		free(fat);
158 		(void)close(dosfs);
159 		return (8);
160 	}
161 
162 	if (!preen)
163 		printf("** Phase 4 - Check for Lost Files\n");
164 
165 	mod |= checklost(dosfs, &boot, fat);
166 	if (mod & FSFATAL) {
167 		finishDosDirSection();
168 		free(fat);
169 		(void)close(dosfs);
170 		return 8;
171 	}
172 
173 	if (mod & FSFATMOD)
174 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?    XXX */
175 
176 	finishDosDirSection();
177 	free(fat);
178 	(void)close(dosfs);
179 	if (mod & FSFATAL)
180 		return 8;
181 
182 	if (boot.NumBad)
183 		pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
184 		      boot.NumFiles,
185 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
186 		      boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
187 	else
188 		pwarn("%d files, %d free (%d clusters)\n",
189 		      boot.NumFiles,
190 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
191 
192 	if (mod & (FSFATAL | FSERROR))
193 		return (8);
194 	if (mod) {
195 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
196 		return (4);
197 	}
198 	return (0);
199 }
200