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