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