dir.c revision 1.6 1 /* $NetBSD: dir.c,v 1.6 1996/09/23 16:27:58 christos 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.6 1996/09/23 16:27:58 christos 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 "util.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 return cp;
191 }
192
193 /*
194 * Calculate a checksum over an 8.3 alias name
195 */
196 static u_char
197 calcShortSum(p)
198 u_char *p;
199 {
200 u_char sum = 0;
201 int i;
202
203 for (i = 0; i < 11; i++) {
204 sum = (sum << 7)|(sum >> 1); /* rotate right */
205 sum += p[i];
206 }
207
208 return sum;
209 }
210
211 /*
212 * Global variables temporarily used during a directory scan
213 */
214 static char longName[DOSLONGNAMELEN] = "";
215 static u_char *buffer = NULL;
216 static u_char *delbuf = NULL;
217
218 struct dosDirEntry *rootDir;
219 static struct dosDirEntry *lostDir;
220
221 /*
222 * Init internal state for a new directory scan.
223 */
224 int
225 resetDosDirSection(boot)
226 struct bootblock *boot;
227 {
228 int b1, b2;
229
230 b1 = boot->RootDirEnts * 32;
231 b2 = boot->SecPerClust * boot->BytesPerSec;
232
233 if (!(buffer = malloc(b1 > b2 ? b1 : b2))
234 || !(delbuf = malloc(b2))
235 || !(rootDir = newDosDirEntry())) {
236 perror("No space for directory");
237 return FSFATAL;
238 }
239 memset(rootDir, 0, sizeof *rootDir);
240 return FSOK;
241 }
242
243 /*
244 * Cleanup after a directory scan
245 */
246 void
247 finishDosDirSection()
248 {
249 struct dirTodoNode *p, *np;
250 struct dosDirEntry *d, *nd;
251
252 for (p = pendingDirectories; p; p = np) {
253 np = p->next;
254 freeDirTodo(p);
255 }
256 pendingDirectories = 0;
257 for (d = rootDir; d; d = nd) {
258 if ((nd = d->child) != NULL) {
259 d->child = 0;
260 continue;
261 }
262 if (!(nd = d->next))
263 nd = d->parent;
264 freeDosDirEntry(d);
265 }
266 rootDir = lostDir = NULL;
267 free(buffer);
268 free(delbuf);
269 buffer = NULL;
270 delbuf = NULL;
271 }
272
273 /*
274 * Delete directory entries between startcl, startoff and endcl, endoff.
275 */
276 static int
277 delete(f, boot, fat, startcl, startoff, endcl, endoff, notlast)
278 int f;
279 struct bootblock *boot;
280 struct fatEntry *fat;
281 cl_t startcl;
282 int startoff;
283 cl_t endcl;
284 int endoff;
285 int notlast;
286 {
287 u_char *s, *e;
288 off_t off;
289 int clsz = boot->SecPerClust * boot->BytesPerSec;
290
291 s = delbuf + startoff;
292 e = delbuf + clsz;
293 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
294 if (startcl == endcl) {
295 if (notlast)
296 break;
297 e = delbuf + endoff;
298 }
299 off = startcl * boot->SecPerClust + boot->ClusterOffset;
300 off *= boot->BytesPerSec;
301 if (lseek(f, off, SEEK_SET) != off
302 || read(f, delbuf, clsz) != clsz) {
303 perror("Unable to read directory");
304 return FSFATAL;
305 }
306 while (s < e) {
307 *s = SLOT_DELETED;
308 s += 32;
309 }
310 if (lseek(f, off, SEEK_SET) != off
311 || write(f, delbuf, clsz) != clsz) {
312 perror("Unable to write directory");
313 return FSFATAL;
314 }
315 if (startcl == endcl)
316 break;
317 startcl = fat[startcl].next;
318 s = delbuf;
319 }
320 return FSOK;
321 }
322
323 static int
324 removede(f, boot, fat, start, end, startcl, endcl, curcl, path, type)
325 int f;
326 struct bootblock *boot;
327 struct fatEntry *fat;
328 u_char *start;
329 u_char *end;
330 cl_t startcl;
331 cl_t endcl;
332 cl_t curcl;
333 char *path;
334 int type;
335 {
336 switch (type) {
337 case 0:
338 pwarn("Invalid long filename entry for %s\n", path);
339 break;
340 case 1:
341 pwarn("Invalid long filename entry at end of directory %s\n", path);
342 break;
343 case 2:
344 pwarn("Invalid long filename entry for volume label\n");
345 break;
346 }
347 if (ask(0, "Remove")) {
348 if (startcl != curcl) {
349 if (delete(f, boot, fat,
350 startcl, start - buffer,
351 endcl, end - buffer,
352 endcl == curcl) == FSFATAL)
353 return FSFATAL;
354 start = buffer;
355 }
356 if (endcl == curcl)
357 for (; start < end; start += 32)
358 *start = SLOT_DELETED;
359 return FSDIRMOD;
360 }
361 return FSERROR;
362 }
363
364 /*
365 * Check an in-memory file entry
366 */
367 static int
368 checksize(boot, fat, p, dir)
369 struct bootblock *boot;
370 struct fatEntry *fat;
371 u_char *p;
372 struct dosDirEntry *dir;
373 {
374 /*
375 * Check size on ordinary files
376 */
377 int32_t physicalSize;
378
379 if (dir->head == CLUST_FREE)
380 physicalSize = 0;
381 else {
382 if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
383 return FSERROR;
384 physicalSize = fat[dir->head].length * boot->ClusterSize;
385 }
386 if (physicalSize < dir->size) {
387 pwarn("size of %s is %u, should at most be %u\n",
388 fullpath(dir), dir->size, physicalSize);
389 if (ask(1, "Truncate")) {
390 dir->size = physicalSize;
391 p[28] = (u_char)physicalSize;
392 p[29] = (u_char)(physicalSize >> 8);
393 p[30] = (u_char)(physicalSize >> 16);
394 p[31] = (u_char)(physicalSize >> 24);
395 return FSDIRMOD;
396 } else
397 return FSERROR;
398 } else if (physicalSize - dir->size >= boot->ClusterSize) {
399 pwarn("%s has too many clusters allocated\n",
400 fullpath(dir));
401 if (ask(1, "Drop superfluous clusters")) {
402 cl_t cl;
403 u_int32_t sz = 0;
404
405 for (cl = dir->head; (sz += boot->ClusterSize) < dir->size;)
406 cl = fat[cl].next;
407 clearchain(boot, fat, fat[cl].next);
408 fat[cl].next = CLUST_EOF;
409 return FSFATMOD;
410 } else
411 return FSERROR;
412 }
413 return FSOK;
414 }
415
416 /*
417 * Read a directory and
418 * - resolve long name records
419 * - enter file and directory records into the parent's list
420 * - push directories onto the todo-stack
421 */
422 static int
423 readDosDirSection(f, boot, fat, dir)
424 int f;
425 struct bootblock *boot;
426 struct fatEntry *fat;
427 struct dosDirEntry *dir;
428 {
429 struct dosDirEntry dirent, *d;
430 u_char *p, *vallfn, *invlfn, *empty;
431 off_t off;
432 int i, j, k, last;
433 cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
434 char *t;
435 u_int lidx = 0;
436 int shortSum;
437 int mod = FSOK;
438 #define THISMOD 0x8000 /* Only used within this routine */
439
440 cl = dir->head;
441 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
442 /*
443 * Already handled somewhere else.
444 */
445 return FSOK;
446 }
447 shortSum = -1;
448 vallfn = invlfn = empty = NULL;
449 do {
450 if (!dir->parent) {
451 last = boot->RootDirEnts * 32;
452 off = boot->ResSectors + boot->FATs * boot->FATsecs;
453 } else {
454 last = boot->SecPerClust * boot->BytesPerSec;
455 off = cl * boot->SecPerClust + boot->ClusterOffset;
456 }
457
458 off *= boot->BytesPerSec;
459 if (lseek(f, off, SEEK_SET) != off
460 || read(f, buffer, last) != last) {
461 perror("Unable to read directory");
462 return FSFATAL;
463 }
464 last /= 32;
465 /*
466 * Check `.' and `..' entries here? XXX
467 */
468 for (p = buffer, i = 0; i < last; i++, p += 32) {
469 if (dir->fsckflags & DIREMPWARN) {
470 *p = SLOT_EMPTY;
471 continue;
472 }
473
474 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
475 if (*p == SLOT_EMPTY) {
476 dir->fsckflags |= DIREMPTY;
477 empty = p;
478 empcl = cl;
479 }
480 continue;
481 }
482
483 if (dir->fsckflags & DIREMPTY) {
484 if (!(dir->fsckflags & DIREMPWARN)) {
485 pwarn("%s has entries after end of directory\n",
486 fullpath(dir));
487 if (ask(1, "Extend")) {
488 dir->fsckflags &= ~DIREMPTY;
489 if (delete(f, boot, fat,
490 empcl, empty - buffer,
491 cl, p - buffer, 0) == FSFATAL)
492 return FSFATAL;
493 } else if (ask(0, "Truncate"))
494 dir->fsckflags |= DIREMPWARN;
495 }
496 if (dir->fsckflags & DIREMPWARN) {
497 *p = SLOT_DELETED;
498 mod |= THISMOD|FSDIRMOD;
499 continue;
500 } else if (dir->fsckflags & DIREMPTY)
501 mod |= FSERROR;
502 empty = NULL;
503 }
504
505 if (p[11] == ATTR_WIN95) {
506 if (*p & LRFIRST) {
507 if (shortSum != -1) {
508 if (!invlfn) {
509 invlfn = vallfn;
510 invcl = valcl;
511 }
512 }
513 memset(longName, 0, sizeof longName);
514 shortSum = p[13];
515 vallfn = p;
516 valcl = cl;
517 } else if (shortSum != p[13]
518 || lidx != (*p & LRNOMASK)) {
519 if (!invlfn) {
520 invlfn = vallfn;
521 invcl = valcl;
522 }
523 if (!invlfn) {
524 invlfn = p;
525 invcl = cl;
526 }
527 vallfn = NULL;
528 }
529 lidx = *p & LRNOMASK;
530 t = longName + --lidx * 13;
531 for (k = 1; k < 11 && t < longName + sizeof(longName); k += 2) {
532 if (!p[k] && !p[k + 1])
533 break;
534 *t++ = p[k];
535 /*
536 * Warn about those unusable chars in msdosfs here? XXX
537 */
538 if (p[k + 1])
539 t[-1] = '?';
540 }
541 if (k >= 11)
542 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
543 if (!p[k] && !p[k + 1])
544 break;
545 *t++ = p[k];
546 if (p[k + 1])
547 t[-1] = '?';
548 }
549 if (k >= 26)
550 for (k = 28; k < 32 && 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 (t >= longName + sizeof(longName)) {
558 pwarn("long filename too long\n");
559 if (!invlfn) {
560 invlfn = vallfn;
561 invcl = valcl;
562 }
563 vallfn = NULL;
564 }
565 if (p[26] | (p[27] << 8)) {
566 pwarn("long filename record cluster start != 0\n");
567 if (!invlfn) {
568 invlfn = vallfn;
569 invcl = cl;
570 }
571 vallfn = NULL;
572 }
573 continue; /* long records don't carry further
574 * information */
575 }
576
577 /*
578 * This is a standard msdosfs directory entry.
579 */
580 memset(&dirent, 0, sizeof dirent);
581
582 /*
583 * it's a short name record, but we need to know
584 * more, so get the flags first.
585 */
586 dirent.flags = p[11];
587
588 /*
589 * Translate from 850 to ISO here XXX
590 */
591 for (j = 0; j < 8; j++)
592 dirent.name[j] = p[j];
593 dirent.name[8] = '\0';
594 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
595 dirent.name[k] = '\0';
596 if (dirent.name[k] != '\0')
597 k++;
598 if (dirent.name[0] == SLOT_E5)
599 dirent.name[0] = 0xe5;
600
601 if (dirent.flags & ATTR_VOLUME) {
602 if (vallfn || invlfn) {
603 mod |= removede(f, boot, fat,
604 invlfn ? invlfn : vallfn, p,
605 invlfn ? invcl : valcl, -1, 0,
606 fullpath(dir), 2);
607 vallfn = NULL;
608 invlfn = NULL;
609 }
610 continue;
611 }
612
613 if (p[8] != ' ')
614 dirent.name[k++] = '.';
615 for (j = 0; j < 3; j++)
616 dirent.name[k++] = p[j+8];
617 dirent.name[k] = '\0';
618 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
619 dirent.name[k] = '\0';
620
621 if (vallfn && shortSum != calcShortSum(p)) {
622 if (!invlfn) {
623 invlfn = vallfn;
624 invcl = valcl;
625 }
626 vallfn = NULL;
627 }
628 dirent.head = p[26] | (p[27] << 8);
629 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
630 if (vallfn) {
631 strcpy(dirent.lname, longName);
632 longName[0] = '\0';
633 shortSum = -1;
634 }
635
636 if (invlfn) {
637 mod |= k = removede(f, boot, fat,
638 invlfn, vallfn ? vallfn : p,
639 invcl, vallfn ? valcl : cl, cl,
640 fullpath(&dirent), 0);
641 if (mod & FSFATAL)
642 return FSFATAL;
643 if (vallfn
644 ? (valcl == cl && vallfn != buffer)
645 : p != buffer)
646 if (k & FSDIRMOD)
647 mod |= THISMOD;
648 }
649 vallfn = NULL; /* not used any longer */
650 invlfn = NULL;
651
652 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
653 if (dirent.head != 0) {
654 pwarn("%s has clusters, but size 0\n",
655 fullpath(&dirent));
656 if (ask(1, "Drop allocated clusters")) {
657 p[26] = p[27] = 0;
658 clearchain(boot, fat, dirent.head);
659 dirent.head = 0;
660 mod |= THISMOD|FSDIRMOD|FSFATMOD;
661 } else
662 mod |= FSERROR;
663 }
664 } else if (dirent.head == 0
665 && !strcmp(dirent.name, "..")
666 && dir->parent /* XXX */
667 && !dir->parent->parent) {
668 /*
669 * Do nothing, the parent is the root
670 */
671 } else if (dirent.head < CLUST_FIRST
672 || dirent.head >= boot->NumClusters
673 || fat[dirent.head].next == CLUST_FREE
674 || (fat[dirent.head].next >= CLUST_RSRVD
675 && fat[dirent.head].next < CLUST_EOFS)
676 || fat[dirent.head].head != dirent.head) {
677 if (dirent.head == 0)
678 pwarn("%s has no clusters\n",
679 fullpath(&dirent));
680 else if (dirent.head < CLUST_FIRST
681 || dirent.head >= boot->NumClusters)
682 pwarn("%s starts with cluster out of range(%d)\n",
683 fullpath(&dirent),
684 dirent.head);
685 else if (fat[dirent.head].next == CLUST_FREE)
686 pwarn("%s starts with free cluster\n",
687 fullpath(&dirent));
688 else if (fat[dirent.head].next >= CLUST_RSRVD)
689 pwarn("%s starts with %s cluster\n",
690 fullpath(&dirent),
691 rsrvdcltype(fat[dirent.head].next));
692 else
693 pwarn("%s doesn't start a cluster chain\n",
694 fullpath(&dirent));
695 if (dirent.flags & ATTR_DIRECTORY) {
696 if (ask(0, "Remove")) {
697 *p = SLOT_DELETED;
698 mod |= THISMOD|FSDIRMOD;
699 } else
700 mod |= FSERROR;
701 continue;
702 } else {
703 if (ask(1, "Truncate")) {
704 p[28] = p[29] = p[30] = p[31] = 0;
705 dirent.size = 0;
706 mod |= THISMOD|FSDIRMOD;
707 } else
708 mod |= FSERROR;
709 }
710 }
711
712 dirent.parent = dir;
713 dirent.next = dir->child;
714 if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
715 fat[dirent.head].flags |= FAT_USED;
716
717 if (dirent.flags & ATTR_DIRECTORY) {
718 /*
719 * gather more info for directories
720 */
721 struct dirTodoNode * n;
722
723 if (dirent.size) {
724 pwarn("Directory %s has size != 0\n",
725 fullpath(&dirent));
726 if (ask(1, "Correct")) {
727 p[28] = p[29] = p[30] = p[31] = 0;
728 dirent.size = 0;
729 mod |= THISMOD|FSDIRMOD;
730 } else
731 mod |= FSERROR;
732 }
733 /*
734 * handle `.' and `..' specially
735 */
736 if (strcmp(dirent.name, ".") == 0) {
737 if (dirent.head != dir->head) {
738 pwarn("`.' entry in %s has incorrect start cluster\n",
739 fullpath(dir));
740 if (ask(1, "Correct")) {
741 dirent.head = dir->head;
742 p[26] = (u_char)dirent.head;
743 p[27] = (u_char)(dirent.head >> 8);
744 mod |= THISMOD|FSDIRMOD;
745 } else
746 mod |= FSERROR;
747 }
748 continue;
749 }
750 if (strcmp(dirent.name, "..") == 0) {
751 if (dir->parent /* XXX */
752 && dirent.head != dir->parent->head) {
753 pwarn("`..' entry in %s has incorrect start cluster\n",
754 fullpath(dir));
755 if (ask(1, "Correct")) {
756 dirent.head = dir->parent->head;
757 p[26] = (u_char)dirent.head;
758 p[27] = (u_char)(dirent.head >> 8);
759 mod |= THISMOD|FSDIRMOD;
760 } else
761 mod |= FSERROR;
762 }
763 continue;
764 }
765
766 boot->NumFiles++;
767
768 /* create directory tree node */
769 if (!(d = newDosDirEntry())) {
770 perror("No space for directory");
771 return FSFATAL;
772 }
773
774 memcpy(d, &dirent, sizeof(struct dosDirEntry));
775 /* link it into the tree */
776 dir->child = d;
777
778 /* Enter this directory into the todo list */
779 if (!(n = newDirTodo())) {
780 perror("No space for todo list");
781 return FSFATAL;
782 }
783 n->next = pendingDirectories;
784 n->dir = d;
785 pendingDirectories = n;
786 } else {
787 mod |= k = checksize(boot, fat, p, &dirent);
788 if (k & FSDIRMOD)
789 mod |= THISMOD;
790 boot->NumFiles++;
791 }
792 }
793 if (mod & THISMOD) {
794 last *= 32;
795 if (lseek(f, off, SEEK_SET) != off
796 || write(f, buffer, last) != last) {
797 perror("Unable to write directory");
798 return FSFATAL;
799 }
800 mod &= ~THISMOD;
801 }
802 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
803 if (invlfn || vallfn)
804 mod |= removede(f, boot, fat,
805 invlfn ? invlfn : vallfn, p,
806 invlfn ? invcl : valcl, -1, 0,
807 fullpath(dir), 1);
808 return mod & ~THISMOD;
809 }
810
811 int
812 handleDirTree(dosfs, boot, fat)
813 int dosfs;
814 struct bootblock *boot;
815 struct fatEntry *fat;
816 {
817 int mod;
818
819 mod = readDosDirSection(dosfs, boot, fat, rootDir);
820 if (mod & FSFATAL)
821 return FSFATAL;
822
823 if (mod & FSFATMOD) {
824 mod &= ~FSFATMOD;
825 mod |= writefat(dosfs, boot, fat); /* delay writing fats? XXX */
826 }
827
828 if (mod & FSFATAL)
829 return FSFATAL;
830
831 /*
832 * process the directory todo list
833 */
834 while (pendingDirectories) {
835 struct dosDirEntry *dir = pendingDirectories->dir;
836 struct dirTodoNode *n = pendingDirectories->next;
837
838 /*
839 * remove TODO entry now, the list might change during
840 * directory reads
841 */
842 freeDirTodo(pendingDirectories);
843 pendingDirectories = n;
844
845 /*
846 * handle subdirectory
847 */
848 mod |= readDosDirSection(dosfs, boot, fat, dir);
849 if (mod & FSFATAL)
850 return FSFATAL;
851 if (mod & FSFATMOD) {
852 mod &= ~FSFATMOD;
853 mod |= writefat(dosfs, boot, fat); /* delay writing fats? XXX */
854 }
855 if (mod & FSFATAL)
856 return FSFATAL;
857 }
858 return mod;
859 }
860
861 /*
862 * Try to reconnect a FAT chain into dir
863 */
864 static u_char *lfbuf;
865 static cl_t lfcl;
866 static off_t lfoff;
867
868 int
869 reconnect(dosfs, boot, fat, head)
870 int dosfs;
871 struct bootblock *boot;
872 struct fatEntry *fat;
873 cl_t head;
874 {
875 struct dosDirEntry d;
876 u_char *p;
877
878 if (!lostDir) {
879 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
880 if (!strcmp(lostDir->name, LOSTDIR))
881 break;
882 }
883 if (!lostDir) { /* Create LOSTDIR? XXX */
884 pwarn("No %s directory\n", LOSTDIR);
885 return FSERROR;
886 }
887 }
888 if (!lfbuf) {
889 lfbuf = malloc(boot->ClusterSize);
890 if (!lfbuf) {
891 perror("No space for buffer");
892 return FSFATAL;
893 }
894 p = NULL;
895 } else
896 p = lfbuf;
897 while (1) {
898 if (p)
899 while (p < lfbuf + boot->ClusterSize)
900 if (*p == SLOT_EMPTY
901 || *p == SLOT_DELETED)
902 break;
903 if (p && p < lfbuf + boot->ClusterSize)
904 break;
905 lfcl = p ? fat[lfcl].next : lostDir->head;
906 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
907 /* Extend LOSTDIR? XXX */
908 pwarn("No space in %s\n", LOSTDIR);
909 return FSERROR;
910 }
911 lfoff = lfcl * boot->ClusterSize
912 + boot->ClusterOffset * boot->BytesPerSec;
913 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
914 || read(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
915 perror("could not read LOST.DIR");
916 return FSFATAL;
917 }
918 p = lfbuf;
919 }
920
921 if (!ask(0, "Reconnect"))
922 return FSERROR;
923
924 boot->NumFiles++;
925 /* Ensure uniqueness of entry here! XXX */
926 memset(&d, 0, sizeof d);
927 sprintf(d.name, "%d", head);
928 d.flags = 0;
929 d.head = head;
930 d.size = fat[head].length * boot->ClusterSize;
931
932 memset(p, 0, 32);
933 memset(p, ' ', 11);
934 memcpy(p, d.name, strlen(d.name));
935 p[26] = (u_char)d.head;
936 p[27] = (u_char)(d.head >> 8);
937 p[28] = (u_char)d.size;
938 p[29] = (u_char)(d.size >> 8);
939 p[30] = (u_char)(d.size >> 16);
940 p[31] = (u_char)(d.size >> 24);
941 fat[head].flags |= FAT_USED;
942 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
943 || write(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
944 perror("could not write LOST.DIR");
945 return FSFATAL;
946 }
947 return FSDIRMOD;
948 }
949
950 void
951 finishlf()
952 {
953 if (lfbuf)
954 free(lfbuf);
955 lfbuf = NULL;
956 }
957