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