Home | History | Annotate | Line # | Download | only in libsa
ustarfs.c revision 1.30
      1  1.30     isaki /*	$NetBSD: ustarfs.c,v 1.30 2007/11/11 13:23:07 isaki Exp $	*/
      2   1.1      ross 
      3   1.1      ross /* [Notice revision 2.2]
      4   1.1      ross  * Copyright (c) 1997, 1998 Avalon Computer Systems, Inc.
      5   1.1      ross  * All rights reserved.
      6   1.1      ross  *
      7   1.1      ross  * Author: Ross Harvey
      8   1.1      ross  *
      9   1.1      ross  * Redistribution and use in source and binary forms, with or without
     10   1.1      ross  * modification, are permitted provided that the following conditions
     11   1.1      ross  * are met:
     12   1.1      ross  * 1. Redistributions of source code must retain the above copyright and
     13   1.1      ross  *    author notice, this list of conditions, and the following disclaimer.
     14   1.1      ross  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      ross  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      ross  *    documentation and/or other materials provided with the distribution.
     17   1.1      ross  * 3. Neither the name of Avalon Computer Systems, Inc. nor the names of
     18   1.1      ross  *    its contributors may be used to endorse or promote products derived
     19   1.1      ross  *    from this software without specific prior written permission.
     20   1.1      ross  * 4. This copyright will be assigned to The NetBSD Foundation on
     21   1.1      ross  *    1/1/2000 unless these terms (including possibly the assignment
     22   1.1      ross  *    date) are updated in writing by Avalon prior to the latest specified
     23   1.1      ross  *    assignment date.
     24   1.1      ross  *
     25   1.1      ross  * THIS SOFTWARE IS PROVIDED BY AVALON COMPUTER SYSTEMS, INC. AND CONTRIBUTORS
     26   1.1      ross  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1      ross  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1      ross  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL AVALON OR THE CONTRIBUTORS
     29   1.1      ross  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1      ross  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1      ross  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1      ross  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1      ross  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1      ross  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1      ross  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1      ross  */
     37   1.1      ross 
     38   1.1      ross 
     39   1.1      ross /*
     40   1.1      ross  ******************************* USTAR FS *******************************
     41   1.1      ross  */
     42   1.1      ross 
     43   1.1      ross /*
     44   1.1      ross  * Implement an ROFS with an 8K boot area followed by ustar-format data.
     45   1.1      ross  * The point: minimal FS overhead, and it's easy (well, `possible') to
     46   1.1      ross  * split files over multiple volumes.
     47   1.1      ross  *
     48   1.1      ross  * XXX - TODO LIST
     49   1.1      ross  * --- - ---- ----
     50   1.1      ross  * XXX - tag volume numbers and verify that the correct volume is
     51   1.1      ross  *       inserted after volume swaps.
     52   1.1      ross  *
     53   1.1      ross  * XXX - stop hardwiring FS metadata for floppies...embed it in a file,
     54   1.1      ross  * 	 file name, or something. (Remember __SYMDEF? :-)
     55   1.1      ross  *
     56   1.8       cgd  * XXX Does not currently implement:
     57   1.8       cgd  * XXX
     58   1.8       cgd  * XXX LIBSA_NO_FS_CLOSE
     59   1.8       cgd  * XXX LIBSA_NO_FS_SEEK
     60   1.8       cgd  * XXX LIBSA_NO_FS_WRITE
     61   1.8       cgd  * XXX LIBSA_NO_FS_SYMLINK (does this even make sense?)
     62   1.8       cgd  * XXX LIBSA_FS_SINGLECOMPONENT
     63   1.1      ross  */
     64   1.1      ross 
     65  1.14   thorpej #ifdef _STANDALONE
     66  1.14   thorpej #include <lib/libkern/libkern.h>
     67  1.14   thorpej #else
     68  1.14   thorpej #include <string.h>
     69  1.14   thorpej #endif
     70   1.1      ross #include "stand.h"
     71   1.1      ross #include "ustarfs.h"
     72   1.1      ross 
     73   1.2      ross #define	BBSIZE	8192
     74   1.1      ross #define	USTAR_NAME_BLOCK 512
     75   1.1      ross 
     76   1.2      ross /*
     77   1.2      ross  * Virtual offset: relative to start of ustar archive
     78   1.2      ross  * Logical offset: volume-relative
     79   1.2      ross  * Physical offset: the usual meaning
     80   1.2      ross  */
     81   1.2      ross 
     82   1.2      ross /* virtual offset to volume number */
     83  1.24     perry 
     84  1.24     perry #define	vda2vn(_v,_volsize) ((_v) / (_volsize))
     85   1.2      ross 
     86   1.2      ross /* conversions between the three different levels of disk addresses */
     87   1.2      ross 
     88   1.2      ross #define	vda2lda(_v,_volsize) ((_v) % (_volsize))
     89   1.2      ross #define	lda2vda(_v,_volsize,_volnumber) ((_v) + (_volsize) * (_volnumber))
     90   1.2      ross 
     91   1.2      ross #define	lda2pda(_lda) ((_lda) + ustarfs_mode_offset)
     92   1.2      ross #define	pda2lda(_pda) ((_pda) - ustarfs_mode_offset)
     93   1.2      ross /*
     94   1.2      ross  * Change this to off_t if you want to support big volumes. If we only use
     95   1.2      ross  * ustarfs on floppies it can stay int for libsa code density.
     96   1.2      ross  *
     97   1.2      ross  * It needs to be signed.
     98   1.2      ross  */
     99   1.2      ross typedef	int ustoffs;
    100   1.2      ross 
    101   1.1      ross typedef struct ustar_struct {
    102   1.1      ross 	char	ust_name[100],
    103   1.1      ross 		ust_mode[8],
    104   1.1      ross 		ust_uid[8],
    105   1.1      ross 		ust_gid[8],
    106   1.1      ross 		ust_size[12],
    107   1.1      ross 		ust_misc[12 + 8 + 1 + 100],
    108  1.18      matt 		ust_magic[6],
    109   1.1      ross 	/* there is more, but we don't care */
    110  1.18      matt 		ust_pad[1];	/* make it aligned */
    111   1.1      ross } ustar_t;
    112   1.1      ross 
    113   1.1      ross /*
    114  1.17     lukem  * We buffer one even cylinder of data...it's actually only really one
    115   1.1      ross  * cyl on a 1.44M floppy, but on other devices it's fast enough with any
    116   1.1      ross  * kind of block buffering, so we optimize for the slowest device.
    117   1.1      ross  */
    118   1.1      ross 
    119  1.30     isaki #ifndef USTAR_SECT_PER_CYL
    120  1.30     isaki #define USTAR_SECT_PER_CYL	(18 * 2)
    121  1.30     isaki #endif
    122  1.30     isaki 
    123   1.1      ross typedef struct ust_active_struct {
    124   1.1      ross 	ustar_t	uas_active;
    125  1.30     isaki 	char	uas_1cyl[USTAR_SECT_PER_CYL * 512];
    126   1.2      ross 	ustoffs	uas_volsize;		/* XXX this is hardwired now */
    127   1.2      ross 	ustoffs	uas_windowbase;		/* relative to volume 0 */
    128   1.2      ross 	ustoffs	uas_filestart;		/* relative to volume 0 */
    129   1.2      ross 	ustoffs	uas_fseek;		/* relative to file */
    130   1.2      ross 	ustoffs	uas_filesize;		/* relative to volume 0 */
    131   1.1      ross 	int	uas_init_window;	/* data present in window */
    132   1.1      ross 	int	uas_init_fs;		/* ust FS actually found */
    133   1.2      ross 	int	uas_volzerosig;		/* ID volume 0 by signature */
    134  1.11      ross 	int	uas_sigdone;		/* did sig already */
    135   1.2      ross 	int	uas_offset;		/* amount of cylinder below lba 0 */
    136   1.1      ross } ust_active_t;
    137   1.1      ross 
    138   1.3      ross static const char formatid[] = "USTARFS",
    139   1.3      ross 		  metaname[] = "USTAR.volsize.";
    140   1.1      ross 
    141  1.21   mycroft static const int ustarfs_mode_offset = BBSIZE;
    142   1.1      ross 
    143   1.2      ross static int checksig __P((ust_active_t *));
    144   1.3      ross static int convert __P((const char *, int, int));
    145   1.2      ross static int get_volume __P((struct open_file *, int));
    146   1.3      ross static void setwindow(ust_active_t *, ustoffs, ustoffs);
    147  1.11      ross static int real_fs_cylinder_read __P((struct open_file *, ustoffs, int));
    148   1.3      ross static int ustarfs_cylinder_read __P((struct open_file *, ustoffs, int));
    149   1.1      ross static void ustarfs_sscanf __P((const char *, const char *, int *));
    150   1.2      ross static int read512block __P((struct open_file *, ustoffs, char block[512]));
    151  1.11      ross static int init_volzero_sig __P((struct open_file *));
    152   1.1      ross 
    153  1.16   minoura #ifdef HAVE_CHANGEDISK_HOOK
    154  1.16   minoura /*
    155  1.16   minoura  * Called when the next volume is prompted.
    156  1.16   minoura  * Machine dependent code can eject the medium etc.
    157  1.16   minoura  * The new medium must be ready when this hook returns.
    158  1.16   minoura  */
    159  1.16   minoura void changedisk_hook __P((struct open_file *));
    160  1.16   minoura #endif
    161  1.16   minoura 
    162   1.1      ross static int
    163   1.1      ross convert(f, base, fw)
    164   1.1      ross 	const char *f;
    165   1.1      ross 	int base, fw;
    166   1.1      ross {
    167   1.1      ross 	int	i, c, result = 0;
    168   1.1      ross 
    169   1.1      ross 	while(fw > 0 && *f == ' ') {
    170   1.1      ross 		--fw;
    171   1.1      ross 		++f;
    172   1.1      ross 	}
    173   1.1      ross 	for(i = 0; i < fw; ++i) {
    174   1.1      ross 		c = f[i];
    175   1.1      ross 		if ('0' <= c && c < '0' + base) {
    176   1.1      ross 			c -= '0';
    177   1.1      ross 			result = result * base + c;
    178   1.1      ross 		} else	break;
    179   1.1      ross 	}
    180   1.1      ross 	return result;
    181   1.1      ross }
    182   1.1      ross 
    183   1.1      ross static void
    184   1.1      ross ustarfs_sscanf(s,f,xi)
    185   1.1      ross 	const char *s,*f;
    186   1.1      ross 	int *xi;
    187   1.1      ross {
    188   1.1      ross 	*xi = convert(s, 8, convert(f + 1, 10, 99));
    189   1.1      ross }
    190   1.1      ross 
    191   1.1      ross static int
    192   1.3      ross ustarfs_cylinder_read(f, seek2, forcelabel)
    193   1.1      ross 	struct open_file *f;
    194   1.2      ross 	ustoffs seek2;
    195  1.27       uwe 	int forcelabel;
    196   1.1      ross {
    197  1.11      ross 	int i, e;
    198  1.11      ross 
    199  1.11      ross 	for (i = 0; i < 3; ++i) {
    200  1.11      ross 		e = real_fs_cylinder_read(f, seek2, forcelabel);
    201  1.11      ross 		if (e == 0)
    202  1.11      ross 			return 0;
    203  1.11      ross 	}
    204  1.11      ross 	return e;
    205  1.11      ross }
    206  1.11      ross 
    207  1.11      ross static int
    208  1.11      ross real_fs_cylinder_read(f, seek2, forcelabel)
    209  1.11      ross 	struct open_file *f;
    210  1.11      ross 	ustoffs seek2;
    211  1.27       uwe 	int forcelabel;
    212  1.11      ross {
    213  1.10      ross 	int i;
    214   1.4       bad 	int e = 0;	/* XXX work around gcc warning */
    215   1.3      ross 	ustoffs	lda;
    216   1.3      ross 	char *xferbase;
    217   1.1      ross 	ust_active_t *ustf;
    218   1.3      ross 	size_t	xferrqst, xfercount;
    219   1.1      ross 
    220   1.1      ross 	ustf = f->f_fsdata;
    221   1.3      ross 	xferrqst = sizeof ustf->uas_1cyl;
    222   1.3      ross 	xferbase = ustf->uas_1cyl;
    223   1.3      ross 	lda = pda2lda(seek2);
    224   1.3      ross 	if (lda < 0) {
    225   1.3      ross 		lda = -lda;
    226   1.3      ross 		ustf->uas_offset = lda;
    227   1.3      ross 		/*
    228   1.3      ross 		 * don't read the label unless we have to. (Preserve
    229   1.3      ross 		 * sequential block access so tape boot works.)
    230   1.3      ross 		 */
    231   1.3      ross 		if (!forcelabel) {
    232   1.3      ross 			memset(xferbase, 0, lda);
    233   1.3      ross 			xferrqst -= lda;
    234   1.3      ross 			xferbase += lda;
    235   1.3      ross 			seek2    += lda;
    236   1.3      ross 		}
    237   1.3      ross 	} else
    238   1.3      ross 		ustf->uas_offset = 0;
    239   1.3      ross 	while(xferrqst > 0) {
    240   1.9  christos #if !defined(LIBSA_NO_TWIDDLE)
    241   1.9  christos 		twiddle();
    242   1.9  christos #endif
    243  1.10      ross 		for (i = 0; i < 3; ++i) {
    244  1.10      ross 			e = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
    245  1.10      ross 			    seek2 / 512, xferrqst, xferbase, &xfercount);
    246  1.10      ross 			if (e == 0)
    247  1.10      ross 				break;
    248  1.10      ross 			printf("@");
    249  1.10      ross 		}
    250   1.3      ross 		if (e)
    251   1.3      ross 			break;
    252   1.3      ross 		if (xfercount != xferrqst)
    253   1.3      ross 			printf("Warning, unexpected short transfer %d/%d\n",
    254   1.3      ross 				(int)xfercount, (int)xferrqst);
    255   1.3      ross 		xferrqst -= xfercount;
    256   1.3      ross 		xferbase += xfercount;
    257  1.10      ross 		seek2    += xfercount;
    258   1.2      ross 	}
    259   1.1      ross 	return e;
    260   1.1      ross }
    261   1.1      ross 
    262   1.1      ross static int
    263   1.2      ross checksig(ustf)
    264   1.2      ross 	ust_active_t *ustf;
    265   1.2      ross {
    266   1.2      ross 	int	i, rcs;
    267   1.2      ross 
    268  1.23      fvdl 	for(i = rcs = 0; i < (int)(sizeof ustf->uas_1cyl); ++i)
    269   1.2      ross 		rcs += ustf->uas_1cyl[i];
    270   1.2      ross 	return rcs;
    271   1.2      ross }
    272   1.2      ross 
    273   1.2      ross static int
    274   1.3      ross get_volume(f, vn)
    275   1.1      ross 	struct open_file *f;
    276   1.2      ross 	int vn;
    277   1.2      ross {
    278   1.2      ross 	int	e, needvolume, havevolume;
    279   1.2      ross 	ust_active_t *ustf;
    280   1.2      ross 
    281   1.2      ross 	ustf = f->f_fsdata;
    282   1.2      ross 	havevolume = vda2vn(ustf->uas_windowbase, ustf->uas_volsize);
    283   1.2      ross 	needvolume = vn;
    284   1.2      ross 	while(havevolume != needvolume) {
    285   1.2      ross 		printf("\nPlease ");
    286   1.2      ross 		if (havevolume >= 0)
    287   1.2      ross 			printf("remove disk %d, ", havevolume + 1);
    288  1.15     lukem 		printf("insert disk %d, and press return...",
    289   1.2      ross 			needvolume + 1);
    290  1.16   minoura #ifdef HAVE_CHANGEDISK_HOOK
    291  1.16   minoura 		changedisk_hook(f);
    292  1.16   minoura #else
    293  1.20      jmmv 		for (;;) {
    294  1.20      jmmv 			int c = getchar();
    295  1.20      jmmv 			if ((c == '\n') || (c == '\r'))
    296  1.20      jmmv 				break;
    297  1.20      jmmv 		}
    298  1.16   minoura #endif
    299   1.2      ross 		printf("\n");
    300  1.11      ross 		e = ustarfs_cylinder_read(f, 0, needvolume != 0);
    301   1.2      ross 		if (e)
    302   1.2      ross 			return e;
    303   1.2      ross 		if(strncmp(formatid, ustf->uas_1cyl, strlen(formatid))) {
    304   1.2      ross 			/* no magic, might be OK if we want volume 0 */
    305   1.2      ross 			if (ustf->uas_volzerosig == checksig(ustf)) {
    306   1.2      ross 				havevolume = 0;
    307   1.2      ross 				continue;
    308   1.2      ross 			}
    309   1.2      ross 			printf("Disk is not from the volume set?!\n");
    310   1.2      ross 			havevolume = -2;
    311   1.2      ross 			continue;
    312   1.2      ross 		}
    313   1.2      ross 		ustarfs_sscanf(ustf->uas_1cyl + strlen(formatid), "%9o",
    314   1.2      ross 			&havevolume);
    315   1.2      ross 		--havevolume;
    316   1.2      ross 	}
    317   1.2      ross 	return 0;
    318   1.2      ross }
    319   1.2      ross 
    320   1.3      ross static void
    321   1.3      ross setwindow(ust_active_t *ustf, ustoffs pda, ustoffs vda)
    322   1.3      ross {
    323   1.3      ross 	ustf->uas_windowbase = lda2vda(pda2lda(pda), ustf->uas_volsize,
    324   1.3      ross 					vda2vn(vda, ustf->uas_volsize))
    325   1.3      ross 			     + ustf->uas_offset;
    326   1.3      ross 	ustf->uas_init_window = 1;
    327   1.3      ross }
    328   1.3      ross 
    329   1.2      ross static int
    330   1.2      ross read512block(f, vda, block)
    331   1.2      ross 	struct open_file *f;
    332   1.2      ross 	ustoffs vda;
    333   1.1      ross 	char block[512];
    334   1.1      ross {
    335   1.3      ross 	ustoffs pda;
    336   1.1      ross 	ssize_t	e;
    337   1.2      ross 	int	dienow;
    338   1.1      ross 	ust_active_t *ustf;
    339   1.1      ross 
    340   1.2      ross 	dienow = 0;
    341   1.1      ross 	ustf = f->f_fsdata;
    342   1.2      ross 
    343   1.2      ross 	/*
    344   1.2      ross 	 * if (vda in window)
    345   1.2      ross 	 * 	copy out and return data
    346   1.2      ross 	 * if (vda is on some other disk)
    347   1.2      ross 	 * 	do disk swap
    348   1.2      ross 	 * get physical disk address
    349   1.2      ross 	 * round down to cylinder boundary
    350  1.17     lukem 	 * read cylinder
    351   1.2      ross 	 * set window (in vda space) and try again
    352   1.2      ross 	 * [ there is an implicit assumption that windowbase always identifies
    353   1.2      ross 	 *    the current volume, even if initwindow == 0. This way, a
    354   1.2      ross 	 *    windowbase of 0 causes the initial volume to be disk 0 ]
    355   1.2      ross 	 */
    356   1.1      ross tryagain:
    357   1.1      ross 	if(ustf->uas_init_window
    358   1.2      ross 	&& ustf->uas_windowbase <= vda && vda <
    359  1.23      fvdl 	   ustf->uas_windowbase +
    360  1.23      fvdl 	     (int)(sizeof ustf->uas_1cyl) - ustf->uas_offset) {
    361   1.2      ross 		memcpy(block, ustf->uas_1cyl
    362   1.2      ross 				+ (vda - ustf->uas_windowbase)
    363   1.2      ross 				+ ustf->uas_offset, 512);
    364   1.1      ross 		return 0;
    365   1.1      ross 	}
    366   1.1      ross 	if (dienow++)
    367   1.1      ross 		panic("ustarfs read512block");
    368   1.2      ross 	ustf->uas_init_window = 0;
    369   1.2      ross 	e = get_volume(f, vda2vn(vda, ustf->uas_volsize));
    370   1.2      ross 	if (e)
    371   1.2      ross 		return e;
    372   1.2      ross 	pda = lda2pda(vda2lda(vda, ustf->uas_volsize));
    373   1.2      ross 	pda-= pda % sizeof ustf->uas_1cyl;
    374   1.3      ross 	e = ustarfs_cylinder_read(f, pda, 0);
    375   1.1      ross 	if (e)
    376   1.1      ross 		return e;
    377   1.3      ross 	setwindow(ustf, pda, vda);
    378   1.1      ross 	goto tryagain;
    379   1.1      ross }
    380   1.1      ross 
    381  1.11      ross static int
    382  1.11      ross init_volzero_sig(f)
    383  1.11      ross 	struct open_file *f;
    384  1.11      ross {
    385  1.11      ross 	int e;
    386  1.11      ross 	ust_active_t *ustf;
    387  1.11      ross 
    388  1.11      ross 	ustf = f->f_fsdata;
    389  1.11      ross 	if (!ustf->uas_sigdone) {
    390  1.11      ross 		e = ustarfs_cylinder_read(f, 0, 0);
    391  1.11      ross 		if (e)
    392  1.11      ross 			return e;
    393  1.11      ross 		ustf->uas_volzerosig = checksig(ustf);
    394  1.11      ross 		setwindow(ustf, 0, 0);
    395  1.11      ross 	}
    396  1.11      ross 	return 0;
    397  1.11      ross }
    398  1.11      ross 
    399   1.1      ross int
    400   1.1      ross ustarfs_open(path, f)
    401  1.22       dsl 	const char *path;
    402   1.1      ross 	struct open_file *f;
    403   1.1      ross 
    404   1.1      ross {
    405   1.1      ross 	ust_active_t *ustf;
    406   1.2      ross 	ustoffs offset;
    407   1.1      ross 	char	block[512];
    408   1.1      ross 	int	filesize;
    409   1.1      ross 	int	e, e2;
    410   1.3      ross 	int	newvolblocks;
    411   1.1      ross 
    412   1.1      ross 	if (*path == '/')
    413   1.1      ross 		++path;
    414   1.1      ross 	f->f_fsdata = ustf = alloc(sizeof *ustf);
    415   1.1      ross 	memset(ustf, 0, sizeof *ustf);
    416   1.2      ross 	offset = 0;
    417   1.3      ross 	/* default to 2880 sector floppy */
    418   1.3      ross 	ustf->uas_volsize = 80 * 2 * 18 * 512 - lda2pda(0);
    419   1.1      ross 	ustf->uas_fseek = 0;
    420  1.11      ross 	e = init_volzero_sig(f);
    421  1.11      ross 	if (e)
    422  1.11      ross 		return e;
    423  1.12      ross 	e2 = EINVAL;
    424   1.1      ross 	for(;;) {
    425   1.1      ross 		ustf->uas_filestart = offset;
    426  1.12      ross 		e = read512block(f, offset, block);
    427  1.12      ross 		if (e)
    428  1.12      ross 			break;
    429  1.12      ross 		memcpy(&ustf->uas_active, block, sizeof ustf->uas_active);
    430  1.12      ross 		if(strncmp(ustf->uas_active.ust_magic, "ustar", 5)) {
    431   1.1      ross 			e = e2;
    432   1.1      ross 			break;
    433   1.1      ross 		}
    434  1.12      ross 		e2 = ENOENT;	/* it must be an actual ustarfs */
    435   1.1      ross 		ustf->uas_init_fs = 1;
    436   1.3      ross 		/* if volume metadata is found, use it */
    437   1.3      ross 		if(strncmp(ustf->uas_active.ust_name, metaname,
    438   1.3      ross 		    strlen(metaname)) == 0) {
    439   1.3      ross 			ustarfs_sscanf(ustf->uas_active.ust_name
    440   1.3      ross 				+ strlen(metaname), "%99o", &newvolblocks);
    441   1.3      ross 			ustf->uas_volsize = newvolblocks * 512
    442   1.3      ross 					  - lda2pda(0);
    443   1.3      ross 		}
    444   1.1      ross 		ustarfs_sscanf(ustf->uas_active.ust_size,"%12o",&filesize);
    445   1.1      ross 		if(strncmp(ustf->uas_active.ust_name, path,
    446   1.1      ross 		    sizeof ustf->uas_active.ust_name) == 0) {
    447   1.1      ross 			ustf->uas_filesize = filesize;
    448   1.1      ross 			break;
    449   1.1      ross 		}
    450   1.1      ross 		offset += USTAR_NAME_BLOCK + filesize;
    451   1.1      ross 		filesize %= 512;
    452   1.1      ross 		if (filesize)
    453   1.1      ross 			offset += 512 - filesize;
    454   1.1      ross 	}
    455   1.1      ross 	if (e) {
    456  1.26  christos 		dealloc(ustf, sizeof *ustf);
    457   1.1      ross 		f->f_fsdata = 0;
    458   1.1      ross 	}
    459   1.1      ross 	return e;
    460   1.1      ross }
    461   1.1      ross 
    462   1.8       cgd #ifndef LIBSA_NO_FS_WRITE
    463   1.1      ross int
    464   1.1      ross ustarfs_write(f, start, size, resid)
    465   1.1      ross 	struct open_file *f;
    466   1.1      ross 	void *start;
    467   1.1      ross 	size_t size;
    468   1.1      ross 	size_t *resid;
    469   1.1      ross {
    470   1.1      ross 	return (EROFS);
    471   1.1      ross }
    472   1.8       cgd #endif /* !LIBSA_NO_FS_WRITE */
    473   1.1      ross 
    474   1.8       cgd #ifndef LIBSA_NO_FS_SEEK
    475   1.1      ross off_t
    476   1.1      ross ustarfs_seek(f, offs, whence)
    477   1.1      ross 	struct open_file *f;
    478   1.1      ross 	off_t offs;
    479   1.1      ross 	int whence;
    480   1.1      ross {
    481   1.1      ross 	ust_active_t *ustf;
    482   1.1      ross 
    483   1.1      ross 	ustf = f->f_fsdata;
    484   1.1      ross 	switch (whence) {
    485   1.1      ross 	    case SEEK_SET:
    486   1.1      ross 		ustf->uas_fseek = offs;
    487   1.1      ross 		break;
    488   1.1      ross 	    case SEEK_CUR:
    489   1.1      ross 		ustf->uas_fseek += offs;
    490   1.1      ross 		break;
    491   1.1      ross 	    case SEEK_END:
    492   1.1      ross 		ustf->uas_fseek = ustf->uas_filesize - offs;
    493   1.1      ross 		break;
    494   1.1      ross 	    default:
    495   1.1      ross 		return -1;
    496   1.1      ross 	}
    497   1.1      ross 	return ustf->uas_fseek;
    498   1.1      ross }
    499  1.19     lukem #endif /* !LIBSA_NO_FS_SEEK */
    500   1.1      ross 
    501   1.1      ross int
    502   1.1      ross ustarfs_read(f, start, size, resid)
    503   1.1      ross 	struct open_file *f;
    504   1.1      ross 	void *start;
    505   1.1      ross 	size_t size;
    506   1.1      ross 	size_t *resid;
    507   1.1      ross {
    508   1.1      ross 	ust_active_t *ustf;
    509   1.1      ross 	int	e;
    510   1.1      ross 	char	*space512;
    511   1.1      ross 	int	blkoffs,
    512   1.1      ross 		readoffs,
    513   1.1      ross 		bufferoffset;
    514   1.1      ross 	size_t	seg;
    515  1.23      fvdl 	size_t	infile,
    516   1.1      ross 		inbuffer;
    517   1.1      ross 
    518   1.1      ross 	e = 0;
    519   1.1      ross 	space512 = alloc(512);
    520   1.1      ross 	ustf = f->f_fsdata;
    521   1.1      ross 	while(size != 0) {
    522   1.1      ross 		if (ustf->uas_fseek >= ustf->uas_filesize)
    523   1.1      ross 			break;
    524   1.1      ross 		bufferoffset = ustf->uas_fseek % 512;
    525   1.1      ross 		blkoffs  = ustf->uas_fseek - bufferoffset;
    526   1.1      ross 		readoffs = ustf->uas_filestart + 512 + blkoffs;
    527   1.1      ross 		e = read512block(f, readoffs, space512);
    528   1.1      ross 		if (e)
    529   1.1      ross 			break;
    530   1.1      ross 		seg = size;
    531   1.1      ross 		inbuffer = 512 - bufferoffset;
    532   1.1      ross 		if (inbuffer < seg)
    533   1.1      ross 			seg = inbuffer;
    534   1.1      ross 		infile = ustf->uas_filesize - ustf->uas_fseek;
    535   1.1      ross 		if (infile < seg)
    536   1.1      ross 			seg = infile;
    537   1.1      ross 		memcpy(start, space512 + bufferoffset, seg);
    538   1.1      ross 		ustf->uas_fseek += seg;
    539  1.29        he 		start = (char *)start + seg;
    540   1.1      ross 		size  -= seg;
    541   1.1      ross 	}
    542   1.1      ross 	if (resid)
    543   1.1      ross 		*resid = size;
    544  1.26  christos 	dealloc(space512, 512);
    545   1.1      ross 	return e;
    546   1.1      ross }
    547   1.1      ross 
    548   1.1      ross int
    549   1.1      ross ustarfs_stat(f, sb)
    550   1.1      ross 	struct open_file *f;
    551   1.1      ross 	struct stat *sb;
    552   1.1      ross {
    553   1.1      ross 	int	mode, uid, gid;
    554   1.1      ross 	ust_active_t *ustf;
    555   1.1      ross 
    556   1.1      ross 	if (f == NULL)
    557   1.1      ross 		return EINVAL;
    558   1.1      ross 	ustf = f->f_fsdata;
    559   1.1      ross 	memset(sb, 0, sizeof *sb);
    560   1.1      ross 	ustarfs_sscanf(ustf->uas_active.ust_mode, "%8o", &mode);
    561   1.1      ross 	ustarfs_sscanf(ustf->uas_active.ust_uid, "%8o", &uid);
    562   1.1      ross 	ustarfs_sscanf(ustf->uas_active.ust_gid, "%8o", &gid);
    563   1.1      ross 	sb->st_mode = mode;
    564   1.1      ross 	sb->st_uid  = uid;
    565   1.1      ross 	sb->st_gid  = gid;
    566   1.1      ross 	sb->st_size = ustf->uas_filesize;
    567   1.1      ross 	return 0;
    568   1.1      ross }
    569   1.1      ross 
    570   1.8       cgd #ifndef LIBSA_NO_FS_CLOSE
    571   1.1      ross int
    572   1.1      ross ustarfs_close(f)
    573   1.1      ross 	struct open_file *f;
    574   1.1      ross {
    575   1.1      ross 	if (f == NULL || f->f_fsdata == NULL)
    576   1.1      ross 		return EINVAL;
    577  1.26  christos 	dealloc(f->f_fsdata, sizeof(ust_active_t));
    578   1.1      ross 	f->f_fsdata = 0;
    579   1.1      ross 	return 0;
    580   1.1      ross }
    581   1.8       cgd #endif /* !LIBSA_NO_FS_CLOSE */
    582