1/* 2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank 3 * Copyright (c) 1995 Martin Husemann 4 * Some structure declaration borrowed from Paul Popelka 5 * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 16 unchanged lines hidden (view full) --- 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 29#include <sys/cdefs.h> 30#ifndef lint 31__RCSID("$NetBSD: dir.c,v 1.14 1998/08/25 19:18:15 ross Exp $"); 32static const char rcsid[] = |
33 "$FreeBSD: head/sbin/fsck_msdosfs/dir.c 203874 2010-02-14 12:31:28Z kib $"; |
34#endif /* not lint */ 35 36#include <stdio.h> 37#include <stdlib.h> 38#include <string.h> 39#include <ctype.h> 40#include <unistd.h> 41#include <time.h> --- 172 unchanged lines hidden (view full) --- 214 */ 215int 216resetDosDirSection(struct bootblock *boot, struct fatEntry *fat) 217{ 218 int b1, b2; 219 cl_t cl; 220 int ret = FSOK; 221 |
222 b1 = boot->bpbRootDirEnts * 32; 223 b2 = boot->bpbSecPerClust * boot->bpbBytesPerSec; |
224 225 if ((buffer = malloc( b1 > b2 ? b1 : b2)) == NULL) { 226 perror("No space for directory buffer"); 227 return FSFATAL; 228 } 229 230 if ((delbuf = malloc(b2)) == NULL) { 231 free(buffer); --- 5 unchanged lines hidden (view full) --- 237 free(buffer); 238 free(delbuf); 239 perror("No space for directory entry"); 240 return FSFATAL; 241 } 242 243 memset(rootDir, 0, sizeof *rootDir); 244 if (boot->flags & FAT32) { |
245 if (boot->bpbRootClust < CLUST_FIRST || boot->bpbRootClust >= boot->NumClusters) { |
246 pfatal("Root directory starts with cluster out of range(%u)", |
247 boot->bpbRootClust); |
248 return FSFATAL; 249 } |
250 cl = fat[boot->bpbRootClust].next; |
251 if (cl < CLUST_FIRST 252 || (cl >= CLUST_RSRVD && cl< CLUST_EOFS) |
253 || fat[boot->bpbRootClust].head != boot->bpbRootClust) { |
254 if (cl == CLUST_FREE) 255 pwarn("Root directory starts with free cluster\n"); 256 else if (cl >= CLUST_RSRVD) 257 pwarn("Root directory starts with cluster marked %s\n", 258 rsrvdcltype(cl)); 259 else { 260 pfatal("Root directory doesn't start a cluster chain"); 261 return FSFATAL; 262 } 263 if (ask(1, "Fix")) { |
264 fat[boot->bpbRootClust].next = CLUST_FREE; |
265 ret = FSFATMOD; 266 } else 267 ret = FSFATAL; 268 } 269 |
270 fat[boot->bpbRootClust].flags |= FAT_USED; 271 rootDir->head = boot->bpbRootClust; |
272 } 273 274 return ret; 275} 276 277/* 278 * Cleanup after a directory scan 279 */ --- 28 unchanged lines hidden (view full) --- 308 * Delete directory entries between startcl, startoff and endcl, endoff. 309 */ 310static int 311delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl, 312 int startoff, cl_t endcl, int endoff, int notlast) 313{ 314 u_char *s, *e; 315 off_t off; |
316 int clsz = boot->bpbSecPerClust * boot->bpbBytesPerSec; |
317 318 s = delbuf + startoff; 319 e = delbuf + clsz; 320 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) { 321 if (startcl == endcl) { 322 if (notlast) 323 break; 324 e = delbuf + endoff; 325 } |
326 off = startcl * boot->bpbSecPerClust + boot->ClusterOffset; 327 off *= boot->bpbBytesPerSec; |
328 if (lseek(f, off, SEEK_SET) != off 329 || read(f, delbuf, clsz) != clsz) { 330 perror("Unable to read directory"); 331 return FSFATAL; 332 } 333 while (s < e) { 334 *s = SLOT_DELETED; 335 s += 32; --- 120 unchanged lines hidden (view full) --- 456 * Already handled somewhere else. 457 */ 458 return FSOK; 459 } 460 shortSum = -1; 461 vallfn = invlfn = empty = NULL; 462 do { 463 if (!(boot->flags & FAT32) && !dir->parent) { |
464 last = boot->bpbRootDirEnts * 32; 465 off = boot->bpbResSectors + boot->bpbFATs * boot->FATsecs; |
466 } else { |
467 last = boot->bpbSecPerClust * boot->bpbBytesPerSec; 468 off = cl * boot->bpbSecPerClust + boot->ClusterOffset; |
469 } 470 |
471 off *= boot->bpbBytesPerSec; |
472 if (lseek(f, off, SEEK_SET) != off 473 || read(f, buffer, last) != last) { 474 perror("Unable to read directory"); 475 return FSFATAL; 476 } 477 last /= 32; 478 /* 479 * Check `.' and `..' entries here? XXX --- 472 unchanged lines hidden (view full) --- 952 break; 953 lfcl = p ? fat[lfcl].next : lostDir->head; 954 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) { 955 /* Extend LOSTDIR? XXX */ 956 pwarn("No space in %s\n", LOSTDIR); 957 return FSERROR; 958 } 959 lfoff = lfcl * boot->ClusterSize |
960 + boot->ClusterOffset * boot->bpbBytesPerSec; |
961 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff 962 || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) { 963 perror("could not read LOST.DIR"); 964 return FSFATAL; 965 } 966 p = lfbuf; 967 } 968 --- 37 unchanged lines hidden --- |