148907Srnordier/*
248907Srnordier * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
348907Srnordier * Copyright (c) 1995 Martin Husemann
448907Srnordier *
548907Srnordier * Redistribution and use in source and binary forms, with or without
648907Srnordier * modification, are permitted provided that the following conditions
748907Srnordier * are met:
848907Srnordier * 1. Redistributions of source code must retain the above copyright
948907Srnordier *    notice, this list of conditions and the following disclaimer.
1048907Srnordier * 2. Redistributions in binary form must reproduce the above copyright
1148907Srnordier *    notice, this list of conditions and the following disclaimer in the
1248907Srnordier *    documentation and/or other materials provided with the distribution.
1348907Srnordier *
1448907Srnordier * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
1548907Srnordier * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1648907Srnordier * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1748907Srnordier * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1848907Srnordier * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1948907Srnordier * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2048907Srnordier * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2148907Srnordier * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2248907Srnordier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2348907Srnordier * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2448907Srnordier */
2548907Srnordier
2650477Speter
2748907Srnordier#include <sys/cdefs.h>
2848907Srnordier#ifndef lint
2948907Srnordier__RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $");
3048907Srnordierstatic const char rcsid[] =
3148907Srnordier  "$FreeBSD$";
3248907Srnordier#endif /* not lint */
3348907Srnordier
3448907Srnordier#include <stdlib.h>
3548907Srnordier#include <string.h>
3648907Srnordier#include <stdio.h>
3748907Srnordier#include <unistd.h>
3848907Srnordier#include <fcntl.h>
3948907Srnordier
4048907Srnordier#include "ext.h"
4148907Srnordier#include "fsutil.h"
4248907Srnordier
43103436Speterint
4448907Srnordiercheckfilesys(const char *fname)
45103436Speter{
46	int dosfs;
47	struct bootblock boot;
48	struct fatEntry *fat = NULL;
49	int finish_dosdirsection=0;
50	u_int i;
51	int mod = 0;
52	int ret = 8;
53
54	rdonly = alwaysno;
55	if (!preen)
56		printf("** %s", fname);
57
58	dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
59	if (dosfs < 0 && !rdonly) {
60		dosfs = open(fname, O_RDONLY, 0);
61		if (dosfs >= 0)
62			pwarn(" (NO WRITE)\n");
63		else if (!preen)
64			printf("\n");
65		rdonly = 1;
66	} else if (!preen)
67		printf("\n");
68
69	if (dosfs < 0) {
70		perr("Can't open `%s'", fname);
71		printf("\n");
72		return 8;
73	}
74
75	if (readboot(dosfs, &boot) != FSOK) {
76		close(dosfs);
77		printf("\n");
78		return 8;
79	}
80
81	if (skipclean && preen && checkdirty(dosfs, &boot)) {
82		printf("%s: ", fname);
83		printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
84		ret = 0;
85		goto out;
86	}
87
88	if (!preen)  {
89		if (boot.ValidFat < 0)
90			printf("** Phase 1 - Read and Compare FATs\n");
91		else
92			printf("** Phase 1 - Read FAT\n");
93	}
94
95	mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
96	if (mod & FSFATAL) {
97		close(dosfs);
98		return 8;
99	}
100
101	if (boot.ValidFat < 0)
102		for (i = 1; i < boot.bpbFATs; i++) {
103			struct fatEntry *currentFat;
104
105			mod |= readfat(dosfs, &boot, i, &currentFat);
106
107			if (mod & FSFATAL)
108				goto out;
109
110			mod |= comparefat(&boot, fat, currentFat, i);
111			free(currentFat);
112			if (mod & FSFATAL)
113				goto out;
114		}
115
116	if (!preen)
117		printf("** Phase 2 - Check Cluster Chains\n");
118
119	mod |= checkfat(&boot, fat);
120	if (mod & FSFATAL)
121		goto out;
122	/* delay writing FATs */
123
124	if (!preen)
125		printf("** Phase 3 - Checking Directories\n");
126
127	mod |= resetDosDirSection(&boot, fat);
128	finish_dosdirsection = 1;
129	if (mod & FSFATAL)
130		goto out;
131	/* delay writing FATs */
132
133	mod |= handleDirTree(dosfs, &boot, fat);
134	if (mod & FSFATAL)
135		goto out;
136
137	if (!preen)
138		printf("** Phase 4 - Checking for Lost Files\n");
139
140	mod |= checklost(dosfs, &boot, fat);
141	if (mod & FSFATAL)
142		goto out;
143
144	/* now write the FATs */
145	if (mod & (FSFATMOD|FSFIXFAT)) {
146		if (ask(1, "Update FATs")) {
147			mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
148			if (mod & FSFATAL)
149				goto out;
150		} else
151			mod |= FSERROR;
152	}
153
154	if (boot.NumBad)
155		pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
156		      boot.NumFiles,
157		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
158		      boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
159	else
160		pwarn("%d files, %d free (%d clusters)\n",
161		      boot.NumFiles,
162		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
163
164	if (mod && (mod & FSERROR) == 0) {
165		if (mod & FSDIRTY) {
166			if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
167				mod &= ~FSDIRTY;
168
169			if (mod & FSDIRTY) {
170				pwarn("MARKING FILE SYSTEM CLEAN\n");
171				mod |= writefat(dosfs, &boot, fat, 1);
172			} else {
173				pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
174				mod |= FSERROR; /* file system not clean */
175			}
176		}
177	}
178
179	if (mod & (FSFATAL | FSERROR))
180		goto out;
181
182	ret = 0;
183
184    out:
185	if (finish_dosdirsection)
186		finishDosDirSection();
187	free(fat);
188	close(dosfs);
189
190	if (mod & (FSFATMOD|FSDIRMOD))
191		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
192
193	return ret;
194}
195