Home | History | Annotate | Line # | Download | only in fsck_msdos
check.c revision 1.9
      1 /*	$NetBSD: check.c,v 1.9 1998/08/25 19:18:15 ross 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.9 1998/08/25 19:18:15 ross 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 
     93 	mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
     94 	if (mod & FSFATAL) {
     95 		close(dosfs);
     96 		return 8;
     97 	}
     98 
     99 	if (boot.ValidFat < 0)
    100 		for (i = 1; i < boot.FATs; i++) {
    101 			struct fatEntry *currentFat;
    102 
    103 			mod |= readfat(dosfs, &boot, i, &currentFat);
    104 
    105 			if (mod & FSFATAL) {
    106 				free(fat);
    107 				close(dosfs);
    108 				return 8;
    109 			}
    110 
    111 			mod |= comparefat(&boot, fat, currentFat, i);
    112 			free(currentFat);
    113 			if (mod & FSFATAL) {
    114 				free(fat);
    115 				close(dosfs);
    116 				return 8;
    117 			}
    118 		}
    119 
    120 	if (!preen)
    121 		printf("** Phase 2 - Check Cluster Chains\n");
    122 
    123 	mod |= checkfat(&boot, fat);
    124 	if (mod & FSFATAL) {
    125 		free(fat);
    126 		close(dosfs);
    127 		return 8;
    128 	}
    129 
    130 	if (mod & FSFATMOD)
    131 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    132 	if (mod & FSFATAL) {
    133 		free(fat);
    134 		close(dosfs);
    135 		return 8;
    136 	}
    137 
    138 	if (!preen)
    139 		printf("** Phase 3 - Checking Directories\n");
    140 
    141 	mod |= resetDosDirSection(&boot, fat);
    142 	if (mod & FSFATAL) {
    143 		free(fat);
    144 		close(dosfs);
    145 		return 8;
    146 	}
    147 
    148 	if (mod & FSFATMOD)
    149 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    150 	if (mod & FSFATAL) {
    151 		finishDosDirSection();
    152 		free(fat);
    153 		close(dosfs);
    154 		return 8;
    155 	}
    156 
    157 	mod |= handleDirTree(dosfs, &boot, fat);
    158 	if (mod & FSFATAL) {
    159 		finishDosDirSection();
    160 		free(fat);
    161 		close(dosfs);
    162 		return 8;
    163 	}
    164 
    165 	if (!preen)
    166 		printf("** Phase 4 - Checking for Lost Files\n");
    167 
    168 	mod |= checklost(dosfs, &boot, fat);
    169 	if (mod & FSFATAL) {
    170 		finishDosDirSection();
    171 		free(fat);
    172 		close(dosfs);
    173 		return 8;
    174 	}
    175 
    176 	if (mod & FSFATMOD)
    177 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    178 
    179 	finishDosDirSection();
    180 	free(fat);
    181 	close(dosfs);
    182 	if (mod & FSFATAL)
    183 		return 8;
    184 
    185 	if (boot.NumBad)
    186 		pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
    187 		      boot.NumFiles,
    188 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
    189 		      boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
    190 	else
    191 		pwarn("%d files, %d free (%d clusters)\n",
    192 		      boot.NumFiles,
    193 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
    194 
    195 	if (mod & (FSFATAL | FSERROR))
    196 		return 8;
    197 	if (mod) {
    198 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    199 		return 4;
    200 	}
    201 	return 0;
    202 }
    203