2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
4 * Some structure declaration borrowed from Paul Popelka
5 * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Martin Husemann
18 * and Wolfgang Solfrank.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/param.h>
49 #define SLOT_EMPTY 0x00 /* slot has never been used */
50 #define SLOT_E5 0x05 /* the real value is 0xe5 */
51 #define SLOT_DELETED 0xe5 /* file in this slot deleted */
53 #define ATTR_NORMAL 0x00 /* normal file */
54 #define ATTR_READONLY 0x01 /* file is readonly */
55 #define ATTR_HIDDEN 0x02 /* file is hidden */
56 #define ATTR_SYSTEM 0x04 /* file is a system file */
57 #define ATTR_VOLUME 0x08 /* entry is a volume label */
58 #define ATTR_DIRECTORY 0x10 /* entry is a directory name */
59 #define ATTR_ARCHIVE 0x20 /* file is new or modified */
61 #define ATTR_WIN95 0x0f /* long name record */
64 * This is the format of the contents of the deTime field in the direntry
66 * We don't use bitfields because we don't know how compilers for
67 * arbitrary machines will lay them out.
69 #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
70 #define DT_2SECONDS_SHIFT 0
71 #define DT_MINUTES_MASK 0x7E0 /* minutes */
72 #define DT_MINUTES_SHIFT 5
73 #define DT_HOURS_MASK 0xF800 /* hours */
74 #define DT_HOURS_SHIFT 11
77 * This is the format of the contents of the deDate field in the direntry
80 #define DD_DAY_MASK 0x1F /* day of month */
81 #define DD_DAY_SHIFT 0
82 #define DD_MONTH_MASK 0x1E0 /* month */
83 #define DD_MONTH_SHIFT 5
84 #define DD_YEAR_MASK 0xFE00 /* year - 1980 */
85 #define DD_YEAR_SHIFT 9
89 static struct dosDirEntry *newDosDirEntry(void);
90 static void freeDosDirEntry(struct dosDirEntry *);
91 static struct dirTodoNode *newDirTodo(void);
92 static void freeDirTodo(struct dirTodoNode *);
93 static char *fullpath(struct dosDirEntry *);
94 static u_char calcShortSum(u_char *);
95 static int delete(int, struct bootblock *, struct fatEntry *, cl_t, int,
97 static int removede(int, struct bootblock *, struct fatEntry *, u_char *,
98 u_char *, cl_t, cl_t, cl_t, char *, int);
99 static int checksize(struct bootblock *, struct fatEntry *, u_char *,
100 struct dosDirEntry *);
101 static int readDosDirSection(int, struct bootblock *, struct fatEntry *,
102 struct dosDirEntry *);
105 * Manage free dosDirEntry structures.
107 static struct dosDirEntry *freede;
109 static struct dosDirEntry *
112 struct dosDirEntry *de;
114 if (!(de = freede)) {
115 if (!(de = (struct dosDirEntry *)malloc(sizeof *de)))
123 freeDosDirEntry(struct dosDirEntry *de)
130 * The same for dirTodoNode structures.
132 static struct dirTodoNode *freedt;
134 static struct dirTodoNode *
137 struct dirTodoNode *dt;
139 if (!(dt = freedt)) {
140 if (!(dt = (struct dirTodoNode *)malloc(sizeof *dt)))
148 freeDirTodo(struct dirTodoNode *dt)
155 * The stack of unread directories
157 struct dirTodoNode *pendingDirectories = NULL;
160 * Return the full pathname for a directory entry.
163 fullpath(struct dosDirEntry *dir)
165 static char namebuf[MAXPATHLEN + 1];
169 cp = namebuf + sizeof namebuf - 1;
172 np = dir->lname[0] ? dir->lname : dir->name;
174 if ((cp -= nl) <= namebuf + 1)
178 } while ((dir = dir->parent) != NULL);
187 * Calculate a checksum over an 8.3 alias name
190 calcShortSum(u_char *p)
195 for (i = 0; i < 11; i++) {
196 sum = (sum << 7)|(sum >> 1); /* rotate right */
204 * Global variables temporarily used during a directory scan
206 static char longName[DOSLONGNAMELEN] = "";
207 static u_char *buffer = NULL;
208 static u_char *delbuf = NULL;
210 struct dosDirEntry *rootDir;
211 static struct dosDirEntry *lostDir;
214 * Init internal state for a new directory scan.
217 resetDosDirSection(struct bootblock *boot, struct fatEntry *fat)
223 b1 = boot->RootDirEnts * 32;
224 b2 = boot->SecPerClust * boot->BytesPerSec;
226 if (!(buffer = malloc(b1 > b2 ? b1 : b2))
227 || !(delbuf = malloc(b2))
228 || !(rootDir = newDosDirEntry())) {
229 perror("No space for directory");
232 memset(rootDir, 0, sizeof *rootDir);
233 if (boot->flags & FAT32) {
234 if (boot->RootCl < CLUST_FIRST || boot->RootCl >= boot->NumClusters) {
235 pfatal("Root directory starts with cluster out of range(%u)",
239 cl = fat[boot->RootCl].next;
241 || (cl >= CLUST_RSRVD && cl< CLUST_EOFS)
242 || fat[boot->RootCl].head != boot->RootCl) {
243 if (cl == CLUST_FREE)
244 pwarn("Root directory starts with free cluster\n");
245 else if (cl >= CLUST_RSRVD)
246 pwarn("Root directory starts with cluster marked %s\n",
249 pfatal("Root directory doesn't start a cluster chain");
253 fat[boot->RootCl].next = CLUST_FREE;
259 fat[boot->RootCl].flags |= FAT_USED;
260 rootDir->head = boot->RootCl;
267 * Cleanup after a directory scan
270 finishDosDirSection(void)
272 struct dirTodoNode *p, *np;
273 struct dosDirEntry *d, *nd;
275 for (p = pendingDirectories; p; p = np) {
279 pendingDirectories = 0;
280 for (d = rootDir; d; d = nd) {
281 if ((nd = d->child) != NULL) {
289 rootDir = lostDir = NULL;
297 * Delete directory entries between startcl, startoff and endcl, endoff.
300 delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
301 int startoff, cl_t endcl, int endoff, int notlast)
305 int clsz = boot->SecPerClust * boot->BytesPerSec;
307 s = delbuf + startoff;
309 while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
310 if (startcl == endcl) {
315 off = startcl * boot->SecPerClust + boot->ClusterOffset;
316 off *= boot->BytesPerSec;
317 if (lseek64(f, off, SEEK_SET) != off) {
318 printf("off = %llu\n", off);
319 perror("Unable to lseek64");
322 if (read(f, delbuf, clsz) != clsz) {
323 perror("Unable to read directory");
330 if (lseek64(f, off, SEEK_SET) != off) {
331 printf("off = %llu\n", off);
332 perror("Unable to lseek64");
335 if (write(f, delbuf, clsz) != clsz) {
336 perror("Unable to write directory");
339 if (startcl == endcl)
341 startcl = fat[startcl].next;
348 removede(int f, struct bootblock *boot, struct fatEntry *fat, u_char *start,
349 u_char *end, cl_t startcl, cl_t endcl, cl_t curcl, char *path, int type)
353 pwarn("Invalid long filename entry for %s\n", path);
356 pwarn("Invalid long filename entry at end of directory %s\n", path);
359 pwarn("Invalid long filename entry for volume label\n");
362 if (ask(1, "Remove")) {
363 if (startcl != curcl) {
364 if (delete(f, boot, fat,
365 startcl, start - buffer,
367 endcl == curcl) == FSFATAL)
372 for (; start < end; start += 32)
373 *start = SLOT_DELETED;
380 * Check an in-memory file entry
383 checksize(struct bootblock *boot, struct fatEntry *fat, u_char *p,
384 struct dosDirEntry *dir)
387 * Check size on ordinary files
389 int32_t physicalSize;
391 if (dir->head == CLUST_FREE)
394 if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
396 physicalSize = fat[dir->head].length * boot->ClusterSize;
398 if (physicalSize < dir->size) {
399 pwarn("size of %s is %u, should at most be %u\n",
400 fullpath(dir), dir->size, physicalSize);
401 if (ask(1, "Truncate")) {
402 dir->size = physicalSize;
403 p[28] = (u_char)physicalSize;
404 p[29] = (u_char)(physicalSize >> 8);
405 p[30] = (u_char)(physicalSize >> 16);
406 p[31] = (u_char)(physicalSize >> 24);
410 } else if (physicalSize - dir->size >= boot->ClusterSize) {
411 pwarn("%s has too many clusters allocated\n",
413 if (ask(1, "Drop superfluous clusters")) {
417 for (cl = dir->head; (sz += boot->ClusterSize) < dir->size;)
419 clearchain(boot, fat, fat[cl].next);
420 fat[cl].next = CLUST_EOF;
429 static u_char dot_header[16]={0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00};
430 static u_char dot_dot_header[16]={0x2E, 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00};
433 * Check for missing or broken '.' and '..' entries.
436 check_dot_dot(int f, struct bootblock *boot, struct fatEntry *fat,struct dosDirEntry *dir)
448 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
453 if (!(boot->flags & FAT32) && !dir->parent) {
454 last = boot->RootDirEnts * 32;
455 off = boot->ResSectors + boot->FATs * boot->FATsecs;
457 last = boot->SecPerClust * boot->BytesPerSec;
458 off = cl * boot->SecPerClust + boot->ClusterOffset;
461 off *= boot->BytesPerSec;
464 perror("Unable to malloc");
467 if (lseek64(f, off, SEEK_SET) != off) {
468 printf("off = %llu\n", off);
469 perror("Unable to lseek64");
473 if (read(f, buf, last) != last) {
474 perror("Unable to read");
480 for (n_count=0, rc=0; n_count < 11; n_count++) {
481 if (dot_header[n_count] != p[n_count]) {
489 for (n_count = 0, rc = 0; n_count < 11; n_count++) {
490 if (dot_dot_header[n_count] != p[n_count+32]) {
498 } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
500 if (!dot || !dotdot) {
502 pwarn("%s: '.' absent for %s.\n",__func__,dir->name);
505 pwarn("%s: '..' absent for %s. \n",__func__,dir->name);
512 * Read a directory and
513 * - resolve long name records
514 * - enter file and directory records into the parent's list
515 * - push directories onto the todo-stack
518 readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
519 struct dosDirEntry *dir)
521 struct dosDirEntry dirent, *d;
522 u_char *p, *vallfn, *invlfn, *empty;
525 cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
532 #define THISMOD 0x8000 /* Only used within this routine */
535 if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
537 * Already handled somewhere else.
542 vallfn = invlfn = empty = NULL;
547 if (!(boot->flags & FAT32) && !dir->parent) {
548 last = boot->RootDirEnts * 32;
549 off = boot->ResSectors + boot->FATs * boot->FATsecs;
551 last = boot->SecPerClust * boot->BytesPerSec;
552 off = cl * boot->SecPerClust + boot->ClusterOffset;
555 off *= boot->BytesPerSec;
556 if (lseek64(f, off, SEEK_SET) != off) {
557 printf("off = %llu\n", off);
558 perror("Unable to lseek64");
561 if (read(f, buffer, last) != last) {
562 perror("Unable to read");
566 for (p = buffer, i = 0; i < last; i++, p += 32) {
567 if (dir->fsckflags & DIREMPWARN) {
572 if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
573 if (*p == SLOT_EMPTY) {
574 dir->fsckflags |= DIREMPTY;
581 if (dir->fsckflags & DIREMPTY) {
582 if (!(dir->fsckflags & DIREMPWARN)) {
583 pwarn("%s has entries after end of directory\n",
585 if (ask(1, "Extend")) {
588 dir->fsckflags &= ~DIREMPTY;
589 if (delete(f, boot, fat,
590 empcl, empty - buffer,
591 cl, p - buffer, 1) == FSFATAL)
593 q = empcl == cl ? empty : buffer;
594 for (; q < p; q += 32)
596 mod |= THISMOD|FSDIRMOD;
597 } else if (ask(1, "Truncate"))
598 dir->fsckflags |= DIREMPWARN;
600 if (dir->fsckflags & DIREMPWARN) {
602 mod |= THISMOD|FSDIRMOD;
604 } else if (dir->fsckflags & DIREMPTY)
609 if (p[11] == ATTR_WIN95) {
611 if (shortSum != -1) {
617 memset(longName, 0, sizeof longName);
621 } else if (shortSum != p[13]
622 || lidx != (*p & LRNOMASK)) {
633 lidx = *p & LRNOMASK;
634 t = longName + --lidx * 13;
635 for (k = 1; k < 11 && t < longName + sizeof(longName); k += 2) {
636 if (!p[k] && !p[k + 1])
640 * Warn about those unusable chars in msdosfs here? XXX
646 for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
647 if (!p[k] && !p[k + 1])
654 for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
655 if (!p[k] && !p[k + 1])
661 if (t >= longName + sizeof(longName)) {
662 pwarn("long filename too long\n");
669 if (p[26] | (p[27] << 8)) {
670 pwarn("long filename record cluster start != 0\n");
677 continue; /* long records don't carry further
682 * This is a standard msdosfs directory entry.
684 memset(&dirent, 0, sizeof dirent);
687 * it's a short name record, but we need to know
688 * more, so get the flags first.
690 dirent.flags = p[11];
693 * Translate from 850 to ISO here XXX
695 for (j = 0; j < 8; j++)
696 dirent.name[j] = p[j];
697 dirent.name[8] = '\0';
698 for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
699 dirent.name[k] = '\0';
700 if (dirent.name[k] != '\0')
702 if (dirent.name[0] == SLOT_E5)
703 dirent.name[0] = 0xe5;
705 if (dirent.flags & ATTR_VOLUME) {
706 if (vallfn || invlfn) {
707 mod |= removede(f, boot, fat,
708 invlfn ? invlfn : vallfn, p,
709 invlfn ? invcl : valcl, -1, 0,
718 dirent.name[k++] = '.';
719 for (j = 0; j < 3; j++)
720 dirent.name[k++] = p[j+8];
721 dirent.name[k] = '\0';
722 for (k--; k >= 0 && dirent.name[k] == ' '; k--)
723 dirent.name[k] = '\0';
725 if (vallfn && shortSum != calcShortSum(p)) {
732 dirent.head = p[26] | (p[27] << 8);
733 if (boot->ClustMask == CLUST32_MASK)
734 dirent.head |= (p[20] << 16) | (p[21] << 24);
735 dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
737 strcpy(dirent.lname, longName);
743 dirent.next = dir->child;
746 mod |= k = removede(f, boot, fat,
747 invlfn, vallfn ? vallfn : p,
748 invcl, vallfn ? valcl : cl, cl,
749 fullpath(&dirent), 0);
753 ? (valcl == cl && vallfn != buffer)
759 vallfn = NULL; /* not used any longer */
762 if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
763 if (dirent.head != 0) {
764 pwarn("%s has clusters, but size 0\n",
766 if (ask(1, "Drop allocated clusters")) {
768 if (boot->ClustMask == CLUST32_MASK)
770 clearchain(boot, fat, dirent.head);
772 mod |= THISMOD|FSDIRMOD|FSFATMOD;
776 } else if (dirent.head == 0
777 && !strcmp(dirent.name, "..")
778 && dir->parent /* XXX */
779 && !dir->parent->parent) {
781 * Do nothing, the parent is the root
783 } else if (dirent.head < CLUST_FIRST
784 || dirent.head >= boot->NumClusters
785 || fat[dirent.head].next == CLUST_FREE
786 || (fat[dirent.head].next >= CLUST_RSRVD
787 && fat[dirent.head].next < CLUST_EOFS)
788 || fat[dirent.head].head != dirent.head) {
789 if (dirent.head == 0)
790 pwarn("%s has no clusters\n",
792 else if (dirent.head < CLUST_FIRST
793 || dirent.head >= boot->NumClusters)
794 pwarn("%s starts with cluster out of range(%u)\n",
797 else if (fat[dirent.head].next == CLUST_FREE)
798 pwarn("%s starts with free cluster\n",
800 else if (fat[dirent.head].next >= CLUST_RSRVD)
801 pwarn("%s starts with cluster marked %s\n",
803 rsrvdcltype(fat[dirent.head].next));
805 pwarn("%s doesn't start a cluster chain\n",
807 if (dirent.flags & ATTR_DIRECTORY) {
808 if (ask(1, "Remove")) {
810 mod |= THISMOD|FSDIRMOD;
815 if (ask(1, "Truncate")) {
816 p[28] = p[29] = p[30] = p[31] = 0;
818 if (boot->ClustMask == CLUST32_MASK)
821 mod |= THISMOD|FSDIRMOD;
827 if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
828 fat[dirent.head].flags |= FAT_USED;
830 if (dirent.flags & ATTR_DIRECTORY) {
832 * gather more info for directories
834 struct dirTodoNode *n;
837 pwarn("Directory %s has size != 0\n",
839 if (ask(1, "Correct")) {
840 p[28] = p[29] = p[30] = p[31] = 0;
842 mod |= THISMOD|FSDIRMOD;
847 * handle '.' and '..' specially
849 if (strcmp(dirent.name, ".") == 0) {
850 if (dirent.head != dir->head) {
851 pwarn("'.' entry in %s has incorrect start cluster\n",
853 if (ask(1, "Correct")) {
854 dirent.head = dir->head;
855 p[26] = (u_char)dirent.head;
856 p[27] = (u_char)(dirent.head >> 8);
857 if (boot->ClustMask == CLUST32_MASK) {
858 p[20] = (u_char)(dirent.head >> 16);
859 p[21] = (u_char)(dirent.head >> 24);
861 mod |= THISMOD|FSDIRMOD;
866 } else if (strcmp(dirent.name, "..") == 0) {
867 if (dir->parent) { /* XXX */
868 if (!dir->parent->parent) {
870 pwarn("'..' entry in %s has non-zero start cluster\n",
872 if (ask(1, "Correct")) {
875 if (boot->ClustMask == CLUST32_MASK)
877 mod |= THISMOD|FSDIRMOD;
881 } else if (dirent.head != dir->parent->head) {
882 pwarn("'..' entry in %s has incorrect start cluster\n",
884 if (ask(1, "Correct")) {
885 dirent.head = dir->parent->head;
886 p[26] = (u_char)dirent.head;
887 p[27] = (u_char)(dirent.head >> 8);
888 if (boot->ClustMask == CLUST32_MASK) {
889 p[20] = (u_char)(dirent.head >> 16);
890 p[21] = (u_char)(dirent.head >> 24);
892 mod |= THISMOD|FSDIRMOD;
898 } else { //only one directory entry can point to dir->head, it's '.'
899 if (dirent.head == dir->head) {
900 pwarn("%s entry in %s has incorrect start cluster.remove\n",
901 dirent.name, fullpath(dir));
902 //we have to remove this directory entry rigth now rigth here
903 if (ask(1, "Remove")) {
905 mod |= THISMOD|FSDIRMOD;
910 /* Consistency checking. a directory must have at least two entries:
911 a dot (.) entry that points to itself, and a dot-dot (..)
912 entry that points to its parent.
914 if (check_dot_dot(f,boot,fat,&dirent)) {
915 //mark directory entry as deleted.
916 if (ask(1, "Remove")) {
918 mod |= THISMOD|FSDIRMOD;
925 /* create directory tree node */
926 if (!(d = newDosDirEntry())) {
927 perror("No space for directory");
930 memcpy(d, &dirent, sizeof(struct dosDirEntry));
931 /* link it into the tree */
934 printf("%s: %s : 0x%02x:head %d, next 0x%0x parent 0x%0x child 0x%0x\n",
935 __func__,d->name,d->flags,d->head,d->next,d->parent,d->child);
937 /* Enter this directory into the todo list */
938 if (!(n = newDirTodo())) {
939 perror("No space for todo list");
942 n->next = pendingDirectories;
944 pendingDirectories = n;
946 mod |= k = checksize(boot, fat, p, &dirent);
954 if (lseek64(f, off, SEEK_SET) != off
955 || write(f, buffer, last) != last) {
956 perror("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,
967 return mod & ~THISMOD;
971 handleDirTree(int dosfs, struct bootblock *boot, struct fatEntry *fat)
975 mod = readDosDirSection(dosfs, boot, fat, rootDir);
980 * process the directory todo list
982 while (pendingDirectories) {
983 struct dosDirEntry *dir = pendingDirectories->dir;
984 struct dirTodoNode *n = pendingDirectories->next;
987 * remove TODO entry now, the list might change during
990 freeDirTodo(pendingDirectories);
991 pendingDirectories = n;
994 * handle subdirectory
996 mod |= readDosDirSection(dosfs, boot, fat, dir);
1005 * Try to reconnect a FAT chain into dir
1007 static u_char *lfbuf;
1009 static loff_t lfoff;
1012 reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
1014 struct dosDirEntry d;
1017 if (!ask(1, "Reconnect"))
1021 for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
1022 if (!strcmp(lostDir->name, LOSTDIR))
1025 if (!lostDir) { /* Create LOSTDIR? XXX */
1026 pwarn("No %s directory\n", LOSTDIR);
1031 lfbuf = malloc(boot->ClusterSize);
1033 perror("No space for buffer");
1041 for (; p < lfbuf + boot->ClusterSize; p += 32)
1042 if (*p == SLOT_EMPTY
1043 || *p == SLOT_DELETED)
1045 if (p && p < lfbuf + boot->ClusterSize)
1047 lfcl = p ? fat[lfcl].next : lostDir->head;
1048 if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
1049 /* Extend LOSTDIR? XXX */
1050 pwarn("No space in %s\n", LOSTDIR);
1051 lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
1054 lfoff = lfcl * boot->ClusterSize
1055 + boot->ClusterOffset * boot->BytesPerSec;
1056 if (lseek64(dosfs, lfoff, SEEK_SET) != lfoff
1057 || read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1058 perror("could not read LOST.DIR");
1065 /* Ensure uniqueness of entry here! XXX */
1066 memset(&d, 0, sizeof d);
1067 (void)snprintf(d.name, sizeof(d.name), "%u", head);
1070 d.size = fat[head].length * boot->ClusterSize;
1074 memcpy(p, d.name, strlen(d.name));
1075 p[26] = (u_char)d.head;
1076 p[27] = (u_char)(d.head >> 8);
1077 if (boot->ClustMask == CLUST32_MASK) {
1078 p[20] = (u_char)(d.head >> 16);
1079 p[21] = (u_char)(d.head >> 24);
1081 p[28] = (u_char)d.size;
1082 p[29] = (u_char)(d.size >> 8);
1083 p[30] = (u_char)(d.size >> 16);
1084 p[31] = (u_char)(d.size >> 24);
1085 fat[head].flags |= FAT_USED;
1086 if (lseek64(dosfs, lfoff, SEEK_SET) != lfoff
1087 || write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
1088 perror("could not write LOST.DIR");