Home | History | Annotate | Line # | Download | only in fsck_msdos
check.c revision 1.7
      1 /*	$NetBSD: check.c,v 1.7 1997/09/14 14:40:11 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996 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.7 1997/09/14 14:40:11 lukem 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 		printf("** Phase 1 - Read and Compare FATs\n");
     88 
     89 	for (i = 0; i < boot.FATs; i++) {
     90 		struct fatEntry *currentFat;
     91 
     92 		mod |= readfat(dosfs, &boot, i, &currentFat);
     93 
     94 		if (mod & FSFATAL) {
     95 			if (fat)
     96 				free(fat);
     97 			close(dosfs);
     98 			return 8;
     99 		}
    100 
    101 		if (fat == NULL)
    102 			fat  = currentFat;
    103 		else {
    104 			mod |= comparefat(&boot, fat, currentFat, i + 1);
    105 			free(currentFat);
    106 			if (mod & FSFATAL) {
    107 				free(fat);
    108 				close(dosfs);
    109 				return 8;
    110 			}
    111 		}
    112 	}
    113 
    114 	if (!preen)
    115 		printf("** Phase 2 - Check Cluster Chains\n");
    116 
    117 	mod |= checkfat(&boot, fat);
    118 	if (mod & FSFATAL) {
    119 		free(fat);
    120 		close(dosfs);
    121 		return 8;
    122 	}
    123 
    124 	if (mod & FSFATMOD)
    125 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    126 	if (mod & FSFATAL) {
    127 		free(fat);
    128 		close(dosfs);
    129 		return 8;
    130 	}
    131 
    132 	if (!preen)
    133 		printf("** Phase 3 - Checking Directories\n");
    134 
    135 	if (resetDosDirSection(&boot) & FSFATAL) {
    136 		free(fat);
    137 		close(dosfs);
    138 		return 8;
    139 	}
    140 
    141 	mod |= handleDirTree(dosfs, &boot, fat);
    142 	if (mod & FSFATAL) {
    143 		finishDosDirSection();
    144 		free(fat);
    145 		close(dosfs);
    146 		return 8;
    147 	}
    148 
    149 	if (!preen)
    150 		printf("** Phase 4 - Checking for Lost Files\n");
    151 
    152 	mod |= checklost(dosfs, &boot, fat);
    153 
    154 	finishDosDirSection();
    155 	free(fat);
    156 	close(dosfs);
    157 
    158 	if (boot.NumBad)
    159 		pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
    160 		      boot.NumFiles,
    161 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
    162 		      boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
    163 	else
    164 		pwarn("%d files, %d free (%d clusters)\n",
    165 		      boot.NumFiles,
    166 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
    167 	if (mod & (FSFATAL | FSERROR))
    168 		return 8;
    169 	if (mod) {
    170 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    171 		return 4;
    172 	}
    173 	return 0;
    174 }
    175