#include <sys/cdefs.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "ext.h"
#include "fsutil.h"
int
checkfilesys(fname)
const char *fname;
{
int dosfs;
struct bootblock boot;
struct fatEntry *fat = NULL;
int i, finish_dosdirsection=0;
int mod = 0;
int ret = 8;
int tryFatalAgain = 1;
int tryErrorAgain = 2;
int tryOthersAgain = 3;
rdonly = alwaysno;
if (!preen)
printf("** %s", fname);
dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0);
if (dosfs < 0 && !rdonly) {
dosfs = open(fname, O_RDONLY, 0);
if (dosfs >= 0)
pwarn(" (NO WRITE)\n");
else if (!preen)
printf("\n");
rdonly = 1;
} else if (!preen)
printf("\n");
if (dosfs < 0) {
perror("Can't open");
return 8;
}
if (readboot(dosfs, &boot) != FSOK) {
close(dosfs);
return 8;
}
Again:
mod = 0;
if (!preen) {
if (boot.ValidFat < 0)
printf("** Phase 1 - Read and Compare FATs\n");
else
printf("** Phase 1 - Read FAT\n");
}
mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
if (mod & FSFATAL) {
close(dosfs);
return 8;
}
if (boot.ValidFat < 0)
for (i = 1; i < boot.FATs; i++) {
struct fatEntry *currentFat;
mod |= readfat(dosfs, &boot, i, ¤tFat);
if (mod & FSFATAL)
goto out;
mod |= comparefat(&boot, fat, currentFat, i);
free(currentFat);
if (mod & FSFATAL)
goto out;
}
if (!preen)
printf("** Phase 2 - Check Cluster Chains\n");
mod |= checkfat(&boot, fat);
if (mod & FSFATAL)
goto out;
if (!preen)
printf("** Phase 3 - Checking Directories\n");
mod |= resetDosDirSection(&boot, fat);
finish_dosdirsection = 1;
if (mod & FSFATAL)
goto out;
mod |= handleDirTree(dosfs, &boot, fat);
if (mod & FSFATAL)
goto out;
if (!preen)
printf("** Phase 4 - Checking for Lost Files\n");
mod |= checklost(dosfs, &boot, fat);
if (mod & FSFATAL)
goto out;
if (mod & FSFATMOD) {
if (ask(1, "Update FATs")) {
mod |= writefat(dosfs, &boot, fat, mod & FSFIXFAT);
if (mod & FSFATAL)
goto out;
} else
mod |= FSERROR;
}
if (boot.NumBad)
pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
boot.NumFiles,
boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
else
pwarn("%d files, %d free (%d clusters)\n",
boot.NumFiles,
boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
if (mod && (mod & FSERROR) == 0) {
if (mod & FSDIRTY) {
if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
mod &= ~FSDIRTY;
if (mod & FSDIRTY) {
pwarn("MARKING FILE SYSTEM CLEAN\n");
mod |= writefat(dosfs, &boot, fat, 1);
} else {
pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
mod |= FSERROR;
}
}
}
if ((mod & FSFATAL) && (--tryFatalAgain > 0))
goto Again;
if ((mod & FSERROR) && (--tryErrorAgain > 0))
goto Again;
if ((mod & (FSFIXFAT | FSDIRTY)) && (--tryOthersAgain > 0))
goto Again;
if (mod & (FSFATAL | FSERROR))
goto out;
ret = 0;
out:
if (finish_dosdirsection)
finishDosDirSection();
free(fat);
close(dosfs);
if (mod & (FSFATMOD|FSDIRMOD))
pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
return ret;
}