Home | History | Annotate | Line # | Download | only in fsck_msdos
check.c revision 1.8
      1 /*	$NetBSD: check.c,v 1.8 1997/10/17 11:19:29 ws 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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Martin Husemann
     18  *	and Wolfgang Solfrank.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __RCSID("$NetBSD: check.c,v 1.8 1997/10/17 11:19:29 ws 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 #include "fsutil.h"
     50 
     51 int
     52 checkfilesys(fname)
     53 	const char *fname;
     54 {
     55 	int dosfs;
     56 	struct bootblock boot;
     57 	struct fatEntry *fat = NULL;
     58 	int i;
     59 	int mod = 0;
     60 
     61 	rdonly = alwaysno;
     62 	if (!preen)
     63 		printf("** %s", fname);
     64 
     65 	dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
     66 	if (dosfs < 0 && !rdonly) {
     67 		dosfs = open(fname, O_RDONLY, 0);
     68 		if (dosfs >= 0)
     69 			pwarn(" (NO WRITE)\n");
     70 		else if (!preen)
     71 			printf("\n");
     72 		rdonly = 1;
     73 	} else if (!preen)
     74 		printf("\n");
     75 
     76 	if (dosfs < 0) {
     77 		perror("Can't open");
     78 		return 8;
     79 	}
     80 
     81 	if (readboot(dosfs, &boot) != FSOK) {
     82 		close(dosfs);
     83 		return 8;
     84 	}
     85 
     86 	if (!preen)
     87 		if (boot.ValidFat < 0)
     88 			printf("** Phase 1 - Read and Compare FATs\n");
     89 		else
     90 			printf("** Phase 1 - Read FAT\n");
     91 
     92 	mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
     93 	if (mod & FSFATAL) {
     94 		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 
    102 			mod |= readfat(dosfs, &boot, i, &currentFat);
    103 
    104 			if (mod & FSFATAL) {
    105 				free(fat);
    106 				close(dosfs);
    107 				return 8;
    108 			}
    109 
    110 			mod |= comparefat(&boot, fat, currentFat, i);
    111 			free(currentFat);
    112 			if (mod & FSFATAL) {
    113 				free(fat);
    114 				close(dosfs);
    115 				return 8;
    116 			}
    117 		}
    118 
    119 	if (!preen)
    120 		printf("** Phase 2 - Check Cluster Chains\n");
    121 
    122 	mod |= checkfat(&boot, fat);
    123 	if (mod & FSFATAL) {
    124 		free(fat);
    125 		close(dosfs);
    126 		return 8;
    127 	}
    128 
    129 	if (mod & FSFATMOD)
    130 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    131 	if (mod & FSFATAL) {
    132 		free(fat);
    133 		close(dosfs);
    134 		return 8;
    135 	}
    136 
    137 	if (!preen)
    138 		printf("** Phase 3 - Checking Directories\n");
    139 
    140 	mod |= resetDosDirSection(&boot, fat);
    141 	if (mod & FSFATAL) {
    142 		free(fat);
    143 		close(dosfs);
    144 		return 8;
    145 	}
    146 
    147 	if (mod & FSFATMOD)
    148 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    149 	if (mod & FSFATAL) {
    150 		finishDosDirSection();
    151 		free(fat);
    152 		close(dosfs);
    153 		return 8;
    154 	}
    155 
    156 	mod |= handleDirTree(dosfs, &boot, fat);
    157 	if (mod & FSFATAL) {
    158 		finishDosDirSection();
    159 		free(fat);
    160 		close(dosfs);
    161 		return 8;
    162 	}
    163 
    164 	if (!preen)
    165 		printf("** Phase 4 - Checking for Lost Files\n");
    166 
    167 	mod |= checklost(dosfs, &boot, fat);
    168 	if (mod & FSFATAL) {
    169 		finishDosDirSection();
    170 		free(fat);
    171 		close(dosfs);
    172 		return 8;
    173 	}
    174 
    175 	if (mod & FSFATMOD)
    176 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    177 
    178 	finishDosDirSection();
    179 	free(fat);
    180 	close(dosfs);
    181 	if (mod & FSFATAL)
    182 		return 8;
    183 
    184 	if (boot.NumBad)
    185 		pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
    186 		      boot.NumFiles,
    187 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
    188 		      boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
    189 	else
    190 		pwarn("%d files, %d free (%d clusters)\n",
    191 		      boot.NumFiles,
    192 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
    193 
    194 	if (mod & (FSFATAL | FSERROR))
    195 		return 8;
    196 	if (mod) {
    197 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    198 		return 4;
    199 	}
    200 	return 0;
    201 }
    202