2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Google LLC
5 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
6 * Copyright (c) 1995 Martin Husemann
7 * Some structure declaration borrowed from Paul Popelka
8 * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: dir.c,v 1.20 2006/06/05 16:51:18 christos Exp $");
35 static const char rcsid[] =
48 #include <sys/param.h>
53 #define SLOT_EMPTY 0x00 /* slot has never been used */
54 #define SLOT_E5 0x05 /* the real value is 0xe5 */
55 #define SLOT_DELETED 0xe5 /* file in this slot deleted */
57 #define ATTR_NORMAL 0x00 /* normal file */
58 #define ATTR_READONLY 0x01 /* file is readonly */
59 #define ATTR_HIDDEN 0x02 /* file is hidden */
60 #define ATTR_SYSTEM 0x04 /* file is a system file */
61 #define ATTR_VOLUME 0x08 /* entry is a volume label */
62 #define ATTR_DIRECTORY 0x10 /* entry is a directory name */
63 #define ATTR_ARCHIVE 0x20 /* file is new or modified */
65 #define ATTR_WIN95 0x0f /* long name record */
68 * This is the format of the contents of the deTime field in the direntry
70 * We don't use bitfields because we don't know how compilers for
71 * arbitrary machines will lay them out.
73 #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
74 #define DT_2SECONDS_SHIFT 0
75 #define DT_MINUTES_MASK 0x7E0 /* minutes */
76 #define DT_MINUTES_SHIFT 5
77 #define DT_HOURS_MASK 0xF800 /* hours */
78 #define DT_HOURS_SHIFT 11
81 * This is the format of the contents of the deDate field in the direntry
84 #define DD_DAY_MASK 0x1F /* day of month */
85 #define DD_DAY_SHIFT 0
86 #define DD_MONTH_MASK 0x1E0 /* month */
87 #define DD_MONTH_SHIFT 5
88 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */
89 #define DD_YEAR_SHIFT 9
93 static struct dosDirEntry *newDosDirEntry(void);
94 static void freeDosDirEntry(struct dosDirEntry *);
95 static struct dirTodoNode *newDirTodo(void);
96 static void freeDirTodo(struct dirTodoNode *);
97 static char *fullpath(struct dosDirEntry *);
98 static u_char calcShortSum(u_char *);
99 static int delete(struct fat_descriptor *, cl_t, int, cl_t, int, int);
100 static int removede(struct fat_descriptor *, u_char *, u_char *,
101 cl_t, cl_t, cl_t, char *, int);
102 static int checksize(struct fat_descriptor *, u_char *, struct dosDirEntry *);
103 static int readDosDirSection(struct fat_descriptor *, struct dosDirEntry *);
106 * Manage free dosDirEntry structures.
108 static struct dosDirEntry *freede;
110 static struct dosDirEntry *
113 struct dosDirEntry *de;
115 if (!(de = freede)) {
116 if (!(de = malloc(sizeof *de)))
124 freeDosDirEntry(struct dosDirEntry *de)
131 * The same for dirTodoNode structures.
133 static struct dirTodoNode *freedt;
135 static struct dirTodoNode *
138 struct dirTodoNode *dt;
140 if (!(dt = freedt)) {
141 if (!(dt = malloc(sizeof *dt)))
149 freeDirTodo(struct dirTodoNode *dt)
156 * The stack of unread directories
158 static struct dirTodoNode *pendingDirectories = NULL;
161 * Return the full pathname for a directory entry.
164 fullpath(struct dosDirEntry *dir)
166 static char namebuf[MAXPATHLEN + 1];
170 cp = namebuf + sizeof namebuf;
174 np = dir->lname[0] ? dir->lname : dir->name;
176 if (cp <= namebuf + 1 + nl) {
192 * Calculate a checksum over an 8.3 alias name
195 calcShortSum(u_char *p)
200 for (i = 0; i < 11; i++) {
201 sum = (sum << 7)|(sum >> 1); /* rotate right */
209 * Global variables temporarily used during a directory scan
211 static char longName[DOSLONGNAMELEN] = "";
212 static u_char *buffer = NULL;
213 static u_char *delbuf = NULL;
215 static struct dosDirEntry *rootDir;
216 static struct dosDirEntry *lostDir;
219 * Init internal state for a new directory scan.
222 resetDosDirSection(struct fat_descriptor *fat)
224 int rootdir_size, cluster_size;
227 struct bootblock *boot;
229 boot = fat_get_boot(fat);
231 rootdir_size = boot->bpbRootDirEnts * 32;
232 cluster_size = boot->bpbSecPerClust * boot->bpbBytesPerSec;
234 if ((buffer = malloc(len = MAX(rootdir_size, cluster_size))) == NULL) {
235 perr("No space for directory buffer (%zu)", len);
239 if ((delbuf = malloc(len = cluster_size)) == NULL) {
241 perr("No space for directory delbuf (%zu)", len);
245 if ((rootDir = newDosDirEntry()) == NULL) {
248 perr("No space for directory entry");
252 memset(rootDir, 0, sizeof *rootDir);
253 if (boot->flags & FAT32) {
254 if (!fat_is_cl_head(fat, boot->bpbRootClust)) {
255 pfatal("Root directory doesn't start a cluster chain");
258 rootDir->head = boot->bpbRootClust;
265 * Cleanup after a directory scan
268 finishDosDirSection(void)
270 struct dirTodoNode *p, *np;
271 struct dosDirEntry *d, *nd;
273 for (p = pendingDirectories; p; p = np) {
277 pendingDirectories = NULL;
278 for (d = rootDir; d; d = nd) {
279 if ((nd = d->child) != NULL) {
287 rootDir = lostDir = NULL;
295 * Delete directory entries between startcl, startoff and endcl, endoff.
298 delete(struct fat_descriptor *fat, cl_t startcl,
299 int startoff, cl_t endcl, int endoff, int notlast)
304 struct bootblock *boot;
306 boot = fat_get_boot(fat);
307 fd = fat_get_fd(fat);
308 clsz = boot->bpbSecPerClust * boot->bpbBytesPerSec;
310 s = delbuf + startoff;
312 while (fat_is_valid_cl(fat, startcl)) {
313 if (startcl == endcl) {
318 off = (startcl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
320 off *= boot->bpbBytesPerSec;
321 if (lseek(fd, off, SEEK_SET) != off) {
322 perr("Unable to lseek to %" PRId64, off);
325 if (read(fd, delbuf, clsz) != clsz) {
326 perr("Unable to read directory");
333 if (lseek(fd, off, SEEK_SET) != off) {
334 perr("Unable to lseek to %" PRId64, off);
337 if (write(fd, delbuf, clsz) != clsz) {
338 perr("Unable to write directory");
341 if (startcl == endcl)
343 startcl = fat_get_cl_next(fat, startcl);
350 removede(struct fat_descriptor *fat, u_char *start,
351 u_char *end, cl_t startcl, cl_t endcl, cl_t curcl,
352 char *path, int type)
356 pwarn("Invalid long filename entry for %s\n", path);
359 pwarn("Invalid long filename entry at end of directory %s\n",
363 pwarn("Invalid long filename entry for volume label\n");
366 if (ask(0, "Remove")) {
367 if (startcl != curcl) {
369 startcl, start - buffer,
371 endcl == curcl) == FSFATAL)
375 /* startcl is < CLUST_FIRST for !FAT32 root */
376 if ((endcl == curcl) || (startcl < CLUST_FIRST))
377 for (; start < end; start += 32)
378 *start = SLOT_DELETED;
385 * Check an in-memory file entry
388 checksize(struct fat_descriptor *fat, u_char *p, struct dosDirEntry *dir)
392 struct bootblock *boot;
394 boot = fat_get_boot(fat);
397 * Check size on ordinary files
399 if (dir->head == CLUST_FREE) {
402 if (!fat_is_valid_cl(fat, dir->head))
404 ret = checkchain(fat, dir->head, &physicalSize);
406 * Upon return, physicalSize would hold the chain length
407 * that checkchain() was able to validate, but if the user
408 * refused the proposed repair, it would be unsafe to
409 * proceed with directory entry fix, so bail out in that
412 if (ret == FSERROR) {
415 physicalSize *= boot->ClusterSize;
417 if (physicalSize < dir->size) {
418 pwarn("size of %s is %u, should at most be %zu\n",
419 fullpath(dir), dir->size, physicalSize);
420 if (ask(1, "Truncate")) {
421 dir->size = physicalSize;
422 p[28] = (u_char)physicalSize;
423 p[29] = (u_char)(physicalSize >> 8);
424 p[30] = (u_char)(physicalSize >> 16);
425 p[31] = (u_char)(physicalSize >> 24);
429 } else if (physicalSize - dir->size >= boot->ClusterSize) {
430 pwarn("%s has too many clusters allocated\n",
432 if (ask(1, "Drop superfluous clusters")) {
436 for (cl = dir->head, len = sz = 0;
437 (sz += boot->ClusterSize) < dir->size; len++)
438 cl = fat_get_cl_next(fat, cl);
439 clearchain(fat, fat_get_cl_next(fat, cl));
440 ret = fat_set_cl_next(fat, cl, CLUST_EOF);
441 return (FSFATMOD | ret);
448 static const u_char dot_name[11] = ". ";
449 static const u_char dotdot_name[11] = ".. ";
452 * Basic sanity check if the subdirectory have good '.' and '..' entries,
453 * and they are directory entries. Further sanity checks are performed
454 * when we traverse into it.
457 check_subdirectory(struct fat_descriptor *fat, struct dosDirEntry *dir)
464 struct bootblock *boot;
466 boot = fat_get_boot(fat);
467 fd = fat_get_fd(fat);
470 if (dir->parent && !fat_is_valid_cl(fat, cl)) {
474 if (!(boot->flags & FAT32) && !dir->parent) {
475 off = boot->bpbResSectors + boot->bpbFATs *
478 off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
482 * We only need to check the first two entries of the directory,
483 * which is found in the first sector of the directory entry,
484 * so read in only the first sector.
486 buf = malloc(boot->bpbBytesPerSec);
488 perr("No space for directory buffer (%u)",
489 boot->bpbBytesPerSec);
493 off *= boot->bpbBytesPerSec;
494 if (lseek(fd, off, SEEK_SET) != off ||
495 read(fd, buf, boot->bpbBytesPerSec) != (ssize_t)boot->bpbBytesPerSec) {
496 perr("Unable to read directory");
502 * Both `.' and `..' must be present and be the first two entries
503 * and be ATTR_DIRECTORY of a valid subdirectory.
506 if (memcmp(cp, dot_name, sizeof(dot_name)) != 0 ||
507 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
508 pwarn("%s: Incorrect `.' for %s.\n", __func__, dir->name);
512 if (memcmp(cp, dotdot_name, sizeof(dotdot_name)) != 0 ||
513 (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
514 pwarn("%s: Incorrect `..' for %s. \n", __func__, dir->name);
523 * Read a directory and
524 * - resolve long name records
525 * - enter file and directory records into the parent's list
526 * - push directories onto the todo-stack
529 readDosDirSection(struct fat_descriptor *fat, struct dosDirEntry *dir)
531 struct bootblock *boot;
532 struct dosDirEntry dirent, *d;
533 u_char *p, *vallfn, *invlfn, *empty;
535 int fd, i, j, k, iosize, entries;
537 cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
543 #define THISMOD 0x8000 /* Only used within this routine */
545 boot = fat_get_boot(fat);
546 fd = fat_get_fd(fat);
549 if (dir->parent && (!fat_is_valid_cl(fat, cl))) {
551 * Already handled somewhere else.
556 vallfn = invlfn = empty = NULL;
559 * If we are checking the legacy root (for FAT12/FAT16),
560 * we will operate on the whole directory; otherwise, we
561 * will operate on one cluster at a time, and also take
562 * this opportunity to examine the chain.
564 * Derive how many entries we are going to encounter from
567 is_legacyroot = (dir->parent == NULL && !(boot->flags & FAT32));
569 iosize = boot->bpbRootDirEnts * 32;
570 entries = boot->bpbRootDirEnts;
572 iosize = boot->bpbSecPerClust * boot->bpbBytesPerSec;
573 entries = iosize / 32;
574 mod |= checkchain(fat, dir->head, &dirclusters);
580 * Special case for FAT12/FAT16 root -- read
581 * in the whole root directory.
583 off = boot->bpbResSectors + boot->bpbFATs *
587 * Otherwise, read in a cluster of the
590 off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
593 off *= boot->bpbBytesPerSec;
594 if (lseek(fd, off, SEEK_SET) != off ||
595 read(fd, buffer, iosize) != iosize) {
596 perr("Unable to read directory");
600 for (p = buffer, i = 0; i < entries; i++, p += 32) {
601 if (dir->fsckflags & DIREMPWARN) {
606 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
607 if (*p == SLOT_EMPTY) {
608 dir->fsckflags |= DIREMPTY;
615 if (dir->fsckflags & DIREMPTY) {
616 if (!(dir->fsckflags & DIREMPWARN)) {
617 pwarn("%s has entries after end of directory\n",
619 if (ask(1, "Extend")) {
622 dir->fsckflags &= ~DIREMPTY;
624 empcl, empty - buffer,
625 cl, p - buffer, 1) == FSFATAL)
627 q = ((empcl == cl) ? empty : buffer);
629 for (; q < p; q += 32)
631 mod |= THISMOD|FSDIRMOD;
632 } else if (ask(0, "Truncate"))
633 dir->fsckflags |= DIREMPWARN;
635 if (dir->fsckflags & DIREMPWARN) {
637 mod |= THISMOD|FSDIRMOD;
639 } else if (dir->fsckflags & DIREMPTY)
644 if (p[11] == ATTR_WIN95) {
646 if (shortSum != -1) {
652 memset(longName, 0, sizeof longName);
656 } else if (shortSum != p[13]
657 || lidx != (*p & LRNOMASK)) {
668 lidx = *p & LRNOMASK;
670 pwarn("invalid long name\n");
678 t = longName + --lidx * 13;
679 for (k = 1; k < 11 && t < longName +
680 sizeof(longName); k += 2) {
681 if (!p[k] && !p[k + 1])
685 * Warn about those unusable chars in msdosfs here? XXX
691 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
692 if (!p[k] && !p[k + 1])
699 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
700 if (!p[k] && !p[k + 1])
706 if (t >= longName + sizeof(longName)) {
707 pwarn("long filename too long\n");
714 if (p[26] | (p[27] << 8)) {
715 pwarn("long filename record cluster start != 0\n");
722 continue; /* long records don't carry further
727 * This is a standard msdosfs directory entry.
729 memset(&dirent, 0, sizeof dirent);
732 * it's a short name record, but we need to know
733 * more, so get the flags first.
735 dirent.flags = p[11];
738 * Translate from 850 to ISO here XXX
740 for (j = 0; j < 8; j++)
741 dirent.name[j] = p[j];
742 dirent.name[8] = '\0';
743 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
744 dirent.name[k] = '\0';
745 if (k < 0 || dirent.name[k] != '\0')
747 if (dirent.name[0] == SLOT_E5)
748 dirent.name[0] = 0xe5;
750 if (dirent.flags & ATTR_VOLUME) {
751 if (vallfn || invlfn) {
753 invlfn ? invlfn : vallfn, p,
754 invlfn ? invcl : valcl, -1, 0,
763 dirent.name[k++] = '.';
764 for (j = 0; j < 3; j++)
765 dirent.name[k++] = p[j+8];
766 dirent.name[k] = '\0';
767 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
768 dirent.name[k] = '\0';
770 if (vallfn && shortSum != calcShortSum(p)) {
777 dirent.head = p[26] | (p[27] << 8);
778 if (boot->ClustMask == CLUST32_MASK)
779 dirent.head |= (p[20] << 16) | (p[21] << 24);
780 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
782 strlcpy(dirent.lname, longName,
783 sizeof(dirent.lname));
789 dirent.next = dir->child;
792 mod |= k = removede(fat,
793 invlfn, vallfn ? vallfn : p,
794 invcl, vallfn ? valcl : cl, cl,
795 fullpath(&dirent), 0);
799 ? (valcl == cl && vallfn != buffer)
805 vallfn = NULL; /* not used any longer */
809 * Check if the directory entry is sane.
811 * '.' and '..' are skipped, their sanity is
812 * checked somewhere else.
814 * For everything else, check if we have a new,
815 * valid cluster chain (beginning of a file or
816 * directory that was never previously claimed
817 * by another file) when it's a non-empty file
818 * or a directory. The sanity of the cluster
819 * chain is checked at a later time when we
820 * traverse into the directory, or examine the
821 * file's directory entry.
823 * The only possible fix is to delete the entry
824 * if it's a directory; for file, we have to
825 * truncate the size to 0.
827 if (!(dirent.flags & ATTR_DIRECTORY) ||
828 (strcmp(dirent.name, ".") != 0 &&
829 strcmp(dirent.name, "..") != 0)) {
830 if ((dirent.size != 0 || (dirent.flags & ATTR_DIRECTORY)) &&
831 ((!fat_is_valid_cl(fat, dirent.head) ||
832 !fat_is_cl_head(fat, dirent.head)))) {
833 if (!fat_is_valid_cl(fat, dirent.head)) {
834 pwarn("%s starts with cluster out of range(%u)\n",
838 pwarn("%s doesn't start a new cluster chain\n",
842 if (dirent.flags & ATTR_DIRECTORY) {
843 if (ask(0, "Remove")) {
845 mod |= THISMOD|FSDIRMOD;
850 if (ask(1, "Truncate")) {
851 p[28] = p[29] = p[30] = p[31] = 0;
853 if (boot->ClustMask == CLUST32_MASK)
857 mod |= THISMOD|FSDIRMOD;
863 if (dirent.flags & ATTR_DIRECTORY) {
865 * gather more info for directories
867 struct dirTodoNode *n;
870 pwarn("Directory %s has size != 0\n",
872 if (ask(1, "Correct")) {
873 p[28] = p[29] = p[30] = p[31] = 0;
875 mod |= THISMOD|FSDIRMOD;
880 * handle `.' and `..' specially
882 if (strcmp(dirent.name, ".") == 0) {
883 if (dirent.head != dir->head) {
884 pwarn("`.' entry in %s has incorrect start cluster\n",
886 if (ask(1, "Correct")) {
887 dirent.head = dir->head;
888 p[26] = (u_char)dirent.head;
889 p[27] = (u_char)(dirent.head >> 8);
890 if (boot->ClustMask == CLUST32_MASK) {
891 p[20] = (u_char)(dirent.head >> 16);
892 p[21] = (u_char)(dirent.head >> 24);
894 mod |= THISMOD|FSDIRMOD;
899 } else if (strcmp(dirent.name, "..") == 0) {
900 if (dir->parent) { /* XXX */
901 if (!dir->parent->parent) {
903 pwarn("`..' entry in %s has non-zero start cluster\n",
905 if (ask(1, "Correct")) {
908 if (boot->ClustMask == CLUST32_MASK)
910 mod |= THISMOD|FSDIRMOD;
914 } else if (dirent.head != dir->parent->head) {
915 pwarn("`..' entry in %s has incorrect start cluster\n",
917 if (ask(1, "Correct")) {
918 dirent.head = dir->parent->head;
919 p[26] = (u_char)dirent.head;
920 p[27] = (u_char)(dirent.head >> 8);
921 if (boot->ClustMask == CLUST32_MASK) {
922 p[20] = (u_char)(dirent.head >> 16);
923 p[21] = (u_char)(dirent.head >> 24);
925 mod |= THISMOD|FSDIRMOD;
933 * Only one directory entry can point
934 * to dir->head, it's '.'.
936 if (dirent.head == dir->head) {
937 pwarn("%s entry in %s has incorrect start cluster\n",
938 dirent.name, fullpath(dir));
939 if (ask(1, "Remove")) {
941 mod |= THISMOD|FSDIRMOD;
945 } else if ((check_subdirectory(fat,
946 &dirent) & FSERROR) == FSERROR) {
948 * A subdirectory should have
949 * a dot (.) entry and a dot-dot
950 * (..) entry of ATTR_DIRECTORY,
951 * we will inspect further when
952 * traversing into it.
954 if (ask(1, "Remove")) {
956 mod |= THISMOD|FSDIRMOD;
963 /* create directory tree node */
964 if (!(d = newDosDirEntry())) {
965 perr("No space for directory");
968 memcpy(d, &dirent, sizeof(struct dosDirEntry));
969 /* link it into the tree */
972 /* Enter this directory into the todo list */
973 if (!(n = newDirTodo())) {
974 perr("No space for todo list");
977 n->next = pendingDirectories;
979 pendingDirectories = n;
981 mod |= k = checksize(fat, p, &dirent);
990 * Don't bother to write back right now because
991 * we may continue to make modification to the
992 * non-FAT32 root directory below.
995 } else if (mod & THISMOD) {
996 if (lseek(fd, off, SEEK_SET) != off
997 || write(fd, buffer, iosize) != iosize) {
998 perr("Unable to write directory");
1003 } while (fat_is_valid_cl(fat, (cl = fat_get_cl_next(fat, cl))));
1004 if (invlfn || vallfn)
1005 mod |= removede(fat,
1006 invlfn ? invlfn : vallfn, p,
1007 invlfn ? invcl : valcl, -1, 0,
1011 * The root directory of non-FAT32 filesystems is in a special
1012 * area and may have been modified above removede() without
1013 * being written out.
1015 if ((mod & FSDIRMOD) && is_legacyroot) {
1016 if (lseek(fd, off, SEEK_SET) != off
1017 || write(fd, buffer, iosize) != iosize) {
1018 perr("Unable to write directory");
1023 return mod & ~THISMOD;
1027 handleDirTree(struct fat_descriptor *fat)
1031 mod = readDosDirSection(fat, rootDir);
1036 * process the directory todo list
1038 while (pendingDirectories) {
1039 struct dosDirEntry *dir = pendingDirectories->dir;
1040 struct dirTodoNode *n = pendingDirectories->next;
1043 * remove TODO entry now, the list might change during
1046 freeDirTodo(pendingDirectories);
1047 pendingDirectories = n;
1050 * handle subdirectory
1052 mod |= readDosDirSection(fat, dir);
1061 * Try to reconnect a FAT chain into dir
1063 static u_char *lfbuf;
1068 reconnect(struct fat_descriptor *fat, cl_t head, size_t length)
1070 struct bootblock *boot = fat_get_boot(fat);
1071 struct dosDirEntry d;
1075 dosfs = fat_get_fd(fat);
1077 if (!ask(1, "Reconnect"))
1081 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
1082 if (!strcmp(lostDir->name, LOSTDIR))
1085 if (!lostDir) { /* Create LOSTDIR? XXX */
1086 pwarn("No %s directory\n", LOSTDIR);
1091 lfbuf = malloc(boot->ClusterSize);
1093 perr("No space for buffer");
1101 for (; p < lfbuf + boot->ClusterSize; p += 32)
1102 if (*p == SLOT_EMPTY
1103 || *p == SLOT_DELETED)
1105 if (p && p < lfbuf + boot->ClusterSize)
1107 lfcl = p ? fat_get_cl_next(fat, lfcl) : lostDir->head;
1108 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
1109 /* Extend LOSTDIR? XXX */
1110 pwarn("No space in %s\n", LOSTDIR);
1111 lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
1114 lfoff = (lfcl - CLUST_FIRST) * boot->ClusterSize
1115 + boot->FirstCluster * boot->bpbBytesPerSec;
1117 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1118 || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1119 perr("could not read LOST.DIR");
1126 /* Ensure uniqueness of entry here! XXX */
1127 memset(&d, 0, sizeof d);
1128 /* worst case -1 = 4294967295, 10 digits */
1129 len = snprintf(d.name, sizeof(d.name), "%u", head);
1132 d.size = length * boot->ClusterSize;
1134 memcpy(p, d.name, len);
1135 memset(p + len, ' ', 11 - len);
1136 memset(p + 11, 0, 32 - 11);
1137 p[26] = (u_char)d.head;
1138 p[27] = (u_char)(d.head >> 8);
1139 if (boot->ClustMask == CLUST32_MASK) {
1140 p[20] = (u_char)(d.head >> 16);
1141 p[21] = (u_char)(d.head >> 24);
1143 p[28] = (u_char)d.size;
1144 p[29] = (u_char)(d.size >> 8);
1145 p[30] = (u_char)(d.size >> 16);
1146 p[31] = (u_char)(d.size >> 24);
1147 if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
1148 || (size_t)write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1149 perr("could not write LOST.DIR");