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