Home | History | Annotate | Line # | Download | only in libsa
dosfs.c revision 1.3
      1 /*	$NetBSD: dosfs.c,v 1.3 2000/11/08 19:38:12 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998 Robert Nordier
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in
     14  *    the documentation and/or other materials provided with the
     15  *    distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
     32  * also supports VFAT.
     33  */
     34 
     35 /*
     36  * XXX DOES NOT SUPPORT:
     37  *
     38  *	LIBSA_FS_SINGLECOMPONENT
     39  */
     40 
     41 #include <sys/param.h>
     42 
     43 #include <msdosfs/bpb.h>
     44 #include <msdosfs/direntry.h>
     45 
     46 #ifdef _STANDALONE
     47 #include <lib/libkern/libkern.h>
     48 #else
     49 #include <string.h>
     50 #include <stddef.h>
     51 #endif
     52 
     53 #include "stand.h"
     54 #include "dosfs.h"
     55 
     56 #define SECSIZ  512		/* sector size */
     57 #define SSHIFT    9		/* SECSIZ shift */
     58 #define DEPSEC   16		/* directory entries per sector */
     59 #define DSHIFT    4		/* DEPSEC shift */
     60 #define LOCLUS    2		/* lowest cluster number */
     61 
     62 typedef union {
     63 	struct direntry de;	/* standard directory entry */
     64 	struct winentry xde;	/* extended directory entry */
     65 } DOS_DIR;
     66 
     67 typedef struct {
     68 	struct open_file *fd;	/* file descriptor */
     69 	u_char *buf;		/* buffer */
     70 	u_int   bufsec;		/* buffered sector */
     71 	u_int   links;		/* active links to structure */
     72 	u_int   spc;		/* sectors per cluster */
     73 	u_int   bsize;		/* cluster size in bytes */
     74 	u_int   bshift;		/* cluster conversion shift */
     75 	u_int   dirents;	/* root directory entries */
     76 	u_int   spf;		/* sectors per fat */
     77 	u_int   rdcl;		/* root directory start cluster */
     78 	u_int   lsnfat;		/* start of fat */
     79 	u_int   lsndir;		/* start of root dir */
     80 	u_int   lsndta;		/* start of data area */
     81 	u_int   fatsz;		/* FAT entry size */
     82 	u_int   xclus;		/* maximum cluster number */
     83 } DOS_FS;
     84 
     85 typedef struct {
     86 	DOS_FS *fs;		/* associated filesystem */
     87 	struct direntry de;	/* directory entry */
     88 	u_int   offset;		/* current offset */
     89 	u_int   c;		/* last cluster read */
     90 } DOS_FILE;
     91 
     92 /* Initial portion of DOS boot sector */
     93 typedef struct {
     94 	u_char  jmp[3];		/* usually 80x86 'jmp' opcode */
     95 	u_char  oem[8];		/* OEM name and version */
     96 	struct byte_bpb710 bpb;	/* BPB */
     97 } DOS_BS;
     98 
     99 /* Supply missing "." and ".." root directory entries */
    100 static const char *const dotstr[2] = {".", ".."};
    101 static const struct direntry dot[2] = {
    102 	{".       ", "   ", ATTR_DIRECTORY,
    103 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
    104 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
    105 
    106 	{"..      ", "   ", ATTR_DIRECTORY,
    107 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
    108 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
    109 };
    110 
    111 /* The usual conversion macros to avoid multiplication and division */
    112 #define bytsec(n)      ((n) >> SSHIFT)
    113 #define secbyt(s)      ((s) << SSHIFT)
    114 #define entsec(e)      ((e) >> DSHIFT)
    115 #define bytblk(fs, n)  ((n) >> (fs)->bshift)
    116 #define blkbyt(fs, b)  ((b) << (fs)->bshift)
    117 #define secblk(fs, s)  ((s) >> ((fs)->bshift - SSHIFT))
    118 #define blksec(fs, b)  ((b) << ((fs)->bshift - SSHIFT))
    119 
    120 /* Convert cluster number to offset within filesystem */
    121 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
    122 
    123 /* Convert cluster number to logical sector number */
    124 #define blklsn(fs, b)  ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
    125 
    126 /* Convert cluster number to offset within FAT */
    127 #define fatoff(sz, c)  ((sz) == 12 ? (c) + ((c) >> 1) :  \
    128                         (sz) == 16 ? (c) << 1 :          \
    129 			(c) << 2)
    130 
    131 /* Does cluster number reference a valid data cluster? */
    132 #define okclus(fs, c)  ((c) >= LOCLUS && (c) <= (fs)->xclus)
    133 
    134 /* Get start cluster from directory entry */
    135 #define stclus(sz, de)  ((sz) != 32 ? getushort((de)->deStartCluster) : \
    136                          ((u_int)getushort((de)->deHighClust) << 16) |  \
    137 			 getushort((de)->deStartCluster))
    138 
    139 static int dosunmount(DOS_FS *);
    140 static int parsebs(DOS_FS *, DOS_BS *);
    141 static int namede(DOS_FS *, const char *, const struct direntry **);
    142 static int lookup(DOS_FS *, u_int, const char *, const struct direntry **);
    143 static void cp_xdnm(u_char *, struct winentry *);
    144 static void cp_sfn(u_char *, struct direntry *);
    145 static off_t fsize(DOS_FS *, struct direntry *);
    146 static int fatcnt(DOS_FS *, u_int);
    147 static int fatget(DOS_FS *, u_int *);
    148 static int fatend(u_int, u_int);
    149 static int ioread(DOS_FS *, u_int, void *, u_int);
    150 static int iobuf(DOS_FS *, u_int);
    151 static int ioget(struct open_file *, u_int, void *, u_int);
    152 
    153 /*
    154  * Mount DOS filesystem
    155  */
    156 static int
    157 dos_mount(DOS_FS * fs, struct open_file * fd)
    158 {
    159 	int     err;
    160 
    161 	bzero(fs, sizeof(DOS_FS));
    162 	fs->fd = fd;
    163 	if ((err = !(fs->buf = alloc(SECSIZ)) ? errno : 0) ||
    164 	    (err = ioget(fs->fd, 0, fs->buf, 1)) ||
    165 	    (err = parsebs(fs, (DOS_BS *) fs->buf))) {
    166 		(void) dosunmount(fs);
    167 		return (err);
    168 	}
    169 	return 0;
    170 }
    171 
    172 /*
    173  * Unmount mounted filesystem
    174  */
    175 static int
    176 dos_unmount(DOS_FS * fs)
    177 {
    178 	int     err;
    179 
    180 	if (fs->links)
    181 		return (EBUSY);
    182 	if ((err = dosunmount(fs)))
    183 		return (err);
    184 	return 0;
    185 }
    186 
    187 /*
    188  * Common code shared by dos_mount() and dos_unmount()
    189  */
    190 static int
    191 dosunmount(DOS_FS * fs)
    192 {
    193 	if (fs->buf)
    194 		free(fs->buf, SECSIZ);
    195 	free(fs, sizeof(DOS_FS));
    196 	return (0);
    197 }
    198 
    199 /*
    200  * Open DOS file
    201  */
    202 int
    203 dosfs_open(char *path, struct open_file *fd)
    204 {
    205 	const struct direntry *de;
    206 	DOS_FILE *f;
    207 	DOS_FS *fs;
    208 	u_int   size, clus;
    209 	int     err = 0;
    210 
    211 	/* Allocate mount structure, associate with open */
    212 	fs = alloc(sizeof(DOS_FS));
    213 
    214 	if ((err = dos_mount(fs, fd)))
    215 		goto out;
    216 
    217 	if ((err = namede(fs, path, &de)))
    218 		goto out;
    219 
    220 	clus = stclus(fs->fatsz, de);
    221 	size = getulong(de->deFileSize);
    222 
    223 	if ((!(de->deAttributes & ATTR_DIRECTORY) && (!clus != !size)) ||
    224 	    ((de->deAttributes & ATTR_DIRECTORY) && size) ||
    225 	    (clus && !okclus(fs, clus))) {
    226 		err = EINVAL;
    227 		goto out;
    228 	}
    229 	f = alloc(sizeof(DOS_FILE));
    230 	bzero(f, sizeof(DOS_FILE));
    231 	f->fs = fs;
    232 	fs->links++;
    233 	f->de = *de;
    234 	fd->f_fsdata = (void *) f;
    235 
    236 out:
    237 	return (err);
    238 }
    239 
    240 /*
    241  * Read from file
    242  */
    243 int
    244 dosfs_read(struct open_file * fd, void *vbuf, size_t nbyte, size_t * resid)
    245 {
    246 	off_t   size;
    247 	u_int8_t *buf = vbuf;
    248 	u_int   nb, off, clus, c, cnt, n;
    249 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    250 	int     err = 0;
    251 
    252 	nb = (u_int) nbyte;
    253 	if ((size = fsize(f->fs, &f->de)) == -1)
    254 		return EINVAL;
    255 	if (nb > (n = size - f->offset))
    256 		nb = n;
    257 	off = f->offset;
    258 	if ((clus = stclus(f->fs->fatsz, &f->de)))
    259 		off &= f->fs->bsize - 1;
    260 	c = f->c;
    261 	cnt = nb;
    262 	while (cnt) {
    263 		n = 0;
    264 		if (!c) {
    265 			if ((c = clus))
    266 				n = bytblk(f->fs, f->offset);
    267 		} else if (!off)
    268 			n++;
    269 		while (n--) {
    270 			if ((err = fatget(f->fs, &c)))
    271 				goto out;
    272 			if (!okclus(f->fs, c)) {
    273 				err = EINVAL;
    274 				goto out;
    275 			}
    276 		}
    277 		if (!clus || (n = f->fs->bsize - off) > cnt)
    278 			n = cnt;
    279 		if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
    280 				secbyt(f->fs->lsndir)) + off,
    281 			    buf, n)))
    282 			goto out;
    283 		f->offset += n;
    284 		f->c = c;
    285 		off = 0;
    286 		buf += n;
    287 		cnt -= n;
    288 	}
    289 out:
    290 	if (resid)
    291 		*resid = nbyte - nb + cnt;
    292 	return (err);
    293 }
    294 
    295 #ifndef LIBSA_NO_FS_WRITE
    296 /*
    297  * Not implemented.
    298  */
    299 int
    300 dosfs_write(struct open_file *fd, void *start, size_t size, size_t *resid)
    301 {
    302 
    303 	return (EROFS);
    304 }
    305 #endif /* !LIBSA_NO_FS_WRITE */
    306 
    307 #ifndef LIBSA_NO_FS_SEEK
    308 /*
    309  * Reposition within file
    310  */
    311 off_t
    312 dosfs_seek(struct open_file * fd, off_t offset, int whence)
    313 {
    314 	off_t   off;
    315 	u_int   size;
    316 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    317 
    318 	size = getulong(f->de.deFileSize);
    319 	switch (whence) {
    320 	case SEEK_SET:
    321 		off = 0;
    322 		break;
    323 	case SEEK_CUR:
    324 		off = f->offset;
    325 		break;
    326 	case SEEK_END:
    327 		off = size;
    328 		break;
    329 	default:
    330 		return (-1);
    331 	}
    332 	off += offset;
    333 	if (off < 0 || off > size)
    334 		return (-1);
    335 	f->offset = (u_int) off;
    336 	f->c = 0;
    337 	return (off);
    338 }
    339 #endif /* !LIBSA_NO_FS_SEEK */
    340 
    341 #ifndef LIBSA_NO_FS_CLOSE
    342 /*
    343  * Close open file
    344  */
    345 int
    346 dosfs_close(struct open_file * fd)
    347 {
    348 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    349 	DOS_FS *fs = f->fs;
    350 
    351 	f->fs->links--;
    352 	free(f, sizeof(DOS_FILE));
    353 	dos_unmount(fs);
    354 	return 0;
    355 }
    356 #endif /* !LIBSA_NO_FS_CLOSE */
    357 
    358 /*
    359  * Return some stat information on a file.
    360  */
    361 int
    362 dosfs_stat(struct open_file * fd, struct stat * sb)
    363 {
    364 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    365 
    366 	/* only important stuff */
    367 	sb->st_mode = (f->de.deAttributes & ATTR_DIRECTORY) ?
    368 	    (S_IFDIR | 0555) : (S_IFREG | 0444);
    369 	sb->st_nlink = 1;
    370 	sb->st_uid = 0;
    371 	sb->st_gid = 0;
    372 	if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
    373 		return EINVAL;
    374 	return (0);
    375 }
    376 
    377 /*
    378  * Parse DOS boot sector
    379  */
    380 static int
    381 parsebs(DOS_FS * fs, DOS_BS * bs)
    382 {
    383 	u_int   sc;
    384 
    385 	if ((bs->jmp[0] != 0x69 &&
    386 		bs->jmp[0] != 0xe9 &&
    387 		(bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
    388 	    bs->bpb.bpbMedia < 0xf0)
    389 		return EINVAL;
    390 	if (getushort(bs->bpb.bpbBytesPerSec) != SECSIZ)
    391 		return EINVAL;
    392 	if (!(fs->spc = bs->bpb.bpbSecPerClust) || fs->spc & (fs->spc - 1))
    393 		return EINVAL;
    394 	fs->bsize = secbyt(fs->spc);
    395 	fs->bshift = ffs(fs->bsize) - 1;
    396 	if ((fs->spf = getushort(bs->bpb.bpbFATsecs))) {
    397 		if (bs->bpb.bpbFATs != 2)
    398 			return EINVAL;
    399 		if (!(fs->dirents = getushort(bs->bpb.bpbRootDirEnts)))
    400 			return EINVAL;
    401 	} else {
    402 		if (!(fs->spf = getulong(bs->bpb.bpbBigFATsecs)))
    403 			return EINVAL;
    404 		if (!bs->bpb.bpbFATs || bs->bpb.bpbFATs > 16)
    405 			return EINVAL;
    406 		if ((fs->rdcl = getulong(bs->bpb.bpbRootClust)) < LOCLUS)
    407 			return EINVAL;
    408 	}
    409 	if (!(fs->lsnfat = getushort(bs->bpb.bpbResSectors)))
    410 		return EINVAL;
    411 	fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.bpbFATs;
    412 	fs->lsndta = fs->lsndir + entsec(fs->dirents);
    413 	if (!(sc = getushort(bs->bpb.bpbSectors)) &&
    414 	    !(sc = getulong(bs->bpb.bpbHugeSectors)))
    415 		return EINVAL;
    416 	if (fs->lsndta > sc)
    417 		return EINVAL;
    418 	if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
    419 		return EINVAL;
    420 	fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
    421 	sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
    422 	if (fs->xclus > sc)
    423 		fs->xclus = sc;
    424 	return 0;
    425 }
    426 
    427 /*
    428  * Return directory entry from path
    429  */
    430 static int
    431 namede(DOS_FS * fs, const char *path, const struct direntry ** dep)
    432 {
    433 	char    name[256];
    434 	const struct direntry *de;
    435 	char   *s;
    436 	size_t  n;
    437 	int     err;
    438 
    439 	err = 0;
    440 	de = dot;
    441 	if (*path == '/')
    442 		path++;
    443 	while (*path) {
    444 		if (!(s = strchr(path, '/')))
    445 			s = strchr(path, 0);
    446 		if ((n = s - path) > 255)
    447 			return ENAMETOOLONG;
    448 		memcpy(name, path, n);
    449 		name[n] = 0;
    450 		path = s;
    451 		if (!(de->deAttributes & ATTR_DIRECTORY))
    452 			return ENOTDIR;
    453 		if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
    454 			return err;
    455 		if (*path == '/')
    456 			path++;
    457 	}
    458 	*dep = de;
    459 	return 0;
    460 }
    461 
    462 /*
    463  * Lookup path segment
    464  */
    465 static int
    466 lookup(DOS_FS * fs, u_int clus, const char *name, const struct direntry ** dep)
    467 {
    468 	DOS_DIR *dir;
    469 	u_char  lfn[261];
    470 	u_char  sfn[13];
    471 	u_int   nsec, lsec, xdn, chk, sec, ent, x;
    472 	int     err = 0, ok, i;
    473 
    474 	if (!clus)
    475 		for (ent = 0; ent < 2; ent++)
    476 			if (!strcasecmp(name, dotstr[ent])) {
    477 				*dep = dot + ent;
    478 				return 0;
    479 			}
    480 
    481 	dir = alloc(sizeof(DOS_DIR) * DEPSEC);
    482 
    483 	if (!clus && fs->fatsz == 32)
    484 		clus = fs->rdcl;
    485 	nsec = !clus ? entsec(fs->dirents) : fs->spc;
    486 	lsec = 0;
    487 	xdn = chk = 0;
    488 	for (;;) {
    489 		if (!clus && !lsec)
    490 			lsec = fs->lsndir;
    491 		else if (okclus(fs, clus))
    492 			lsec = blklsn(fs, clus);
    493 		else {
    494 			err = EINVAL;
    495 			goto out;
    496 		}
    497 		for (sec = 0; sec < nsec; sec++) {
    498 			if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
    499 				goto out;
    500 			for (ent = 0; ent < DEPSEC; ent++) {
    501 				if (!*dir[ent].de.deName) {
    502 					err = ENOENT;
    503 					goto out;
    504 				}
    505 				if (*dir[ent].de.deName != 0xe5) {
    506 					if (dir[ent].de.deAttributes ==
    507 					    ATTR_WIN95) {
    508 						x = dir[ent].xde.weCnt;
    509 						if (x & WIN_LAST ||
    510 						    (x + 1 == xdn &&
    511 						     dir[ent].xde.weChksum ==
    512 						     chk)) {
    513 							if (x & WIN_LAST) {
    514 								chk = dir[ent].xde.weChksum;
    515 								x &= WIN_CNT;
    516 							}
    517 							if (x >= 1 && x <= 20) {
    518 								cp_xdnm(lfn, &dir[ent].xde);
    519 								xdn = x;
    520 								continue;
    521 							}
    522 						}
    523 					} else if (!(dir[ent].de.deAttributes &
    524 						     ATTR_VOLUME)) {
    525 						if ((ok = xdn == 1)) {
    526 							for (x = 0, i = 0;
    527 							     i < 11; i++)
    528 								x = ((((x & 1) << 7) | (x >> 1)) +
    529 								    dir[ent].de.deName[i]) & 0xff;
    530 							ok = chk == x &&
    531 							    !strcasecmp(name, (const char *) lfn);
    532 						}
    533 						if (!ok) {
    534 							cp_sfn(sfn, &dir[ent].de);
    535 							ok = !strcasecmp(name, (const char *) sfn);
    536 						}
    537 						if (ok) {
    538 							*dep = &dir[ent].de;
    539 							goto out;
    540 						}
    541 					}
    542 				}
    543 				xdn = 0;
    544 			}
    545 		}
    546 		if (!clus)
    547 			break;
    548 		if ((err = fatget(fs, &clus)))
    549 			goto out;
    550 		if (fatend(fs->fatsz, clus))
    551 			break;
    552 	}
    553 	err = ENOENT;
    554  out:
    555 	free(dir, sizeof(DOS_DIR) * DEPSEC);
    556 	return (err);
    557 }
    558 
    559 /*
    560  * Copy name from extended directory entry
    561  */
    562 static void
    563 cp_xdnm(u_char * lfn, struct winentry * xde)
    564 {
    565 	static const struct {
    566 		u_int   off;
    567 		u_int   dim;
    568 	} ix[3] = {
    569 		{ offsetof(struct winentry, wePart1),
    570 		    sizeof(xde->wePart1) / 2 },
    571 		{ offsetof(struct winentry, wePart2),
    572 		    sizeof(xde->wePart2) / 2 },
    573 		{ offsetof(struct winentry, wePart3),
    574 		    sizeof(xde->wePart3) / 2 }
    575 	};
    576 	u_char *p;
    577 	u_int   n, x, c;
    578 
    579 	lfn += 13 * ((xde->weCnt & WIN_CNT) - 1);
    580 	for (n = 0; n < 3; n++)
    581 		for (p = (u_char *) xde + ix[n].off, x = ix[n].dim; x;
    582 		    p += 2, x--) {
    583 			if ((c = getushort(p)) && (c < 32 || c > 127))
    584 				c = '?';
    585 			if (!(*lfn++ = c))
    586 				return;
    587 		}
    588 	if (xde->weCnt & WIN_LAST)
    589 		*lfn = 0;
    590 }
    591 
    592 /*
    593  * Copy short filename
    594  */
    595 static void
    596 cp_sfn(u_char * sfn, struct direntry * de)
    597 {
    598 	u_char *p;
    599 	int     j, i;
    600 
    601 	p = sfn;
    602 	if (*de->deName != ' ') {
    603 		for (j = 7; de->deName[j] == ' '; j--);
    604 		for (i = 0; i <= j; i++)
    605 			*p++ = de->deName[i];
    606 		if (*de->deExtension != ' ') {
    607 			*p++ = '.';
    608 			for (j = 2; de->deExtension[j] == ' '; j--);
    609 			for (i = 0; i <= j; i++)
    610 				*p++ = de->deExtension[i];
    611 		}
    612 	}
    613 	*p = 0;
    614 	if (*sfn == 5)
    615 		*sfn = 0xe5;
    616 }
    617 
    618 /*
    619  * Return size of file in bytes
    620  */
    621 static  off_t
    622 fsize(DOS_FS * fs, struct direntry * de)
    623 {
    624 	u_long  size;
    625 	u_int   c;
    626 	int     n;
    627 
    628 	if (!(size = getulong(de->deFileSize)) &&
    629 	    de->deAttributes & ATTR_DIRECTORY) {
    630 		if (!(c = getushort(de->deStartCluster)))
    631 			size = fs->dirents * sizeof(struct direntry);
    632 		else {
    633 			if ((n = fatcnt(fs, c)) == -1)
    634 				return n;
    635 			size = blkbyt(fs, n);
    636 		}
    637 	}
    638 	return size;
    639 }
    640 
    641 /*
    642  * Count number of clusters in chain
    643  */
    644 static int
    645 fatcnt(DOS_FS * fs, u_int c)
    646 {
    647 	int     n;
    648 
    649 	for (n = 0; okclus(fs, c); n++)
    650 		if (fatget(fs, &c))
    651 			return -1;
    652 	return fatend(fs->fatsz, c) ? n : -1;
    653 }
    654 
    655 /*
    656  * Get next cluster in cluster chain
    657  */
    658 static int
    659 fatget(DOS_FS * fs, u_int * c)
    660 {
    661 	u_char  buf[4];
    662 	u_int   x;
    663 	int     err;
    664 
    665 	err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
    666 	    fs->fatsz != 32 ? 2 : 4);
    667 	if (err)
    668 		return err;
    669 	x = fs->fatsz != 32 ? getushort(buf) : getulong(buf);
    670 	*c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
    671 	return 0;
    672 }
    673 
    674 /*
    675  * Is cluster an end-of-chain marker?
    676  */
    677 static int
    678 fatend(u_int sz, u_int c)
    679 {
    680 	return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
    681 }
    682 
    683 /*
    684  * Offset-based I/O primitive
    685  */
    686 static int
    687 ioread(DOS_FS * fs, u_int offset, void *buf, u_int nbyte)
    688 {
    689 	char   *s;
    690 	u_int   off, n;
    691 	int     err;
    692 
    693 	s = buf;
    694 	if ((off = offset & (SECSIZ - 1))) {
    695 		offset -= off;
    696 		if ((err = iobuf(fs, bytsec(offset))))
    697 			return err;
    698 		offset += SECSIZ;
    699 		if ((n = SECSIZ - off) > nbyte)
    700 			n = nbyte;
    701 		memcpy(s, fs->buf + off, n);
    702 		s += n;
    703 		nbyte -= n;
    704 	}
    705 	n = nbyte & (SECSIZ - 1);
    706 	if (nbyte -= n) {
    707 		if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
    708 			return err;
    709 		offset += nbyte;
    710 		s += nbyte;
    711 	}
    712 	if (n) {
    713 		if ((err = iobuf(fs, bytsec(offset))))
    714 			return err;
    715 		memcpy(s, fs->buf, n);
    716 	}
    717 	return 0;
    718 }
    719 
    720 /*
    721  * Buffered sector-based I/O primitive
    722  */
    723 static int
    724 iobuf(DOS_FS * fs, u_int lsec)
    725 {
    726 	int     err;
    727 
    728 	if (fs->bufsec != lsec) {
    729 		if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
    730 			return err;
    731 		fs->bufsec = lsec;
    732 	}
    733 	return 0;
    734 }
    735 
    736 /*
    737  * Sector-based I/O primitive
    738  */
    739 static int
    740 ioget(struct open_file * fd, u_int lsec, void *buf, u_int nsec)
    741 {
    742 	size_t rsize;
    743 	int err;
    744 
    745 #ifndef LIBSA_NO_TWIDDLE
    746 	twiddle();
    747 #endif
    748 	err = DEV_STRATEGY(fd->f_dev)(fd->f_devdata, F_READ, lsec,
    749 	    secbyt(nsec), buf, &rsize);
    750 	return (err);
    751 }
    752