dir.c revision 1.2 1 /* $NetBSD: dir.c,v 1.2 1996/05/25 17:09:44 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.2 1996/05/25 17:09:44 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
54 #define SLOT_EMPTY 0x00 /* slot has never been used */
55 #define SLOT_E5 0x05 /* the real value is 0xe5 */
56 #define SLOT_DELETED 0xe5 /* file in this slot deleted */
57
58 #define ATTR_NORMAL 0x00 /* normal file */
59 #define ATTR_READONLY 0x01 /* file is readonly */
60 #define ATTR_HIDDEN 0x02 /* file is hidden */
61 #define ATTR_SYSTEM 0x04 /* file is a system file */
62 #define ATTR_VOLUME 0x08 /* entry is a volume label */
63 #define ATTR_DIRECTORY 0x10 /* entry is a directory name */
64 #define ATTR_ARCHIVE 0x20 /* file is new or modified */
65
66 #define ATTR_WIN95 0x0f /* long name record */
67
68 /*
69 * This is the format of the contents of the deTime field in the direntry
70 * structure.
71 * We don't use bitfields because we don't know how compilers for
72 * arbitrary machines will lay them out.
73 */
74 #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
75 #define DT_2SECONDS_SHIFT 0
76 #define DT_MINUTES_MASK 0x7E0 /* minutes */
77 #define DT_MINUTES_SHIFT 5
78 #define DT_HOURS_MASK 0xF800 /* hours */
79 #define DT_HOURS_SHIFT 11
80
81 /*
82 * This is the format of the contents of the deDate field in the direntry
83 * structure.
84 */
85 #define DD_DAY_MASK 0x1F /* day of month */
86 #define DD_DAY_SHIFT 0
87 #define DD_MONTH_MASK 0x1E0 /* month */
88 #define DD_MONTH_SHIFT 5
89 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */
90 #define DD_YEAR_SHIFT 9
91
92 /*
93 * Return the full pathname for a directory entry.
94 */
95 static char *
96 fullpath(dir)
97 struct dosDirEntry *dir;
98 {
99 static char namebuf[MAXPATHLEN + 1];
100 char *cp, *np;
101 int nl;
102
103 cp = namebuf + sizeof namebuf - 1;
104 *cp = '\0';
105 do {
106 np = dir->lname[0] ? dir->lname : dir->name;
107 nl = strlen(np);
108 if ((cp -= nl) <= namebuf + 1)
109 break;
110 memcpy(cp, np, nl);
111 *--cp = '/';
112 } while (dir = dir->parent);
113 if (dir->parent)
114 *--cp = '?';
115 return cp;
116 }
117
118 /*
119 * Calculate a checksum over an 8.3 alias name
120 */
121 static u_char
122 calcShortSum(p)
123 u_char *p;
124 {
125 u_char sum = 0;
126 int i;
127
128 for (i = 0; i < 11; i++) {
129 sum = (sum << 7)|(sum >> 1); /* rotate right */
130 sum += p[i];
131 }
132
133 return sum;
134 }
135
136 /*
137 * Global variables temporarily used during a directory scan
138 */
139 static char longName[DOSLONGNAMELEN] = "";
140 static u_char *buffer = NULL;
141 static u_char *delbuf = NULL;
142
143 /*
144 * Init internal state for a new directory scan.
145 */
146 int
147 resetDosDirSection(boot)
148 struct bootblock *boot;
149 {
150 int b1, b2;
151
152 b1 = boot->RootDirEnts * 32;
153 b2 = boot->SecPerClust * boot->BytesPerSec;
154
155 if (!(buffer = malloc(b1 > b2 ? b1 : b2))
156 || !(delbuf = malloc(b2))) {
157 perror("No space for directory");
158 return FSFATAL;
159 }
160 return FSOK;
161 }
162
163 /*
164 * Cleanup after a directory scan
165 */
166 void
167 finishDosDirSection()
168 {
169 free(buffer);
170 free(delbuf);
171 buffer = NULL;
172 delbuf = NULL;
173 }
174
175 /*
176 * Delete directory entries between startcl, startoff and endcl, endoff.
177 */
178 static int
179 delete(f, boot, fat, startcl, startoff, endcl, endoff, notlast)
180 int f;
181 struct bootblock *boot;
182 struct fatEntry *fat;
183 cl_t startcl;
184 int startoff;
185 cl_t endcl;
186 int endoff;
187 int notlast;
188 {
189 u_char *s, *e;
190 off_t off;
191 int clsz = boot->SecPerClust * boot->BytesPerSec;
192
193 s = delbuf + startoff;
194 e = delbuf + clsz;
195 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
196 if (startcl == endcl) {
197 if (notlast)
198 break;
199 e = delbuf + endoff;
200 }
201 off = startcl * boot->SecPerClust + boot->ClusterOffset;
202 off *= boot->BytesPerSec;
203 if (lseek(f, off, SEEK_SET) != off
204 || read(f, delbuf, clsz) != clsz) {
205 perror("Unable to read directory");
206 return FSFATAL;
207 }
208 while (s < e) {
209 *s = SLOT_DELETED;
210 s += 32;
211 }
212 if (lseek(f, off, SEEK_SET) != off
213 || write(f, delbuf, clsz) != clsz) {
214 perror("Unable to write directory");
215 return FSFATAL;
216 }
217 if (startcl == endcl)
218 break;
219 startcl = fat[startcl].next;
220 s = delbuf;
221 }
222 return FSOK;
223 }
224
225 static int
226 removede(f, boot, fat, start, end, startcl, endcl, curcl, path, eof)
227 int f;
228 struct bootblock *boot;
229 struct fatEntry *fat;
230 u_char *start;
231 u_char *end;
232 cl_t startcl;
233 cl_t endcl;
234 cl_t curcl;
235 char *path;
236 int eof;
237 {
238 if (!eof)
239 pwarn("Invalid long filename entry for %s\n", path);
240 else
241 pwarn("Invalid long filename entry at end of directory %s\n", path);
242 if (ask(0, "Remove")) {
243 if (startcl != curcl) {
244 if (delete(f, boot, fat,
245 startcl, start - buffer,
246 endcl, end - buffer,
247 endcl == curcl) == FSFATAL)
248 return FSFATAL;
249 start = buffer;
250 }
251 if (endcl == curcl)
252 for (; start < end; start += 32)
253 *start = SLOT_DELETED;
254 return FSDIRMOD;
255 }
256 return FSERROR;
257 }
258
259 /*
260 * Check an in-memory file entry
261 */
262 static int
263 checksize(boot, fat, p, dir)
264 struct bootblock *boot;
265 struct fatEntry *fat;
266 u_char *p;
267 struct dosDirEntry *dir;
268 {
269 /*
270 * Check size on ordinary files
271 */
272 u_int32_t physicalSize;
273
274 if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
275 return FSERROR;
276 physicalSize = fat[dir->head].length * boot->ClusterSize;
277 if (physicalSize < dir->size) {
278 pwarn("size of %s is %lu, should at most be %lu\n",
279 fullpath(dir), dir->size, physicalSize);
280 if (ask(1, "Truncate")) {
281 dir->size = physicalSize;
282 p[28] = (u_char)physicalSize;
283 p[29] = (u_char)(physicalSize >> 8);
284 p[30] = (u_char)(physicalSize >> 16);
285 p[31] = (u_char)(physicalSize >> 24);
286 return FSDIRMOD;
287 } else
288 return FSERROR;
289 } else if (physicalSize - dir->size >= boot->ClusterSize) {
290 pwarn("%s has too many clusters allocated\n",
291 fullpath(dir));
292 if (ask(1, "Drop superfluous clusters")) {
293 cl_t cl;
294 u_int32_t sz = 0;
295
296 for (cl = dir->head; (sz += boot->ClusterSize) < dir->size;)
297 cl = fat[cl].next;
298 clearchain(boot, fat, fat[cl].next);
299 fat[cl].next = CLUST_EOF;
300 return FSFATMOD;
301 } else
302 return FSERROR;
303 }
304 return FSOK;
305 }
306
307 /*
308 * The stack of unread directories
309 */
310 struct dirTodoNode *pendingDirectories = NULL;
311
312 /*
313 * Read a directory and
314 * - resolve long name records
315 * - enter file and directory records into the parent's list
316 * - push directories onto the todo-stack
317 */
318 int
319 readDosDirSection(f, boot, fat, dir)
320 int f;
321 struct bootblock *boot;
322 struct fatEntry *fat;
323 struct dosDirEntry *dir;
324 {
325 struct dosDirEntry dirent, *d;
326 u_char *p, *vallfn, *invlfn, *empty;
327 off_t off;
328 int i, j, k, last;
329 cl_t cl, valcl, invcl, empcl;
330 char *t;
331 u_int lidx = 0;
332 int shortSum;
333 int mod = FSOK;
334 #define THISMOD 0x8000 /* Only used within this routine */
335
336 cl = dir->head;
337 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
338 /*
339 * Already handled somewhere else.
340 */
341 return FSOK;
342 }
343 shortSum = -1;
344 vallfn = invlfn = empty = NULL;
345 do {
346 if (!dir->parent) {
347 last = boot->RootDirEnts * 32;
348 off = boot->ResSectors + boot->FATs * boot->FATsecs;
349 } else {
350 last = boot->SecPerClust * boot->BytesPerSec;
351 off = cl * boot->SecPerClust + boot->ClusterOffset;
352 }
353
354 off *= boot->BytesPerSec;
355 if (lseek(f, off, SEEK_SET) != off
356 || read(f, buffer, last) != last) {
357 perror("Unable to read directory");
358 return FSFATAL;
359 }
360 last /= 32;
361 /*
362 * Check `.' and `..' entries here? XXX
363 */
364 for (p = buffer, i = 0; i < last; i++, p += 32) {
365 if (dir->fsckflags & DIREMPWARN) {
366 *p = SLOT_EMPTY;
367 continue;
368 }
369
370 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
371 if (*p == SLOT_EMPTY) {
372 dir->fsckflags |= DIREMPTY;
373 empty = p;
374 empcl = cl;
375 }
376 continue;
377 }
378
379 if (dir->fsckflags & DIREMPTY) {
380 if (!(dir->fsckflags & DIREMPWARN)) {
381 pwarn("%s has entries after end of directory\n",
382 fullpath(dir));
383 if (ask(1, "Extend")) {
384 dir->fsckflags &= ~DIREMPTY;
385 if (delete(f, boot, fat,
386 empcl, empty - buffer,
387 cl, p - buffer) == FSFATAL)
388 return FSFATAL;
389 } else if (ask(0, "Truncate"))
390 dir->fsckflags |= DIREMPWARN;
391 }
392 if (dir->fsckflags & DIREMPWARN) {
393 *p = SLOT_DELETED;
394 mod |= THISMOD|FSDIRMOD;
395 continue;
396 } else if (dir->fsckflags & DIREMPTY)
397 mod |= FSERROR;
398 empty = NULL;
399 }
400
401 if (p[11] == ATTR_WIN95) {
402 if (*p & LRFIRST) {
403 if (shortSum != -1) {
404 if (!invlfn) {
405 invlfn = vallfn;
406 invcl = valcl;
407 }
408 }
409 memset(longName, 0, sizeof longName);
410 shortSum = p[13];
411 vallfn = p;
412 valcl = cl;
413 } else if (shortSum != p[13]
414 || lidx != *p & LRNOMASK) {
415 if (!invlfn) {
416 invlfn = vallfn;
417 invcl = valcl;
418 }
419 if (!invlfn) {
420 invlfn = p;
421 invcl = cl;
422 }
423 vallfn = NULL;
424 }
425 lidx = *p & LRNOMASK;
426 t = longName + --lidx * 13;
427 for (k = 1; k < 11 && t < longName + sizeof(longName); k += 2) {
428 if (!p[k] && !p[k + 1])
429 break;
430 *t++ = p[k];
431 /*
432 * Warn about those unusable chars in msdosfs here? XXX
433 */
434 if (p[k + 1])
435 t[-1] = '?';
436 }
437 if (k >= 11)
438 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
439 if (!p[k] && !p[k + 1])
440 break;
441 *t++ = p[k];
442 if (p[k + 1])
443 t[-1] = '?';
444 }
445 if (k >= 26)
446 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
447 if (!p[k] && !p[k + 1])
448 break;
449 *t++ = p[k];
450 if (p[k + 1])
451 t[-1] = '?';
452 }
453 if (t >= longName + sizeof(longName)) {
454 pwarn("long filename too long\n");
455 if (!invlfn) {
456 invlfn = vallfn;
457 invcl = valcl;
458 }
459 vallfn = NULL;
460 }
461 if (p[26] | (p[27] << 8)) {
462 pwarn("long filename record cluster start != 0\n");
463 if (!invlfn) {
464 invlfn = vallfn;
465 invcl = cl;
466 }
467 vallfn = NULL;
468 }
469 continue; /* long records don't carry further
470 * information */
471 }
472
473 /*
474 * This is a standard msdosfs directory entry.
475 */
476 memset(&dirent, 0, sizeof dirent);
477
478 /*
479 * it's a short name record, but we need to know
480 * more, so get the flags first.
481 */
482 dirent.flags = p[11];
483
484 /*
485 * Translate from 850 to ISO here XXX
486 */
487 for (j = 0; j < 8; j++)
488 dirent.name[j] = p[j];
489 dirent.name[8] = '\0';
490 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
491 dirent.name[k] = '\0';
492 if (dirent.name[k] != '\0')
493 k++;
494 if (dirent.name[0] == SLOT_E5)
495 dirent.name[0] = 0xe5;
496 /*
497 * What about volume names with extensions? XXX
498 */
499 if ((dirent.flags & ATTR_VOLUME) == 0 && p[8] != ' ')
500 dirent.name[k++] = '.';
501 for (j = 0; j < 3; j++)
502 dirent.name[k++] = p[j+8];
503 dirent.name[k] = '\0';
504 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
505 dirent.name[k] = '\0';
506
507 if (vallfn && shortSum != calcShortSum(p)) {
508 if (!invlfn) {
509 invlfn = vallfn;
510 invcl = valcl;
511 }
512 vallfn = NULL;
513 }
514 dirent.head = p[26] | (p[27] << 8);
515 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
516 if (vallfn) {
517 strcpy(dirent.lname, longName);
518 longName[0] = '\0';
519 shortSum = -1;
520 }
521
522 if (invlfn) {
523 mod |= k = removede(f, boot, fat,
524 invlfn, vallfn ? vallfn : p,
525 invcl, vallfn ? valcl : cl, cl,
526 fullpath(&dirent), 0);
527 if (mod & FSFATAL)
528 return FSFATAL;
529 if (vallfn
530 ? (valcl == cl && vallfn != buffer)
531 : p != buffer)
532 if (k & FSDIRMOD)
533 mod |= THISMOD;
534 }
535 vallfn = NULL; /* not used any longer */
536 invlfn = NULL;
537
538 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
539 if (dirent.head != 0) {
540 pwarn("%s has clusters, but size 0\n",
541 fullpath(&dirent));
542 if (ask(1, "Drop allocated clusters")) {
543 p[26] = p[27] = 0;
544 clearchain(boot, fat, dirent.head);
545 dirent.head = 0;
546 mod |= THISMOD|FSDIRMOD|FSFATMOD;
547 } else
548 mod |= FSERROR;
549 }
550 } else if (dirent.head == 0
551 && !strcmp(dirent.name, "..")
552 && !dir->parent->parent) {
553 /*
554 * Do nothing, the parent is the root
555 */
556 } else if (dirent.head < CLUST_FIRST
557 || dirent.head >= boot->NumClusters
558 || fat[dirent.head].next == CLUST_FREE
559 || (fat[dirent.head].next >= CLUST_RSRVD
560 && fat[dirent.head].next < CLUST_EOFS)
561 || fat[dirent.head].head != dirent.head) {
562 if (dirent.head == 0)
563 pwarn("%s has no clusters\n",
564 fullpath(&dirent));
565 else if (dirent.head < CLUST_FIRST
566 || dirent.head >= boot->NumClusters)
567 pwarn("%s starts with cluster out of range(%d)\n",
568 fullpath(&dirent),
569 dirent.head);
570 else if (fat[dirent.head].next == CLUST_FREE)
571 pwarn("%s starts with free cluster\n",
572 fullpath(&dirent));
573 else if (fat[dirent.head].next >= CLUST_RSRVD)
574 pwarn("%s starts with %s cluster\n",
575 fullpath(&dirent),
576 rsrvdcltype(fat[dirent.head].next));
577 else
578 pwarn("%s doesn't start a cluster chain\n",
579 fullpath(&dirent));
580 if (dirent.flags & ATTR_DIRECTORY) {
581 if (ask(0, "Remove")) {
582 *p = SLOT_DELETED;
583 mod |= THISMOD|FSDIRMOD;
584 } else
585 mod |= FSERROR;
586 continue;
587 } else {
588 if (ask(1, "Truncate")) {
589 p[28] = p[29] = p[30] = p[31] = 0;
590 dirent.size = 0;
591 mod |= THISMOD|FSDIRMOD;
592 } else
593 mod |= FSERROR;
594 }
595 }
596
597 /* create directory tree node */
598 if (!(d = malloc(sizeof(struct dosDirEntry)))) {
599 perror("No space for directory");
600 return FSFATAL;
601 }
602
603 memcpy(d, &dirent, sizeof(struct dosDirEntry));
604 /* link it into the directory tree */
605 d->parent = dir;
606 d->next = dir->child;
607 dir->child = d;
608 if (d->head >= CLUST_FIRST && d->head < boot->NumClusters)
609 fat[d->head].dirp = d;
610
611 if (d->flags & ATTR_DIRECTORY) {
612 /*
613 * gather more info for directories
614 */
615 struct dirTodoNode * n;
616
617 if (d->size) {
618 pwarn("Directory %s has size != 0\n",
619 fullpath(d));
620 if (ask(1, "Correct")) {
621 p[28] = p[29] = p[30] = p[31] = 0;
622 d->size = 0;
623 mod |= THISMOD|FSDIRMOD;
624 } else
625 mod |= FSERROR;
626 }
627 /*
628 * handle `.' and `..' specially
629 */
630 if (strcmp(d->name, ".") == 0) {
631 if (d->head != dir->head) {
632 pwarn("`.' entry in %s has incorrect start cluster\n",
633 fullpath(dir));
634 if (ask(1, "Correct")) {
635 d->head = dir->head;
636 p[26] = (u_char)d->head;
637 p[27] = (u_char)(d->head >> 8);
638 mod |= THISMOD|FSDIRMOD;
639 } else
640 mod |= FSERROR;
641 }
642 continue;
643 }
644 if (strcmp(d->name, "..") == 0) {
645 if (d->head != dir->parent->head) {
646 pwarn("`..' entry in %s has incorrect start cluster\n",
647 fullpath(dir));
648 if (ask(1, "Correct")) {
649 d->head = dir->parent->head;
650 p[26] = (u_char)d->head;
651 p[27] = (u_char)(d->head >> 8);
652 mod |= THISMOD|FSDIRMOD;
653 } else
654 mod |= FSERROR;
655 }
656 continue;
657 }
658
659 boot->NumFiles++;
660 /* Enter this directory into the todo list */
661 if (!(n = malloc(sizeof(struct dirTodoNode)))) {
662 perror("No space for todo list");
663 return FSFATAL;
664 }
665 n->next = pendingDirectories;
666 n->dir = d;
667 pendingDirectories = n;
668 } else {
669 mod |= k = checksize(boot, fat, p, d);
670 if (k & FSDIRMOD)
671 mod |= THISMOD;
672 boot->NumFiles++;
673 }
674 }
675 if (mod & THISMOD) {
676 last *= 32;
677 if (lseek(f, off, SEEK_SET) != off
678 || write(f, buffer, last) != last) {
679 perror("Unable to write directory");
680 return FSFATAL;
681 }
682 mod &= ~THISMOD;
683 }
684 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
685 if (invlfn || vallfn)
686 mod |= removede(f, boot, fat,
687 invlfn ? invlfn : vallfn, p,
688 invlfn ? invcl : valcl, -1, 0,
689 fullpath(dir), 1);
690 return mod & ~THISMOD;
691 }
692
693 /*
694 * Try to reconnect a FAT chain into dir
695 */
696 static u_char *lfbuf;
697 static cl_t lfcl;
698 static off_t lfoff;
699
700 int
701 reconnect(dosfs, boot, fat, head, dir)
702 int dosfs;
703 struct bootblock *boot;
704 struct fatEntry *fat;
705 cl_t head;
706 struct dosDirEntry *dir;
707 {
708 struct dosDirEntry d;
709 u_char *p;
710
711 if (!dir) /* Create lfdir? XXX */
712 return FSERROR;
713 if (!lfbuf) {
714 lfbuf = malloc(boot->ClusterSize);
715 if (!lfbuf) {
716 perror("No space for buffer");
717 return FSFATAL;
718 }
719 p = NULL;
720 } else
721 p = lfbuf;
722 while (1) {
723 if (p)
724 while (p < lfbuf + boot->ClusterSize)
725 if (*p == SLOT_EMPTY
726 || *p == SLOT_DELETED)
727 break;
728 if (p && p < lfbuf + boot->ClusterSize)
729 break;
730 lfcl = p ? fat[lfcl].next : dir->head;
731 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
732 /* Extend lfdir? XXX */
733 pwarn("No space in %s\n", LOSTDIR);
734 return FSERROR;
735 }
736 lfoff = lfcl * boot->ClusterSize
737 + boot->ClusterOffset * boot->BytesPerSec;
738 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
739 || read(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
740 perror("could not read LOST.DIR");
741 return FSFATAL;
742 }
743 p = lfbuf;
744 }
745
746 if (!ask(0, "Reconnect"))
747 return FSERROR;
748
749 boot->NumFiles++;
750 /* Ensure uniqueness of entry here! XXX */
751 memset(&d, 0, sizeof d);
752 sprintf(d.name, "%d", head);
753 d.flags = 0;
754 d.head = head;
755 d.size = fat[head].length * boot->ClusterSize;
756 d.parent = dir;
757 d.next = dir->child;
758 if (!(dir->child = malloc(sizeof(struct dosDirEntry)))) {
759 perror("No space for directory");
760 return FSFATAL;
761 }
762 memcpy(dir->child, &d, sizeof(struct dosDirEntry));
763
764 memset(p, 0, 32);
765 memset(p, ' ', 11);
766 memcpy(p, dir->name, strlen(dir->name));
767 p[26] = (u_char)dir->head;
768 p[27] = (u_char)(dir->head >> 8);
769 p[28] = (u_char)dir->size;
770 p[29] = (u_char)(dir->size >> 8);
771 p[30] = (u_char)(dir->size >> 16);
772 p[31] = (u_char)(dir->size >> 24);
773 fat[head].dirp = dir;
774 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
775 || write(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
776 perror("could not write LOST.DIR");
777 return FSFATAL;
778 }
779 return FSDIRMOD;
780 }
781
782 void
783 finishlf()
784 {
785 if (lfbuf)
786 free(lfbuf);
787 lfbuf = NULL;
788 }
789