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 * Some structure declaration borrowed from Paul Popelka
7 * ([email protected]), see /sys/msdosfs/ for reference.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __RCSID("$NetBSD: dir.c,v 1.20 2006/06/05 16:51:18 christos Exp $");
34 static const char rcsid[] =
35 "$FreeBSD$";
36 #endif /* not lint */
37
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <unistd.h>
44 #include <time.h>
45
46 #include <sys/param.h>
47
48 #include "ext.h"
49 #include "fsutil.h"
50
51 #define SLOT_EMPTY 0x00 /* slot has never been used */
52 #define SLOT_E5 0x05 /* the real value is 0xe5 */
53 #define SLOT_DELETED 0xe5 /* file in this slot deleted */
54
55 #define ATTR_NORMAL 0x00 /* normal file */
56 #define ATTR_READONLY 0x01 /* file is readonly */
57 #define ATTR_HIDDEN 0x02 /* file is hidden */
58 #define ATTR_SYSTEM 0x04 /* file is a system file */
59 #define ATTR_VOLUME 0x08 /* entry is a volume label */
60 #define ATTR_DIRECTORY 0x10 /* entry is a directory name */
61 #define ATTR_ARCHIVE 0x20 /* file is new or modified */
62
63 #define ATTR_WIN95 0x0f /* long name record */
64
65 /*
66 * This is the format of the contents of the deTime field in the direntry
67 * structure.
68 * We don't use bitfields because we don't know how compilers for
69 * arbitrary machines will lay them out.
70 */
71 #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
72 #define DT_2SECONDS_SHIFT 0
73 #define DT_MINUTES_MASK 0x7E0 /* minutes */
74 #define DT_MINUTES_SHIFT 5
75 #define DT_HOURS_MASK 0xF800 /* hours */
76 #define DT_HOURS_SHIFT 11
77
78 /*
79 * This is the format of the contents of the deDate field in the direntry
80 * structure.
81 */
82 #define DD_DAY_MASK 0x1F /* day of month */
83 #define DD_DAY_SHIFT 0
84 #define DD_MONTH_MASK 0x1E0 /* month */
85 #define DD_MONTH_SHIFT 5
86 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */
87 #define DD_YEAR_SHIFT 9
88
89
90 /* dir.c */
91 static struct dosDirEntry *newDosDirEntry(void);
92 static void freeDosDirEntry(struct dosDirEntry *);
93 static struct dirTodoNode *newDirTodo(void);
94 static void freeDirTodo(struct dirTodoNode *);
95 static char *fullpath(struct dosDirEntry *);
96 static u_char calcShortSum(u_char *);
97 static int delete(int, struct bootblock *, struct fatEntry *, cl_t, int,
98 cl_t, int, int);
99 static int removede(int, struct bootblock *, struct fatEntry *, u_char *,
100 u_char *, cl_t, cl_t, cl_t, char *, int);
101 static int checksize(struct bootblock *, struct fatEntry *, u_char *,
102 struct dosDirEntry *);
103 static int readDosDirSection(int, struct bootblock *, struct fatEntry *,
104 struct dosDirEntry *);
105
106 /*
107 * Manage free dosDirEntry structures.
108 */
109 static struct dosDirEntry *freede;
110
111 static struct dosDirEntry *
newDosDirEntry(void)112 newDosDirEntry(void)
113 {
114 struct dosDirEntry *de;
115
116 if (!(de = freede)) {
117 if (!(de = (struct dosDirEntry *)malloc(sizeof *de)))
118 return 0;
119 } else
120 freede = de->next;
121 return de;
122 }
123
124 static void
freeDosDirEntry(struct dosDirEntry * de)125 freeDosDirEntry(struct dosDirEntry *de)
126 {
127 de->next = freede;
128 freede = de;
129 }
130
131 /*
132 * The same for dirTodoNode structures.
133 */
134 static struct dirTodoNode *freedt;
135
136 static struct dirTodoNode *
newDirTodo(void)137 newDirTodo(void)
138 {
139 struct dirTodoNode *dt;
140
141 if (!(dt = freedt)) {
142 if (!(dt = (struct dirTodoNode *)malloc(sizeof *dt)))
143 return 0;
144 } else
145 freedt = dt->next;
146 return dt;
147 }
148
149 static void
freeDirTodo(struct dirTodoNode * dt)150 freeDirTodo(struct dirTodoNode *dt)
151 {
152 dt->next = freedt;
153 freedt = dt;
154 }
155
156 /*
157 * The stack of unread directories
158 */
159 static struct dirTodoNode *pendingDirectories = NULL;
160
161 /*
162 * Return the full pathname for a directory entry.
163 */
164 static char *
fullpath(struct dosDirEntry * dir)165 fullpath(struct dosDirEntry *dir)
166 {
167 static char namebuf[MAXPATHLEN + 1];
168 char *cp, *np;
169 int nl;
170
171 cp = namebuf + sizeof namebuf;
172 *--cp = '\0';
173
174 for(;;) {
175 np = dir->lname[0] ? dir->lname : dir->name;
176 nl = strlen(np);
177 if (cp <= namebuf + 1 + nl) {
178 *--cp = '?';
179 break;
180 }
181 cp -= nl;
182 memcpy(cp, np, nl);
183 dir = dir->parent;
184 if (!dir)
185 break;
186 *--cp = '/';
187 }
188
189 return cp;
190 }
191
192 /*
193 * Calculate a checksum over an 8.3 alias name
194 */
195 static u_char
calcShortSum(u_char * p)196 calcShortSum(u_char *p)
197 {
198 u_char sum = 0;
199 int i;
200
201 for (i = 0; i < 11; i++) {
202 sum = (sum << 7)|(sum >> 1); /* rotate right */
203 sum += p[i];
204 }
205
206 return sum;
207 }
208
209 /*
210 * Global variables temporarily used during a directory scan
211 */
212 static char longName[DOSLONGNAMELEN] = "";
213 static u_char *buffer = NULL;
214 static u_char *delbuf = NULL;
215
216 static struct dosDirEntry *rootDir;
217 static struct dosDirEntry *lostDir;
218
219 /*
220 * Init internal state for a new directory scan.
221 */
222 int
resetDosDirSection(struct bootblock * boot,struct fatEntry * fat)223 resetDosDirSection(struct bootblock *boot, struct fatEntry *fat)
224 {
225 int b1, b2;
226 int ret = FSOK;
227 size_t len;
228
229 b1 = boot->bpbRootDirEnts * 32;
230 b2 = boot->bpbSecPerClust * boot->bpbBytesPerSec;
231
232 if ((buffer = malloc(len = MAX(b1, b2))) == NULL) {
233 perr("No space for directory buffer (%zu)", len);
234 return FSFATAL;
235 }
236
237 if ((delbuf = malloc(len = b2)) == NULL) {
238 free(buffer);
239 perr("No space for directory delbuf (%zu)", len);
240 return FSFATAL;
241 }
242
243 if ((rootDir = newDosDirEntry()) == NULL) {
244 free(buffer);
245 free(delbuf);
246 perr("No space for directory entry");
247 return FSFATAL;
248 }
249
250 memset(rootDir, 0, sizeof *rootDir);
251 if (boot->flags & FAT32) {
252 if (boot->bpbRootClust < CLUST_FIRST ||
253 boot->bpbRootClust >= boot->NumClusters) {
254 pfatal("Root directory starts with cluster out of range(%u)",
255 boot->bpbRootClust);
256 return FSFATAL;
257 }
258 if (fat[boot->bpbRootClust].head != boot->bpbRootClust) {
259 pfatal("Root directory doesn't start a cluster chain");
260 return FSFATAL;
261 }
262
263 fat[boot->bpbRootClust].flags |= FAT_USED;
264 rootDir->head = boot->bpbRootClust;
265 }
266
267 return ret;
268 }
269
270 /*
271 * Cleanup after a directory scan
272 */
273 void
finishDosDirSection(void)274 finishDosDirSection(void)
275 {
276 struct dirTodoNode *p, *np;
277 struct dosDirEntry *d, *nd;
278
279 for (p = pendingDirectories; p; p = np) {
280 np = p->next;
281 freeDirTodo(p);
282 }
283 pendingDirectories = NULL;
284 for (d = rootDir; d; d = nd) {
285 if ((nd = d->child) != NULL) {
286 d->child = 0;
287 continue;
288 }
289 if (!(nd = d->next))
290 nd = d->parent;
291 freeDosDirEntry(d);
292 }
293 rootDir = lostDir = NULL;
294 free(buffer);
295 free(delbuf);
296 buffer = NULL;
297 delbuf = NULL;
298 }
299
300 /*
301 * Delete directory entries between startcl, startoff and endcl, endoff.
302 */
303 static int
delete(int f,struct bootblock * boot,struct fatEntry * fat,cl_t startcl,int startoff,cl_t endcl,int endoff,int notlast)304 delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
305 int startoff, cl_t endcl, int endoff, int notlast)
306 {
307 u_char *s, *e;
308 off_t off;
309 int clsz = boot->bpbSecPerClust * boot->bpbBytesPerSec;
310
311 s = delbuf + startoff;
312 e = delbuf + clsz;
313 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
314 if (startcl == endcl) {
315 if (notlast)
316 break;
317 e = delbuf + endoff;
318 }
319 off = startcl * boot->bpbSecPerClust + boot->ClusterOffset;
320 off *= boot->bpbBytesPerSec;
321 if (lseek(f, off, SEEK_SET) != off) {
322 perr("Unable to lseek to %" PRId64, off);
323 return FSFATAL;
324 }
325 if (read(f, delbuf, clsz) != clsz) {
326 perr("Unable to read directory");
327 return FSFATAL;
328 }
329 while (s < e) {
330 *s = SLOT_DELETED;
331 s += 32;
332 }
333 if (lseek(f, off, SEEK_SET) != off) {
334 perr("Unable to lseek to %" PRId64, off);
335 return FSFATAL;
336 }
337 if (write(f, delbuf, clsz) != clsz) {
338 perr("Unable to write directory");
339 return FSFATAL;
340 }
341 if (startcl == endcl)
342 break;
343 startcl = fat[startcl].next;
344 s = delbuf;
345 }
346 return FSOK;
347 }
348
349 static int
removede(int f,struct bootblock * boot,struct fatEntry * fat,u_char * start,u_char * end,cl_t startcl,cl_t endcl,cl_t curcl,char * path,int type)350 removede(int f, struct bootblock *boot, struct fatEntry *fat, u_char *start,
351 u_char *end, cl_t startcl, cl_t endcl, cl_t curcl, char *path, int type)
352 {
353 switch (type) {
354 case 0:
355 pwarn("Invalid long filename entry for %s\n", path);
356 break;
357 case 1:
358 pwarn("Invalid long filename entry at end of directory %s\n",
359 path);
360 break;
361 case 2:
362 pwarn("Invalid long filename entry for volume label\n");
363 break;
364 }
365 if (ask(0, "Remove")) {
366 if (startcl != curcl) {
367 if (delete(f, boot, fat,
368 startcl, start - buffer,
369 endcl, end - buffer,
370 endcl == curcl) == FSFATAL)
371 return FSFATAL;
372 start = buffer;
373 }
374 /* startcl is < CLUST_FIRST for !fat32 root */
375 if ((endcl == curcl) || (startcl < CLUST_FIRST))
376 for (; start < end; start += 32)
377 *start = SLOT_DELETED;
378 return FSDIRMOD;
379 }
380 return FSERROR;
381 }
382
383 /*
384 * Check an in-memory file entry
385 */
386 static int
checksize(struct bootblock * boot,struct fatEntry * fat,u_char * p,struct dosDirEntry * dir)387 checksize(struct bootblock *boot, struct fatEntry *fat, u_char *p,
388 struct dosDirEntry *dir)
389 {
390 /*
391 * Check size on ordinary files
392 */
393 u_int32_t physicalSize;
394
395 if (dir->head == CLUST_FREE)
396 physicalSize = 0;
397 else {
398 if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
399 return FSERROR;
400 physicalSize = fat[dir->head].length * boot->ClusterSize;
401 }
402 if (physicalSize < dir->size) {
403 pwarn("size of %s is %u, should at most be %u\n",
404 fullpath(dir), dir->size, physicalSize);
405 if (ask(1, "Truncate")) {
406 dir->size = physicalSize;
407 p[28] = (u_char)physicalSize;
408 p[29] = (u_char)(physicalSize >> 8);
409 p[30] = (u_char)(physicalSize >> 16);
410 p[31] = (u_char)(physicalSize >> 24);
411 return FSDIRMOD;
412 } else
413 return FSERROR;
414 } else if (physicalSize - dir->size >= boot->ClusterSize) {
415 pwarn("%s has too many clusters allocated\n",
416 fullpath(dir));
417 if (ask(1, "Drop superfluous clusters")) {
418 cl_t cl;
419 u_int32_t sz, len;
420
421 for (cl = dir->head, len = sz = 0;
422 (sz += boot->ClusterSize) < dir->size; len++)
423 cl = fat[cl].next;
424 clearchain(boot, fat, fat[cl].next);
425 fat[cl].next = CLUST_EOF;
426 fat[dir->head].length = len;
427 return FSFATMOD;
428 } else
429 return FSERROR;
430 }
431 return FSOK;
432 }
433
434 /*
435 * Read a directory and
436 * - resolve long name records
437 * - enter file and directory records into the parent's list
438 * - push directories onto the todo-stack
439 */
440 static int
readDosDirSection(int f,struct bootblock * boot,struct fatEntry * fat,struct dosDirEntry * dir)441 readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
442 struct dosDirEntry *dir)
443 {
444 struct dosDirEntry dirent, *d;
445 u_char *p, *vallfn, *invlfn, *empty;
446 off_t off;
447 int i, j, k, last;
448 cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
449 char *t;
450 u_int lidx = 0;
451 int shortSum;
452 int mod = FSOK;
453 #define THISMOD 0x8000 /* Only used within this routine */
454
455 cl = dir->head;
456 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
457 /*
458 * Already handled somewhere else.
459 */
460 return FSOK;
461 }
462 shortSum = -1;
463 vallfn = invlfn = empty = NULL;
464 do {
465 if (!(boot->flags & FAT32) && !dir->parent) {
466 last = boot->bpbRootDirEnts * 32;
467 off = boot->bpbResSectors + boot->bpbFATs *
468 boot->FATsecs;
469 } else {
470 last = boot->bpbSecPerClust * boot->bpbBytesPerSec;
471 off = cl * boot->bpbSecPerClust + boot->ClusterOffset;
472 }
473
474 off *= boot->bpbBytesPerSec;
475 if (lseek(f, off, SEEK_SET) != off
476 || read(f, buffer, last) != last) {
477 perr("Unable to read directory");
478 return FSFATAL;
479 }
480 last /= 32;
481 /*
482 * Check `.' and `..' entries here? XXX
483 */
484 for (p = buffer, i = 0; i < last; i++, p += 32) {
485 if (dir->fsckflags & DIREMPWARN) {
486 *p = SLOT_EMPTY;
487 continue;
488 }
489
490 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
491 if (*p == SLOT_EMPTY) {
492 dir->fsckflags |= DIREMPTY;
493 empty = p;
494 empcl = cl;
495 }
496 continue;
497 }
498
499 if (dir->fsckflags & DIREMPTY) {
500 if (!(dir->fsckflags & DIREMPWARN)) {
501 pwarn("%s has entries after end of directory\n",
502 fullpath(dir));
503 if (ask(1, "Extend")) {
504 u_char *q;
505
506 dir->fsckflags &= ~DIREMPTY;
507 if (delete(f, boot, fat,
508 empcl, empty - buffer,
509 cl, p - buffer, 1) == FSFATAL)
510 return FSFATAL;
511 q = empcl == cl ? empty : buffer;
512 for (; q < p; q += 32)
513 *q = SLOT_DELETED;
514 mod |= THISMOD|FSDIRMOD;
515 } else if (ask(0, "Truncate"))
516 dir->fsckflags |= DIREMPWARN;
517 }
518 if (dir->fsckflags & DIREMPWARN) {
519 *p = SLOT_DELETED;
520 mod |= THISMOD|FSDIRMOD;
521 continue;
522 } else if (dir->fsckflags & DIREMPTY)
523 mod |= FSERROR;
524 empty = NULL;
525 }
526
527 if (p[11] == ATTR_WIN95) {
528 if (*p & LRFIRST) {
529 if (shortSum != -1) {
530 if (!invlfn) {
531 invlfn = vallfn;
532 invcl = valcl;
533 }
534 }
535 memset(longName, 0, sizeof longName);
536 shortSum = p[13];
537 vallfn = p;
538 valcl = cl;
539 } else if (shortSum != p[13]
540 || lidx != (*p & LRNOMASK)) {
541 if (!invlfn) {
542 invlfn = vallfn;
543 invcl = valcl;
544 }
545 if (!invlfn) {
546 invlfn = p;
547 invcl = cl;
548 }
549 vallfn = NULL;
550 }
551 lidx = *p & LRNOMASK;
552 t = longName + --lidx * 13;
553 for (k = 1; k < 11 && t < longName +
554 sizeof(longName); k += 2) {
555 if (!p[k] && !p[k + 1])
556 break;
557 *t++ = p[k];
558 /*
559 * Warn about those unusable chars in msdosfs here? XXX
560 */
561 if (p[k + 1])
562 t[-1] = '?';
563 }
564 if (k >= 11)
565 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
566 if (!p[k] && !p[k + 1])
567 break;
568 *t++ = p[k];
569 if (p[k + 1])
570 t[-1] = '?';
571 }
572 if (k >= 26)
573 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
574 if (!p[k] && !p[k + 1])
575 break;
576 *t++ = p[k];
577 if (p[k + 1])
578 t[-1] = '?';
579 }
580 if (t >= longName + sizeof(longName)) {
581 pwarn("long filename too long\n");
582 if (!invlfn) {
583 invlfn = vallfn;
584 invcl = valcl;
585 }
586 vallfn = NULL;
587 }
588 if (p[26] | (p[27] << 8)) {
589 pwarn("long filename record cluster start != 0\n");
590 if (!invlfn) {
591 invlfn = vallfn;
592 invcl = cl;
593 }
594 vallfn = NULL;
595 }
596 continue; /* long records don't carry further
597 * information */
598 }
599
600 /*
601 * This is a standard msdosfs directory entry.
602 */
603 memset(&dirent, 0, sizeof dirent);
604
605 /*
606 * it's a short name record, but we need to know
607 * more, so get the flags first.
608 */
609 dirent.flags = p[11];
610
611 /*
612 * Translate from 850 to ISO here XXX
613 */
614 for (j = 0; j < 8; j++)
615 dirent.name[j] = p[j];
616 dirent.name[8] = '\0';
617 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
618 dirent.name[k] = '\0';
619 if (k < 0 || dirent.name[k] != '\0')
620 k++;
621 if (dirent.name[0] == SLOT_E5)
622 dirent.name[0] = 0xe5;
623
624 if (dirent.flags & ATTR_VOLUME) {
625 if (vallfn || invlfn) {
626 mod |= removede(f, boot, fat,
627 invlfn ? invlfn : vallfn, p,
628 invlfn ? invcl : valcl, -1, 0,
629 fullpath(dir), 2);
630 vallfn = NULL;
631 invlfn = NULL;
632 }
633 continue;
634 }
635
636 if (p[8] != ' ')
637 dirent.name[k++] = '.';
638 for (j = 0; j < 3; j++)
639 dirent.name[k++] = p[j+8];
640 dirent.name[k] = '\0';
641 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
642 dirent.name[k] = '\0';
643
644 if (vallfn && shortSum != calcShortSum(p)) {
645 if (!invlfn) {
646 invlfn = vallfn;
647 invcl = valcl;
648 }
649 vallfn = NULL;
650 }
651 dirent.head = p[26] | (p[27] << 8);
652 if (boot->ClustMask == CLUST32_MASK)
653 dirent.head |= (p[20] << 16) | (p[21] << 24);
654 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
655 if (vallfn) {
656 strlcpy(dirent.lname, longName,
657 sizeof(dirent.lname));
658 longName[0] = '\0';
659 shortSum = -1;
660 }
661
662 dirent.parent = dir;
663 dirent.next = dir->child;
664
665 if (invlfn) {
666 mod |= k = removede(f, boot, fat,
667 invlfn, vallfn ? vallfn : p,
668 invcl, vallfn ? valcl : cl, cl,
669 fullpath(&dirent), 0);
670 if (mod & FSFATAL)
671 return FSFATAL;
672 if (vallfn
673 ? (valcl == cl && vallfn != buffer)
674 : p != buffer)
675 if (k & FSDIRMOD)
676 mod |= THISMOD;
677 }
678
679 vallfn = NULL; /* not used any longer */
680 invlfn = NULL;
681
682 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
683 if (dirent.head != 0) {
684 pwarn("%s has clusters, but size 0\n",
685 fullpath(&dirent));
686 if (ask(1, "Drop allocated clusters")) {
687 p[26] = p[27] = 0;
688 if (boot->ClustMask == CLUST32_MASK)
689 p[20] = p[21] = 0;
690 clearchain(boot, fat, dirent.head);
691 dirent.head = 0;
692 mod |= THISMOD|FSDIRMOD|FSFATMOD;
693 } else
694 mod |= FSERROR;
695 }
696 } else if (dirent.head == 0
697 && !strcmp(dirent.name, "..")
698 && dir->parent /* XXX */
699 && !dir->parent->parent) {
700 /*
701 * Do nothing, the parent is the root
702 */
703 } else if (dirent.head < CLUST_FIRST
704 || dirent.head >= boot->NumClusters
705 || fat[dirent.head].next == CLUST_FREE
706 || (fat[dirent.head].next >= CLUST_RSRVD
707 && fat[dirent.head].next < CLUST_EOFS)
708 || fat[dirent.head].head != dirent.head) {
709 if (dirent.head == 0)
710 pwarn("%s has no clusters\n",
711 fullpath(&dirent));
712 else if (dirent.head < CLUST_FIRST
713 || dirent.head >= boot->NumClusters)
714 pwarn("%s starts with cluster out of range(%u)\n",
715 fullpath(&dirent),
716 dirent.head);
717 else if (fat[dirent.head].next == CLUST_FREE)
718 pwarn("%s starts with free cluster\n",
719 fullpath(&dirent));
720 else if (fat[dirent.head].next >= CLUST_RSRVD)
721 pwarn("%s starts with cluster marked %s\n",
722 fullpath(&dirent),
723 rsrvdcltype(fat[dirent.head].next));
724 else
725 pwarn("%s doesn't start a cluster chain\n",
726 fullpath(&dirent));
727 if (dirent.flags & ATTR_DIRECTORY) {
728 if (ask(0, "Remove")) {
729 *p = SLOT_DELETED;
730 mod |= THISMOD|FSDIRMOD;
731 } else
732 mod |= FSERROR;
733 continue;
734 } else {
735 if (ask(1, "Truncate")) {
736 p[28] = p[29] = p[30] = p[31] = 0;
737 p[26] = p[27] = 0;
738 if (boot->ClustMask == CLUST32_MASK)
739 p[20] = p[21] = 0;
740 dirent.size = 0;
741 mod |= THISMOD|FSDIRMOD;
742 } else
743 mod |= FSERROR;
744 }
745 }
746
747 if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
748 fat[dirent.head].flags |= FAT_USED;
749
750 if (dirent.flags & ATTR_DIRECTORY) {
751 /*
752 * gather more info for directories
753 */
754 struct dirTodoNode *n;
755
756 if (dirent.size) {
757 pwarn("Directory %s has size != 0\n",
758 fullpath(&dirent));
759 if (ask(1, "Correct")) {
760 p[28] = p[29] = p[30] = p[31] = 0;
761 dirent.size = 0;
762 mod |= THISMOD|FSDIRMOD;
763 } else
764 mod |= FSERROR;
765 }
766 /*
767 * handle `.' and `..' specially
768 */
769 if (strcmp(dirent.name, ".") == 0) {
770 if (dirent.head != dir->head) {
771 pwarn("`.' entry in %s has incorrect start cluster\n",
772 fullpath(dir));
773 if (ask(1, "Correct")) {
774 dirent.head = dir->head;
775 p[26] = (u_char)dirent.head;
776 p[27] = (u_char)(dirent.head >> 8);
777 if (boot->ClustMask == CLUST32_MASK) {
778 p[20] = (u_char)(dirent.head >> 16);
779 p[21] = (u_char)(dirent.head >> 24);
780 }
781 mod |= THISMOD|FSDIRMOD;
782 } else
783 mod |= FSERROR;
784 }
785 continue;
786 }
787 if (strcmp(dirent.name, "..") == 0) {
788 if (dir->parent) { /* XXX */
789 if (!dir->parent->parent) {
790 if (dirent.head) {
791 pwarn("`..' entry in %s has non-zero start cluster\n",
792 fullpath(dir));
793 if (ask(1, "Correct")) {
794 dirent.head = 0;
795 p[26] = p[27] = 0;
796 if (boot->ClustMask == CLUST32_MASK)
797 p[20] = p[21] = 0;
798 mod |= THISMOD|FSDIRMOD;
799 } else
800 mod |= FSERROR;
801 }
802 } else if (dirent.head != dir->parent->head) {
803 pwarn("`..' entry in %s has incorrect start cluster\n",
804 fullpath(dir));
805 if (ask(1, "Correct")) {
806 dirent.head = dir->parent->head;
807 p[26] = (u_char)dirent.head;
808 p[27] = (u_char)(dirent.head >> 8);
809 if (boot->ClustMask == CLUST32_MASK) {
810 p[20] = (u_char)(dirent.head >> 16);
811 p[21] = (u_char)(dirent.head >> 24);
812 }
813 mod |= THISMOD|FSDIRMOD;
814 } else
815 mod |= FSERROR;
816 }
817 }
818 continue;
819 }
820
821 /* create directory tree node */
822 if (!(d = newDosDirEntry())) {
823 perr("No space for directory");
824 return FSFATAL;
825 }
826 memcpy(d, &dirent, sizeof(struct dosDirEntry));
827 /* link it into the tree */
828 dir->child = d;
829
830 /* Enter this directory into the todo list */
831 if (!(n = newDirTodo())) {
832 perr("No space for todo list");
833 return FSFATAL;
834 }
835 n->next = pendingDirectories;
836 n->dir = d;
837 pendingDirectories = n;
838 } else {
839 mod |= k = checksize(boot, fat, p, &dirent);
840 if (k & FSDIRMOD)
841 mod |= THISMOD;
842 }
843 boot->NumFiles++;
844 }
845
846 if (!(boot->flags & FAT32) && !dir->parent)
847 break;
848
849 if (mod & THISMOD) {
850 last *= 32;
851 if (lseek(f, off, SEEK_SET) != off
852 || write(f, buffer, last) != last) {
853 perr("Unable to write directory");
854 return FSFATAL;
855 }
856 mod &= ~THISMOD;
857 }
858 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
859 if (invlfn || vallfn)
860 mod |= removede(f, boot, fat,
861 invlfn ? invlfn : vallfn, p,
862 invlfn ? invcl : valcl, -1, 0,
863 fullpath(dir), 1);
864
865 /* The root directory of non fat32 filesystems is in a special
866 * area and may have been modified above without being written out.
867 */
868 if ((mod & FSDIRMOD) && !(boot->flags & FAT32) && !dir->parent) {
869 last *= 32;
870 if (lseek(f, off, SEEK_SET) != off
871 || write(f, buffer, last) != last) {
872 perr("Unable to write directory");
873 return FSFATAL;
874 }
875 mod &= ~THISMOD;
876 }
877 return mod & ~THISMOD;
878 }
879
880 int
handleDirTree(int dosfs,struct bootblock * boot,struct fatEntry * fat)881 handleDirTree(int dosfs, struct bootblock *boot, struct fatEntry *fat)
882 {
883 int mod;
884
885 mod = readDosDirSection(dosfs, boot, fat, rootDir);
886 if (mod & FSFATAL)
887 return FSFATAL;
888
889 /*
890 * process the directory todo list
891 */
892 while (pendingDirectories) {
893 struct dosDirEntry *dir = pendingDirectories->dir;
894 struct dirTodoNode *n = pendingDirectories->next;
895
896 /*
897 * remove TODO entry now, the list might change during
898 * directory reads
899 */
900 freeDirTodo(pendingDirectories);
901 pendingDirectories = n;
902
903 /*
904 * handle subdirectory
905 */
906 mod |= readDosDirSection(dosfs, boot, fat, dir);
907 if (mod & FSFATAL)
908 return FSFATAL;
909 }
910
911 return mod;
912 }
913
914 /*
915 * Try to reconnect a FAT chain into dir
916 */
917 static u_char *lfbuf;
918 static cl_t lfcl;
919 static off_t lfoff;
920
921 int
reconnect(int dosfs,struct bootblock * boot,struct fatEntry * fat,cl_t head)922 reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
923 {
924 struct dosDirEntry d;
925 int len;
926 u_char *p;
927
928 if (!ask(1, "Reconnect"))
929 return FSERROR;
930
931 if (!lostDir) {
932 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
933 if (!strcmp(lostDir->name, LOSTDIR))
934 break;
935 }
936 if (!lostDir) { /* Create LOSTDIR? XXX */
937 pwarn("No %s directory\n", LOSTDIR);
938 return FSERROR;
939 }
940 }
941 if (!lfbuf) {
942 lfbuf = malloc(boot->ClusterSize);
943 if (!lfbuf) {
944 perr("No space for buffer");
945 return FSFATAL;
946 }
947 p = NULL;
948 } else
949 p = lfbuf;
950 while (1) {
951 if (p)
952 for (; p < lfbuf + boot->ClusterSize; p += 32)
953 if (*p == SLOT_EMPTY
954 || *p == SLOT_DELETED)
955 break;
956 if (p && p < lfbuf + boot->ClusterSize)
957 break;
958 lfcl = p ? fat[lfcl].next : lostDir->head;
959 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
960 /* Extend LOSTDIR? XXX */
961 pwarn("No space in %s\n", LOSTDIR);
962 return FSERROR;
963 }
964 lfoff = lfcl * boot->ClusterSize
965 + boot->ClusterOffset * boot->bpbBytesPerSec;
966 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
967 || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
968 perr("could not read LOST.DIR");
969 return FSFATAL;
970 }
971 p = lfbuf;
972 }
973
974 boot->NumFiles++;
975 /* Ensure uniqueness of entry here! XXX */
976 memset(&d, 0, sizeof d);
977 /* worst case -1 = 4294967295, 10 digits */
978 len = snprintf(d.name, sizeof(d.name), "%u", head);
979 d.flags = 0;
980 d.head = head;
981 d.size = fat[head].length * boot->ClusterSize;
982
983 memcpy(p, d.name, len);
984 memset(p + len, ' ', 11 - len);
985 memset(p + 11, 0, 32 - 11);
986 p[26] = (u_char)d.head;
987 p[27] = (u_char)(d.head >> 8);
988 if (boot->ClustMask == CLUST32_MASK) {
989 p[20] = (u_char)(d.head >> 16);
990 p[21] = (u_char)(d.head >> 24);
991 }
992 p[28] = (u_char)d.size;
993 p[29] = (u_char)(d.size >> 8);
994 p[30] = (u_char)(d.size >> 16);
995 p[31] = (u_char)(d.size >> 24);
996 fat[head].flags |= FAT_USED;
997 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
998 || (size_t)write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
999 perr("could not write LOST.DIR");
1000 return FSFATAL;
1001 }
1002 return FSDIRMOD;
1003 }
1004
1005 void
finishlf(void)1006 finishlf(void)
1007 {
1008 if (lfbuf)
1009 free(lfbuf);
1010 lfbuf = NULL;
1011 }
1012