2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
5 * Copyright (c) 1995 Martin Husemann
6 * Some structure declaration borrowed from Paul Popelka
7 * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: dir.c,v 1.20 2006/06/05 16:51:18 christos Exp $");
34 static const char rcsid[] =
47 #include <sys/param.h>
52 #define SLOT_EMPTY 0x00 /* slot has never been used */
53 #define SLOT_E5 0x05 /* the real value is 0xe5 */
54 #define SLOT_DELETED 0xe5 /* file in this slot deleted */
56 #define ATTR_NORMAL 0x00 /* normal file */
57 #define ATTR_READONLY 0x01 /* file is readonly */
58 #define ATTR_HIDDEN 0x02 /* file is hidden */
59 #define ATTR_SYSTEM 0x04 /* file is a system file */
60 #define ATTR_VOLUME 0x08 /* entry is a volume label */
61 #define ATTR_DIRECTORY 0x10 /* entry is a directory name */
62 #define ATTR_ARCHIVE 0x20 /* file is new or modified */
64 #define ATTR_WIN95 0x0f /* long name record */
67 * This is the format of the contents of the deTime field in the direntry
69 * We don't use bitfields because we don't know how compilers for
70 * arbitrary machines will lay them out.
72 #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
73 #define DT_2SECONDS_SHIFT 0
74 #define DT_MINUTES_MASK 0x7E0 /* minutes */
75 #define DT_MINUTES_SHIFT 5
76 #define DT_HOURS_MASK 0xF800 /* hours */
77 #define DT_HOURS_SHIFT 11
80 * This is the format of the contents of the deDate field in the direntry
83 #define DD_DAY_MASK 0x1F /* day of month */
84 #define DD_DAY_SHIFT 0
85 #define DD_MONTH_MASK 0x1E0 /* month */
86 #define DD_MONTH_SHIFT 5
87 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */
88 #define DD_YEAR_SHIFT 9
92 static struct dosDirEntry *newDosDirEntry(void);
93 static void freeDosDirEntry(struct dosDirEntry *);
94 static struct dirTodoNode *newDirTodo(void);
95 static void freeDirTodo(struct dirTodoNode *);
96 static char *fullpath(struct dosDirEntry *);
97 static u_char calcShortSum(u_char *);
98 static int delete(int, struct bootblock *, struct fatEntry *, cl_t, int,
100 static int removede(int, struct bootblock *, struct fatEntry *, u_char *,
101 u_char *, cl_t, cl_t, cl_t, char *, int);
102 static int checksize(struct bootblock *, struct fatEntry *, u_char *,
103 struct dosDirEntry *);
104 static int readDosDirSection(int, struct bootblock *, struct fatEntry *,
105 struct dosDirEntry *);
108 * Manage free dosDirEntry structures.
110 static struct dosDirEntry *freede;
112 static struct dosDirEntry *
115 struct dosDirEntry *de;
117 if (!(de = freede)) {
118 if (!(de = malloc(sizeof *de)))
126 freeDosDirEntry(struct dosDirEntry *de)
133 * The same for dirTodoNode structures.
135 static struct dirTodoNode *freedt;
137 static struct dirTodoNode *
140 struct dirTodoNode *dt;
142 if (!(dt = freedt)) {
143 if (!(dt = malloc(sizeof *dt)))
151 freeDirTodo(struct dirTodoNode *dt)
158 * The stack of unread directories
160 static struct dirTodoNode *pendingDirectories = NULL;
163 * Return the full pathname for a directory entry.
166 fullpath(struct dosDirEntry *dir)
168 static char namebuf[MAXPATHLEN + 1];
172 cp = namebuf + sizeof namebuf;
176 np = dir->lname[0] ? dir->lname : dir->name;
178 if (cp <= namebuf + 1 + nl) {
194 * Calculate a checksum over an 8.3 alias name
197 calcShortSum(u_char *p)
202 for (i = 0; i < 11; i++) {
203 sum = (sum << 7)|(sum >> 1); /* rotate right */
211 * Global variables temporarily used during a directory scan
213 static char longName[DOSLONGNAMELEN] = "";
214 static u_char *buffer = NULL;
215 static u_char *delbuf = NULL;
217 static struct dosDirEntry *rootDir;
218 static struct dosDirEntry *lostDir;
221 * Init internal state for a new directory scan.
224 resetDosDirSection(struct bootblock *boot, struct fatEntry *fat)
230 b1 = boot->bpbRootDirEnts * 32;
231 b2 = boot->bpbSecPerClust * boot->bpbBytesPerSec;
233 if ((buffer = malloc(len = MAX(b1, b2))) == NULL) {
234 perr("No space for directory buffer (%zu)", len);
238 if ((delbuf = malloc(len = b2)) == NULL) {
240 perr("No space for directory delbuf (%zu)", len);
244 if ((rootDir = newDosDirEntry()) == NULL) {
247 perr("No space for directory entry");
251 memset(rootDir, 0, sizeof *rootDir);
252 if (boot->flags & FAT32) {
253 if (boot->bpbRootClust < CLUST_FIRST ||
254 boot->bpbRootClust >= boot->NumClusters) {
255 pfatal("Root directory starts with cluster out of range(%u)",
259 if (fat[boot->bpbRootClust].head != boot->bpbRootClust) {
260 pfatal("Root directory doesn't start a cluster chain");
264 fat[boot->bpbRootClust].flags |= FAT_USED;
265 rootDir->head = boot->bpbRootClust;
272 * Cleanup after a directory scan
275 finishDosDirSection(void)
277 struct dirTodoNode *p, *np;
278 struct dosDirEntry *d, *nd;
280 for (p = pendingDirectories; p; p = np) {
284 pendingDirectories = NULL;
285 for (d = rootDir; d; d = nd) {
286 if ((nd = d->child) != NULL) {
294 rootDir = lostDir = NULL;
302 * Delete directory entries between startcl, startoff and endcl, endoff.
305 delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
306 int startoff, cl_t endcl, int endoff, int notlast)
310 int clsz = boot->bpbSecPerClust * boot->bpbBytesPerSec;
312 s = delbuf + startoff;
314 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
315 if (startcl == endcl) {
320 off = (startcl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
322 off *= boot->bpbBytesPerSec;
323 if (lseek(f, off, SEEK_SET) != off) {
324 perr("Unable to lseek to %" PRId64, off);
327 if (read(f, delbuf, clsz) != clsz) {
328 perr("Unable to read directory");
335 if (lseek(f, off, SEEK_SET) != off) {
336 perr("Unable to lseek to %" PRId64, off);
339 if (write(f, delbuf, clsz) != clsz) {
340 perr("Unable to write directory");
343 if (startcl == endcl)
345 startcl = fat[startcl].next;
352 removede(int f, struct bootblock *boot, struct fatEntry *fat, u_char *start,
353 u_char *end, cl_t startcl, cl_t endcl, cl_t curcl, char *path, int type)
357 pwarn("Invalid long filename entry for %s\n", path);
360 pwarn("Invalid long filename entry at end of directory %s\n",
364 pwarn("Invalid long filename entry for volume label\n");
367 if (ask(0, "Remove")) {
368 if (startcl != curcl) {
369 if (delete(f, boot, fat,
370 startcl, start - buffer,
372 endcl == curcl) == FSFATAL)
376 /* startcl is < CLUST_FIRST for !fat32 root */
377 if ((endcl == curcl) || (startcl < CLUST_FIRST))
378 for (; start < end; start += 32)
379 *start = SLOT_DELETED;
386 * Check an in-memory file entry
389 checksize(struct bootblock *boot, struct fatEntry *fat, u_char *p,
390 struct dosDirEntry *dir)
393 * Check size on ordinary files
395 u_int32_t physicalSize;
397 if (dir->head == CLUST_FREE)
400 if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
402 physicalSize = fat[dir->head].length * boot->ClusterSize;
404 if (physicalSize < dir->size) {
405 pwarn("size of %s is %u, should at most be %u\n",
406 fullpath(dir), dir->size, physicalSize);
407 if (ask(1, "Truncate")) {
408 dir->size = physicalSize;
409 p[28] = (u_char)physicalSize;
410 p[29] = (u_char)(physicalSize >> 8);
411 p[30] = (u_char)(physicalSize >> 16);
412 p[31] = (u_char)(physicalSize >> 24);
416 } else if (physicalSize - dir->size >= boot->ClusterSize) {
417 pwarn("%s has too many clusters allocated\n",
419 if (ask(1, "Drop superfluous clusters")) {
423 for (cl = dir->head, len = sz = 0;
424 (sz += boot->ClusterSize) < dir->size; len++)
426 clearchain(boot, fat, fat[cl].next);
427 fat[cl].next = CLUST_EOF;
428 fat[dir->head].length = len;
436 static const u_char dot_name[11] = ". ";
437 static const u_char dotdot_name[11] = ".. ";
440 * Basic sanity check if the subdirectory have good '.' and '..' entries,
441 * and they are directory entries. Further sanity checks are performed
442 * when we traverse into it.
445 check_subdirectory(int f, struct bootblock *boot, struct dosDirEntry *dir)
453 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
457 if (!(boot->flags & FAT32) && !dir->parent) {
458 off = boot->bpbResSectors + boot->bpbFATs *
461 off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
465 * We only need to check the first two entries of the directory,
466 * which is found in the first sector of the directory entry,
467 * so read in only the first sector.
469 buf = malloc(boot->bpbBytesPerSec);
471 perr("No space for directory buffer (%u)",
472 boot->bpbBytesPerSec);
476 off *= boot->bpbBytesPerSec;
477 if (lseek(f, off, SEEK_SET) != off ||
478 read(f, buf, boot->bpbBytesPerSec) != (ssize_t)boot->bpbBytesPerSec) {
479 perr("Unable to read directory");
485 * Both `.' and `..' must be present and be the first two entries
486 * and be ATTR_DIRECTORY of a valid subdirectory.
489 if (memcmp(cp, dot_name, sizeof(dot_name)) != 0 ||
490 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
491 pwarn("%s: Incorrect `.' for %s.\n", __func__, dir->name);
495 if (memcmp(cp, dotdot_name, sizeof(dotdot_name)) != 0 ||
496 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
497 pwarn("%s: Incorrect `..' for %s. \n", __func__, dir->name);
506 * Read a directory and
507 * - resolve long name records
508 * - enter file and directory records into the parent's list
509 * - push directories onto the todo-stack
512 readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
513 struct dosDirEntry *dir)
515 struct dosDirEntry dirent, *d;
516 u_char *p, *vallfn, *invlfn, *empty;
519 cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
524 #define THISMOD 0x8000 /* Only used within this routine */
527 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
529 * Already handled somewhere else.
534 vallfn = invlfn = empty = NULL;
536 if (!(boot->flags & FAT32) && !dir->parent) {
537 last = boot->bpbRootDirEnts * 32;
538 off = boot->bpbResSectors + boot->bpbFATs *
541 last = boot->bpbSecPerClust * boot->bpbBytesPerSec;
542 off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
545 off *= boot->bpbBytesPerSec;
546 if (lseek(f, off, SEEK_SET) != off
547 || read(f, buffer, last) != last) {
548 perr("Unable to read directory");
552 for (p = buffer, i = 0; i < last; i++, p += 32) {
553 if (dir->fsckflags & DIREMPWARN) {
558 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
559 if (*p == SLOT_EMPTY) {
560 dir->fsckflags |= DIREMPTY;
567 if (dir->fsckflags & DIREMPTY) {
568 if (!(dir->fsckflags & DIREMPWARN)) {
569 pwarn("%s has entries after end of directory\n",
571 if (ask(1, "Extend")) {
574 dir->fsckflags &= ~DIREMPTY;
575 if (delete(f, boot, fat,
576 empcl, empty - buffer,
577 cl, p - buffer, 1) == FSFATAL)
579 q = ((empcl == cl) ? empty : buffer);
581 for (; q < p; q += 32)
583 mod |= THISMOD|FSDIRMOD;
584 } else if (ask(0, "Truncate"))
585 dir->fsckflags |= DIREMPWARN;
587 if (dir->fsckflags & DIREMPWARN) {
589 mod |= THISMOD|FSDIRMOD;
591 } else if (dir->fsckflags & DIREMPTY)
596 if (p[11] == ATTR_WIN95) {
598 if (shortSum != -1) {
604 memset(longName, 0, sizeof longName);
608 } else if (shortSum != p[13]
609 || lidx != (*p & LRNOMASK)) {
620 lidx = *p & LRNOMASK;
622 pwarn("invalid long name\n");
630 t = longName + --lidx * 13;
631 for (k = 1; k < 11 && t < longName +
632 sizeof(longName); k += 2) {
633 if (!p[k] && !p[k + 1])
637 * Warn about those unusable chars in msdosfs here? XXX
643 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
644 if (!p[k] && !p[k + 1])
651 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
652 if (!p[k] && !p[k + 1])
658 if (t >= longName + sizeof(longName)) {
659 pwarn("long filename too long\n");
666 if (p[26] | (p[27] << 8)) {
667 pwarn("long filename record cluster start != 0\n");
674 continue; /* long records don't carry further
679 * This is a standard msdosfs directory entry.
681 memset(&dirent, 0, sizeof dirent);
684 * it's a short name record, but we need to know
685 * more, so get the flags first.
687 dirent.flags = p[11];
690 * Translate from 850 to ISO here XXX
692 for (j = 0; j < 8; j++)
693 dirent.name[j] = p[j];
694 dirent.name[8] = '\0';
695 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
696 dirent.name[k] = '\0';
697 if (k < 0 || dirent.name[k] != '\0')
699 if (dirent.name[0] == SLOT_E5)
700 dirent.name[0] = 0xe5;
702 if (dirent.flags & ATTR_VOLUME) {
703 if (vallfn || invlfn) {
704 mod |= removede(f, boot, fat,
705 invlfn ? invlfn : vallfn, p,
706 invlfn ? invcl : valcl, -1, 0,
715 dirent.name[k++] = '.';
716 for (j = 0; j < 3; j++)
717 dirent.name[k++] = p[j+8];
718 dirent.name[k] = '\0';
719 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
720 dirent.name[k] = '\0';
722 if (vallfn && shortSum != calcShortSum(p)) {
729 dirent.head = p[26] | (p[27] << 8);
730 if (boot->ClustMask == CLUST32_MASK)
731 dirent.head |= (p[20] << 16) | (p[21] << 24);
732 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
734 strlcpy(dirent.lname, longName,
735 sizeof(dirent.lname));
741 dirent.next = dir->child;
744 mod |= k = removede(f, boot, fat,
745 invlfn, vallfn ? vallfn : p,
746 invcl, vallfn ? valcl : cl, cl,
747 fullpath(&dirent), 0);
751 ? (valcl == cl && vallfn != buffer)
757 vallfn = NULL; /* not used any longer */
760 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
761 if (dirent.head != 0) {
762 pwarn("%s has clusters, but size 0\n",
764 if (ask(1, "Drop allocated clusters")) {
766 if (boot->ClustMask == CLUST32_MASK)
768 clearchain(boot, fat, dirent.head);
770 mod |= THISMOD|FSDIRMOD|FSFATMOD;
774 } else if (dirent.head == 0
775 && !strcmp(dirent.name, "..")
776 && dir->parent /* XXX */
777 && !dir->parent->parent) {
779 * Do nothing, the parent is the root
781 } else if (dirent.head < CLUST_FIRST
782 || dirent.head >= boot->NumClusters
783 || fat[dirent.head].next == CLUST_FREE
784 || (fat[dirent.head].next >= CLUST_RSRVD
785 && fat[dirent.head].next < CLUST_EOFS)
786 || fat[dirent.head].head != dirent.head) {
787 if (dirent.head == 0)
788 pwarn("%s has no clusters\n",
790 else if (dirent.head < CLUST_FIRST
791 || dirent.head >= boot->NumClusters)
792 pwarn("%s starts with cluster out of range(%u)\n",
795 else if (fat[dirent.head].next == CLUST_FREE)
796 pwarn("%s starts with free cluster\n",
798 else if (fat[dirent.head].next >= CLUST_RSRVD)
799 pwarn("%s starts with cluster marked %s\n",
801 rsrvdcltype(fat[dirent.head].next));
803 pwarn("%s doesn't start a cluster chain\n",
805 if (dirent.flags & ATTR_DIRECTORY) {
806 if (ask(0, "Remove")) {
808 mod |= THISMOD|FSDIRMOD;
813 if (ask(1, "Truncate")) {
814 p[28] = p[29] = p[30] = p[31] = 0;
816 if (boot->ClustMask == CLUST32_MASK)
819 mod |= THISMOD|FSDIRMOD;
825 if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
826 fat[dirent.head].flags |= FAT_USED;
828 if (dirent.flags & ATTR_DIRECTORY) {
830 * gather more info for directories
832 struct dirTodoNode *n;
835 pwarn("Directory %s has size != 0\n",
837 if (ask(1, "Correct")) {
838 p[28] = p[29] = p[30] = p[31] = 0;
840 mod |= THISMOD|FSDIRMOD;
845 * handle `.' and `..' specially
847 if (strcmp(dirent.name, ".") == 0) {
848 if (dirent.head != dir->head) {
849 pwarn("`.' entry in %s has incorrect start cluster\n",
851 if (ask(1, "Correct")) {
852 dirent.head = dir->head;
853 p[26] = (u_char)dirent.head;
854 p[27] = (u_char)(dirent.head >> 8);
855 if (boot->ClustMask == CLUST32_MASK) {
856 p[20] = (u_char)(dirent.head >> 16);
857 p[21] = (u_char)(dirent.head >> 24);
859 mod |= THISMOD|FSDIRMOD;
865 if (strcmp(dirent.name, "..") == 0) {
866 if (dir->parent) { /* XXX */
867 if (!dir->parent->parent) {
869 pwarn("`..' entry in %s has non-zero start cluster\n",
871 if (ask(1, "Correct")) {
874 if (boot->ClustMask == CLUST32_MASK)
876 mod |= THISMOD|FSDIRMOD;
880 } else if (dirent.head != dir->parent->head) {
881 pwarn("`..' entry in %s has incorrect start cluster\n",
883 if (ask(1, "Correct")) {
884 dirent.head = dir->parent->head;
885 p[26] = (u_char)dirent.head;
886 p[27] = (u_char)(dirent.head >> 8);
887 if (boot->ClustMask == CLUST32_MASK) {
888 p[20] = (u_char)(dirent.head >> 16);
889 p[21] = (u_char)(dirent.head >> 24);
891 mod |= THISMOD|FSDIRMOD;
899 * Only one directory entry can point
900 * to dir->head, it's '.'.
902 if (dirent.head == dir->head) {
903 pwarn("%s entry in %s has incorrect start cluster\n",
904 dirent.name, fullpath(dir));
905 if (ask(1, "Remove")) {
907 mod |= THISMOD|FSDIRMOD;
911 } else if ((check_subdirectory(f, boot,
912 &dirent) & FSERROR) == FSERROR) {
914 * A subdirectory should have
915 * a dot (.) entry and a dot-dot
916 * (..) entry of ATTR_DIRECTORY,
917 * we will inspect further when
918 * traversing into it.
920 if (ask(1, "Remove")) {
922 mod |= THISMOD|FSDIRMOD;
929 /* create directory tree node */
930 if (!(d = newDosDirEntry())) {
931 perr("No space for directory");
934 memcpy(d, &dirent, sizeof(struct dosDirEntry));
935 /* link it into the tree */
938 /* Enter this directory into the todo list */
939 if (!(n = newDirTodo())) {
940 perr("No space for todo list");
943 n->next = pendingDirectories;
945 pendingDirectories = n;
947 mod |= k = checksize(boot, fat, p, &dirent);
954 if (!(boot->flags & FAT32) && !dir->parent)
959 if (lseek(f, off, SEEK_SET) != off
960 || write(f, buffer, last) != last) {
961 perr("Unable to write directory");
966 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
967 if (invlfn || vallfn)
968 mod |= removede(f, boot, fat,
969 invlfn ? invlfn : vallfn, p,
970 invlfn ? invcl : valcl, -1, 0,
973 /* The root directory of non fat32 filesystems is in a special
974 * area and may have been modified above without being written out.
976 if ((mod & FSDIRMOD) && !(boot->flags & FAT32) && !dir->parent) {
978 if (lseek(f, off, SEEK_SET) != off
979 || write(f, buffer, last) != last) {
980 perr("Unable to write directory");
985 return mod & ~THISMOD;
989 handleDirTree(int dosfs, struct bootblock *boot, struct fatEntry *fat)
993 mod = readDosDirSection(dosfs, boot, fat, rootDir);
998 * process the directory todo list
1000 while (pendingDirectories) {
1001 struct dosDirEntry *dir = pendingDirectories->dir;
1002 struct dirTodoNode *n = pendingDirectories->next;
1005 * remove TODO entry now, the list might change during
1008 freeDirTodo(pendingDirectories);
1009 pendingDirectories = n;
1012 * handle subdirectory
1014 mod |= readDosDirSection(dosfs, boot, fat, dir);
1023 * Try to reconnect a FAT chain into dir
1025 static u_char *lfbuf;
1030 reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
1032 struct dosDirEntry d;
1036 if (!ask(1, "Reconnect"))
1040 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
1041 if (!strcmp(lostDir->name, LOSTDIR))
1044 if (!lostDir) { /* Create LOSTDIR? XXX */
1045 pwarn("No %s directory\n", LOSTDIR);
1050 lfbuf = malloc(boot->ClusterSize);
1052 perr("No space for buffer");
1060 for (; p < lfbuf + boot->ClusterSize; p += 32)
1061 if (*p == SLOT_EMPTY
1062 || *p == SLOT_DELETED)
1064 if (p && p < lfbuf + boot->ClusterSize)
1066 lfcl = p ? fat[lfcl].next : lostDir->head;
1067 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
1068 /* Extend LOSTDIR? XXX */
1069 pwarn("No space in %s\n", LOSTDIR);
1070 lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
1073 lfoff = (lfcl - CLUST_FIRST) * boot->ClusterSize
1074 + boot->FirstCluster * boot->bpbBytesPerSec;
1076 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1077 || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1078 perr("could not read LOST.DIR");
1085 /* Ensure uniqueness of entry here! XXX */
1086 memset(&d, 0, sizeof d);
1087 /* worst case -1 = 4294967295, 10 digits */
1088 len = snprintf(d.name, sizeof(d.name), "%u", head);
1091 d.size = fat[head].length * boot->ClusterSize;
1093 memcpy(p, d.name, len);
1094 memset(p + len, ' ', 11 - len);
1095 memset(p + 11, 0, 32 - 11);
1096 p[26] = (u_char)d.head;
1097 p[27] = (u_char)(d.head >> 8);
1098 if (boot->ClustMask == CLUST32_MASK) {
1099 p[20] = (u_char)(d.head >> 16);
1100 p[21] = (u_char)(d.head >> 24);
1102 p[28] = (u_char)d.size;
1103 p[29] = (u_char)(d.size >> 8);
1104 p[30] = (u_char)(d.size >> 16);
1105 p[31] = (u_char)(d.size >> 24);
1106 fat[head].flags |= FAT_USED;
1107 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1108 || (size_t)write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1109 perr("could not write LOST.DIR");