check.c (5702B)
1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 "freebsd-compat.h" 29 #if defined(__RCSID) && !defined(lint) 30 __RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $"); 31 #endif /* not lint */ 32 33 #ifdef HAVE_LIBUTIL_H 34 #include <libutil.h> 35 #endif 36 #include <stdlib.h> 37 #include <string.h> 38 #include <stdio.h> 39 #include <unistd.h> 40 #include <fcntl.h> 41 #include <errno.h> 42 43 #include "ext.h" 44 #include "fsutil.h" 45 46 int 47 checkfilesys(const char *fname) 48 { 49 int dosfs; 50 struct bootblock boot; 51 struct fat_descriptor *fat = NULL; 52 int finish_dosdirsection = 0; 53 int flags = O_RDWR; 54 int mod = 0; 55 int ret = 8; 56 int64_t freebytes; 57 int64_t badbytes; 58 59 rdonly = alwaysno; 60 if (!preen) 61 printf("** %s", fname); 62 63 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX) 64 /* Claim the block device before repairing it. */ 65 flags |= O_EXCL; 66 #endif 67 dosfs = open(fname, rdonly ? O_RDONLY : flags); 68 if (dosfs < 0 && !rdonly && errno == EBUSY) { 69 if (!preen) 70 printf("\n"); 71 perr("`%s' is in use\n", fname); 72 return 8; 73 } 74 if (dosfs < 0 && !rdonly) { 75 dosfs = open(fname, O_RDONLY, 0); 76 if (dosfs >= 0) 77 pwarn(" (NO WRITE)\n"); 78 else if (!preen) 79 printf("\n"); 80 rdonly = 1; 81 } else if (!preen) 82 printf("\n"); 83 84 if (dosfs < 0) { 85 perr("Can't open `%s'", fname); 86 printf("\n"); 87 return 8; 88 } 89 90 if (readboot(dosfs, &boot) == FSFATAL) { 91 close(dosfs); 92 printf("\n"); 93 return 8; 94 } 95 96 if (skipclean && preen && checkdirty(dosfs, &boot)) { 97 printf("%s: ", fname); 98 printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); 99 ret = 0; 100 goto out; 101 } 102 103 if (!preen) { 104 printf("** Phase 1 - Read FAT and checking connectivity\n"); 105 } 106 107 mod |= readfat(dosfs, &boot, &fat); 108 if (mod & FSFATAL) { 109 close(dosfs); 110 return 8; 111 } 112 113 if (!preen) 114 printf("** Phase 2 - Checking Directories\n"); 115 116 mod |= resetDosDirSection(fat); 117 finish_dosdirsection = 1; 118 if (mod & FSFATAL) 119 goto out; 120 /* delay writing FATs */ 121 122 mod |= handleDirTree(fat); 123 if (mod & FSFATAL) 124 goto out; 125 126 if (!preen) 127 printf("** Phase 3 - Checking for Lost Files\n"); 128 129 mod |= checklost(fat); 130 if (mod & FSFATAL) 131 goto out; 132 133 /* now write the FATs */ 134 if (mod & FSFATMOD) { 135 if (ask(1, "Update FATs")) { 136 mod |= writefat(fat); 137 if (mod & FSFATAL) 138 goto out; 139 } else 140 mod |= FSERROR; 141 } 142 143 freebytes = (int64_t)boot.NumFree * boot.ClusterSize; 144 badbytes = (int64_t)boot.NumBad * boot.ClusterSize; 145 146 #ifdef HAVE_LIBUTIL_H 147 char freestr[7], badstr[7]; 148 149 humanize_number( 150 freestr, sizeof(freestr), freebytes, "", HN_AUTOSCALE, HN_DECIMAL | HN_IEC_PREFIXES 151 ); 152 if (boot.NumBad) { 153 humanize_number( 154 badstr, sizeof(badstr), badbytes, "", HN_AUTOSCALE, HN_B | HN_DECIMAL | HN_IEC_PREFIXES 155 ); 156 157 pwarn( 158 "%d files, %sB free (%d clusters), %sB bad (%d clusters)\n", 159 boot.NumFiles, 160 freestr, 161 boot.NumFree, 162 badstr, 163 boot.NumBad 164 ); 165 } else { 166 pwarn("%d files, %sB free (%d clusters)\n", boot.NumFiles, freestr, boot.NumFree); 167 } 168 #else 169 if (boot.NumBad) 170 pwarn( 171 "%d files, %jd KiB free (%d clusters), %jd KiB bad (%d clusters)\n", 172 boot.NumFiles, 173 (intmax_t)freebytes / 1024, 174 boot.NumFree, 175 (intmax_t)badbytes / 1024, 176 boot.NumBad 177 ); 178 else 179 pwarn( 180 "%d files, %jd KiB free (%d clusters)\n", 181 boot.NumFiles, 182 (intmax_t)freebytes / 1024, 183 boot.NumFree 184 ); 185 #endif 186 187 if (mod && (mod & FSERROR) == 0) { 188 if (mod & FSDIRTY) { 189 if (ask(1, "MARK FILE SYSTEM CLEAN") == 0) 190 mod &= ~FSDIRTY; 191 192 if (mod & FSDIRTY) { 193 pwarn("MARKING FILE SYSTEM CLEAN\n"); 194 mod |= cleardirty(fat); 195 } else { 196 pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n"); 197 mod |= FSERROR; /* file system not clean */ 198 } 199 } 200 } 201 202 if (mod & (FSFATAL | FSERROR)) 203 goto out; 204 205 ret = 0; 206 207 out: 208 if (finish_dosdirsection) 209 finishDosDirSection(); 210 free(fat); 211 close(dosfs); 212 213 if (mod & (FSFATMOD | FSDIRMOD)) 214 pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 215 216 return ret; 217 }