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.
39 #include <sys/param.h>
44 #define SLOT_EMPTY 0x00 /* slot has never been used */
45 #define SLOT_E5 0x05 /* the real value is 0xe5 */
46 #define SLOT_DELETED 0xe5 /* file in this slot deleted */
48 #define ATTR_NORMAL 0x00 /* normal file */
49 #define ATTR_READONLY 0x01 /* file is readonly */
50 #define ATTR_HIDDEN 0x02 /* file is hidden */
51 #define ATTR_SYSTEM 0x04 /* file is a system file */
52 #define ATTR_VOLUME 0x08 /* entry is a volume label */
53 #define ATTR_DIRECTORY 0x10 /* entry is a directory name */
54 #define ATTR_ARCHIVE 0x20 /* file is new or modified */
56 #define ATTR_WIN95 0x0f /* long name record */
59 * This is the format of the contents of the deTime field in the direntry
61 * We don't use bitfields because we don't know how compilers for
62 * arbitrary machines will lay them out.
64 #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
65 #define DT_2SECONDS_SHIFT 0
66 #define DT_MINUTES_MASK 0x7E0 /* minutes */
67 #define DT_MINUTES_SHIFT 5
68 #define DT_HOURS_MASK 0xF800 /* hours */
69 #define DT_HOURS_SHIFT 11
72 * This is the format of the contents of the deDate field in the direntry
75 #define DD_DAY_MASK 0x1F /* day of month */
76 #define DD_DAY_SHIFT 0
77 #define DD_MONTH_MASK 0x1E0 /* month */
78 #define DD_MONTH_SHIFT 5
79 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */
80 #define DD_YEAR_SHIFT 9
84 static struct dosDirEntry *newDosDirEntry(void);
85 static void freeDosDirEntry(struct dosDirEntry *);
86 static struct dirTodoNode *newDirTodo(void);
87 static void freeDirTodo(struct dirTodoNode *);
88 static char *fullpath(struct dosDirEntry *);
89 static u_char calcShortSum(u_char *);
90 static int delete(int, struct bootblock *, struct fatEntry *, cl_t, int,
92 static int removede(int, struct bootblock *, struct fatEntry *, u_char *,
93 u_char *, cl_t, cl_t, cl_t, char *, int);
94 static int checksize(struct bootblock *, struct fatEntry *, u_char *,
95 struct dosDirEntry *);
96 static int readDosDirSection(int, struct bootblock *, struct fatEntry *,
97 struct dosDirEntry *);
100 * Manage free dosDirEntry structures.
102 static struct dosDirEntry *freede;
104 static struct dosDirEntry *
107 struct dosDirEntry *de;
109 if (!(de = freede)) {
110 if (!(de = malloc(sizeof(*de))))
118 freeDosDirEntry(struct dosDirEntry *de)
125 * The same for dirTodoNode structures.
127 static struct dirTodoNode *freedt;
129 static struct dirTodoNode *
132 struct dirTodoNode *dt;
134 if (!(dt = freedt)) {
135 if (!(dt = malloc(sizeof(*dt))))
143 freeDirTodo(struct dirTodoNode *dt)
150 * The stack of unread directories
152 static struct dirTodoNode *pendingDirectories = NULL;
155 * Return the full pathname for a directory entry.
158 fullpath(struct dosDirEntry *dir)
160 static char namebuf[MAXPATHLEN + 1];
164 cp = namebuf + sizeof namebuf;
168 np = dir->lname[0] ? dir->lname : dir->name;
170 if (cp <= namebuf + 1 + nl) {
186 * Calculate a checksum over an 8.3 alias name
189 calcShortSum(u_char *p)
194 for (i = 0; i < 11; i++) {
195 sum = (sum << 7)|(sum >> 1); /* rotate right */
203 * Global variables temporarily used during a directory scan
205 static char longName[DOSLONGNAMELEN] = "";
206 static u_char *buffer = NULL;
207 static u_char *delbuf = NULL;
209 static struct dosDirEntry *rootDir;
210 static struct dosDirEntry *lostDir;
213 * Init internal state for a new directory scan.
216 resetDosDirSection(struct bootblock *boot, struct fatEntry *fat)
222 b1 = boot->bpbRootDirEnts * 32;
223 b2 = boot->bpbSecPerClust * boot->bpbBytesPerSec;
225 if ((buffer = malloc(len = MAX(b1, b2))) == NULL) {
226 perr("No space for directory buffer (%zu)", len);
230 if ((delbuf = malloc(len = b2)) == NULL) {
232 perr("No space for directory delbuf (%zu)", len);
236 if ((rootDir = newDosDirEntry()) == NULL) {
239 perr("No space for directory entry");
243 memset(rootDir, 0, sizeof *rootDir);
244 if (boot->flags & FAT32) {
245 if (boot->bpbRootClust < CLUST_FIRST ||
246 boot->bpbRootClust >= boot->NumClusters) {
247 pfatal("Root directory starts with cluster out of range(%u)",
251 if (fat[boot->bpbRootClust].head != boot->bpbRootClust) {
252 pfatal("Root directory doesn't start a cluster chain");
256 fat[boot->bpbRootClust].flags |= FAT_USED;
257 rootDir->head = boot->bpbRootClust;
264 * Cleanup after a directory scan
267 finishDosDirSection(void)
269 struct dirTodoNode *p, *np;
270 struct dosDirEntry *d, *nd;
272 for (p = pendingDirectories; p; p = np) {
276 pendingDirectories = NULL;
277 for (d = rootDir; d; d = nd) {
278 if ((nd = d->child) != NULL) {
286 rootDir = lostDir = NULL;
294 * Delete directory entries between startcl, startoff and endcl, endoff.
297 delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
298 int startoff, cl_t endcl, int endoff, int notlast)
302 int clsz = boot->bpbSecPerClust * boot->bpbBytesPerSec;
304 s = delbuf + startoff;
306 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
307 if (startcl == endcl) {
312 off = startcl * boot->bpbSecPerClust + boot->ClusterOffset;
313 off *= boot->bpbBytesPerSec;
314 if (lseek(f, off, SEEK_SET) != off) {
315 perr("Unable to lseek to %" PRId64, off);
318 if (read(f, delbuf, clsz) != clsz) {
319 perr("Unable to read directory");
326 if (lseek(f, off, SEEK_SET) != off) {
327 perr("Unable to lseek to %" PRId64, off);
330 if (write(f, delbuf, clsz) != clsz) {
331 perr("Unable to write directory");
334 if (startcl == endcl)
336 startcl = fat[startcl].next;
343 removede(int f, struct bootblock *boot, struct fatEntry *fat, u_char *start,
344 u_char *end, cl_t startcl, cl_t endcl, cl_t curcl, char *path, int type)
348 pwarn("Invalid long filename entry for %s\n", path);
351 pwarn("Invalid long filename entry at end of directory %s\n",
355 pwarn("Invalid long filename entry for volume label\n");
358 if (ask(0, "Remove")) {
359 if (startcl != curcl) {
360 if (delete(f, boot, fat,
361 startcl, start - buffer,
363 endcl == curcl) == FSFATAL)
367 /* startcl is < CLUST_FIRST for !fat32 root */
368 if ((endcl == curcl) || (startcl < CLUST_FIRST))
369 for (; start < end; start += 32)
370 *start = SLOT_DELETED;
377 * Check an in-memory file entry
380 checksize(struct bootblock *boot, struct fatEntry *fat, u_char *p,
381 struct dosDirEntry *dir)
384 * Check size on ordinary files
386 u_int32_t physicalSize;
388 if (dir->head == CLUST_FREE)
391 if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
393 physicalSize = fat[dir->head].length * boot->ClusterSize;
395 if (physicalSize < dir->size) {
396 pwarn("size of %s is %u, should at most be %u\n",
397 fullpath(dir), dir->size, physicalSize);
398 if (ask(1, "Truncate")) {
399 dir->size = physicalSize;
400 p[28] = (u_char)physicalSize;
401 p[29] = (u_char)(physicalSize >> 8);
402 p[30] = (u_char)(physicalSize >> 16);
403 p[31] = (u_char)(physicalSize >> 24);
407 } else if (physicalSize - dir->size >= boot->ClusterSize) {
408 pwarn("%s has too many clusters allocated\n",
410 if (ask(1, "Drop superfluous clusters")) {
414 for (cl = dir->head, len = sz = 0;
415 (sz += boot->ClusterSize) < dir->size; len++)
417 clearchain(boot, fat, fat[cl].next);
418 fat[cl].next = CLUST_EOF;
419 fat[dir->head].length = len;
427 static const u_char dot_name[11] = ". ";
428 static const u_char dotdot_name[11] = ".. ";
431 * Basic sanity check if the subdirectory have good '.' and '..' entries,
432 * and they are directory entries. Further sanity checks are performed
433 * when we traverse into it.
436 check_subdirectory(int f, struct bootblock *boot, struct dosDirEntry *dir)
444 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
448 if (!(boot->flags & FAT32) && !dir->parent) {
449 off = boot->bpbResSectors + boot->bpbFATs *
452 off = cl * boot->bpbSecPerClust + boot->ClusterOffset;
456 * We only need to check the first two entries of the directory,
457 * which is found in the first sector of the directory entry,
458 * so read in only the first sector.
460 buf = malloc(boot->bpbBytesPerSec);
462 perr("No space for directory buffer (%u)",
463 boot->bpbBytesPerSec);
467 off *= boot->bpbBytesPerSec;
468 if (lseek(f, off, SEEK_SET) != off ||
469 read(f, buf, boot->bpbBytesPerSec) != (ssize_t)boot->bpbBytesPerSec) {
470 perr("Unable to read directory");
476 * Both `.' and `..' must be present and be the first two entries
477 * and be ATTR_DIRECTORY of a valid subdirectory.
480 if (memcmp(cp, dot_name, sizeof(dot_name)) != 0 ||
481 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
482 pwarn("%s: Incorrect `.' for %s.\n", __func__, dir->name);
486 if (memcmp(cp, dotdot_name, sizeof(dotdot_name)) != 0 ||
487 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
488 pwarn("%s: Incorrect `..' for %s. \n", __func__, dir->name);
497 * Read a directory and
498 * - resolve long name records
499 * - enter file and directory records into the parent's list
500 * - push directories onto the todo-stack
503 readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
504 struct dosDirEntry *dir)
506 struct dosDirEntry dirent, *d;
507 u_char *p, *vallfn, *invlfn, *empty;
510 cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
515 #define THISMOD 0x8000 /* Only used within this routine */
518 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
520 * Already handled somewhere else.
525 vallfn = invlfn = empty = NULL;
527 if (!(boot->flags & FAT32) && !dir->parent) {
528 last = boot->bpbRootDirEnts * 32;
529 off = boot->bpbResSectors + boot->bpbFATs *
532 last = boot->bpbSecPerClust * boot->bpbBytesPerSec;
533 off = cl * boot->bpbSecPerClust + boot->ClusterOffset;
536 off *= boot->bpbBytesPerSec;
537 if (lseek(f, off, SEEK_SET) != off
538 || read(f, buffer, last) != last) {
539 perr("Unable to read directory");
543 for (p = buffer, i = 0; i < last; i++, p += 32) {
544 if (dir->fsckflags & DIREMPWARN) {
549 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
550 if (*p == SLOT_EMPTY) {
551 dir->fsckflags |= DIREMPTY;
558 if (dir->fsckflags & DIREMPTY) {
559 if (!(dir->fsckflags & DIREMPWARN)) {
560 pwarn("%s has entries after end of directory\n",
562 if (ask(1, "Extend")) {
565 dir->fsckflags &= ~DIREMPTY;
566 if (delete(f, boot, fat,
567 empcl, empty - buffer,
568 cl, p - buffer, 1) == FSFATAL)
570 q = ((empcl == cl) ? empty : buffer);
572 for (; q < p; q += 32)
574 mod |= THISMOD|FSDIRMOD;
575 } else if (ask(0, "Truncate"))
576 dir->fsckflags |= DIREMPWARN;
578 if (dir->fsckflags & DIREMPWARN) {
580 mod |= THISMOD|FSDIRMOD;
582 } else if (dir->fsckflags & DIREMPTY)
587 if (p[11] == ATTR_WIN95) {
589 if (shortSum != -1) {
595 memset(longName, 0, sizeof longName);
599 } else if (shortSum != p[13]
600 || lidx != (*p & LRNOMASK)) {
611 lidx = *p & LRNOMASK;
613 pwarn("invalid long name\n");
621 t = longName + --lidx * 13;
622 for (k = 1; k < 11 && t < longName +
623 sizeof(longName); k += 2) {
624 if (!p[k] && !p[k + 1])
628 * Warn about those unusable chars in msdosfs here? XXX
634 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
635 if (!p[k] && !p[k + 1])
642 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
643 if (!p[k] && !p[k + 1])
649 if (t >= longName + sizeof(longName)) {
650 pwarn("long filename too long\n");
657 if (p[26] | (p[27] << 8)) {
658 pwarn("long filename record cluster start != 0\n");
665 continue; /* long records don't carry further
670 * This is a standard msdosfs directory entry.
672 memset(&dirent, 0, sizeof dirent);
675 * it's a short name record, but we need to know
676 * more, so get the flags first.
678 dirent.flags = p[11];
681 * Translate from 850 to ISO here XXX
683 for (j = 0; j < 8; j++)
684 dirent.name[j] = p[j];
685 dirent.name[8] = '\0';
686 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
687 dirent.name[k] = '\0';
688 if (k < 0 || dirent.name[k] != '\0')
690 if (dirent.name[0] == SLOT_E5)
691 dirent.name[0] = 0xe5;
693 if (dirent.flags & ATTR_VOLUME) {
694 if (vallfn || invlfn) {
695 mod |= removede(f, boot, fat,
696 invlfn ? invlfn : vallfn, p,
697 invlfn ? invcl : valcl, -1, 0,
706 dirent.name[k++] = '.';
707 for (j = 0; j < 3; j++)
708 dirent.name[k++] = p[j+8];
709 dirent.name[k] = '\0';
710 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
711 dirent.name[k] = '\0';
713 if (vallfn && shortSum != calcShortSum(p)) {
720 dirent.head = p[26] | (p[27] << 8);
721 if (boot->ClustMask == CLUST32_MASK)
722 dirent.head |= (p[20] << 16) | (p[21] << 24);
723 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
725 strlcpy(dirent.lname, longName,
726 sizeof(dirent.lname));
732 dirent.next = dir->child;
735 mod |= k = removede(f, boot, fat,
736 invlfn, vallfn ? vallfn : p,
737 invcl, vallfn ? valcl : cl, cl,
738 fullpath(&dirent), 0);
742 ? (valcl == cl && vallfn != buffer)
748 vallfn = NULL; /* not used any longer */
751 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
752 if (dirent.head != 0) {
753 pwarn("%s has clusters, but size 0\n",
755 if (ask(1, "Drop allocated clusters")) {
757 if (boot->ClustMask == CLUST32_MASK)
759 clearchain(boot, fat, dirent.head);
761 mod |= THISMOD|FSDIRMOD|FSFATMOD;
765 } else if (dirent.head == 0
766 && !strcmp(dirent.name, "..")
767 && dir->parent /* XXX */
768 && !dir->parent->parent) {
770 * Do nothing, the parent is the root
772 } else if (dirent.head < CLUST_FIRST
773 || dirent.head >= boot->NumClusters
774 || fat[dirent.head].next == CLUST_FREE
775 || (fat[dirent.head].next >= CLUST_RSRVD
776 && fat[dirent.head].next < CLUST_EOFS)
777 || fat[dirent.head].head != dirent.head) {
778 if (dirent.head == 0)
779 pwarn("%s has no clusters\n",
781 else if (dirent.head < CLUST_FIRST
782 || dirent.head >= boot->NumClusters)
783 pwarn("%s starts with cluster out of range(%u)\n",
786 else if (fat[dirent.head].next == CLUST_FREE)
787 pwarn("%s starts with free cluster\n",
789 else if (fat[dirent.head].next >= CLUST_RSRVD)
790 pwarn("%s starts with cluster marked %s\n",
792 rsrvdcltype(fat[dirent.head].next));
794 pwarn("%s doesn't start a cluster chain\n",
796 if (dirent.flags & ATTR_DIRECTORY) {
797 if (ask(0, "Remove")) {
799 mod |= THISMOD|FSDIRMOD;
804 if (ask(1, "Truncate")) {
805 p[28] = p[29] = p[30] = p[31] = 0;
807 if (boot->ClustMask == CLUST32_MASK)
810 mod |= THISMOD|FSDIRMOD;
816 if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
817 fat[dirent.head].flags |= FAT_USED;
819 if (dirent.flags & ATTR_DIRECTORY) {
821 * gather more info for directories
823 struct dirTodoNode *n;
826 pwarn("Directory %s has size != 0\n",
828 if (ask(1, "Correct")) {
829 p[28] = p[29] = p[30] = p[31] = 0;
831 mod |= THISMOD|FSDIRMOD;
836 * handle `.' and `..' specially
838 if (strcmp(dirent.name, ".") == 0) {
839 if (dirent.head != dir->head) {
840 pwarn("`.' entry in %s has incorrect start cluster\n",
842 if (ask(1, "Correct")) {
843 dirent.head = dir->head;
844 p[26] = (u_char)dirent.head;
845 p[27] = (u_char)(dirent.head >> 8);
846 if (boot->ClustMask == CLUST32_MASK) {
847 p[20] = (u_char)(dirent.head >> 16);
848 p[21] = (u_char)(dirent.head >> 24);
850 mod |= THISMOD|FSDIRMOD;
856 if (strcmp(dirent.name, "..") == 0) {
857 if (dir->parent) { /* XXX */
858 if (!dir->parent->parent) {
860 pwarn("`..' entry in %s has non-zero start cluster\n",
862 if (ask(1, "Correct")) {
865 if (boot->ClustMask == CLUST32_MASK)
867 mod |= THISMOD|FSDIRMOD;
871 } else if (dirent.head != dir->parent->head) {
872 pwarn("`..' entry in %s has incorrect start cluster\n",
874 if (ask(1, "Correct")) {
875 dirent.head = dir->parent->head;
876 p[26] = (u_char)dirent.head;
877 p[27] = (u_char)(dirent.head >> 8);
878 if (boot->ClustMask == CLUST32_MASK) {
879 p[20] = (u_char)(dirent.head >> 16);
880 p[21] = (u_char)(dirent.head >> 24);
882 mod |= THISMOD|FSDIRMOD;
890 * Only one directory entry can point
891 * to dir->head, it's '.'.
893 if (dirent.head == dir->head) {
894 pwarn("%s entry in %s has incorrect start cluster\n",
895 dirent.name, fullpath(dir));
896 if (ask(1, "Remove")) {
898 mod |= THISMOD|FSDIRMOD;
902 } else if ((check_subdirectory(f, boot,
903 &dirent) & FSERROR) == FSERROR) {
905 * A subdirectory should have
906 * a dot (.) entry and a dot-dot
907 * (..) entry of ATTR_DIRECTORY,
908 * we will inspect further when
909 * traversing into it.
911 if (ask(1, "Remove")) {
913 mod |= THISMOD|FSDIRMOD;
920 /* create directory tree node */
921 if (!(d = newDosDirEntry())) {
922 perr("No space for directory");
925 memcpy(d, &dirent, sizeof(struct dosDirEntry));
926 /* link it into the tree */
929 /* Enter this directory into the todo list */
930 if (!(n = newDirTodo())) {
931 perr("No space for todo list");
934 n->next = pendingDirectories;
936 pendingDirectories = n;
938 mod |= k = checksize(boot, fat, p, &dirent);
945 if (!(boot->flags & FAT32) && !dir->parent)
950 if (lseek(f, off, SEEK_SET) != off
951 || write(f, buffer, last) != last) {
952 perr("Unable to write directory");
957 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
958 if (invlfn || vallfn)
959 mod |= removede(f, boot, fat,
960 invlfn ? invlfn : vallfn, p,
961 invlfn ? invcl : valcl, -1, 0,
964 /* The root directory of non fat32 filesystems is in a special
965 * area and may have been modified above without being written out.
967 if ((mod & FSDIRMOD) && !(boot->flags & FAT32) && !dir->parent) {
969 if (lseek(f, off, SEEK_SET) != off
970 || write(f, buffer, last) != last) {
971 perr("Unable to write directory");
976 return mod & ~THISMOD;
980 handleDirTree(int dosfs, struct bootblock *boot, struct fatEntry *fat)
984 mod = readDosDirSection(dosfs, boot, fat, rootDir);
989 * process the directory todo list
991 while (pendingDirectories) {
992 struct dosDirEntry *dir = pendingDirectories->dir;
993 struct dirTodoNode *n = pendingDirectories->next;
996 * remove TODO entry now, the list might change during
999 freeDirTodo(pendingDirectories);
1000 pendingDirectories = n;
1003 * handle subdirectory
1005 mod |= readDosDirSection(dosfs, boot, fat, dir);
1014 * Try to reconnect a FAT chain into dir
1016 static u_char *lfbuf;
1021 reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
1023 struct dosDirEntry d;
1027 if (!ask(1, "Reconnect"))
1031 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
1032 if (!strcmp(lostDir->name, LOSTDIR))
1035 if (!lostDir) { /* Create LOSTDIR? XXX */
1036 pwarn("No %s directory\n", LOSTDIR);
1041 lfbuf = malloc(boot->ClusterSize);
1043 perr("No space for buffer");
1051 for (; p < lfbuf + boot->ClusterSize; p += 32)
1052 if (*p == SLOT_EMPTY
1053 || *p == SLOT_DELETED)
1055 if (p && p < lfbuf + boot->ClusterSize)
1057 lfcl = p ? fat[lfcl].next : lostDir->head;
1058 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
1059 /* Extend LOSTDIR? XXX */
1060 pwarn("No space in %s\n", LOSTDIR);
1061 lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
1064 lfoff = lfcl * boot->ClusterSize
1065 + boot->ClusterOffset * boot->bpbBytesPerSec;
1066 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1067 || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1068 perr("could not read LOST.DIR");
1075 /* Ensure uniqueness of entry here! XXX */
1076 memset(&d, 0, sizeof d);
1077 /* worst case -1 = 4294967295, 10 digits */
1078 len = snprintf(d.name, sizeof(d.name), "%u", head);
1081 d.size = fat[head].length * boot->ClusterSize;
1083 memcpy(p, d.name, len);
1084 memset(p + len, ' ', 11 - len);
1085 memset(p + 11, 0, 32 - 11);
1086 p[26] = (u_char)d.head;
1087 p[27] = (u_char)(d.head >> 8);
1088 if (boot->ClustMask == CLUST32_MASK) {
1089 p[20] = (u_char)(d.head >> 16);
1090 p[21] = (u_char)(d.head >> 24);
1092 p[28] = (u_char)d.size;
1093 p[29] = (u_char)(d.size >> 8);
1094 p[30] = (u_char)(d.size >> 16);
1095 p[31] = (u_char)(d.size >> 24);
1096 fat[head].flags |= FAT_USED;
1097 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1098 || (size_t)write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1099 perr("could not write LOST.DIR");