1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <stdlib.h> 29 #include <string.h> 30 #include <stdio.h> 31 #include <unistd.h> 32 #include <fcntl.h> 33 #include <libutil.h> 34 35 #include "ext.h" 36 #include "fsutil.h" 37 38 int 39 checkfilesys(const char *fname) 40 { 41 int dosfs; 42 struct bootblock boot; 43 struct fat_descriptor *fat = NULL; 44 int finish_dosdirsection=0; 45 int mod = 0; 46 int ret = 8; 47 48 rdonly = alwaysno; 49 if (!preen) 50 printf("** %s", fname); 51 52 dosfs = open(fname, rdonly ? O_RDONLY : O_RDWR, 0); 53 if (dosfs < 0 && !rdonly) { 54 dosfs = open(fname, O_RDONLY, 0); 55 if (dosfs >= 0) 56 pwarn(" (NO WRITE)\n"); 57 else if (!preen) 58 printf("\n"); 59 rdonly = 1; 60 } else if (!preen) 61 printf("\n"); 62 63 if (dosfs < 0) { 64 perr("Can't open `%s'", fname); 65 printf("\n"); 66 return 8; 67 } 68 69 if (readboot(dosfs, &boot) == FSFATAL) { 70 close(dosfs); 71 printf("\n"); 72 return 8; 73 } 74 75 if (!preen) { 76 printf("** Phase 1 - Read FAT and checking connectivity\n"); 77 } 78 79 mod |= readfat(dosfs, &boot, &fat); 80 if (mod & FSFATAL) { 81 close(dosfs); 82 return 8; 83 } 84 85 if (!preen) 86 printf("** Phase 2 - Checking Directories\n"); 87 88 mod |= resetDosDirSection(fat); 89 finish_dosdirsection = 1; 90 if (mod & FSFATAL) 91 goto out; 92 /* delay writing FATs */ 93 94 mod |= handleDirTree(fat); 95 if (mod & FSFATAL) 96 goto out; 97 98 if (!preen) 99 printf("** Phase 3 - Checking for Lost Files\n"); 100 101 mod |= checklost(fat); 102 if (mod & FSFATAL) 103 goto out; 104 105 /* now write the FATs */ 106 if (mod & FSFATMOD) { 107 if (ask(1, "Update FATs")) { 108 mod |= writefat(fat); 109 if (mod & FSFATAL) 110 goto out; 111 } else 112 mod |= FSERROR; 113 } 114 115 #if 1 116 char freestr[7], badstr[7]; 117 118 int64_t freebytes = boot.NumFree * boot.ClusterSize; 119 humanize_number(freestr, sizeof(freestr), freebytes, "", 120 HN_AUTOSCALE, HN_DECIMAL | HN_IEC_PREFIXES); 121 if (boot.NumBad) { 122 int64_t badbytes = boot.NumBad * boot.ClusterSize; 123 124 humanize_number(badstr, sizeof(badstr), badbytes, "", 125 HN_AUTOSCALE, HN_B | HN_DECIMAL | HN_IEC_PREFIXES); 126 127 pwarn("%d files, %sB free (%d clusters), %sB bad (%d clusters)\n", 128 boot.NumFiles, 129 freestr, boot.NumFree, 130 badstr, boot.NumBad); 131 } else { 132 pwarn("%d files, %sB free (%d clusters)\n", 133 boot.NumFiles, 134 freestr, boot.NumFree); 135 } 136 #else 137 if (boot.NumBad) 138 pwarn("%d files, %d KiB free (%d clusters), %d KiB bad (%d clusters)\n", 139 boot.NumFiles, 140 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree, 141 boot.NumBad * boot.ClusterSize / 1024, boot.NumBad); 142 else 143 pwarn("%d files, %d KiB free (%d clusters)\n", 144 boot.NumFiles, 145 boot.NumFree * boot.ClusterSize / 1024, boot.NumFree); 146 #endif 147 148 if (mod && (mod & FSERROR) == 0) { 149 if (mod & FSDIRTY) { 150 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0) 151 mod &= ~FSDIRTY; 152 153 if (mod & FSDIRTY) { 154 pwarn("MARKING FILE SYSTEM CLEAN\n"); 155 mod |= writefat(fat); 156 } else { 157 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n"); 158 mod |= FSERROR; /* file system not clean */ 159 } 160 } 161 } 162 163 if (mod & (FSFATAL | FSERROR)) 164 goto out; 165 166 ret = 0; 167 168 out: 169 if (finish_dosdirsection) 170 finishDosDirSection(); 171 free(fat); 172 close(dosfs); 173 174 if (mod & (FSFATMOD|FSDIRMOD)) 175 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 176 177 return ret; 178 } 179