dir.c revision 1.8 1 /* $NetBSD: dir.c,v 1.8 1996/09/27 23:22:52 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.8 1996/09/27 23:22:52 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 "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 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 u_char *q;
489
490 dir->fsckflags &= ~DIREMPTY;
491 if (delete(f, boot, fat,
492 empcl, empty - buffer,
493 cl, p - buffer, 1) == FSFATAL)
494 return FSFATAL;
495 q = empcl == cl ? empty : buffer;
496 for (; q < p; q += 32)
497 *q = SLOT_DELETED;
498 mod |= THISMOD|FSDIRMOD;
499 } else if (ask(0, "Truncate"))
500 dir->fsckflags |= DIREMPWARN;
501 }
502 if (dir->fsckflags & DIREMPWARN) {
503 *p = SLOT_DELETED;
504 mod |= THISMOD|FSDIRMOD;
505 continue;
506 } else if (dir->fsckflags & DIREMPTY)
507 mod |= FSERROR;
508 empty = NULL;
509 }
510
511 if (p[11] == ATTR_WIN95) {
512 if (*p & LRFIRST) {
513 if (shortSum != -1) {
514 if (!invlfn) {
515 invlfn = vallfn;
516 invcl = valcl;
517 }
518 }
519 memset(longName, 0, sizeof longName);
520 shortSum = p[13];
521 vallfn = p;
522 valcl = cl;
523 } else if (shortSum != p[13]
524 || lidx != (*p & LRNOMASK)) {
525 if (!invlfn) {
526 invlfn = vallfn;
527 invcl = valcl;
528 }
529 if (!invlfn) {
530 invlfn = p;
531 invcl = cl;
532 }
533 vallfn = NULL;
534 }
535 lidx = *p & LRNOMASK;
536 t = longName + --lidx * 13;
537 for (k = 1; k < 11 && t < longName + sizeof(longName); k += 2) {
538 if (!p[k] && !p[k + 1])
539 break;
540 *t++ = p[k];
541 /*
542 * Warn about those unusable chars in msdosfs here? XXX
543 */
544 if (p[k + 1])
545 t[-1] = '?';
546 }
547 if (k >= 11)
548 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
549 if (!p[k] && !p[k + 1])
550 break;
551 *t++ = p[k];
552 if (p[k + 1])
553 t[-1] = '?';
554 }
555 if (k >= 26)
556 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
557 if (!p[k] && !p[k + 1])
558 break;
559 *t++ = p[k];
560 if (p[k + 1])
561 t[-1] = '?';
562 }
563 if (t >= longName + sizeof(longName)) {
564 pwarn("long filename too long\n");
565 if (!invlfn) {
566 invlfn = vallfn;
567 invcl = valcl;
568 }
569 vallfn = NULL;
570 }
571 if (p[26] | (p[27] << 8)) {
572 pwarn("long filename record cluster start != 0\n");
573 if (!invlfn) {
574 invlfn = vallfn;
575 invcl = cl;
576 }
577 vallfn = NULL;
578 }
579 continue; /* long records don't carry further
580 * information */
581 }
582
583 /*
584 * This is a standard msdosfs directory entry.
585 */
586 memset(&dirent, 0, sizeof dirent);
587
588 /*
589 * it's a short name record, but we need to know
590 * more, so get the flags first.
591 */
592 dirent.flags = p[11];
593
594 /*
595 * Translate from 850 to ISO here XXX
596 */
597 for (j = 0; j < 8; j++)
598 dirent.name[j] = p[j];
599 dirent.name[8] = '\0';
600 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
601 dirent.name[k] = '\0';
602 if (dirent.name[k] != '\0')
603 k++;
604 if (dirent.name[0] == SLOT_E5)
605 dirent.name[0] = 0xe5;
606
607 if (dirent.flags & ATTR_VOLUME) {
608 if (vallfn || invlfn) {
609 mod |= removede(f, boot, fat,
610 invlfn ? invlfn : vallfn, p,
611 invlfn ? invcl : valcl, -1, 0,
612 fullpath(dir), 2);
613 vallfn = NULL;
614 invlfn = NULL;
615 }
616 continue;
617 }
618
619 if (p[8] != ' ')
620 dirent.name[k++] = '.';
621 for (j = 0; j < 3; j++)
622 dirent.name[k++] = p[j+8];
623 dirent.name[k] = '\0';
624 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
625 dirent.name[k] = '\0';
626
627 if (vallfn && shortSum != calcShortSum(p)) {
628 if (!invlfn) {
629 invlfn = vallfn;
630 invcl = valcl;
631 }
632 vallfn = NULL;
633 }
634 dirent.head = p[26] | (p[27] << 8);
635 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
636 if (vallfn) {
637 strcpy(dirent.lname, longName);
638 longName[0] = '\0';
639 shortSum = -1;
640 }
641
642 if (invlfn) {
643 mod |= k = removede(f, boot, fat,
644 invlfn, vallfn ? vallfn : p,
645 invcl, vallfn ? valcl : cl, cl,
646 fullpath(&dirent), 0);
647 if (mod & FSFATAL)
648 return FSFATAL;
649 if (vallfn
650 ? (valcl == cl && vallfn != buffer)
651 : p != buffer)
652 if (k & FSDIRMOD)
653 mod |= THISMOD;
654 }
655 vallfn = NULL; /* not used any longer */
656 invlfn = NULL;
657
658 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
659 if (dirent.head != 0) {
660 pwarn("%s has clusters, but size 0\n",
661 fullpath(&dirent));
662 if (ask(1, "Drop allocated clusters")) {
663 p[26] = p[27] = 0;
664 clearchain(boot, fat, dirent.head);
665 dirent.head = 0;
666 mod |= THISMOD|FSDIRMOD|FSFATMOD;
667 } else
668 mod |= FSERROR;
669 }
670 } else if (dirent.head == 0
671 && !strcmp(dirent.name, "..")
672 && dir->parent /* XXX */
673 && !dir->parent->parent) {
674 /*
675 * Do nothing, the parent is the root
676 */
677 } else if (dirent.head < CLUST_FIRST
678 || dirent.head >= boot->NumClusters
679 || fat[dirent.head].next == CLUST_FREE
680 || (fat[dirent.head].next >= CLUST_RSRVD
681 && fat[dirent.head].next < CLUST_EOFS)
682 || fat[dirent.head].head != dirent.head) {
683 if (dirent.head == 0)
684 pwarn("%s has no clusters\n",
685 fullpath(&dirent));
686 else if (dirent.head < CLUST_FIRST
687 || dirent.head >= boot->NumClusters)
688 pwarn("%s starts with cluster out of range(%d)\n",
689 fullpath(&dirent),
690 dirent.head);
691 else if (fat[dirent.head].next == CLUST_FREE)
692 pwarn("%s starts with free cluster\n",
693 fullpath(&dirent));
694 else if (fat[dirent.head].next >= CLUST_RSRVD)
695 pwarn("%s starts with %s cluster\n",
696 fullpath(&dirent),
697 rsrvdcltype(fat[dirent.head].next));
698 else
699 pwarn("%s doesn't start a cluster chain\n",
700 fullpath(&dirent));
701 if (dirent.flags & ATTR_DIRECTORY) {
702 if (ask(0, "Remove")) {
703 *p = SLOT_DELETED;
704 mod |= THISMOD|FSDIRMOD;
705 } else
706 mod |= FSERROR;
707 continue;
708 } else {
709 if (ask(1, "Truncate")) {
710 p[28] = p[29] = p[30] = p[31] = 0;
711 dirent.size = 0;
712 mod |= THISMOD|FSDIRMOD;
713 } else
714 mod |= FSERROR;
715 }
716 }
717
718 dirent.parent = dir;
719 dirent.next = dir->child;
720 if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
721 fat[dirent.head].flags |= FAT_USED;
722
723 if (dirent.flags & ATTR_DIRECTORY) {
724 /*
725 * gather more info for directories
726 */
727 struct dirTodoNode * n;
728
729 if (dirent.size) {
730 pwarn("Directory %s has size != 0\n",
731 fullpath(&dirent));
732 if (ask(1, "Correct")) {
733 p[28] = p[29] = p[30] = p[31] = 0;
734 dirent.size = 0;
735 mod |= THISMOD|FSDIRMOD;
736 } else
737 mod |= FSERROR;
738 }
739 /*
740 * handle `.' and `..' specially
741 */
742 if (strcmp(dirent.name, ".") == 0) {
743 if (dirent.head != dir->head) {
744 pwarn("`.' entry in %s has incorrect start cluster\n",
745 fullpath(dir));
746 if (ask(1, "Correct")) {
747 dirent.head = dir->head;
748 p[26] = (u_char)dirent.head;
749 p[27] = (u_char)(dirent.head >> 8);
750 mod |= THISMOD|FSDIRMOD;
751 } else
752 mod |= FSERROR;
753 }
754 continue;
755 }
756 if (strcmp(dirent.name, "..") == 0) {
757 if (dir->parent /* XXX */
758 && dirent.head != dir->parent->head) {
759 pwarn("`..' entry in %s has incorrect start cluster\n",
760 fullpath(dir));
761 if (ask(1, "Correct")) {
762 dirent.head = dir->parent->head;
763 p[26] = (u_char)dirent.head;
764 p[27] = (u_char)(dirent.head >> 8);
765 mod |= THISMOD|FSDIRMOD;
766 } else
767 mod |= FSERROR;
768 }
769 continue;
770 }
771
772 boot->NumFiles++;
773
774 /* create directory tree node */
775 if (!(d = newDosDirEntry())) {
776 perror("No space for directory");
777 return FSFATAL;
778 }
779
780 memcpy(d, &dirent, sizeof(struct dosDirEntry));
781 /* link it into the tree */
782 dir->child = d;
783
784 /* Enter this directory into the todo list */
785 if (!(n = newDirTodo())) {
786 perror("No space for todo list");
787 return FSFATAL;
788 }
789 n->next = pendingDirectories;
790 n->dir = d;
791 pendingDirectories = n;
792 } else {
793 mod |= k = checksize(boot, fat, p, &dirent);
794 if (k & FSDIRMOD)
795 mod |= THISMOD;
796 boot->NumFiles++;
797 }
798 }
799 if (mod & THISMOD) {
800 last *= 32;
801 if (lseek(f, off, SEEK_SET) != off
802 || write(f, buffer, last) != last) {
803 perror("Unable to write directory");
804 return FSFATAL;
805 }
806 mod &= ~THISMOD;
807 }
808 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
809 if (invlfn || vallfn)
810 mod |= removede(f, boot, fat,
811 invlfn ? invlfn : vallfn, p,
812 invlfn ? invcl : valcl, -1, 0,
813 fullpath(dir), 1);
814 return mod & ~THISMOD;
815 }
816
817 int
818 handleDirTree(dosfs, boot, fat)
819 int dosfs;
820 struct bootblock *boot;
821 struct fatEntry *fat;
822 {
823 int mod;
824
825 mod = readDosDirSection(dosfs, boot, fat, rootDir);
826 if (mod & FSFATAL)
827 return FSFATAL;
828
829 if (mod & FSFATMOD) {
830 mod &= ~FSFATMOD;
831 mod |= writefat(dosfs, boot, fat); /* delay writing fats? XXX */
832 }
833
834 if (mod & FSFATAL)
835 return FSFATAL;
836
837 /*
838 * process the directory todo list
839 */
840 while (pendingDirectories) {
841 struct dosDirEntry *dir = pendingDirectories->dir;
842 struct dirTodoNode *n = pendingDirectories->next;
843
844 /*
845 * remove TODO entry now, the list might change during
846 * directory reads
847 */
848 freeDirTodo(pendingDirectories);
849 pendingDirectories = n;
850
851 /*
852 * handle subdirectory
853 */
854 mod |= readDosDirSection(dosfs, boot, fat, dir);
855 if (mod & FSFATAL)
856 return FSFATAL;
857 if (mod & FSFATMOD) {
858 mod &= ~FSFATMOD;
859 mod |= writefat(dosfs, boot, fat); /* delay writing fats? XXX */
860 }
861 if (mod & FSFATAL)
862 return FSFATAL;
863 }
864 return mod;
865 }
866
867 /*
868 * Try to reconnect a FAT chain into dir
869 */
870 static u_char *lfbuf;
871 static cl_t lfcl;
872 static off_t lfoff;
873
874 int
875 reconnect(dosfs, boot, fat, head)
876 int dosfs;
877 struct bootblock *boot;
878 struct fatEntry *fat;
879 cl_t head;
880 {
881 struct dosDirEntry d;
882 u_char *p;
883
884 if (!lostDir) {
885 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
886 if (!strcmp(lostDir->name, LOSTDIR))
887 break;
888 }
889 if (!lostDir) { /* Create LOSTDIR? XXX */
890 pwarn("No %s directory\n", LOSTDIR);
891 return FSERROR;
892 }
893 }
894 if (!lfbuf) {
895 lfbuf = malloc(boot->ClusterSize);
896 if (!lfbuf) {
897 perror("No space for buffer");
898 return FSFATAL;
899 }
900 p = NULL;
901 } else
902 p = lfbuf;
903 while (1) {
904 if (p)
905 while (p < lfbuf + boot->ClusterSize)
906 if (*p == SLOT_EMPTY
907 || *p == SLOT_DELETED)
908 break;
909 if (p && p < lfbuf + boot->ClusterSize)
910 break;
911 lfcl = p ? fat[lfcl].next : lostDir->head;
912 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
913 /* Extend LOSTDIR? XXX */
914 pwarn("No space in %s\n", LOSTDIR);
915 return FSERROR;
916 }
917 lfoff = lfcl * boot->ClusterSize
918 + boot->ClusterOffset * boot->BytesPerSec;
919 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
920 || read(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
921 perror("could not read LOST.DIR");
922 return FSFATAL;
923 }
924 p = lfbuf;
925 }
926
927 if (!ask(0, "Reconnect"))
928 return FSERROR;
929
930 boot->NumFiles++;
931 /* Ensure uniqueness of entry here! XXX */
932 memset(&d, 0, sizeof d);
933 sprintf(d.name, "%d", head);
934 d.flags = 0;
935 d.head = head;
936 d.size = fat[head].length * boot->ClusterSize;
937
938 memset(p, 0, 32);
939 memset(p, ' ', 11);
940 memcpy(p, d.name, strlen(d.name));
941 p[26] = (u_char)d.head;
942 p[27] = (u_char)(d.head >> 8);
943 p[28] = (u_char)d.size;
944 p[29] = (u_char)(d.size >> 8);
945 p[30] = (u_char)(d.size >> 16);
946 p[31] = (u_char)(d.size >> 24);
947 fat[head].flags |= FAT_USED;
948 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
949 || write(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
950 perror("could not write LOST.DIR");
951 return FSFATAL;
952 }
953 return FSDIRMOD;
954 }
955
956 void
957 finishlf()
958 {
959 if (lfbuf)
960 free(lfbuf);
961 lfbuf = NULL;
962 }
963