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