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 - 1;
175 np = dir->lname[0] ? dir->lname : dir->name;
177 if ((cp -= nl) <= namebuf + 1)
181 } while ((dir = dir->parent) != NULL);
190 * Calculate a checksum over an 8.3 alias name
193 calcShortSum(u_char *p)
198 for (i = 0; i < 11; i++) {
199 sum = (sum << 7)|(sum >> 1); /* rotate right */
207 * Global variables temporarily used during a directory scan
209 static char longName[DOSLONGNAMELEN] = "";
210 static u_char *buffer = NULL;
211 static u_char *delbuf = NULL;
213 static struct dosDirEntry *rootDir;
214 static struct dosDirEntry *lostDir;
217 * Init internal state for a new directory scan.
220 resetDosDirSection(struct bootblock *boot, struct fatEntry *fat)
226 b1 = boot->bpbRootDirEnts * 32;
227 b2 = boot->bpbSecPerClust * boot->bpbBytesPerSec;
229 if ((buffer = malloc(len = MAX(b1, b2))) == NULL) {
230 perr("No space for directory buffer (%zu)", len);
234 if ((delbuf = malloc(len = b2)) == NULL) {
236 perr("No space for directory delbuf (%zu)", len);
240 if ((rootDir = newDosDirEntry()) == NULL) {
243 perr("No space for directory entry");
247 memset(rootDir, 0, sizeof *rootDir);
248 if (boot->flags & FAT32) {
249 if (boot->bpbRootClust < CLUST_FIRST ||
250 boot->bpbRootClust >= boot->NumClusters) {
251 pfatal("Root directory starts with cluster out of range(%u)",
255 if (fat[boot->bpbRootClust].head != boot->bpbRootClust) {
256 pfatal("Root directory doesn't start a cluster chain");
260 fat[boot->bpbRootClust].flags |= FAT_USED;
261 rootDir->head = boot->bpbRootClust;
268 * Cleanup after a directory scan
271 finishDosDirSection(void)
273 struct dirTodoNode *p, *np;
274 struct dosDirEntry *d, *nd;
276 for (p = pendingDirectories; p; p = np) {
280 pendingDirectories = NULL;
281 for (d = rootDir; d; d = nd) {
282 if ((nd = d->child) != NULL) {
290 rootDir = lostDir = NULL;
298 * Delete directory entries between startcl, startoff and endcl, endoff.
301 delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
302 int startoff, cl_t endcl, int endoff, int notlast)
306 int clsz = boot->bpbSecPerClust * boot->bpbBytesPerSec;
308 s = delbuf + startoff;
310 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
311 if (startcl == endcl) {
316 off = startcl * boot->bpbSecPerClust + boot->ClusterOffset;
317 off *= boot->bpbBytesPerSec;
318 if (lseek(f, off, SEEK_SET) != off) {
319 perr("Unable to lseek to %" PRId64, off);
322 if (read(f, delbuf, clsz) != clsz) {
323 perr("Unable to read directory");
330 if (lseek(f, off, SEEK_SET) != off) {
331 perr("Unable to lseek to %" PRId64, off);
334 if (write(f, delbuf, clsz) != clsz) {
335 perr("Unable to write directory");
338 if (startcl == endcl)
340 startcl = fat[startcl].next;
347 removede(int f, struct bootblock *boot, struct fatEntry *fat, u_char *start,
348 u_char *end, cl_t startcl, cl_t endcl, cl_t curcl, char *path, int type)
352 pwarn("Invalid long filename entry for %s\n", path);
355 pwarn("Invalid long filename entry at end of directory %s\n",
359 pwarn("Invalid long filename entry for volume label\n");
362 if (ask(0, "Remove")) {
363 if (startcl != curcl) {
364 if (delete(f, boot, fat,
365 startcl, start - buffer,
367 endcl == curcl) == FSFATAL)
371 /* startcl is < CLUST_FIRST for !fat32 root */
372 if ((endcl == curcl) || (startcl < CLUST_FIRST))
373 for (; start < end; start += 32)
374 *start = SLOT_DELETED;
381 * Check an in-memory file entry
384 checksize(struct bootblock *boot, struct fatEntry *fat, u_char *p,
385 struct dosDirEntry *dir)
388 * Check size on ordinary files
390 u_int32_t physicalSize;
392 if (dir->head == CLUST_FREE)
395 if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
397 physicalSize = fat[dir->head].length * boot->ClusterSize;
399 if (physicalSize < dir->size) {
400 pwarn("size of %s is %u, should at most be %u\n",
401 fullpath(dir), dir->size, physicalSize);
402 if (ask(1, "Truncate")) {
403 dir->size = physicalSize;
404 p[28] = (u_char)physicalSize;
405 p[29] = (u_char)(physicalSize >> 8);
406 p[30] = (u_char)(physicalSize >> 16);
407 p[31] = (u_char)(physicalSize >> 24);
411 } else if (physicalSize - dir->size >= boot->ClusterSize) {
412 pwarn("%s has too many clusters allocated\n",
414 if (ask(1, "Drop superfluous clusters")) {
418 for (cl = dir->head, len = sz = 0;
419 (sz += boot->ClusterSize) < dir->size; len++)
421 clearchain(boot, fat, fat[cl].next);
422 fat[cl].next = CLUST_EOF;
423 fat[dir->head].length = len;
431 static const u_char dot_name[11] = ". ";
432 static const u_char dotdot_name[11] = ".. ";
435 * Basic sanity check if the subdirectory have good '.' and '..' entries,
436 * and they are directory entries. Further sanity checks are performed
437 * when we traverse into it.
440 check_subdirectory(int f, struct bootblock *boot, struct dosDirEntry *dir)
448 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
452 if (!(boot->flags & FAT32) && !dir->parent) {
453 off = boot->bpbResSectors + boot->bpbFATs *
456 off = cl * boot->bpbSecPerClust + boot->ClusterOffset;
460 * We only need to check the first two entries of the directory,
461 * which is found in the first sector of the directory entry,
462 * so read in only the first sector.
464 buf = malloc(boot->bpbBytesPerSec);
466 perr("No space for directory buffer (%u)",
467 boot->bpbBytesPerSec);
471 off *= boot->bpbBytesPerSec;
472 if (lseek(f, off, SEEK_SET) != off ||
473 read(f, buf, boot->bpbBytesPerSec) != (ssize_t)boot->bpbBytesPerSec) {
474 perr("Unable to read directory");
480 * Both `.' and `..' must be present and be the first two entries
481 * and be ATTR_DIRECTORY of a valid subdirectory.
484 if (memcmp(cp, dot_name, sizeof(dot_name)) != 0 ||
485 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
486 pwarn("%s: Incorrect `.' for %s.\n", __func__, dir->name);
490 if (memcmp(cp, dotdot_name, sizeof(dotdot_name)) != 0 ||
491 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
492 pwarn("%s: Incorrect `..' for %s. \n", __func__, dir->name);
501 * Read a directory and
502 * - resolve long name records
503 * - enter file and directory records into the parent's list
504 * - push directories onto the todo-stack
507 readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
508 struct dosDirEntry *dir)
510 struct dosDirEntry dirent, *d;
511 u_char *p, *vallfn, *invlfn, *empty;
514 cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
519 #define THISMOD 0x8000 /* Only used within this routine */
522 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
524 * Already handled somewhere else.
529 vallfn = invlfn = empty = NULL;
531 if (!(boot->flags & FAT32) && !dir->parent) {
532 last = boot->bpbRootDirEnts * 32;
533 off = boot->bpbResSectors + boot->bpbFATs *
536 last = boot->bpbSecPerClust * boot->bpbBytesPerSec;
537 off = cl * boot->bpbSecPerClust + boot->ClusterOffset;
540 off *= boot->bpbBytesPerSec;
541 if (lseek(f, off, SEEK_SET) != off
542 || read(f, buffer, last) != last) {
543 perr("Unable to read directory");
547 for (p = buffer, i = 0; i < last; i++, p += 32) {
548 if (dir->fsckflags & DIREMPWARN) {
553 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
554 if (*p == SLOT_EMPTY) {
555 dir->fsckflags |= DIREMPTY;
562 if (dir->fsckflags & DIREMPTY) {
563 if (!(dir->fsckflags & DIREMPWARN)) {
564 pwarn("%s has entries after end of directory\n",
566 if (ask(1, "Extend")) {
569 dir->fsckflags &= ~DIREMPTY;
570 if (delete(f, boot, fat,
571 empcl, empty - buffer,
572 cl, p - buffer, 1) == FSFATAL)
574 q = ((empcl == cl) ? empty : buffer);
576 for (; q < p; q += 32)
578 mod |= THISMOD|FSDIRMOD;
579 } else if (ask(0, "Truncate"))
580 dir->fsckflags |= DIREMPWARN;
582 if (dir->fsckflags & DIREMPWARN) {
584 mod |= THISMOD|FSDIRMOD;
586 } else if (dir->fsckflags & DIREMPTY)
591 if (p[11] == ATTR_WIN95) {
593 if (shortSum != -1) {
599 memset(longName, 0, sizeof longName);
603 } else if (shortSum != p[13]
604 || lidx != (*p & LRNOMASK)) {
615 lidx = *p & LRNOMASK;
617 pwarn("invalid long name\n");
625 t = longName + --lidx * 13;
626 for (k = 1; k < 11 && t < longName +
627 sizeof(longName); k += 2) {
628 if (!p[k] && !p[k + 1])
632 * Warn about those unusable chars in msdosfs here? XXX
638 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
639 if (!p[k] && !p[k + 1])
646 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
647 if (!p[k] && !p[k + 1])
653 if (t >= longName + sizeof(longName)) {
654 pwarn("long filename too long\n");
661 if (p[26] | (p[27] << 8)) {
662 pwarn("long filename record cluster start != 0\n");
669 continue; /* long records don't carry further
674 * This is a standard msdosfs directory entry.
676 memset(&dirent, 0, sizeof dirent);
679 * it's a short name record, but we need to know
680 * more, so get the flags first.
682 dirent.flags = p[11];
685 * Translate from 850 to ISO here XXX
687 for (j = 0; j < 8; j++)
688 dirent.name[j] = p[j];
689 dirent.name[8] = '\0';
690 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
691 dirent.name[k] = '\0';
692 if (k < 0 || dirent.name[k] != '\0')
694 if (dirent.name[0] == SLOT_E5)
695 dirent.name[0] = 0xe5;
697 if (dirent.flags & ATTR_VOLUME) {
698 if (vallfn || invlfn) {
699 mod |= removede(f, boot, fat,
700 invlfn ? invlfn : vallfn, p,
701 invlfn ? invcl : valcl, -1, 0,
710 dirent.name[k++] = '.';
711 for (j = 0; j < 3; j++)
712 dirent.name[k++] = p[j+8];
713 dirent.name[k] = '\0';
714 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
715 dirent.name[k] = '\0';
717 if (vallfn && shortSum != calcShortSum(p)) {
724 dirent.head = p[26] | (p[27] << 8);
725 if (boot->ClustMask == CLUST32_MASK)
726 dirent.head |= (p[20] << 16) | (p[21] << 24);
727 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
729 strlcpy(dirent.lname, longName,
730 sizeof(dirent.lname));
736 dirent.next = dir->child;
739 mod |= k = removede(f, boot, fat,
740 invlfn, vallfn ? vallfn : p,
741 invcl, vallfn ? valcl : cl, cl,
742 fullpath(&dirent), 0);
746 ? (valcl == cl && vallfn != buffer)
752 vallfn = NULL; /* not used any longer */
755 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
756 if (dirent.head != 0) {
757 pwarn("%s has clusters, but size 0\n",
759 if (ask(1, "Drop allocated clusters")) {
761 if (boot->ClustMask == CLUST32_MASK)
763 clearchain(boot, fat, dirent.head);
765 mod |= THISMOD|FSDIRMOD|FSFATMOD;
769 } else if (dirent.head == 0
770 && !strcmp(dirent.name, "..")
771 && dir->parent /* XXX */
772 && !dir->parent->parent) {
774 * Do nothing, the parent is the root
776 } else if (dirent.head < CLUST_FIRST
777 || dirent.head >= boot->NumClusters
778 || fat[dirent.head].next == CLUST_FREE
779 || (fat[dirent.head].next >= CLUST_RSRVD
780 && fat[dirent.head].next < CLUST_EOFS)
781 || fat[dirent.head].head != dirent.head) {
782 if (dirent.head == 0)
783 pwarn("%s has no clusters\n",
785 else if (dirent.head < CLUST_FIRST
786 || dirent.head >= boot->NumClusters)
787 pwarn("%s starts with cluster out of range(%u)\n",
790 else if (fat[dirent.head].next == CLUST_FREE)
791 pwarn("%s starts with free cluster\n",
793 else if (fat[dirent.head].next >= CLUST_RSRVD)
794 pwarn("%s starts with cluster marked %s\n",
796 rsrvdcltype(fat[dirent.head].next));
798 pwarn("%s doesn't start a cluster chain\n",
800 if (dirent.flags & ATTR_DIRECTORY) {
801 if (ask(0, "Remove")) {
803 mod |= THISMOD|FSDIRMOD;
808 if (ask(1, "Truncate")) {
809 p[28] = p[29] = p[30] = p[31] = 0;
811 if (boot->ClustMask == CLUST32_MASK)
814 mod |= THISMOD|FSDIRMOD;
820 if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
821 fat[dirent.head].flags |= FAT_USED;
823 if (dirent.flags & ATTR_DIRECTORY) {
825 * gather more info for directories
827 struct dirTodoNode *n;
830 pwarn("Directory %s has size != 0\n",
832 if (ask(1, "Correct")) {
833 p[28] = p[29] = p[30] = p[31] = 0;
835 mod |= THISMOD|FSDIRMOD;
840 * handle `.' and `..' specially
842 if (strcmp(dirent.name, ".") == 0) {
843 if (dirent.head != dir->head) {
844 pwarn("`.' entry in %s has incorrect start cluster\n",
846 if (ask(1, "Correct")) {
847 dirent.head = dir->head;
848 p[26] = (u_char)dirent.head;
849 p[27] = (u_char)(dirent.head >> 8);
850 if (boot->ClustMask == CLUST32_MASK) {
851 p[20] = (u_char)(dirent.head >> 16);
852 p[21] = (u_char)(dirent.head >> 24);
854 mod |= THISMOD|FSDIRMOD;
860 if (strcmp(dirent.name, "..") == 0) {
861 if (dir->parent) { /* XXX */
862 if (!dir->parent->parent) {
864 pwarn("`..' entry in %s has non-zero start cluster\n",
866 if (ask(1, "Correct")) {
869 if (boot->ClustMask == CLUST32_MASK)
871 mod |= THISMOD|FSDIRMOD;
875 } else if (dirent.head != dir->parent->head) {
876 pwarn("`..' entry in %s has incorrect start cluster\n",
878 if (ask(1, "Correct")) {
879 dirent.head = dir->parent->head;
880 p[26] = (u_char)dirent.head;
881 p[27] = (u_char)(dirent.head >> 8);
882 if (boot->ClustMask == CLUST32_MASK) {
883 p[20] = (u_char)(dirent.head >> 16);
884 p[21] = (u_char)(dirent.head >> 24);
886 mod |= THISMOD|FSDIRMOD;
894 * Only one directory entry can point
895 * to dir->head, it's '.'.
897 if (dirent.head == dir->head) {
898 pwarn("%s entry in %s has incorrect start cluster\n",
899 dirent.name, fullpath(dir));
900 if (ask(1, "Remove")) {
902 mod |= THISMOD|FSDIRMOD;
906 } else if ((check_subdirectory(f, boot,
907 &dirent) & FSERROR) == FSERROR) {
909 * A subdirectory should have
910 * a dot (.) entry and a dot-dot
911 * (..) entry of ATTR_DIRECTORY,
912 * we will inspect further when
913 * traversing into it.
915 if (ask(1, "Remove")) {
917 mod |= THISMOD|FSDIRMOD;
924 /* create directory tree node */
925 if (!(d = newDosDirEntry())) {
926 perr("No space for directory");
929 memcpy(d, &dirent, sizeof(struct dosDirEntry));
930 /* link it into the tree */
933 /* Enter this directory into the todo list */
934 if (!(n = newDirTodo())) {
935 perr("No space for todo list");
938 n->next = pendingDirectories;
940 pendingDirectories = n;
942 mod |= k = checksize(boot, fat, p, &dirent);
949 if (!(boot->flags & FAT32) && !dir->parent)
954 if (lseek(f, off, SEEK_SET) != off
955 || write(f, buffer, last) != last) {
956 perr("Unable to write directory");
961 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
962 if (invlfn || vallfn)
963 mod |= removede(f, boot, fat,
964 invlfn ? invlfn : vallfn, p,
965 invlfn ? invcl : valcl, -1, 0,
968 /* The root directory of non fat32 filesystems is in a special
969 * area and may have been modified above without being written out.
971 if ((mod & FSDIRMOD) && !(boot->flags & FAT32) && !dir->parent) {
973 if (lseek(f, off, SEEK_SET) != off
974 || write(f, buffer, last) != last) {
975 perr("Unable to write directory");
980 return mod & ~THISMOD;
984 handleDirTree(int dosfs, struct bootblock *boot, struct fatEntry *fat)
988 mod = readDosDirSection(dosfs, boot, fat, rootDir);
993 * process the directory todo list
995 while (pendingDirectories) {
996 struct dosDirEntry *dir = pendingDirectories->dir;
997 struct dirTodoNode *n = pendingDirectories->next;
1000 * remove TODO entry now, the list might change during
1003 freeDirTodo(pendingDirectories);
1004 pendingDirectories = n;
1007 * handle subdirectory
1009 mod |= readDosDirSection(dosfs, boot, fat, dir);
1018 * Try to reconnect a FAT chain into dir
1020 static u_char *lfbuf;
1025 reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
1027 struct dosDirEntry d;
1031 if (!ask(1, "Reconnect"))
1035 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
1036 if (!strcmp(lostDir->name, LOSTDIR))
1039 if (!lostDir) { /* Create LOSTDIR? XXX */
1040 pwarn("No %s directory\n", LOSTDIR);
1045 lfbuf = malloc(boot->ClusterSize);
1047 perr("No space for buffer");
1055 for (; p < lfbuf + boot->ClusterSize; p += 32)
1056 if (*p == SLOT_EMPTY
1057 || *p == SLOT_DELETED)
1059 if (p && p < lfbuf + boot->ClusterSize)
1061 lfcl = p ? fat[lfcl].next : lostDir->head;
1062 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
1063 /* Extend LOSTDIR? XXX */
1064 pwarn("No space in %s\n", LOSTDIR);
1065 lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
1068 lfoff = lfcl * boot->ClusterSize
1069 + boot->ClusterOffset * boot->bpbBytesPerSec;
1070 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1071 || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1072 perr("could not read LOST.DIR");
1079 /* Ensure uniqueness of entry here! XXX */
1080 memset(&d, 0, sizeof d);
1081 /* worst case -1 = 4294967295, 10 digits */
1082 len = snprintf(d.name, sizeof(d.name), "%u", head);
1085 d.size = fat[head].length * boot->ClusterSize;
1087 memcpy(p, d.name, len);
1088 memset(p + len, ' ', 11 - len);
1089 memset(p + 11, 0, 32 - 11);
1090 p[26] = (u_char)d.head;
1091 p[27] = (u_char)(d.head >> 8);
1092 if (boot->ClustMask == CLUST32_MASK) {
1093 p[20] = (u_char)(d.head >> 16);
1094 p[21] = (u_char)(d.head >> 24);
1096 p[28] = (u_char)d.size;
1097 p[29] = (u_char)(d.size >> 8);
1098 p[30] = (u_char)(d.size >> 16);
1099 p[31] = (u_char)(d.size >> 24);
1100 fat[head].flags |= FAT_USED;
1101 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1102 || (size_t)write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1103 perr("could not write LOST.DIR");