Home | History | Annotate | Line # | Download | only in fsck_msdos
dir.c revision 1.3
      1  1.3  ws /*	$NetBSD: dir.c,v 1.3 1996/05/28 19:51:17 ws Exp $	*/
      2  1.1  ws 
      3  1.1  ws /*
      4  1.1  ws  * Copyright (C) 1995, 1996 Wolfgang Solfrank
      5  1.1  ws  * Copyright (c) 1995 Martin Husemann
      6  1.1  ws  * Some structure declaration borrowed from Paul Popelka
      7  1.1  ws  * (paulp (at) uts.amdahl.com), see /sys/msdosfs/ for reference.
      8  1.1  ws  *
      9  1.1  ws  * Redistribution and use in source and binary forms, with or without
     10  1.1  ws  * modification, are permitted provided that the following conditions
     11  1.1  ws  * are met:
     12  1.1  ws  * 1. Redistributions of source code must retain the above copyright
     13  1.1  ws  *    notice, this list of conditions and the following disclaimer.
     14  1.1  ws  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  ws  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  ws  *    documentation and/or other materials provided with the distribution.
     17  1.1  ws  * 3. All advertising materials mentioning features or use of this software
     18  1.1  ws  *    must display the following acknowledgement:
     19  1.1  ws  *	This product includes software developed by Martin Husemann
     20  1.1  ws  *	and Wolfgang Solfrank.
     21  1.1  ws  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  ws  *    may be used to endorse or promote products derived from this software
     23  1.1  ws  *    without specific prior written permission.
     24  1.1  ws  *
     25  1.1  ws  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     26  1.1  ws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27  1.1  ws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  1.1  ws  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     29  1.1  ws  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     30  1.1  ws  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31  1.1  ws  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32  1.1  ws  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33  1.1  ws  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     34  1.1  ws  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  1.1  ws  */
     36  1.1  ws 
     37  1.1  ws 
     38  1.1  ws #ifndef lint
     39  1.3  ws static char rcsid[] = "$NetBSD: dir.c,v 1.3 1996/05/28 19:51:17 ws Exp $";
     40  1.1  ws #endif /* not lint */
     41  1.1  ws 
     42  1.1  ws #include <stdio.h>
     43  1.1  ws #include <stdlib.h>
     44  1.1  ws #include <string.h>
     45  1.1  ws #include <ctype.h>
     46  1.1  ws #include <stdio.h>
     47  1.1  ws #include <unistd.h>
     48  1.1  ws #include <time.h>
     49  1.1  ws 
     50  1.2  ws #include <sys/param.h>
     51  1.2  ws 
     52  1.1  ws #include "ext.h"
     53  1.1  ws 
     54  1.1  ws #define	SLOT_EMPTY	0x00		/* slot has never been used */
     55  1.1  ws #define	SLOT_E5		0x05		/* the real value is 0xe5 */
     56  1.1  ws #define	SLOT_DELETED	0xe5		/* file in this slot deleted */
     57  1.1  ws 
     58  1.1  ws #define	ATTR_NORMAL	0x00		/* normal file */
     59  1.1  ws #define	ATTR_READONLY	0x01		/* file is readonly */
     60  1.1  ws #define	ATTR_HIDDEN	0x02		/* file is hidden */
     61  1.1  ws #define	ATTR_SYSTEM	0x04		/* file is a system file */
     62  1.1  ws #define	ATTR_VOLUME	0x08		/* entry is a volume label */
     63  1.1  ws #define	ATTR_DIRECTORY	0x10		/* entry is a directory name */
     64  1.1  ws #define	ATTR_ARCHIVE	0x20		/* file is new or modified */
     65  1.1  ws 
     66  1.1  ws #define	ATTR_WIN95	0x0f		/* long name record */
     67  1.1  ws 
     68  1.1  ws /*
     69  1.1  ws  * This is the format of the contents of the deTime field in the direntry
     70  1.1  ws  * structure.
     71  1.1  ws  * We don't use bitfields because we don't know how compilers for
     72  1.1  ws  * arbitrary machines will lay them out.
     73  1.1  ws  */
     74  1.1  ws #define DT_2SECONDS_MASK	0x1F	/* seconds divided by 2 */
     75  1.1  ws #define DT_2SECONDS_SHIFT	0
     76  1.1  ws #define DT_MINUTES_MASK		0x7E0	/* minutes */
     77  1.1  ws #define DT_MINUTES_SHIFT	5
     78  1.1  ws #define DT_HOURS_MASK		0xF800	/* hours */
     79  1.1  ws #define DT_HOURS_SHIFT		11
     80  1.1  ws 
     81  1.1  ws /*
     82  1.1  ws  * This is the format of the contents of the deDate field in the direntry
     83  1.1  ws  * structure.
     84  1.1  ws  */
     85  1.1  ws #define DD_DAY_MASK		0x1F	/* day of month */
     86  1.1  ws #define DD_DAY_SHIFT		0
     87  1.1  ws #define DD_MONTH_MASK		0x1E0	/* month */
     88  1.1  ws #define DD_MONTH_SHIFT		5
     89  1.1  ws #define DD_YEAR_MASK		0xFE00	/* year - 1980 */
     90  1.1  ws #define DD_YEAR_SHIFT		9
     91  1.1  ws 
     92  1.1  ws /*
     93  1.3  ws  * Manage free dosDirEntry structures.
     94  1.3  ws  */
     95  1.3  ws static struct dosDirEntry *freede;
     96  1.3  ws 
     97  1.3  ws static struct dosDirEntry *
     98  1.3  ws newDosDirEntry()
     99  1.3  ws {
    100  1.3  ws 	struct dosDirEntry *de;
    101  1.3  ws 
    102  1.3  ws 	if (!(de = freede)) {
    103  1.3  ws 		if (!(de = (struct dosDirEntry *)malloc(sizeof *de)))
    104  1.3  ws 			return 0;
    105  1.3  ws 	} else
    106  1.3  ws 		freede = de->next;
    107  1.3  ws 	return de;
    108  1.3  ws }
    109  1.3  ws 
    110  1.3  ws static void
    111  1.3  ws freeDosDirEntry(de)
    112  1.3  ws 	struct dosDirEntry *de;
    113  1.3  ws {
    114  1.3  ws 	de->next = freede;
    115  1.3  ws 	freede = de;
    116  1.3  ws }
    117  1.3  ws 
    118  1.3  ws /*
    119  1.3  ws  * The same for dirTodoNode structures.
    120  1.3  ws  */
    121  1.3  ws static struct dirTodoNode *freedt;
    122  1.3  ws 
    123  1.3  ws static struct dirTodoNode *
    124  1.3  ws newDirTodo()
    125  1.3  ws {
    126  1.3  ws 	struct dirTodoNode *dt;
    127  1.3  ws 
    128  1.3  ws 	if (!(dt = freedt)) {
    129  1.3  ws 		if (!(dt = (struct dirTodoNode *)malloc(sizeof *dt)))
    130  1.3  ws 			return 0;
    131  1.3  ws 	} else
    132  1.3  ws 		freedt = dt->next;
    133  1.3  ws 	return dt;
    134  1.3  ws }
    135  1.3  ws 
    136  1.3  ws static void
    137  1.3  ws freeDirTodo(dt)
    138  1.3  ws 	struct dirTodoNode *dt;
    139  1.3  ws {
    140  1.3  ws 	dt->next = freedt;
    141  1.3  ws 	freedt = dt;
    142  1.3  ws }
    143  1.3  ws 
    144  1.3  ws /*
    145  1.3  ws  * The stack of unread directories
    146  1.3  ws  */
    147  1.3  ws struct dirTodoNode *pendingDirectories = NULL;
    148  1.3  ws 
    149  1.3  ws /*
    150  1.2  ws  * Return the full pathname for a directory entry.
    151  1.2  ws  */
    152  1.2  ws static char *
    153  1.2  ws fullpath(dir)
    154  1.2  ws 	struct dosDirEntry *dir;
    155  1.2  ws {
    156  1.2  ws 	static char namebuf[MAXPATHLEN + 1];
    157  1.2  ws 	char *cp, *np;
    158  1.2  ws 	int nl;
    159  1.2  ws 
    160  1.2  ws 	cp = namebuf + sizeof namebuf - 1;
    161  1.2  ws 	*cp = '\0';
    162  1.2  ws 	do {
    163  1.2  ws 		np = dir->lname[0] ? dir->lname : dir->name;
    164  1.2  ws 		nl = strlen(np);
    165  1.2  ws 		if ((cp -= nl) <= namebuf + 1)
    166  1.2  ws 			break;
    167  1.2  ws 		memcpy(cp, np, nl);
    168  1.2  ws 		*--cp = '/';
    169  1.3  ws 	} while (dir = dir->parent);
    170  1.2  ws 	if (dir->parent)
    171  1.2  ws 		*--cp = '?';
    172  1.2  ws 	return cp;
    173  1.2  ws }
    174  1.2  ws 
    175  1.2  ws /*
    176  1.1  ws  * Calculate a checksum over an 8.3 alias name
    177  1.1  ws  */
    178  1.1  ws static u_char
    179  1.1  ws calcShortSum(p)
    180  1.1  ws 	u_char *p;
    181  1.1  ws {
    182  1.1  ws 	u_char sum = 0;
    183  1.1  ws 	int i;
    184  1.1  ws 
    185  1.1  ws 	for (i = 0; i < 11; i++) {
    186  1.1  ws 		sum = (sum << 7)|(sum >> 1);	/* rotate right */
    187  1.1  ws 		sum += p[i];
    188  1.1  ws 	}
    189  1.1  ws 
    190  1.1  ws 	return sum;
    191  1.1  ws }
    192  1.1  ws 
    193  1.1  ws /*
    194  1.1  ws  * Global variables temporarily used during a directory scan
    195  1.1  ws  */
    196  1.1  ws static char longName[DOSLONGNAMELEN] = "";
    197  1.1  ws static u_char *buffer = NULL;
    198  1.1  ws static u_char *delbuf = NULL;
    199  1.1  ws 
    200  1.3  ws struct dosDirEntry *rootDir;
    201  1.3  ws static struct dosDirEntry *lostDir;
    202  1.3  ws 
    203  1.1  ws /*
    204  1.1  ws  * Init internal state for a new directory scan.
    205  1.1  ws  */
    206  1.1  ws int
    207  1.1  ws resetDosDirSection(boot)
    208  1.1  ws 	struct bootblock *boot;
    209  1.1  ws {
    210  1.1  ws 	int b1, b2;
    211  1.1  ws 
    212  1.1  ws 	b1 = boot->RootDirEnts * 32;
    213  1.1  ws 	b2 = boot->SecPerClust * boot->BytesPerSec;
    214  1.1  ws 
    215  1.1  ws 	if (!(buffer = malloc(b1 > b2 ? b1 : b2))
    216  1.3  ws 	    || !(delbuf = malloc(b2))
    217  1.3  ws 	    || !(rootDir = newDosDirEntry())) {
    218  1.1  ws 		perror("No space for directory");
    219  1.1  ws 		return FSFATAL;
    220  1.1  ws 	}
    221  1.3  ws 	memset(rootDir, 0, sizeof *rootDir);
    222  1.1  ws 	return FSOK;
    223  1.1  ws }
    224  1.1  ws 
    225  1.1  ws /*
    226  1.1  ws  * Cleanup after a directory scan
    227  1.1  ws  */
    228  1.1  ws void
    229  1.1  ws finishDosDirSection()
    230  1.1  ws {
    231  1.3  ws 	struct dirTodoNode *p, *np;
    232  1.3  ws 	struct dosDirEntry *d, *nd;
    233  1.3  ws 
    234  1.3  ws 	for (p = pendingDirectories; p; p = np) {
    235  1.3  ws 		np = p->next;
    236  1.3  ws 		freeDirTodo(p);
    237  1.3  ws 	}
    238  1.3  ws 	pendingDirectories = 0;
    239  1.3  ws 	for (d = rootDir; d; d = nd) {
    240  1.3  ws 		if (nd = d->child) {
    241  1.3  ws 			d->child = 0;
    242  1.3  ws 			continue;
    243  1.3  ws 		}
    244  1.3  ws 		if (!(nd = d->next))
    245  1.3  ws 			nd = d->parent;
    246  1.3  ws 		freeDosDirEntry(d);
    247  1.3  ws 	}
    248  1.3  ws 	rootDir = lostDir = NULL;
    249  1.1  ws 	free(buffer);
    250  1.1  ws 	free(delbuf);
    251  1.1  ws 	buffer = NULL;
    252  1.1  ws 	delbuf = NULL;
    253  1.1  ws }
    254  1.1  ws 
    255  1.1  ws /*
    256  1.1  ws  * Delete directory entries between startcl, startoff and endcl, endoff.
    257  1.1  ws  */
    258  1.1  ws static int
    259  1.1  ws delete(f, boot, fat, startcl, startoff, endcl, endoff, notlast)
    260  1.1  ws 	int f;
    261  1.1  ws 	struct bootblock *boot;
    262  1.1  ws 	struct fatEntry *fat;
    263  1.1  ws 	cl_t startcl;
    264  1.1  ws 	int startoff;
    265  1.1  ws 	cl_t endcl;
    266  1.1  ws 	int endoff;
    267  1.1  ws 	int notlast;
    268  1.1  ws {
    269  1.1  ws 	u_char *s, *e;
    270  1.1  ws 	off_t off;
    271  1.1  ws 	int clsz = boot->SecPerClust * boot->BytesPerSec;
    272  1.1  ws 
    273  1.1  ws 	s = delbuf + startoff;
    274  1.1  ws 	e = delbuf + clsz;
    275  1.1  ws 	while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
    276  1.1  ws 		if (startcl == endcl) {
    277  1.1  ws 			if (notlast)
    278  1.1  ws 				break;
    279  1.1  ws 			e = delbuf + endoff;
    280  1.1  ws 		}
    281  1.1  ws 		off = startcl * boot->SecPerClust + boot->ClusterOffset;
    282  1.1  ws 		off *= boot->BytesPerSec;
    283  1.1  ws 		if (lseek(f, off, SEEK_SET) != off
    284  1.1  ws 		    || read(f, delbuf, clsz) != clsz) {
    285  1.1  ws 			perror("Unable to read directory");
    286  1.1  ws 			return FSFATAL;
    287  1.1  ws 		}
    288  1.1  ws 		while (s < e) {
    289  1.1  ws 			*s = SLOT_DELETED;
    290  1.1  ws 			s += 32;
    291  1.1  ws 		}
    292  1.1  ws 		if (lseek(f, off, SEEK_SET) != off
    293  1.1  ws 		    || write(f, delbuf, clsz) != clsz) {
    294  1.1  ws 			perror("Unable to write directory");
    295  1.1  ws 			return FSFATAL;
    296  1.1  ws 		}
    297  1.1  ws 		if (startcl == endcl)
    298  1.1  ws 			break;
    299  1.1  ws 		startcl = fat[startcl].next;
    300  1.1  ws 		s = delbuf;
    301  1.1  ws 	}
    302  1.1  ws 	return FSOK;
    303  1.1  ws }
    304  1.1  ws 
    305  1.1  ws static int
    306  1.3  ws removede(f, boot, fat, start, end, startcl, endcl, curcl, path, type)
    307  1.1  ws 	int f;
    308  1.1  ws 	struct bootblock *boot;
    309  1.1  ws 	struct fatEntry *fat;
    310  1.1  ws 	u_char *start;
    311  1.1  ws 	u_char *end;
    312  1.1  ws 	cl_t startcl;
    313  1.1  ws 	cl_t endcl;
    314  1.1  ws 	cl_t curcl;
    315  1.1  ws 	char *path;
    316  1.3  ws 	int type;
    317  1.1  ws {
    318  1.3  ws 	switch (type) {
    319  1.3  ws 	case 0:
    320  1.1  ws 		pwarn("Invalid long filename entry for %s\n", path);
    321  1.3  ws 		break;
    322  1.3  ws 	case 1:
    323  1.1  ws 		pwarn("Invalid long filename entry at end of directory %s\n", path);
    324  1.3  ws 		break;
    325  1.3  ws 	case 2:
    326  1.3  ws 		pwarn("Invalid long filename entry for volume label\n");
    327  1.3  ws 		break;
    328  1.3  ws 	}
    329  1.1  ws 	if (ask(0, "Remove")) {
    330  1.1  ws 		if (startcl != curcl) {
    331  1.1  ws 			if (delete(f, boot, fat,
    332  1.1  ws 				   startcl, start - buffer,
    333  1.1  ws 				   endcl, end - buffer,
    334  1.1  ws 				   endcl == curcl) == FSFATAL)
    335  1.1  ws 				return FSFATAL;
    336  1.1  ws 			start = buffer;
    337  1.1  ws 		}
    338  1.1  ws 		if (endcl == curcl)
    339  1.1  ws 			for (; start < end; start += 32)
    340  1.1  ws 				*start = SLOT_DELETED;
    341  1.1  ws 		return FSDIRMOD;
    342  1.1  ws 	}
    343  1.1  ws 	return FSERROR;
    344  1.1  ws }
    345  1.1  ws 
    346  1.1  ws /*
    347  1.1  ws  * Check an in-memory file entry
    348  1.1  ws  */
    349  1.1  ws static int
    350  1.1  ws checksize(boot, fat, p, dir)
    351  1.1  ws 	struct bootblock *boot;
    352  1.1  ws 	struct fatEntry *fat;
    353  1.1  ws 	u_char *p;
    354  1.1  ws 	struct dosDirEntry *dir;
    355  1.1  ws {
    356  1.1  ws 	/*
    357  1.1  ws 	 * Check size on ordinary files
    358  1.1  ws 	 */
    359  1.3  ws 	int32_t physicalSize;
    360  1.1  ws 
    361  1.3  ws 	if (dir->head == CLUST_FREE)
    362  1.3  ws 		physicalSize = 0;
    363  1.3  ws 	else {
    364  1.3  ws 		if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
    365  1.3  ws 			return FSERROR;
    366  1.3  ws 		physicalSize = fat[dir->head].length * boot->ClusterSize;
    367  1.3  ws 	}
    368  1.1  ws 	if (physicalSize < dir->size) {
    369  1.1  ws 		pwarn("size of %s is %lu, should at most be %lu\n",
    370  1.2  ws 		      fullpath(dir), dir->size, physicalSize);
    371  1.1  ws 		if (ask(1, "Truncate")) {
    372  1.1  ws 			dir->size = physicalSize;
    373  1.1  ws 			p[28] = (u_char)physicalSize;
    374  1.1  ws 			p[29] = (u_char)(physicalSize >> 8);
    375  1.1  ws 			p[30] = (u_char)(physicalSize >> 16);
    376  1.1  ws 			p[31] = (u_char)(physicalSize >> 24);
    377  1.1  ws 			return FSDIRMOD;
    378  1.1  ws 		} else
    379  1.1  ws 			return FSERROR;
    380  1.1  ws 	} else if (physicalSize - dir->size >= boot->ClusterSize) {
    381  1.1  ws 		pwarn("%s has too many clusters allocated\n",
    382  1.2  ws 		      fullpath(dir));
    383  1.1  ws 		if (ask(1, "Drop superfluous clusters")) {
    384  1.1  ws 			cl_t cl;
    385  1.1  ws 			u_int32_t sz = 0;
    386  1.1  ws 
    387  1.1  ws 			for (cl = dir->head; (sz += boot->ClusterSize) < dir->size;)
    388  1.1  ws 				cl = fat[cl].next;
    389  1.1  ws 			clearchain(boot, fat, fat[cl].next);
    390  1.1  ws 			fat[cl].next = CLUST_EOF;
    391  1.1  ws 			return FSFATMOD;
    392  1.1  ws 		} else
    393  1.1  ws 			return FSERROR;
    394  1.1  ws 	}
    395  1.1  ws 	return FSOK;
    396  1.1  ws }
    397  1.1  ws 
    398  1.1  ws /*
    399  1.1  ws  * Read a directory and
    400  1.1  ws  *   - resolve long name records
    401  1.1  ws  *   - enter file and directory records into the parent's list
    402  1.1  ws  *   - push directories onto the todo-stack
    403  1.1  ws  */
    404  1.3  ws static int
    405  1.1  ws readDosDirSection(f, boot, fat, dir)
    406  1.1  ws 	int f;
    407  1.1  ws 	struct bootblock *boot;
    408  1.1  ws 	struct fatEntry *fat;
    409  1.1  ws 	struct dosDirEntry *dir;
    410  1.1  ws {
    411  1.1  ws 	struct dosDirEntry dirent, *d;
    412  1.1  ws 	u_char *p, *vallfn, *invlfn, *empty;
    413  1.1  ws 	off_t off;
    414  1.1  ws 	int i, j, k, last;
    415  1.1  ws 	cl_t cl, valcl, invcl, empcl;
    416  1.1  ws 	char *t;
    417  1.1  ws 	u_int lidx = 0;
    418  1.1  ws 	int shortSum;
    419  1.1  ws 	int mod = FSOK;
    420  1.1  ws #define	THISMOD	0x8000			/* Only used within this routine */
    421  1.1  ws 
    422  1.1  ws 	cl = dir->head;
    423  1.2  ws 	if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
    424  1.1  ws 		/*
    425  1.1  ws 		 * Already handled somewhere else.
    426  1.1  ws 		 */
    427  1.1  ws 		return FSOK;
    428  1.1  ws 	}
    429  1.1  ws 	shortSum = -1;
    430  1.1  ws 	vallfn = invlfn = empty = NULL;
    431  1.1  ws 	do {
    432  1.2  ws 		if (!dir->parent) {
    433  1.1  ws 			last = boot->RootDirEnts * 32;
    434  1.1  ws 			off = boot->ResSectors + boot->FATs * boot->FATsecs;
    435  1.1  ws 		} else {
    436  1.1  ws 			last = boot->SecPerClust * boot->BytesPerSec;
    437  1.1  ws 			off = cl * boot->SecPerClust + boot->ClusterOffset;
    438  1.1  ws 		}
    439  1.1  ws 
    440  1.1  ws 		off *= boot->BytesPerSec;
    441  1.1  ws 		if (lseek(f, off, SEEK_SET) != off
    442  1.1  ws 		    || read(f, buffer, last) != last) {
    443  1.1  ws 			perror("Unable to read directory");
    444  1.1  ws 			return FSFATAL;
    445  1.1  ws 		}
    446  1.1  ws 		last /= 32;
    447  1.1  ws 		/*
    448  1.1  ws 		 * Check `.' and `..' entries here?			XXX
    449  1.1  ws 		 */
    450  1.1  ws 		for (p = buffer, i = 0; i < last; i++, p += 32) {
    451  1.1  ws 			if (dir->fsckflags & DIREMPWARN) {
    452  1.1  ws 				*p = SLOT_EMPTY;
    453  1.1  ws 				continue;
    454  1.1  ws 			}
    455  1.1  ws 
    456  1.1  ws 			if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
    457  1.1  ws 				if (*p == SLOT_EMPTY) {
    458  1.1  ws 					dir->fsckflags |= DIREMPTY;
    459  1.1  ws 					empty = p;
    460  1.1  ws 					empcl = cl;
    461  1.1  ws 				}
    462  1.1  ws 				continue;
    463  1.1  ws 			}
    464  1.1  ws 
    465  1.1  ws 			if (dir->fsckflags & DIREMPTY) {
    466  1.1  ws 				if (!(dir->fsckflags & DIREMPWARN)) {
    467  1.1  ws 					pwarn("%s has entries after end of directory\n",
    468  1.2  ws 					      fullpath(dir));
    469  1.1  ws 					if (ask(1, "Extend")) {
    470  1.1  ws 						dir->fsckflags &= ~DIREMPTY;
    471  1.1  ws 						if (delete(f, boot, fat,
    472  1.1  ws 							   empcl, empty - buffer,
    473  1.1  ws 							   cl, p - buffer) == FSFATAL)
    474  1.1  ws 							return FSFATAL;
    475  1.1  ws 					} else if (ask(0, "Truncate"))
    476  1.1  ws 						dir->fsckflags |= DIREMPWARN;
    477  1.1  ws 				}
    478  1.1  ws 				if (dir->fsckflags & DIREMPWARN) {
    479  1.1  ws 					*p = SLOT_DELETED;
    480  1.1  ws 					mod |= THISMOD|FSDIRMOD;
    481  1.1  ws 					continue;
    482  1.1  ws 				} else if (dir->fsckflags & DIREMPTY)
    483  1.1  ws 					mod |= FSERROR;
    484  1.1  ws 				empty = NULL;
    485  1.1  ws 			}
    486  1.1  ws 
    487  1.1  ws 			if (p[11] == ATTR_WIN95) {
    488  1.1  ws 				if (*p & LRFIRST) {
    489  1.1  ws 					if (shortSum != -1) {
    490  1.1  ws 						if (!invlfn) {
    491  1.1  ws 							invlfn = vallfn;
    492  1.1  ws 							invcl = valcl;
    493  1.1  ws 						}
    494  1.1  ws 					}
    495  1.1  ws 					memset(longName, 0, sizeof longName);
    496  1.1  ws 					shortSum = p[13];
    497  1.1  ws 					vallfn = p;
    498  1.1  ws 					valcl = cl;
    499  1.1  ws 				} else if (shortSum != p[13]
    500  1.1  ws 					   || lidx != *p & LRNOMASK) {
    501  1.1  ws 					if (!invlfn) {
    502  1.1  ws 						invlfn = vallfn;
    503  1.1  ws 						invcl = valcl;
    504  1.1  ws 					}
    505  1.1  ws 					if (!invlfn) {
    506  1.1  ws 						invlfn = p;
    507  1.1  ws 						invcl = cl;
    508  1.1  ws 					}
    509  1.1  ws 					vallfn = NULL;
    510  1.1  ws 				}
    511  1.1  ws 				lidx = *p & LRNOMASK;
    512  1.1  ws 				t = longName + --lidx * 13;
    513  1.1  ws 				for (k = 1; k < 11 && t < longName + sizeof(longName); k += 2) {
    514  1.1  ws 					if (!p[k] && !p[k + 1])
    515  1.1  ws 						break;
    516  1.1  ws 					*t++ = p[k];
    517  1.1  ws 					/*
    518  1.1  ws 					 * Warn about those unusable chars in msdosfs here?	XXX
    519  1.1  ws 					 */
    520  1.1  ws 					if (p[k + 1])
    521  1.1  ws 						t[-1] = '?';
    522  1.1  ws 				}
    523  1.1  ws 				if (k >= 11)
    524  1.1  ws 					for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
    525  1.1  ws 						if (!p[k] && !p[k + 1])
    526  1.1  ws 							break;
    527  1.1  ws 						*t++ = p[k];
    528  1.1  ws 						if (p[k + 1])
    529  1.1  ws 							t[-1] = '?';
    530  1.1  ws 					}
    531  1.1  ws 				if (k >= 26)
    532  1.1  ws 					for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
    533  1.1  ws 						if (!p[k] && !p[k + 1])
    534  1.1  ws 							break;
    535  1.1  ws 						*t++ = p[k];
    536  1.1  ws 						if (p[k + 1])
    537  1.1  ws 							t[-1] = '?';
    538  1.1  ws 					}
    539  1.1  ws 				if (t >= longName + sizeof(longName)) {
    540  1.1  ws 					pwarn("long filename too long\n");
    541  1.1  ws 					if (!invlfn) {
    542  1.1  ws 						invlfn = vallfn;
    543  1.1  ws 						invcl = valcl;
    544  1.1  ws 					}
    545  1.1  ws 					vallfn = NULL;
    546  1.1  ws 				}
    547  1.1  ws 				if (p[26] | (p[27] << 8)) {
    548  1.1  ws 					pwarn("long filename record cluster start != 0\n");
    549  1.1  ws 					if (!invlfn) {
    550  1.1  ws 						invlfn = vallfn;
    551  1.1  ws 						invcl = cl;
    552  1.1  ws 					}
    553  1.1  ws 					vallfn = NULL;
    554  1.1  ws 				}
    555  1.1  ws 				continue;	/* long records don't carry further
    556  1.1  ws 						 * information */
    557  1.1  ws 			}
    558  1.1  ws 
    559  1.1  ws 			/*
    560  1.1  ws 			 * This is a standard msdosfs directory entry.
    561  1.1  ws 			 */
    562  1.1  ws 			memset(&dirent, 0, sizeof dirent);
    563  1.1  ws 
    564  1.1  ws 			/*
    565  1.1  ws 			 * it's a short name record, but we need to know
    566  1.1  ws 			 * more, so get the flags first.
    567  1.1  ws 			 */
    568  1.1  ws 			dirent.flags = p[11];
    569  1.1  ws 
    570  1.1  ws 			/*
    571  1.1  ws 			 * Translate from 850 to ISO here		XXX
    572  1.1  ws 			 */
    573  1.1  ws 			for (j = 0; j < 8; j++)
    574  1.1  ws 				dirent.name[j] = p[j];
    575  1.1  ws 			dirent.name[8] = '\0';
    576  1.1  ws 			for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
    577  1.1  ws 				dirent.name[k] = '\0';
    578  1.1  ws 			if (dirent.name[k] != '\0')
    579  1.1  ws 				k++;
    580  1.1  ws 			if (dirent.name[0] == SLOT_E5)
    581  1.1  ws 				dirent.name[0] = 0xe5;
    582  1.3  ws 
    583  1.3  ws 			if (dirent.flags & ATTR_VOLUME) {
    584  1.3  ws 				if (vallfn || invlfn) {
    585  1.3  ws 					mod |= removede(f, boot, fat,
    586  1.3  ws 							invlfn ? invlfn : vallfn, p,
    587  1.3  ws 							invlfn ? invcl : valcl, -1, 0,
    588  1.3  ws 							fullpath(dir), 2);
    589  1.3  ws 					vallfn = NULL;
    590  1.3  ws 					invlfn = NULL;
    591  1.3  ws 				}
    592  1.3  ws 				continue;
    593  1.3  ws 			}
    594  1.3  ws 
    595  1.3  ws 			if (p[8] != ' ')
    596  1.1  ws 				dirent.name[k++] = '.';
    597  1.1  ws 			for (j = 0; j < 3; j++)
    598  1.1  ws 				dirent.name[k++] = p[j+8];
    599  1.1  ws 			dirent.name[k] = '\0';
    600  1.1  ws 			for (k--; k >= 0 && dirent.name[k] == ' '; k--)
    601  1.1  ws 				dirent.name[k] = '\0';
    602  1.1  ws 
    603  1.1  ws 			if (vallfn && shortSum != calcShortSum(p)) {
    604  1.1  ws 				if (!invlfn) {
    605  1.1  ws 					invlfn = vallfn;
    606  1.1  ws 					invcl = valcl;
    607  1.1  ws 				}
    608  1.1  ws 				vallfn = NULL;
    609  1.1  ws 			}
    610  1.1  ws 			dirent.head = p[26] | (p[27] << 8);
    611  1.1  ws 			dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
    612  1.1  ws 			if (vallfn) {
    613  1.1  ws 				strcpy(dirent.lname, longName);
    614  1.1  ws 				longName[0] = '\0';
    615  1.1  ws 				shortSum = -1;
    616  1.1  ws 			}
    617  1.3  ws 
    618  1.1  ws 			if (invlfn) {
    619  1.1  ws 				mod |= k = removede(f, boot, fat,
    620  1.1  ws 						    invlfn, vallfn ? vallfn : p,
    621  1.1  ws 						    invcl, vallfn ? valcl : cl, cl,
    622  1.2  ws 						    fullpath(&dirent), 0);
    623  1.1  ws 				if (mod & FSFATAL)
    624  1.1  ws 					return FSFATAL;
    625  1.1  ws 				if (vallfn
    626  1.1  ws 				    ? (valcl == cl && vallfn != buffer)
    627  1.1  ws 				    : p != buffer)
    628  1.1  ws 					if (k & FSDIRMOD)
    629  1.1  ws 						mod |= THISMOD;
    630  1.1  ws 			}
    631  1.1  ws 			vallfn = NULL; /* not used any longer */
    632  1.1  ws 			invlfn = NULL;
    633  1.1  ws 
    634  1.1  ws 			if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
    635  1.1  ws 				if (dirent.head != 0) {
    636  1.1  ws 					pwarn("%s has clusters, but size 0\n",
    637  1.2  ws 					      fullpath(&dirent));
    638  1.1  ws 					if (ask(1, "Drop allocated clusters")) {
    639  1.1  ws 						p[26] = p[27] = 0;
    640  1.1  ws 						clearchain(boot, fat, dirent.head);
    641  1.1  ws 						dirent.head = 0;
    642  1.1  ws 						mod |= THISMOD|FSDIRMOD|FSFATMOD;
    643  1.1  ws 					} else
    644  1.1  ws 						mod |= FSERROR;
    645  1.1  ws 				}
    646  1.1  ws 			} else if (dirent.head == 0
    647  1.1  ws 				   && !strcmp(dirent.name, "..")
    648  1.3  ws 				   && dir->parent			/* XXX */
    649  1.2  ws 				   && !dir->parent->parent) {
    650  1.1  ws 				/*
    651  1.1  ws 				 *  Do nothing, the parent is the root
    652  1.1  ws 				 */
    653  1.1  ws 			} else if (dirent.head < CLUST_FIRST
    654  1.1  ws 				   || dirent.head >= boot->NumClusters
    655  1.1  ws 				   || fat[dirent.head].next == CLUST_FREE
    656  1.1  ws 				   || (fat[dirent.head].next >= CLUST_RSRVD
    657  1.1  ws 				       && fat[dirent.head].next < CLUST_EOFS)
    658  1.1  ws 				   || fat[dirent.head].head != dirent.head) {
    659  1.1  ws 				if (dirent.head == 0)
    660  1.1  ws 					pwarn("%s has no clusters\n",
    661  1.2  ws 					      fullpath(&dirent));
    662  1.1  ws 				else if (dirent.head < CLUST_FIRST
    663  1.1  ws 					 || dirent.head >= boot->NumClusters)
    664  1.1  ws 					pwarn("%s starts with cluster out of range(%d)\n",
    665  1.2  ws 					      fullpath(&dirent),
    666  1.1  ws 					      dirent.head);
    667  1.1  ws 				else if (fat[dirent.head].next == CLUST_FREE)
    668  1.1  ws 					pwarn("%s starts with free cluster\n",
    669  1.2  ws 					      fullpath(&dirent));
    670  1.1  ws 				else if (fat[dirent.head].next >= CLUST_RSRVD)
    671  1.1  ws 					pwarn("%s starts with %s cluster\n",
    672  1.2  ws 					      fullpath(&dirent),
    673  1.1  ws 					      rsrvdcltype(fat[dirent.head].next));
    674  1.1  ws 				else
    675  1.1  ws 					pwarn("%s doesn't start a cluster chain\n",
    676  1.2  ws 					      fullpath(&dirent));
    677  1.1  ws 				if (dirent.flags & ATTR_DIRECTORY) {
    678  1.1  ws 					if (ask(0, "Remove")) {
    679  1.1  ws 						*p = SLOT_DELETED;
    680  1.1  ws 						mod |= THISMOD|FSDIRMOD;
    681  1.1  ws 					} else
    682  1.1  ws 						mod |= FSERROR;
    683  1.1  ws 					continue;
    684  1.1  ws 				} else {
    685  1.1  ws 					if (ask(1, "Truncate")) {
    686  1.1  ws 						p[28] = p[29] = p[30] = p[31] = 0;
    687  1.1  ws 						dirent.size = 0;
    688  1.1  ws 						mod |= THISMOD|FSDIRMOD;
    689  1.1  ws 					} else
    690  1.1  ws 						mod |= FSERROR;
    691  1.1  ws 				}
    692  1.1  ws 			}
    693  1.1  ws 
    694  1.3  ws 			dirent.parent = dir;
    695  1.3  ws 			dirent.next = dir->child;
    696  1.3  ws 			if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
    697  1.3  ws 				fat[dirent.head].flags |= FAT_USED;
    698  1.1  ws 
    699  1.3  ws 			if (dirent.flags & ATTR_DIRECTORY) {
    700  1.1  ws 				/*
    701  1.1  ws 				 * gather more info for directories
    702  1.1  ws 				 */
    703  1.1  ws 				struct dirTodoNode * n;
    704  1.1  ws 
    705  1.3  ws 				if (dirent.size) {
    706  1.1  ws 					pwarn("Directory %s has size != 0\n",
    707  1.3  ws 					      fullpath(&dirent));
    708  1.1  ws 					if (ask(1, "Correct")) {
    709  1.1  ws 						p[28] = p[29] = p[30] = p[31] = 0;
    710  1.3  ws 						dirent.size = 0;
    711  1.1  ws 						mod |= THISMOD|FSDIRMOD;
    712  1.1  ws 					} else
    713  1.1  ws 						mod |= FSERROR;
    714  1.1  ws 				}
    715  1.1  ws 				/*
    716  1.1  ws 				 * handle `.' and `..' specially
    717  1.1  ws 				 */
    718  1.3  ws 				if (strcmp(dirent.name, ".") == 0) {
    719  1.3  ws 					if (dirent.head != dir->head) {
    720  1.1  ws 						pwarn("`.' entry in %s has incorrect start cluster\n",
    721  1.2  ws 						      fullpath(dir));
    722  1.1  ws 						if (ask(1, "Correct")) {
    723  1.3  ws 							dirent.head = dir->head;
    724  1.3  ws 							p[26] = (u_char)dirent.head;
    725  1.3  ws 							p[27] = (u_char)(dirent.head >> 8);
    726  1.1  ws 							mod |= THISMOD|FSDIRMOD;
    727  1.1  ws 						} else
    728  1.1  ws 							mod |= FSERROR;
    729  1.1  ws 					}
    730  1.1  ws 					continue;
    731  1.1  ws 				}
    732  1.3  ws 				if (strcmp(dirent.name, "..") == 0) {
    733  1.3  ws 					if (dir->parent			/* XXX */
    734  1.3  ws 					    && dirent.head != dir->parent->head) {
    735  1.1  ws 						pwarn("`..' entry in %s has incorrect start cluster\n",
    736  1.2  ws 						      fullpath(dir));
    737  1.1  ws 						if (ask(1, "Correct")) {
    738  1.3  ws 							dirent.head = dir->parent->head;
    739  1.3  ws 							p[26] = (u_char)dirent.head;
    740  1.3  ws 							p[27] = (u_char)(dirent.head >> 8);
    741  1.1  ws 							mod |= THISMOD|FSDIRMOD;
    742  1.1  ws 						} else
    743  1.1  ws 							mod |= FSERROR;
    744  1.1  ws 					}
    745  1.1  ws 					continue;
    746  1.1  ws 				}
    747  1.1  ws 
    748  1.1  ws 				boot->NumFiles++;
    749  1.3  ws 
    750  1.3  ws 				/* create directory tree node */
    751  1.3  ws 				if (!(d = newDosDirEntry())) {
    752  1.3  ws 					perror("No space for directory");
    753  1.3  ws 					return FSFATAL;
    754  1.3  ws 				}
    755  1.3  ws 
    756  1.3  ws 				memcpy(d, &dirent, sizeof(struct dosDirEntry));
    757  1.3  ws 				/* link it into the tree */
    758  1.3  ws 				dir->child = d;
    759  1.3  ws 
    760  1.1  ws 				/* Enter this directory into the todo list */
    761  1.3  ws 				if (!(n = newDirTodo())) {
    762  1.2  ws 					perror("No space for todo list");
    763  1.2  ws 					return FSFATAL;
    764  1.2  ws 				}
    765  1.1  ws 				n->next = pendingDirectories;
    766  1.1  ws 				n->dir = d;
    767  1.1  ws 				pendingDirectories = n;
    768  1.1  ws 			} else {
    769  1.3  ws 				mod |= k = checksize(boot, fat, p, &dirent);
    770  1.1  ws 				if (k & FSDIRMOD)
    771  1.1  ws 					mod |= THISMOD;
    772  1.1  ws 				boot->NumFiles++;
    773  1.1  ws 			}
    774  1.1  ws 		}
    775  1.1  ws 		if (mod & THISMOD) {
    776  1.1  ws 			last *= 32;
    777  1.1  ws 			if (lseek(f, off, SEEK_SET) != off
    778  1.1  ws 			    || write(f, buffer, last) != last) {
    779  1.1  ws 				perror("Unable to write directory");
    780  1.1  ws 				return FSFATAL;
    781  1.1  ws 			}
    782  1.1  ws 			mod &= ~THISMOD;
    783  1.1  ws 		}
    784  1.1  ws 	} while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
    785  1.1  ws 	if (invlfn || vallfn)
    786  1.1  ws 		mod |= removede(f, boot, fat,
    787  1.1  ws 				invlfn ? invlfn : vallfn, p,
    788  1.1  ws 				invlfn ? invcl : valcl, -1, 0,
    789  1.2  ws 				fullpath(dir), 1);
    790  1.1  ws 	return mod & ~THISMOD;
    791  1.1  ws }
    792  1.1  ws 
    793  1.3  ws int
    794  1.3  ws handleDirTree(dosfs, boot, fat)
    795  1.3  ws 	int dosfs;
    796  1.3  ws 	struct bootblock *boot;
    797  1.3  ws 	struct fatEntry *fat;
    798  1.3  ws {
    799  1.3  ws 	int mod;
    800  1.3  ws 
    801  1.3  ws 	mod = readDosDirSection(dosfs, boot, fat, rootDir);
    802  1.3  ws 	if (mod & FSFATAL)
    803  1.3  ws 		return FSFATAL;
    804  1.3  ws 
    805  1.3  ws 	if (mod & FSFATMOD) {
    806  1.3  ws 		mod &= ~FSFATMOD;
    807  1.3  ws 		mod |= writefat(dosfs, boot, fat); /* delay writing fats?	XXX */
    808  1.3  ws 	}
    809  1.3  ws 
    810  1.3  ws 	if (mod & FSFATAL)
    811  1.3  ws 		return FSFATAL;
    812  1.3  ws 
    813  1.3  ws 	/*
    814  1.3  ws 	 * process the directory todo list
    815  1.3  ws 	 */
    816  1.3  ws 	while (pendingDirectories) {
    817  1.3  ws 		struct dosDirEntry *dir = pendingDirectories->dir;
    818  1.3  ws 		struct dirTodoNode *n = pendingDirectories->next;
    819  1.3  ws 
    820  1.3  ws 		/*
    821  1.3  ws 		 * remove TODO entry now, the list might change during
    822  1.3  ws 		 * directory reads
    823  1.3  ws 		 */
    824  1.3  ws 		freeDirTodo(pendingDirectories);
    825  1.3  ws 		pendingDirectories = n;
    826  1.3  ws 
    827  1.3  ws 		/*
    828  1.3  ws 		 * handle subdirectory
    829  1.3  ws 		 */
    830  1.3  ws 		mod |= readDosDirSection(dosfs, boot, fat, dir);
    831  1.3  ws 		if (mod & FSFATAL)
    832  1.3  ws 			return FSFATAL;
    833  1.3  ws 		if (mod & FSFATMOD) {
    834  1.3  ws 			mod &= ~FSFATMOD;
    835  1.3  ws 			mod |= writefat(dosfs, boot, fat); /* delay writing fats? XXX */
    836  1.3  ws 		}
    837  1.3  ws 		if (mod & FSFATAL)
    838  1.3  ws 			return FSFATAL;
    839  1.3  ws 	}
    840  1.3  ws 	return mod;
    841  1.3  ws }
    842  1.3  ws 
    843  1.1  ws /*
    844  1.1  ws  * Try to reconnect a FAT chain into dir
    845  1.1  ws  */
    846  1.1  ws static u_char *lfbuf;
    847  1.1  ws static cl_t lfcl;
    848  1.1  ws static off_t lfoff;
    849  1.1  ws 
    850  1.1  ws int
    851  1.3  ws reconnect(dosfs, boot, fat, head)
    852  1.1  ws 	int dosfs;
    853  1.1  ws 	struct bootblock *boot;
    854  1.1  ws 	struct fatEntry *fat;
    855  1.1  ws 	cl_t head;
    856  1.1  ws {
    857  1.1  ws 	struct dosDirEntry d;
    858  1.1  ws 	u_char *p;
    859  1.1  ws 
    860  1.3  ws 	if (!lostDir) {
    861  1.3  ws 		for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
    862  1.3  ws 			if (!strcmp(lostDir->name, LOSTDIR))
    863  1.3  ws 				break;
    864  1.3  ws 		}
    865  1.3  ws 		if (!lostDir) {		/* Create LOSTDIR?		XXX */
    866  1.3  ws 			pwarn("No %s directory\n", LOSTDIR);
    867  1.3  ws 			return FSERROR;
    868  1.3  ws 		}
    869  1.3  ws 	}
    870  1.1  ws 	if (!lfbuf) {
    871  1.1  ws 		lfbuf = malloc(boot->ClusterSize);
    872  1.1  ws 		if (!lfbuf) {
    873  1.1  ws 			perror("No space for buffer");
    874  1.1  ws 			return FSFATAL;
    875  1.1  ws 		}
    876  1.1  ws 		p = NULL;
    877  1.1  ws 	} else
    878  1.1  ws 		p = lfbuf;
    879  1.1  ws 	while (1) {
    880  1.1  ws 		if (p)
    881  1.1  ws 			while (p < lfbuf + boot->ClusterSize)
    882  1.1  ws 				if (*p == SLOT_EMPTY
    883  1.1  ws 				    || *p == SLOT_DELETED)
    884  1.1  ws 					break;
    885  1.1  ws 		if (p && p < lfbuf + boot->ClusterSize)
    886  1.1  ws 			break;
    887  1.3  ws 		lfcl = p ? fat[lfcl].next : lostDir->head;
    888  1.1  ws 		if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
    889  1.3  ws 			/* Extend LOSTDIR?				XXX */
    890  1.1  ws 			pwarn("No space in %s\n", LOSTDIR);
    891  1.1  ws 			return FSERROR;
    892  1.1  ws 		}
    893  1.1  ws 		lfoff = lfcl * boot->ClusterSize
    894  1.1  ws 		    + boot->ClusterOffset * boot->BytesPerSec;
    895  1.1  ws 		if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
    896  1.1  ws 		    || read(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
    897  1.1  ws 			perror("could not read LOST.DIR");
    898  1.1  ws 			return FSFATAL;
    899  1.1  ws 		}
    900  1.1  ws 		p = lfbuf;
    901  1.1  ws 	}
    902  1.1  ws 
    903  1.1  ws 	if (!ask(0, "Reconnect"))
    904  1.1  ws 		return FSERROR;
    905  1.1  ws 
    906  1.1  ws 	boot->NumFiles++;
    907  1.1  ws 	/* Ensure uniqueness of entry here!				XXX */
    908  1.1  ws 	memset(&d, 0, sizeof d);
    909  1.1  ws 	sprintf(d.name, "%d", head);
    910  1.1  ws 	d.flags = 0;
    911  1.1  ws 	d.head = head;
    912  1.1  ws 	d.size = fat[head].length * boot->ClusterSize;
    913  1.1  ws 
    914  1.1  ws 	memset(p, 0, 32);
    915  1.1  ws 	memset(p, ' ', 11);
    916  1.3  ws 	memcpy(p, d.name, strlen(d.name));
    917  1.3  ws 	p[26] = (u_char)d.head;
    918  1.3  ws 	p[27] = (u_char)(d.head >> 8);
    919  1.3  ws 	p[28] = (u_char)d.size;
    920  1.3  ws 	p[29] = (u_char)(d.size >> 8);
    921  1.3  ws 	p[30] = (u_char)(d.size >> 16);
    922  1.3  ws 	p[31] = (u_char)(d.size >> 24);
    923  1.3  ws 	fat[head].flags |= FAT_USED;
    924  1.1  ws 	if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
    925  1.1  ws 	    || write(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
    926  1.1  ws 		perror("could not write LOST.DIR");
    927  1.1  ws 		return FSFATAL;
    928  1.1  ws 	}
    929  1.1  ws 	return FSDIRMOD;
    930  1.1  ws }
    931  1.1  ws 
    932  1.1  ws void
    933  1.1  ws finishlf()
    934  1.1  ws {
    935  1.1  ws 	if (lfbuf)
    936  1.1  ws 		free(lfbuf);
    937  1.1  ws 	lfbuf = NULL;
    938  1.1  ws }
    939