xref: /openbsd/sbin/fsck_msdos/check.c (revision 8529ddd3)
1 /*	$OpenBSD: check.c,v 1.17 2015/01/16 06:39:58 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/dkio.h>
32 #include <sys/disklabel.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <fcntl.h>
40 #include <util.h>
41 
42 #include "ext.h"
43 
44 struct disklabel lab;
45 
46 int
47 checkfilesys(const char *fname)
48 {
49 	int dosfs;
50 	struct bootblock boot;
51 	struct fatEntry *fat = NULL;
52 	char *realdev;
53 	int i;
54 	int mod = 0;
55 
56 	rdonly = alwaysno;
57 
58 	dosfs = opendev(fname, rdonly ? O_RDONLY : O_RDWR, 0, &realdev);
59 	if (dosfs < 0 && !rdonly) {
60 		dosfs = opendev(fname, O_RDONLY, 0, &realdev);
61 		rdonly = 1;
62 	}
63 	if (dosfs < 0) {
64 		xperror("Can't open");
65 		return (8);
66 	}
67 
68 	if (!preen) {
69 		printf("** %s", realdev);
70 		if (strncmp(fname, realdev, PATH_MAX) != 0)
71 			printf(" (%s)", fname);
72 		if (rdonly)
73 			printf(" (NO WRITE)");
74 		printf("\n");
75 	}
76 
77 	if (ioctl(dosfs, DIOCGDINFO, (char *)&lab) < 0)
78 		pfatal("can't read disk label for %s\n", fname);
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