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