Home | History | Annotate | Line # | Download | only in fsck_msdos
check.c revision 1.3
      1 /*	$NetBSD: check.c,v 1.3 1996/05/28 19:51:11 ws 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 #ifndef lint
     37 static char rcsid[] = "$NetBSD: check.c,v 1.3 1996/05/28 19:51:11 ws Exp $";
     38 #endif /* not lint */
     39 
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <ctype.h>
     43 #include <stdio.h>
     44 #include <unistd.h>
     45 #include <fcntl.h>
     46 
     47 #include "ext.h"
     48 
     49 int
     50 checkfilesys(fname)
     51 	const char *fname;
     52 {
     53 	int dosfs;
     54 	struct bootblock boot;
     55 	struct fatEntry * fat = NULL;
     56 	int i;
     57 	int mod = 0;
     58 
     59 	rdonly = alwaysno;
     60 	if (!preen)
     61 		printf("** %s", fname);
     62 
     63 	dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
     64 	if (dosfs < 0 && !rdonly) {
     65 		dosfs = open(fname, O_RDONLY, 0);
     66 		if (dosfs >= 0)
     67 			pwarn(" (NO WRITE)\n");
     68 		else if (!preen)
     69 			printf("\n");
     70 		rdonly = 1;
     71 	} else if (!preen)
     72 		printf("\n");
     73 
     74 	if (dosfs < 0) {
     75 		perror("Can't open");
     76 		return 8;
     77 	}
     78 
     79 	if (readboot(dosfs, &boot) != FSOK) {
     80 		close(dosfs);
     81 		return 8;
     82 	}
     83 
     84 	if (!preen)
     85 		printf("** Phase 1 - Read and Compare FATs\n");
     86 
     87 	for (i = 0; i < boot.FATs; i++) {
     88 		struct fatEntry *currentFat;
     89 
     90 		mod |= readfat(dosfs, &boot, i, &currentFat);
     91 
     92 		if (mod & FSFATAL) {
     93 			if (fat)
     94 				free(fat);
     95 			close(dosfs);
     96 			return 8;
     97 		}
     98 
     99 		if (fat == NULL)
    100 			fat  = currentFat;
    101 		else {
    102 			mod |= comparefat(&boot, fat, currentFat, i + 1);
    103 			free(currentFat);
    104 			if (mod & FSFATAL) {
    105 				free(fat);
    106 				close(dosfs);
    107 				return 8;
    108 			}
    109 		}
    110 	}
    111 
    112 	if (!preen)
    113 		printf("** Phase 2 - Check Cluster Chains\n");
    114 
    115 	mod |= checkfat(&boot, fat);
    116 	if (mod & FSFATAL) {
    117 		free(fat);
    118 		close(dosfs);
    119 		return 8;
    120 	}
    121 
    122 	if (mod & FSFATMOD)
    123 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
    124 	if (mod & FSFATAL) {
    125 		free(fat);
    126 		close(dosfs);
    127 		return 8;
    128 	}
    129 
    130 	if (!preen)
    131 		printf("** Phase 3 - Checking Directories\n");
    132 
    133 	if (resetDosDirSection(&boot) & FSFATAL) {
    134 		free(fat);
    135 		close(dosfs);
    136 		return 8;
    137 	}
    138 
    139 	mod |= handleDirTree(dosfs, &boot, fat);
    140 	if (mod & FSFATAL) {
    141 		finishDosDirSection();
    142 		free(fat);
    143 		close(dosfs);
    144 		return 8;
    145 	}
    146 
    147 	if (!preen)
    148 		printf("** Phase 4 - Checking for Lost Files\n");
    149 
    150 	mod |= checklost(dosfs, &boot, fat);
    151 
    152 	finishDosDirSection();
    153 	free(fat);
    154 	close(dosfs);
    155 
    156 	pwarn("%d files, %d free (%d clusters)\n",
    157 	      boot.NumFiles, boot.NumFree * boot.ClusterSize / 1024,
    158 	      boot.NumFree);
    159 	if (mod & (FSFATAL | FSERROR))
    160 		return 8;
    161 	if (mod) {
    162 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    163 		return 4;
    164 	}
    165 	return 0;
    166 }
    167