Home | History | Annotate | Line # | Download | only in pax
ftree.c revision 1.19
      1  1.19     lukem /*	$NetBSD: ftree.c,v 1.19 2002/04/12 04:44:08 lukem Exp $	*/
      2   1.4       cgd 
      3   1.1       jtc /*-
      4   1.1       jtc  * Copyright (c) 1992 Keith Muller.
      5   1.1       jtc  * Copyright (c) 1992, 1993
      6   1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      7   1.1       jtc  *
      8   1.1       jtc  * This code is derived from software contributed to Berkeley by
      9   1.1       jtc  * Keith Muller of the University of California, San Diego.
     10   1.1       jtc  *
     11   1.1       jtc  * Redistribution and use in source and binary forms, with or without
     12   1.1       jtc  * modification, are permitted provided that the following conditions
     13   1.1       jtc  * are met:
     14   1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     15   1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     16   1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       jtc  *    documentation and/or other materials provided with the distribution.
     19   1.1       jtc  * 3. All advertising materials mentioning features or use of this software
     20   1.1       jtc  *    must display the following acknowledgement:
     21   1.1       jtc  *	This product includes software developed by the University of
     22   1.1       jtc  *	California, Berkeley and its contributors.
     23   1.1       jtc  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       jtc  *    may be used to endorse or promote products derived from this software
     25   1.1       jtc  *    without specific prior written permission.
     26   1.1       jtc  *
     27   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       jtc  * SUCH DAMAGE.
     38   1.1       jtc  */
     39   1.1       jtc 
     40  1.12     lukem /*-
     41  1.12     lukem  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     42  1.12     lukem  * All rights reserved.
     43  1.12     lukem  *
     44  1.12     lukem  * This code is derived from software contributed to The NetBSD Foundation
     45  1.12     lukem  * by Luke Mewburn of Wasabi Systems.
     46  1.12     lukem  *
     47  1.12     lukem  * Redistribution and use in source and binary forms, with or without
     48  1.12     lukem  * modification, are permitted provided that the following conditions
     49  1.12     lukem  * are met:
     50  1.12     lukem  * 1. Redistributions of source code must retain the above copyright
     51  1.12     lukem  *    notice, this list of conditions and the following disclaimer.
     52  1.12     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     53  1.12     lukem  *    notice, this list of conditions and the following disclaimer in the
     54  1.12     lukem  *    documentation and/or other materials provided with the distribution.
     55  1.12     lukem  * 3. All advertising materials mentioning features or use of this software
     56  1.12     lukem  *    must display the following acknowledgement:
     57  1.12     lukem  *        This product includes software developed by the NetBSD
     58  1.12     lukem  *        Foundation, Inc. and its contributors.
     59  1.12     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     60  1.12     lukem  *    contributors may be used to endorse or promote products derived
     61  1.12     lukem  *    from this software without specific prior written permission.
     62  1.12     lukem  *
     63  1.12     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     64  1.12     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     65  1.12     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     66  1.12     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     67  1.12     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     68  1.12     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     69  1.12     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     70  1.12     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     71  1.12     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     72  1.12     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     73  1.12     lukem  * POSSIBILITY OF SUCH DAMAGE.
     74  1.12     lukem  */
     75  1.12     lukem 
     76   1.6  christos #include <sys/cdefs.h>
     77  1.17        tv #if defined(__RCSID) && !defined(lint)
     78   1.4       cgd #if 0
     79   1.4       cgd static char sccsid[] = "@(#)ftree.c	8.2 (Berkeley) 4/18/94";
     80   1.4       cgd #else
     81  1.19     lukem __RCSID("$NetBSD: ftree.c,v 1.19 2002/04/12 04:44:08 lukem Exp $");
     82   1.4       cgd #endif
     83   1.1       jtc #endif /* not lint */
     84   1.1       jtc 
     85   1.1       jtc #include <sys/types.h>
     86   1.1       jtc #include <sys/time.h>
     87   1.1       jtc #include <sys/stat.h>
     88   1.1       jtc #include <sys/param.h>
     89   1.1       jtc #include <ctype.h>
     90   1.1       jtc #include <errno.h>
     91  1.17        tv #include <fts.h>
     92  1.17        tv #include <stdio.h>
     93   1.1       jtc #include <stdlib.h>
     94  1.17        tv #include <string.h>
     95  1.17        tv #include <unistd.h>
     96   1.1       jtc #include "pax.h"
     97   1.1       jtc #include "ftree.h"
     98   1.1       jtc #include "extern.h"
     99  1.18     lukem #ifndef SMALL
    100  1.12     lukem #include "mtree.h"
    101  1.18     lukem #endif	/* SMALL */
    102  1.16        tv 
    103   1.1       jtc /*
    104   1.1       jtc  * routines to interface with the fts library function.
    105   1.1       jtc  *
    106   1.1       jtc  * file args supplied to pax are stored on a single linked list (of type FTREE)
    107   1.1       jtc  * and given to fts to be processed one at a time. pax "selects" files from
    108   1.1       jtc  * the expansion of each arg into the corresponding file tree (if the arg is a
    109   1.1       jtc  * directory, otherwise the node itself is just passed to pax). The selection
    110   1.1       jtc  * is modified by the -n and -u flags. The user is informed when a specific
    111   1.1       jtc  * file arg does not generate any selected files. -n keeps expanding the file
    112   1.1       jtc  * tree arg until one of its files is selected, then skips to the next file
    113   1.1       jtc  * arg. when the user does not supply the file trees as command line args to
    114   1.1       jtc  * pax, they are read from stdin
    115   1.1       jtc  */
    116   1.1       jtc 
    117  1.10     itohy static FTS *ftsp = NULL;		/* current FTS handle */
    118   1.1       jtc static int ftsopts;			/* options to be used on fts_open */
    119   1.1       jtc static char *farray[2];			/* array for passing each arg to fts */
    120   1.1       jtc static FTREE *fthead = NULL;		/* head of linked list of file args */
    121   1.1       jtc static FTREE *fttail = NULL;		/* tail of linked list of file args */
    122   1.1       jtc static FTREE *ftcur = NULL;		/* current file arg being processed */
    123   1.1       jtc static FTSENT *ftent = NULL;		/* current file tree entry */
    124   1.1       jtc static int ftree_skip;			/* when set skip to next file arg */
    125  1.18     lukem #ifndef SMALL
    126  1.12     lukem static NODE *ftnode = NULL;		/* mtree(8) specfile; used by -M */
    127  1.18     lukem #endif	/* SMALL */
    128   1.1       jtc 
    129  1.11     lukem static int ftree_arg(void);
    130  1.11     lukem 
    131  1.11     lukem #ifdef NET2_FTS
    132  1.11     lukem #define	FTS_ERRNO(x)	errno
    133  1.11     lukem #else
    134  1.11     lukem #define	FTS_ERRNO(x)	(x)->fts_errno
    135  1.11     lukem #endif
    136   1.1       jtc 
    137   1.1       jtc /*
    138   1.1       jtc  * ftree_start()
    139   1.1       jtc  *	initialize the options passed to fts_open() during this run of pax
    140   1.1       jtc  *	options are based on the selection of pax options by the user
    141   1.1       jtc  *	fts_start() also calls fts_arg() to open the first valid file arg. We
    142   1.1       jtc  *	also attempt to reset directory access times when -t (tflag) is set.
    143   1.1       jtc  * Return:
    144   1.1       jtc  *	0 if there is at least one valid file arg to process, -1 otherwise
    145   1.1       jtc  */
    146   1.1       jtc 
    147   1.1       jtc int
    148   1.1       jtc ftree_start(void)
    149   1.1       jtc {
    150  1.12     lukem 
    151  1.18     lukem #ifndef SMALL
    152  1.12     lukem 	/*
    153  1.12     lukem 	 * if -M is given, the list of filenames on stdin is actually
    154  1.12     lukem 	 * an mtree(8) specfile, so parse the specfile into a NODE *
    155  1.12     lukem 	 * tree at ftnode, for use by next_file()
    156  1.12     lukem 	 */
    157  1.12     lukem 	if (Mflag) {
    158  1.12     lukem 		if (fthead != NULL) {
    159  1.12     lukem 			tty_warn(1,
    160  1.12     lukem 	    "The -M flag is only supported when reading file list from stdin");
    161  1.12     lukem 			return(-1);
    162  1.12     lukem 		}
    163  1.12     lukem 		ftnode = spec(stdin);
    164  1.12     lukem 		if (ftnode != NULL &&
    165  1.12     lukem 		    (ftnode->type != F_DIR || strcmp(ftnode->name, ".") != 0)) {
    166  1.12     lukem 			tty_warn(1,
    167  1.12     lukem 			    "First node of specfile is not `.' directory");
    168  1.12     lukem 			return(-1);
    169  1.12     lukem 		}
    170  1.12     lukem 		return(0);
    171  1.12     lukem 	}
    172  1.18     lukem #endif	/* SMALL */
    173  1.12     lukem 
    174   1.1       jtc 	/*
    175   1.1       jtc 	 * set up the operation mode of fts, open the first file arg. We must
    176   1.1       jtc 	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
    177   1.1       jtc 	 * if fts did a chdir off into the boondocks, we may create an archive
    178   1.1       jtc 	 * volume in an place where the user did not expect to.
    179   1.1       jtc 	 */
    180   1.1       jtc 	ftsopts = FTS_NOCHDIR;
    181   1.1       jtc 
    182   1.1       jtc 	/*
    183   1.1       jtc 	 * optional user flags that effect file traversal
    184   1.1       jtc 	 * -H command line symlink follow only (half follow)
    185   1.1       jtc 	 * -L follow sylinks (logical)
    186   1.1       jtc 	 * -P do not follow sylinks (physical). This is the default.
    187   1.1       jtc 	 * -X do not cross over mount points
    188   1.1       jtc 	 * -t preserve access times on files read.
    189   1.1       jtc 	 * -n select only the first member of a file tree when a match is found
    190   1.1       jtc 	 * -d do not extract subtrees rooted at a directory arg.
    191   1.1       jtc 	 */
    192   1.1       jtc 	if (Lflag)
    193   1.1       jtc 		ftsopts |= FTS_LOGICAL;
    194   1.1       jtc 	else
    195   1.1       jtc 		ftsopts |= FTS_PHYSICAL;
    196   1.1       jtc 	if (Hflag)
    197  1.11     lukem #ifdef NET2_FTS
    198   1.6  christos 		tty_warn(0, "The -H flag is not supported on this version");
    199  1.11     lukem #else
    200   1.1       jtc 		ftsopts |= FTS_COMFOLLOW;
    201  1.11     lukem #endif
    202   1.1       jtc 	if (Xflag)
    203   1.1       jtc 		ftsopts |= FTS_XDEV;
    204   1.1       jtc 
    205   1.1       jtc 	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
    206   1.6  christos 		tty_warn(1, "Unable to allocate memory for file name buffer");
    207   1.1       jtc 		return(-1);
    208   1.1       jtc 	}
    209   1.1       jtc 
    210   1.1       jtc 	if (ftree_arg() < 0)
    211   1.1       jtc 		return(-1);
    212   1.1       jtc 	if (tflag && (atdir_start() < 0))
    213   1.1       jtc 		return(-1);
    214   1.1       jtc 	return(0);
    215   1.1       jtc }
    216   1.1       jtc 
    217   1.1       jtc /*
    218   1.1       jtc  * ftree_add()
    219   1.1       jtc  *	add the arg to the linked list of files to process. Each will be
    220   1.1       jtc  *	processed by fts one at a time
    221   1.1       jtc  * Return:
    222   1.1       jtc  *	0 if added to the linked list, -1 if failed
    223   1.1       jtc  */
    224   1.1       jtc 
    225   1.1       jtc int
    226   1.9        is ftree_add(char *str, int isdir)
    227   1.1       jtc {
    228   1.5       tls 	FTREE *ft;
    229   1.5       tls 	int len;
    230   1.1       jtc 
    231   1.1       jtc 	/*
    232   1.1       jtc 	 * simple check for bad args
    233   1.1       jtc 	 */
    234   1.1       jtc 	if ((str == NULL) || (*str == '\0')) {
    235   1.6  christos 		tty_warn(0, "Invalid file name arguement");
    236   1.1       jtc 		return(-1);
    237   1.1       jtc 	}
    238   1.1       jtc 
    239   1.1       jtc 	/*
    240   1.1       jtc 	 * allocate FTREE node and add to the end of the linked list (args are
    241   1.1       jtc 	 * processed in the same order they were passed to pax). Get rid of any
    242   1.1       jtc 	 * trailing / the user may pass us. (watch out for / by itself).
    243   1.1       jtc 	 */
    244   1.1       jtc 	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
    245   1.6  christos 		tty_warn(0, "Unable to allocate memory for filename");
    246   1.1       jtc 		return(-1);
    247   1.1       jtc 	}
    248   1.1       jtc 
    249   1.1       jtc 	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
    250   1.1       jtc 		str[len] = '\0';
    251   1.1       jtc 	ft->fname = str;
    252   1.9        is 	ft->refcnt = -isdir;
    253   1.1       jtc 	ft->fow = NULL;
    254   1.1       jtc 	if (fthead == NULL) {
    255   1.1       jtc 		fttail = fthead = ft;
    256   1.1       jtc 		return(0);
    257   1.1       jtc 	}
    258   1.1       jtc 	fttail->fow = ft;
    259   1.1       jtc 	fttail = ft;
    260   1.1       jtc 	return(0);
    261   1.1       jtc }
    262   1.1       jtc 
    263   1.1       jtc /*
    264   1.1       jtc  * ftree_sel()
    265   1.1       jtc  *	this entry has been selected by pax. bump up reference count and handle
    266   1.1       jtc  *	-n and -d processing.
    267   1.1       jtc  */
    268   1.1       jtc 
    269   1.1       jtc void
    270   1.5       tls ftree_sel(ARCHD *arcn)
    271   1.1       jtc {
    272   1.1       jtc 	/*
    273   1.1       jtc 	 * set reference bit for this pattern. This linked list is only used
    274   1.1       jtc 	 * when file trees are supplied pax as args. The list is not used when
    275   1.1       jtc 	 * the trees are read from stdin.
    276   1.1       jtc 	 */
    277  1.10     itohy 	if (ftcur != NULL)
    278   1.1       jtc 		ftcur->refcnt = 1;
    279   1.1       jtc 
    280   1.1       jtc 	/*
    281   1.1       jtc 	 * if -n we are done with this arg, force a skip to the next arg when
    282   1.1       jtc 	 * pax asks for the next file in next_file().
    283  1.12     lukem 	 * if -M we don't use fts(3), so the rest of this function is moot.
    284   1.1       jtc 	 * if -d we tell fts only to match the directory (if the arg is a dir)
    285   1.1       jtc 	 * and not the entire file tree rooted at that point.
    286   1.1       jtc 	 */
    287   1.1       jtc 	if (nflag)
    288   1.1       jtc 		ftree_skip = 1;
    289   1.1       jtc 
    290  1.12     lukem 	if (Mflag || !dflag || (arcn->type != PAX_DIR))
    291   1.1       jtc 		return;
    292   1.1       jtc 
    293   1.1       jtc 	if (ftent != NULL)
    294   1.1       jtc 		(void)fts_set(ftsp, ftent, FTS_SKIP);
    295   1.1       jtc }
    296   1.1       jtc 
    297   1.1       jtc /*
    298   1.1       jtc  * ftree_chk()
    299   1.1       jtc  *	called at end on pax execution. Prints all those file args that did not
    300   1.1       jtc  *	have a selected member (reference count still 0)
    301   1.1       jtc  */
    302   1.1       jtc 
    303   1.1       jtc void
    304   1.1       jtc ftree_chk(void)
    305   1.1       jtc {
    306   1.5       tls 	FTREE *ft;
    307   1.5       tls 	int wban = 0;
    308   1.1       jtc 
    309   1.1       jtc 	/*
    310   1.1       jtc 	 * make sure all dir access times were reset.
    311   1.1       jtc 	 */
    312   1.1       jtc 	if (tflag)
    313   1.1       jtc 		atdir_end();
    314   1.1       jtc 
    315   1.1       jtc 	/*
    316   1.1       jtc 	 * walk down list and check reference count. Print out those members
    317   1.1       jtc 	 * that never had a match
    318   1.1       jtc 	 */
    319   1.1       jtc 	for (ft = fthead; ft != NULL; ft = ft->fow) {
    320   1.9        is 		if (ft->refcnt != 0)
    321   1.1       jtc 			continue;
    322   1.1       jtc 		if (wban == 0) {
    323   1.6  christos 			tty_warn(1,
    324   1.6  christos 			    "WARNING! These file names were not selected:");
    325   1.1       jtc 			++wban;
    326   1.1       jtc 		}
    327   1.1       jtc 		(void)fprintf(stderr, "%s\n", ft->fname);
    328   1.1       jtc 	}
    329   1.1       jtc }
    330   1.1       jtc 
    331   1.1       jtc /*
    332   1.1       jtc  * ftree_arg()
    333   1.1       jtc  *	Get the next file arg for fts to process. Can be from either the linked
    334   1.1       jtc  *	list or read from stdin when the user did not them as args to pax. Each
    335   1.1       jtc  *	arg is processed until the first successful fts_open().
    336   1.1       jtc  * Return:
    337   1.1       jtc  *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
    338   1.1       jtc  *	stdin).
    339   1.1       jtc  */
    340   1.1       jtc 
    341   1.1       jtc static int
    342   1.1       jtc ftree_arg(void)
    343   1.1       jtc {
    344   1.5       tls 	char *pt;
    345   1.1       jtc 
    346   1.1       jtc 	/*
    347   1.1       jtc 	 * close off the current file tree
    348   1.1       jtc 	 */
    349   1.1       jtc 	if (ftsp != NULL) {
    350   1.1       jtc 		(void)fts_close(ftsp);
    351   1.1       jtc 		ftsp = NULL;
    352   1.1       jtc 	}
    353   1.1       jtc 
    354   1.1       jtc 	/*
    355   1.1       jtc 	 * keep looping until we get a valid file tree to process. Stop when we
    356   1.1       jtc 	 * reach the end of the list (or get an eof on stdin)
    357   1.1       jtc 	 */
    358   1.1       jtc 	for(;;) {
    359   1.1       jtc 		if (fthead == NULL) {
    360   1.1       jtc 			/*
    361   1.1       jtc 			 * the user didn't supply any args, get the file trees
    362  1.10     itohy 			 * to process from stdin;
    363   1.1       jtc 			 */
    364   1.1       jtc 			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
    365   1.1       jtc 				return(-1);
    366   1.1       jtc 			if ((pt = strchr(farray[0], '\n')) != NULL)
    367   1.1       jtc 				*pt = '\0';
    368   1.1       jtc 		} else {
    369   1.1       jtc 			/*
    370   1.1       jtc 			 * the user supplied the file args as arguements to pax
    371   1.1       jtc 			 */
    372   1.1       jtc 			if (ftcur == NULL)
    373   1.1       jtc 				ftcur = fthead;
    374   1.1       jtc 			else if ((ftcur = ftcur->fow) == NULL)
    375   1.1       jtc 				return(-1);
    376   1.9        is 
    377   1.9        is 			if (ftcur->refcnt < 0) {
    378   1.9        is 				/*
    379   1.9        is 				 * chdir entry.
    380   1.9        is 				 * Change directory and retry loop.
    381   1.9        is 				 */
    382   1.9        is 				if (ar_dochdir(ftcur->fname))
    383   1.9        is 					return (-1);
    384   1.9        is 				continue;
    385   1.9        is 			}
    386   1.1       jtc 			farray[0] = ftcur->fname;
    387   1.1       jtc 		}
    388   1.1       jtc 
    389   1.1       jtc 		/*
    390   1.1       jtc 		 * watch it, fts wants the file arg stored in a array of char
    391   1.1       jtc 		 * ptrs, with the last one a null. we use a two element array
    392   1.1       jtc 		 * and set farray[0] to point at the buffer with the file name
    393  1.10     itohy 		 * in it. We cannot pass all the file args to fts at one shot
    394   1.1       jtc 		 * as we need to keep a handle on which file arg generates what
    395   1.1       jtc 		 * files (the -n and -d flags need this). If the open is
    396   1.1       jtc 		 * successful, return a 0.
    397   1.1       jtc 		 */
    398   1.1       jtc 		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
    399   1.1       jtc 			break;
    400   1.1       jtc 	}
    401   1.1       jtc 	return(0);
    402   1.1       jtc }
    403   1.1       jtc 
    404   1.1       jtc /*
    405   1.1       jtc  * next_file()
    406   1.1       jtc  *	supplies the next file to process in the supplied archd structure.
    407   1.1       jtc  * Return:
    408   1.1       jtc  *	0 when contents of arcn have been set with the next file, -1 when done.
    409   1.1       jtc  */
    410   1.1       jtc 
    411   1.1       jtc int
    412   1.5       tls next_file(ARCHD *arcn)
    413   1.1       jtc {
    414  1.18     lukem #ifndef SMALL
    415  1.12     lukem 	static	char	curdir[PAXPATHLEN+2], curpath[PAXPATHLEN+2];
    416  1.12     lukem 	static	int	curdirlen;
    417  1.12     lukem 
    418  1.12     lukem 	struct stat	statbuf;
    419  1.12     lukem 	FTSENT		Mftent;
    420  1.18     lukem #endif	/* SMALL */
    421  1.12     lukem 	int		cnt;
    422  1.12     lukem 	time_t		atime, mtime;
    423  1.12     lukem 	char		*curlink;
    424  1.12     lukem #define MFTENT_DUMMY_DEV	UINT_MAX
    425  1.12     lukem 
    426  1.12     lukem 	curlink = NULL;
    427  1.18     lukem #ifndef SMALL
    428  1.12     lukem 	/*
    429  1.12     lukem 	 * if parsing an mtree(8) specfile, build up `dummy' ftsent
    430  1.12     lukem 	 * from specfile info, and jump below to complete setup of arcn.
    431  1.12     lukem 	 */
    432  1.12     lukem 	if (Mflag) {
    433  1.19     lukem 		int	optionalfake;
    434  1.19     lukem 
    435  1.19     lukem  next_ftnode:
    436  1.19     lukem 		optionalfake = 0;
    437  1.12     lukem 		if (ftnode == NULL)		/* tree is empty */
    438  1.12     lukem 			return (-1);
    439  1.12     lukem 
    440  1.12     lukem 						/* get current name */
    441  1.12     lukem 		if (snprintf(curpath, sizeof(curpath), "%s%s%s",
    442  1.12     lukem 		    curdir, curdirlen ? "/" : "", ftnode->name)
    443  1.12     lukem 		    >= sizeof(curpath)) {
    444  1.15     lukem 			tty_warn(1, "line %lu: %s: %s", (u_long)ftnode->lineno,
    445  1.14     lukem 			    curdir, strerror(ENAMETOOLONG));
    446  1.12     lukem 			return (-1);
    447  1.12     lukem 		}
    448  1.12     lukem 		ftnode->flags |= F_VISIT;	/* mark node visited */
    449  1.12     lukem 
    450  1.12     lukem 						/* construct dummy FTSENT */
    451  1.12     lukem 		Mftent.fts_path = curpath;
    452  1.12     lukem 		Mftent.fts_statp = &statbuf;
    453  1.12     lukem 		Mftent.fts_pointer = ftnode;
    454  1.12     lukem 		ftent = &Mftent;
    455  1.18     lukem 						/* look for existing file */
    456  1.12     lukem 		if (lstat(Mftent.fts_path, &statbuf) == -1) {
    457  1.19     lukem 			if (ftnode->flags & F_OPT)
    458  1.19     lukem 				optionalfake = 1;
    459  1.19     lukem 
    460  1.18     lukem 						/* missing: fake up stat info */
    461  1.12     lukem 			memset(&statbuf, 0, sizeof(statbuf));
    462  1.12     lukem 			statbuf.st_dev = MFTENT_DUMMY_DEV;
    463  1.12     lukem 			statbuf.st_ino = ftnode->lineno;
    464  1.12     lukem 			statbuf.st_size = 0;
    465  1.12     lukem #define NODETEST(t, m)							\
    466  1.12     lukem 			if (!(t)) {					\
    467  1.15     lukem 				tty_warn(1, "line %lu: %s: %s not specified", \
    468  1.15     lukem 				    (u_long)ftnode->lineno,		\
    469  1.15     lukem 				    ftent->fts_path, m);		\
    470  1.12     lukem 				return(-1);				\
    471  1.12     lukem 			}
    472  1.12     lukem 			statbuf.st_mode = nodetoino(ftnode->type);
    473  1.12     lukem 			NODETEST(ftnode->flags & F_TYPE, "type");
    474  1.12     lukem 			NODETEST(ftnode->flags & F_MODE, "mode");
    475  1.12     lukem 			if (!(ftnode->flags & F_TIME))
    476  1.12     lukem 				statbuf.st_mtime = starttime;
    477  1.13     lukem 			NODETEST(ftnode->flags & (F_GID | F_GNAME), "group");
    478  1.13     lukem 			NODETEST(ftnode->flags & (F_UID | F_UNAME), "user");
    479  1.12     lukem 			if (ftnode->type == F_BLOCK || ftnode->type == F_CHAR)
    480  1.12     lukem 				NODETEST(ftnode->flags & F_DEV,
    481  1.12     lukem 				    "device number");
    482  1.12     lukem 			if (ftnode->type == F_LINK)
    483  1.12     lukem 				NODETEST(ftnode->flags & F_SLINK, "symlink");
    484  1.12     lukem 			/* don't require F_FLAGS or F_SIZE */
    485  1.12     lukem #undef NODETEST
    486  1.18     lukem 		} else {
    487  1.18     lukem 			if (ftnode->flags & F_TYPE && nodetoino(ftnode->type)
    488  1.18     lukem 			    != (statbuf.st_mode & S_IFMT)) {
    489  1.18     lukem 				tty_warn(1,
    490  1.15     lukem 			    "line %lu: %s: type mismatch: specfile %s, tree %s",
    491  1.18     lukem 				    (u_long)ftnode->lineno, ftent->fts_path,
    492  1.18     lukem 				    inotype(nodetoino(ftnode->type)),
    493  1.18     lukem 				    inotype(statbuf.st_mode));
    494  1.18     lukem 				return(-1);
    495  1.18     lukem 			}
    496  1.12     lukem 		}
    497  1.12     lukem 		/*
    498  1.12     lukem 		 * override settings with those from specfile
    499  1.12     lukem 		 */
    500  1.12     lukem 		if (ftnode->flags & F_MODE) {
    501  1.12     lukem 			statbuf.st_mode &= ~ALLPERMS;
    502  1.12     lukem 			statbuf.st_mode |= (ftnode->st_mode & ALLPERMS);
    503  1.12     lukem 		}
    504  1.12     lukem 		if (ftnode->flags & (F_GID | F_GNAME))
    505  1.12     lukem 			statbuf.st_gid = ftnode->st_gid;
    506  1.12     lukem 		if (ftnode->flags & (F_UID | F_UNAME))
    507  1.12     lukem 			statbuf.st_uid = ftnode->st_uid;
    508  1.16        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    509  1.12     lukem 		if (ftnode->flags & F_FLAGS)
    510  1.12     lukem 			statbuf.st_flags = ftnode->st_flags;
    511  1.16        tv #endif
    512  1.12     lukem 		if (ftnode->flags & F_TIME)
    513  1.16        tv #ifdef BSD4_4
    514  1.12     lukem 			statbuf.st_mtimespec = ftnode->st_mtimespec;
    515  1.16        tv #else
    516  1.16        tv 			statbuf.st_mtime = ftnode->st_mtimespec.tv_sec;
    517  1.16        tv #endif
    518  1.12     lukem 		if (ftnode->flags & F_DEV)
    519  1.12     lukem 			statbuf.st_rdev = ftnode->st_rdev;
    520  1.12     lukem 		if (ftnode->flags & F_SLINK)
    521  1.12     lukem 			curlink = ftnode->slink;
    522  1.12     lukem 				/* ignore F_SIZE */
    523  1.12     lukem 
    524  1.12     lukem 		/*
    525  1.12     lukem 		 * find next node
    526  1.12     lukem 		 */
    527  1.12     lukem 		if (ftnode->type == F_DIR && ftnode->child != NULL) {
    528  1.12     lukem 					/* directory with unseen child */
    529  1.12     lukem 			ftnode = ftnode->child;
    530  1.12     lukem 			curdirlen = l_strncpy(curdir, curpath, sizeof(curdir));
    531  1.12     lukem 		} else do {
    532  1.12     lukem 			if (ftnode->next != NULL) {
    533  1.12     lukem 					/* next node at current level */
    534  1.12     lukem 				ftnode = ftnode->next;
    535  1.12     lukem 			} else {	/* move back to parent */
    536  1.12     lukem 					/* reset time only on first cd.. */
    537  1.12     lukem 				if (Mftent.fts_pointer == ftnode && tflag &&
    538  1.12     lukem 				    (get_atdir(MFTENT_DUMMY_DEV, ftnode->lineno,
    539  1.12     lukem 				    &mtime, &atime) == 0)) {
    540  1.12     lukem 					set_ftime(ftent->fts_path,
    541  1.12     lukem 					    mtime, atime, 1);
    542  1.12     lukem 				}
    543  1.12     lukem 				if (ftnode->parent == ftnode)
    544  1.12     lukem 					ftnode = NULL;
    545  1.12     lukem 				else
    546  1.12     lukem 					ftnode = ftnode->parent;
    547  1.12     lukem 				if (ftnode != NULL) {
    548  1.12     lukem 					curdirlen -= strlen(ftnode->name) + 1;
    549  1.12     lukem 					curdir[curdirlen] = '\0';
    550  1.12     lukem 				}
    551  1.12     lukem 			}
    552  1.12     lukem 		} while (ftnode != NULL && ftnode->flags & F_VISIT);
    553  1.19     lukem 		if (optionalfake)	/* skip optional faked up entries */
    554  1.19     lukem 			goto next_ftnode;
    555  1.12     lukem 		goto got_ftent;
    556  1.12     lukem 	}
    557  1.18     lukem #endif	/* SMALL */
    558   1.1       jtc 
    559   1.1       jtc 	/*
    560   1.1       jtc 	 * ftree_sel() might have set the ftree_skip flag if the user has the
    561   1.1       jtc 	 * -n option and a file was selected from this file arg tree. (-n says
    562  1.10     itohy 	 * only one member is matched for each pattern) ftree_skip being 1
    563   1.1       jtc 	 * forces us to go to the next arg now.
    564   1.1       jtc 	 */
    565   1.1       jtc 	if (ftree_skip) {
    566   1.1       jtc 		/*
    567   1.1       jtc 		 * clear and go to next arg
    568   1.1       jtc 		 */
    569   1.1       jtc 		ftree_skip = 0;
    570   1.1       jtc 		if (ftree_arg() < 0)
    571   1.1       jtc 			return(-1);
    572   1.1       jtc 	}
    573   1.1       jtc 
    574   1.1       jtc 	/*
    575   1.1       jtc 	 * loop until we get a valid file to process
    576   1.1       jtc 	 */
    577   1.1       jtc 	for(;;) {
    578   1.1       jtc 		if ((ftent = fts_read(ftsp)) == NULL) {
    579   1.1       jtc 			/*
    580   1.1       jtc 			 * out of files in this tree, go to next arg, if none
    581   1.1       jtc 			 * we are done
    582   1.1       jtc 			 */
    583   1.1       jtc 			if (ftree_arg() < 0)
    584   1.1       jtc 				return(-1);
    585   1.1       jtc 			continue;
    586   1.1       jtc 		}
    587   1.1       jtc 
    588   1.1       jtc 		/*
    589   1.1       jtc 		 * handle each type of fts_read() flag
    590   1.1       jtc 		 */
    591   1.1       jtc 		switch(ftent->fts_info) {
    592   1.1       jtc 		case FTS_D:
    593   1.7       mrg 			/*
    594   1.7       mrg 			 * cpio does *not* decend directories listed in the
    595   1.7       mrg 			 * arguments, unlike pax/tar, so needs special handling
    596   1.7       mrg 			 * here.  failure to do so results in massive amounts
    597   1.7       mrg 			 * of duplicated files in the output.
    598   1.7       mrg 			 */
    599   1.7       mrg 			if (cpio_mode)
    600   1.7       mrg 				continue;
    601   1.8   mycroft 			/* FALLTHROUGH */
    602   1.1       jtc 		case FTS_DEFAULT:
    603   1.1       jtc 		case FTS_F:
    604   1.1       jtc 		case FTS_SL:
    605   1.1       jtc 		case FTS_SLNONE:
    606   1.1       jtc 			/*
    607   1.1       jtc 			 * these are all ok
    608   1.1       jtc 			 */
    609   1.1       jtc 			break;
    610   1.1       jtc 		case FTS_DP:
    611   1.1       jtc 			/*
    612   1.1       jtc 			 * already saw this directory. If the user wants file
    613   1.1       jtc 			 * access times reset, we use this to restore the
    614   1.1       jtc 			 * access time for this directory since this is the
    615   1.1       jtc 			 * last time we will see it in this file subtree
    616   1.1       jtc 			 * remember to force the time (this is -t on a read
    617   1.1       jtc 			 * directory, not a created directory).
    618   1.1       jtc 			 */
    619  1.11     lukem 			if (!tflag || (get_atdir(
    620  1.11     lukem #ifdef NET2_FTS
    621  1.11     lukem 			    ftent->fts_statb.st_dev, ftent->fts_statb.st_ino,
    622  1.11     lukem #else
    623  1.11     lukem 			    ftent->fts_statp->st_dev, ftent->fts_statp->st_ino,
    624  1.11     lukem #endif
    625  1.11     lukem 			    &mtime, &atime) < 0))
    626   1.1       jtc 				continue;
    627   1.1       jtc 			set_ftime(ftent->fts_path, mtime, atime, 1);
    628   1.1       jtc 			continue;
    629   1.1       jtc 		case FTS_DC:
    630   1.1       jtc 			/*
    631   1.1       jtc 			 * fts claims a file system cycle
    632   1.1       jtc 			 */
    633   1.6  christos 			tty_warn(1,"File system cycle found at %s",
    634   1.6  christos 			    ftent->fts_path);
    635   1.1       jtc 			continue;
    636   1.1       jtc 		case FTS_DNR:
    637  1.11     lukem 			syswarn(1, FTS_ERRNO(ftent),
    638   1.1       jtc 			    "Unable to read directory %s", ftent->fts_path);
    639   1.1       jtc 			continue;
    640   1.1       jtc 		case FTS_ERR:
    641  1.11     lukem 			syswarn(1, FTS_ERRNO(ftent),
    642   1.1       jtc 			    "File system traversal error");
    643   1.1       jtc 			continue;
    644   1.1       jtc 		case FTS_NS:
    645   1.1       jtc 		case FTS_NSOK:
    646  1.11     lukem 			syswarn(1, FTS_ERRNO(ftent),
    647   1.1       jtc 			    "Unable to access %s", ftent->fts_path);
    648   1.1       jtc 			continue;
    649   1.1       jtc 		}
    650   1.1       jtc 
    651  1.18     lukem #ifndef SMALL
    652  1.12     lukem  got_ftent:
    653  1.18     lukem #endif	/* SMALL */
    654   1.1       jtc 		/*
    655   1.1       jtc 		 * ok got a file tree node to process. copy info into arcn
    656   1.1       jtc 		 * structure (initialize as required)
    657   1.1       jtc 		 */
    658   1.1       jtc 		arcn->skip = 0;
    659   1.1       jtc 		arcn->pad = 0;
    660   1.1       jtc 		arcn->ln_nlen = 0;
    661   1.1       jtc 		arcn->ln_name[0] = '\0';
    662  1.11     lukem #ifdef NET2_FTS
    663   1.1       jtc 		arcn->sb = ftent->fts_statb;
    664  1.11     lukem #else
    665   1.1       jtc 		arcn->sb = *(ftent->fts_statp);
    666  1.11     lukem #endif
    667   1.1       jtc 
    668   1.1       jtc 		/*
    669   1.1       jtc 		 * file type based set up and copy into the arcn struct
    670   1.1       jtc 		 * SIDE NOTE:
    671   1.1       jtc 		 * we try to reset the access time on all files and directories
    672   1.1       jtc 		 * we may read when the -t flag is specified. files are reset
    673   1.1       jtc 		 * when we close them after copying. we reset the directories
    674   1.1       jtc 		 * when we are done with their file tree (we also clean up at
    675   1.1       jtc 		 * end in case we cut short a file tree traversal). However
    676   1.1       jtc 		 * there is no way to reset access times on symlinks.
    677   1.1       jtc 		 */
    678   1.1       jtc 		switch(S_IFMT & arcn->sb.st_mode) {
    679   1.1       jtc 		case S_IFDIR:
    680   1.1       jtc 			arcn->type = PAX_DIR;
    681   1.1       jtc 			if (!tflag)
    682   1.1       jtc 				break;
    683   1.1       jtc 			add_atdir(ftent->fts_path, arcn->sb.st_dev,
    684   1.1       jtc 			    arcn->sb.st_ino, arcn->sb.st_mtime,
    685   1.1       jtc 			    arcn->sb.st_atime);
    686   1.1       jtc 			break;
    687   1.1       jtc 		case S_IFCHR:
    688   1.1       jtc 			arcn->type = PAX_CHR;
    689   1.1       jtc 			break;
    690   1.1       jtc 		case S_IFBLK:
    691   1.1       jtc 			arcn->type = PAX_BLK;
    692   1.1       jtc 			break;
    693   1.1       jtc 		case S_IFREG:
    694   1.1       jtc 			/*
    695   1.1       jtc 			 * only regular files with have data to store on the
    696   1.1       jtc 			 * archive. all others will store a zero length skip.
    697   1.1       jtc 			 * the skip field is used by pax for actual data it has
    698   1.1       jtc 			 * to read (or skip over).
    699   1.1       jtc 			 */
    700   1.1       jtc 			arcn->type = PAX_REG;
    701   1.1       jtc 			arcn->skip = arcn->sb.st_size;
    702   1.1       jtc 			break;
    703   1.1       jtc 		case S_IFLNK:
    704   1.1       jtc 			arcn->type = PAX_SLK;
    705  1.12     lukem 			if (curlink != NULL) {
    706  1.12     lukem 				cnt = l_strncpy(arcn->ln_name, curlink,
    707  1.12     lukem 				    sizeof(arcn->ln_name));
    708   1.1       jtc 			/*
    709   1.1       jtc 			 * have to read the symlink path from the file
    710   1.1       jtc 			 */
    711  1.12     lukem 			} else if ((cnt =
    712  1.12     lukem 			    readlink(ftent->fts_path, arcn->ln_name,
    713   1.1       jtc 			    PAXPATHLEN)) < 0) {
    714   1.1       jtc 				syswarn(1, errno, "Unable to read symlink %s",
    715   1.1       jtc 				    ftent->fts_path);
    716   1.1       jtc 				continue;
    717   1.1       jtc 			}
    718   1.1       jtc 			/*
    719   1.1       jtc 			 * set link name length, watch out readlink does not
    720   1.1       jtc 			 * allways null terminate the link path
    721   1.1       jtc 			 */
    722   1.1       jtc 			arcn->ln_name[cnt] = '\0';
    723   1.1       jtc 			arcn->ln_nlen = cnt;
    724   1.1       jtc 			break;
    725   1.1       jtc 		case S_IFSOCK:
    726   1.1       jtc 			/*
    727   1.1       jtc 			 * under BSD storing a socket is senseless but we will
    728   1.1       jtc 			 * let the format specific write function make the
    729   1.1       jtc 			 * decision of what to do with it.
    730   1.1       jtc 			 */
    731   1.1       jtc 			arcn->type = PAX_SCK;
    732   1.1       jtc 			break;
    733   1.1       jtc 		case S_IFIFO:
    734   1.1       jtc 			arcn->type = PAX_FIF;
    735   1.1       jtc 			break;
    736   1.1       jtc 		}
    737   1.1       jtc 		break;
    738   1.1       jtc 	}
    739   1.1       jtc 
    740   1.1       jtc 	/*
    741   1.1       jtc 	 * copy file name, set file name length
    742   1.1       jtc 	 */
    743   1.1       jtc 	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, PAXPATHLEN+1);
    744   1.1       jtc 	arcn->name[arcn->nlen] = '\0';
    745   1.1       jtc 	arcn->org_name = ftent->fts_path;
    746   1.1       jtc 	return(0);
    747   1.1       jtc }
    748