xref: /freebsd/bin/pax/ftree.c (revision 3d65b2f568c1ad1f527ad8cc257b31758adf11a4)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
34b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
44b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
54b88c807SRodney W. Grimes  *
64b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
74b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
104b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
114b88c807SRodney W. Grimes  * are met:
124b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
144b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
164b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
344b88c807SRodney W. Grimes #ifndef lint
35c9a8d1f4SPhilippe Charnier #if 0
36c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ftree.c	8.2 (Berkeley) 4/18/94";
37c9a8d1f4SPhilippe Charnier #endif
384b88c807SRodney W. Grimes #endif /* not lint */
392749b141SDavid E. O'Brien #include <sys/cdefs.h>
402749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include <sys/types.h>
434b88c807SRodney W. Grimes #include <sys/time.h>
444b88c807SRodney W. Grimes #include <sys/stat.h>
454b88c807SRodney W. Grimes #include <unistd.h>
464b88c807SRodney W. Grimes #include <string.h>
474b88c807SRodney W. Grimes #include <stdio.h>
484b88c807SRodney W. Grimes #include <errno.h>
494b88c807SRodney W. Grimes #include <stdlib.h>
504b88c807SRodney W. Grimes #include <fts.h>
514b88c807SRodney W. Grimes #include "pax.h"
524b88c807SRodney W. Grimes #include "ftree.h"
534b88c807SRodney W. Grimes #include "extern.h"
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes /*
564b88c807SRodney W. Grimes  * routines to interface with the fts library function.
574b88c807SRodney W. Grimes  *
584b88c807SRodney W. Grimes  * file args supplied to pax are stored on a single linked list (of type FTREE)
594b88c807SRodney W. Grimes  * and given to fts to be processed one at a time. pax "selects" files from
604b88c807SRodney W. Grimes  * the expansion of each arg into the corresponding file tree (if the arg is a
614b88c807SRodney W. Grimes  * directory, otherwise the node itself is just passed to pax). The selection
624b88c807SRodney W. Grimes  * is modified by the -n and -u flags. The user is informed when a specific
634b88c807SRodney W. Grimes  * file arg does not generate any selected files. -n keeps expanding the file
644b88c807SRodney W. Grimes  * tree arg until one of its files is selected, then skips to the next file
654b88c807SRodney W. Grimes  * arg. when the user does not supply the file trees as command line args to
664b88c807SRodney W. Grimes  * pax, they are read from stdin
674b88c807SRodney W. Grimes  */
684b88c807SRodney W. Grimes 
6946be34b9SKris Kennaway static FTS *ftsp = NULL;		/* current FTS handle */
704b88c807SRodney W. Grimes static int ftsopts;			/* options to be used on fts_open */
714b88c807SRodney W. Grimes static char *farray[2];			/* array for passing each arg to fts */
724b88c807SRodney W. Grimes static FTREE *fthead = NULL;		/* head of linked list of file args */
734b88c807SRodney W. Grimes static FTREE *fttail = NULL;		/* tail of linked list of file args */
744b88c807SRodney W. Grimes static FTREE *ftcur = NULL;		/* current file arg being processed */
754b88c807SRodney W. Grimes static FTSENT *ftent = NULL;		/* current file tree entry */
764b88c807SRodney W. Grimes static int ftree_skip;			/* when set skip to next file arg */
774b88c807SRodney W. Grimes 
7846251ddeSWarner Losh static int ftree_arg(void);
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes /*
814b88c807SRodney W. Grimes  * ftree_start()
824b88c807SRodney W. Grimes  *	initialize the options passed to fts_open() during this run of pax
834b88c807SRodney W. Grimes  *	options are based on the selection of pax options by the user
844b88c807SRodney W. Grimes  *	fts_start() also calls fts_arg() to open the first valid file arg. We
854b88c807SRodney W. Grimes  *	also attempt to reset directory access times when -t (tflag) is set.
864b88c807SRodney W. Grimes  * Return:
874b88c807SRodney W. Grimes  *	0 if there is at least one valid file arg to process, -1 otherwise
884b88c807SRodney W. Grimes  */
894b88c807SRodney W. Grimes 
904b88c807SRodney W. Grimes int
914b88c807SRodney W. Grimes ftree_start(void)
924b88c807SRodney W. Grimes {
934b88c807SRodney W. Grimes 	/*
94cd8fb74dSGiorgos Keramidas 	 * Set up the operation mode of fts, open the first file arg. We must
954b88c807SRodney W. Grimes 	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
964b88c807SRodney W. Grimes 	 * if fts did a chdir off into the boondocks, we may create an archive
97cd8fb74dSGiorgos Keramidas 	 * volume in a place where the user did not expect to.
984b88c807SRodney W. Grimes 	 */
994b88c807SRodney W. Grimes 	ftsopts = FTS_NOCHDIR;
1004b88c807SRodney W. Grimes 
1014b88c807SRodney W. Grimes 	/*
1024b88c807SRodney W. Grimes 	 * optional user flags that effect file traversal
1034b88c807SRodney W. Grimes 	 * -H command line symlink follow only (half follow)
1044b88c807SRodney W. Grimes 	 * -L follow sylinks (logical)
1054b88c807SRodney W. Grimes 	 * -P do not follow sylinks (physical). This is the default.
1064b88c807SRodney W. Grimes 	 * -X do not cross over mount points
1074b88c807SRodney W. Grimes 	 * -t preserve access times on files read.
1084b88c807SRodney W. Grimes 	 * -n select only the first member of a file tree when a match is found
1094b88c807SRodney W. Grimes 	 * -d do not extract subtrees rooted at a directory arg.
1104b88c807SRodney W. Grimes 	 */
1114b88c807SRodney W. Grimes 	if (Lflag)
1124b88c807SRodney W. Grimes 		ftsopts |= FTS_LOGICAL;
1134b88c807SRodney W. Grimes 	else
1144b88c807SRodney W. Grimes 		ftsopts |= FTS_PHYSICAL;
1154b88c807SRodney W. Grimes 	if (Hflag)
1164b88c807SRodney W. Grimes #	ifdef NET2_FTS
117778766feSKris Kennaway 		paxwarn(0, "The -H flag is not supported on this version");
1184b88c807SRodney W. Grimes #	else
1194b88c807SRodney W. Grimes 		ftsopts |= FTS_COMFOLLOW;
1204b88c807SRodney W. Grimes #	endif
1214b88c807SRodney W. Grimes 	if (Xflag)
1224b88c807SRodney W. Grimes 		ftsopts |= FTS_XDEV;
1234b88c807SRodney W. Grimes 
1244b88c807SRodney W. Grimes 	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
125778766feSKris Kennaway 		paxwarn(1, "Unable to allocate memory for file name buffer");
1264b88c807SRodney W. Grimes 		return(-1);
1274b88c807SRodney W. Grimes 	}
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes 	if (ftree_arg() < 0)
1304b88c807SRodney W. Grimes 		return(-1);
1314b88c807SRodney W. Grimes 	if (tflag && (atdir_start() < 0))
1324b88c807SRodney W. Grimes 		return(-1);
1334b88c807SRodney W. Grimes 	return(0);
1344b88c807SRodney W. Grimes }
1354b88c807SRodney W. Grimes 
1364b88c807SRodney W. Grimes /*
1374b88c807SRodney W. Grimes  * ftree_add()
1384b88c807SRodney W. Grimes  *	add the arg to the linked list of files to process. Each will be
1394b88c807SRodney W. Grimes  *	processed by fts one at a time
1404b88c807SRodney W. Grimes  * Return:
1414b88c807SRodney W. Grimes  *	0 if added to the linked list, -1 if failed
1424b88c807SRodney W. Grimes  */
1434b88c807SRodney W. Grimes 
1444b88c807SRodney W. Grimes int
145f789b261SWarner Losh ftree_add(char *str, int chflg)
1464b88c807SRodney W. Grimes {
147f789b261SWarner Losh 	FTREE *ft;
148f789b261SWarner Losh 	int len;
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes 	/*
1514b88c807SRodney W. Grimes 	 * simple check for bad args
1524b88c807SRodney W. Grimes 	 */
1534b88c807SRodney W. Grimes 	if ((str == NULL) || (*str == '\0')) {
154778766feSKris Kennaway 		paxwarn(0, "Invalid file name argument");
1554b88c807SRodney W. Grimes 		return(-1);
1564b88c807SRodney W. Grimes 	}
1574b88c807SRodney W. Grimes 
1584b88c807SRodney W. Grimes 	/*
1594b88c807SRodney W. Grimes 	 * allocate FTREE node and add to the end of the linked list (args are
1604b88c807SRodney W. Grimes 	 * processed in the same order they were passed to pax). Get rid of any
1614b88c807SRodney W. Grimes 	 * trailing / the user may pass us. (watch out for / by itself).
1624b88c807SRodney W. Grimes 	 */
1634b88c807SRodney W. Grimes 	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
164778766feSKris Kennaway 		paxwarn(0, "Unable to allocate memory for filename");
1654b88c807SRodney W. Grimes 		return(-1);
1664b88c807SRodney W. Grimes 	}
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes 	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
1694b88c807SRodney W. Grimes 		str[len] = '\0';
1704b88c807SRodney W. Grimes 	ft->fname = str;
1714b88c807SRodney W. Grimes 	ft->refcnt = 0;
172b1787decSKris Kennaway 	ft->chflg = chflg;
1734b88c807SRodney W. Grimes 	ft->fow = NULL;
1744b88c807SRodney W. Grimes 	if (fthead == NULL) {
1754b88c807SRodney W. Grimes 		fttail = fthead = ft;
1764b88c807SRodney W. Grimes 		return(0);
1774b88c807SRodney W. Grimes 	}
1784b88c807SRodney W. Grimes 	fttail->fow = ft;
1794b88c807SRodney W. Grimes 	fttail = ft;
1804b88c807SRodney W. Grimes 	return(0);
1814b88c807SRodney W. Grimes }
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes /*
1844b88c807SRodney W. Grimes  * ftree_sel()
1854b88c807SRodney W. Grimes  *	this entry has been selected by pax. bump up reference count and handle
1864b88c807SRodney W. Grimes  *	-n and -d processing.
1874b88c807SRodney W. Grimes  */
1884b88c807SRodney W. Grimes 
1894b88c807SRodney W. Grimes void
190f789b261SWarner Losh ftree_sel(ARCHD *arcn)
1914b88c807SRodney W. Grimes {
1924b88c807SRodney W. Grimes 	/*
1934b88c807SRodney W. Grimes 	 * set reference bit for this pattern. This linked list is only used
1944b88c807SRodney W. Grimes 	 * when file trees are supplied pax as args. The list is not used when
1954b88c807SRodney W. Grimes 	 * the trees are read from stdin.
1964b88c807SRodney W. Grimes 	 */
1974b88c807SRodney W. Grimes 	if (ftcur != NULL)
1984b88c807SRodney W. Grimes 		ftcur->refcnt = 1;
1994b88c807SRodney W. Grimes 
2004b88c807SRodney W. Grimes 	/*
2014b88c807SRodney W. Grimes 	 * if -n we are done with this arg, force a skip to the next arg when
2024b88c807SRodney W. Grimes 	 * pax asks for the next file in next_file().
2034b88c807SRodney W. Grimes 	 * if -d we tell fts only to match the directory (if the arg is a dir)
2044b88c807SRodney W. Grimes 	 * and not the entire file tree rooted at that point.
2054b88c807SRodney W. Grimes 	 */
2064b88c807SRodney W. Grimes 	if (nflag)
2074b88c807SRodney W. Grimes 		ftree_skip = 1;
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes 	if (!dflag || (arcn->type != PAX_DIR))
2104b88c807SRodney W. Grimes 		return;
2114b88c807SRodney W. Grimes 
2124b88c807SRodney W. Grimes 	if (ftent != NULL)
2134b88c807SRodney W. Grimes 		(void)fts_set(ftsp, ftent, FTS_SKIP);
2144b88c807SRodney W. Grimes }
2154b88c807SRodney W. Grimes 
2164b88c807SRodney W. Grimes /*
2170e32d64eSBrian Somers  * ftree_notsel()
2180e32d64eSBrian Somers  *	this entry has not been selected by pax.
2190e32d64eSBrian Somers  */
2200e32d64eSBrian Somers 
2210e32d64eSBrian Somers void
2223d65b2f5SEd Schouten ftree_notsel(void)
2230e32d64eSBrian Somers {
2240e32d64eSBrian Somers 	if (ftent != NULL)
2250e32d64eSBrian Somers 		(void)fts_set(ftsp, ftent, FTS_SKIP);
2260e32d64eSBrian Somers }
2270e32d64eSBrian Somers 
2280e32d64eSBrian Somers /*
2294b88c807SRodney W. Grimes  * ftree_chk()
2304b88c807SRodney W. Grimes  *	called at end on pax execution. Prints all those file args that did not
2314b88c807SRodney W. Grimes  *	have a selected member (reference count still 0)
2324b88c807SRodney W. Grimes  */
2334b88c807SRodney W. Grimes 
2344b88c807SRodney W. Grimes void
2354b88c807SRodney W. Grimes ftree_chk(void)
2364b88c807SRodney W. Grimes {
237f789b261SWarner Losh 	FTREE *ft;
238f789b261SWarner Losh 	int wban = 0;
2394b88c807SRodney W. Grimes 
2404b88c807SRodney W. Grimes 	/*
2414b88c807SRodney W. Grimes 	 * make sure all dir access times were reset.
2424b88c807SRodney W. Grimes 	 */
2434b88c807SRodney W. Grimes 	if (tflag)
2444b88c807SRodney W. Grimes 		atdir_end();
2454b88c807SRodney W. Grimes 
2464b88c807SRodney W. Grimes 	/*
2474b88c807SRodney W. Grimes 	 * walk down list and check reference count. Print out those members
2484b88c807SRodney W. Grimes 	 * that never had a match
2494b88c807SRodney W. Grimes 	 */
2504b88c807SRodney W. Grimes 	for (ft = fthead; ft != NULL; ft = ft->fow) {
251b1787decSKris Kennaway 		if ((ft->refcnt > 0) || ft->chflg)
2524b88c807SRodney W. Grimes 			continue;
2534b88c807SRodney W. Grimes 		if (wban == 0) {
254778766feSKris Kennaway 			paxwarn(1,"WARNING! These file names were not selected:");
2554b88c807SRodney W. Grimes 			++wban;
2564b88c807SRodney W. Grimes 		}
2574b88c807SRodney W. Grimes 		(void)fprintf(stderr, "%s\n", ft->fname);
2584b88c807SRodney W. Grimes 	}
2594b88c807SRodney W. Grimes }
2604b88c807SRodney W. Grimes 
2614b88c807SRodney W. Grimes /*
2624b88c807SRodney W. Grimes  * ftree_arg()
2634b88c807SRodney W. Grimes  *	Get the next file arg for fts to process. Can be from either the linked
2644b88c807SRodney W. Grimes  *	list or read from stdin when the user did not them as args to pax. Each
2654b88c807SRodney W. Grimes  *	arg is processed until the first successful fts_open().
2664b88c807SRodney W. Grimes  * Return:
2674b88c807SRodney W. Grimes  *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
2684b88c807SRodney W. Grimes  *	stdin).
2694b88c807SRodney W. Grimes  */
2704b88c807SRodney W. Grimes 
2714b88c807SRodney W. Grimes static int
2724b88c807SRodney W. Grimes ftree_arg(void)
2734b88c807SRodney W. Grimes {
274f789b261SWarner Losh 	char *pt;
2754b88c807SRodney W. Grimes 
2764b88c807SRodney W. Grimes 	/*
2774b88c807SRodney W. Grimes 	 * close off the current file tree
2784b88c807SRodney W. Grimes 	 */
2794b88c807SRodney W. Grimes 	if (ftsp != NULL) {
2804b88c807SRodney W. Grimes 		(void)fts_close(ftsp);
2814b88c807SRodney W. Grimes 		ftsp = NULL;
2824b88c807SRodney W. Grimes 	}
2834b88c807SRodney W. Grimes 
2844b88c807SRodney W. Grimes 	/*
2854b88c807SRodney W. Grimes 	 * keep looping until we get a valid file tree to process. Stop when we
2864b88c807SRodney W. Grimes 	 * reach the end of the list (or get an eof on stdin)
2874b88c807SRodney W. Grimes 	 */
2884b88c807SRodney W. Grimes 	for(;;) {
2894b88c807SRodney W. Grimes 		if (fthead == NULL) {
2904b88c807SRodney W. Grimes 			/*
2914b88c807SRodney W. Grimes 			 * the user didn't supply any args, get the file trees
2924b88c807SRodney W. Grimes 			 * to process from stdin;
2934b88c807SRodney W. Grimes 			 */
2944b88c807SRodney W. Grimes 			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
2954b88c807SRodney W. Grimes 				return(-1);
2964b88c807SRodney W. Grimes 			if ((pt = strchr(farray[0], '\n')) != NULL)
2974b88c807SRodney W. Grimes 				*pt = '\0';
2984b88c807SRodney W. Grimes 		} else {
2994b88c807SRodney W. Grimes 			/*
300778766feSKris Kennaway 			 * the user supplied the file args as arguments to pax
3014b88c807SRodney W. Grimes 			 */
3024b88c807SRodney W. Grimes 			if (ftcur == NULL)
3034b88c807SRodney W. Grimes 				ftcur = fthead;
3044b88c807SRodney W. Grimes 			else if ((ftcur = ftcur->fow) == NULL)
3054b88c807SRodney W. Grimes 				return(-1);
306b1787decSKris Kennaway 			if (ftcur->chflg) {
307b1787decSKris Kennaway 				/* First fchdir() back... */
308b1787decSKris Kennaway 				if (fchdir(cwdfd) < 0) {
309b1787decSKris Kennaway 					syswarn(1, errno,
310b1787decSKris Kennaway 					  "Can't fchdir to starting directory");
311b1787decSKris Kennaway 					return(-1);
312b1787decSKris Kennaway 				}
313b1787decSKris Kennaway 				if (chdir(ftcur->fname) < 0) {
314b1787decSKris Kennaway 					syswarn(1, errno, "Can't chdir to %s",
315b1787decSKris Kennaway 					    ftcur->fname);
316b1787decSKris Kennaway 					return(-1);
317b1787decSKris Kennaway 				}
318b1787decSKris Kennaway 				continue;
319b1787decSKris Kennaway 			} else
3204b88c807SRodney W. Grimes 				farray[0] = ftcur->fname;
3214b88c807SRodney W. Grimes 		}
3224b88c807SRodney W. Grimes 
3234b88c807SRodney W. Grimes 		/*
3249d5abbddSJens Schweikhardt 		 * Watch it, fts wants the file arg stored in an array of char
3259d5abbddSJens Schweikhardt 		 * ptrs, with the last one a null. We use a two element array
3264b88c807SRodney W. Grimes 		 * and set farray[0] to point at the buffer with the file name
32746be34b9SKris Kennaway 		 * in it. We cannot pass all the file args to fts at one shot
3284b88c807SRodney W. Grimes 		 * as we need to keep a handle on which file arg generates what
3294b88c807SRodney W. Grimes 		 * files (the -n and -d flags need this). If the open is
3304b88c807SRodney W. Grimes 		 * successful, return a 0.
3314b88c807SRodney W. Grimes 		 */
3324b88c807SRodney W. Grimes 		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
3334b88c807SRodney W. Grimes 			break;
3344b88c807SRodney W. Grimes 	}
3354b88c807SRodney W. Grimes 	return(0);
3364b88c807SRodney W. Grimes }
3374b88c807SRodney W. Grimes 
3384b88c807SRodney W. Grimes /*
3394b88c807SRodney W. Grimes  * next_file()
3404b88c807SRodney W. Grimes  *	supplies the next file to process in the supplied archd structure.
3414b88c807SRodney W. Grimes  * Return:
3424b88c807SRodney W. Grimes  *	0 when contents of arcn have been set with the next file, -1 when done.
3434b88c807SRodney W. Grimes  */
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes int
346f789b261SWarner Losh next_file(ARCHD *arcn)
3474b88c807SRodney W. Grimes {
348f789b261SWarner Losh 	int cnt;
3494b88c807SRodney W. Grimes 	time_t atime;
3504b88c807SRodney W. Grimes 	time_t mtime;
3514b88c807SRodney W. Grimes 
3524b88c807SRodney W. Grimes 	/*
3534b88c807SRodney W. Grimes 	 * ftree_sel() might have set the ftree_skip flag if the user has the
3544b88c807SRodney W. Grimes 	 * -n option and a file was selected from this file arg tree. (-n says
3554b88c807SRodney W. Grimes 	 * only one member is matched for each pattern) ftree_skip being 1
3564b88c807SRodney W. Grimes 	 * forces us to go to the next arg now.
3574b88c807SRodney W. Grimes 	 */
3584b88c807SRodney W. Grimes 	if (ftree_skip) {
3594b88c807SRodney W. Grimes 		/*
3604b88c807SRodney W. Grimes 		 * clear and go to next arg
3614b88c807SRodney W. Grimes 		 */
3624b88c807SRodney W. Grimes 		ftree_skip = 0;
3634b88c807SRodney W. Grimes 		if (ftree_arg() < 0)
3644b88c807SRodney W. Grimes 			return(-1);
3654b88c807SRodney W. Grimes 	}
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes 	/*
3684b88c807SRodney W. Grimes 	 * loop until we get a valid file to process
3694b88c807SRodney W. Grimes 	 */
3704b88c807SRodney W. Grimes 	for(;;) {
3714b88c807SRodney W. Grimes 		if ((ftent = fts_read(ftsp)) == NULL) {
3724b88c807SRodney W. Grimes 			/*
3734b88c807SRodney W. Grimes 			 * out of files in this tree, go to next arg, if none
3744b88c807SRodney W. Grimes 			 * we are done
3754b88c807SRodney W. Grimes 			 */
3764b88c807SRodney W. Grimes 			if (ftree_arg() < 0)
3774b88c807SRodney W. Grimes 				return(-1);
3784b88c807SRodney W. Grimes 			continue;
3794b88c807SRodney W. Grimes 		}
3804b88c807SRodney W. Grimes 
3814b88c807SRodney W. Grimes 		/*
3824b88c807SRodney W. Grimes 		 * handle each type of fts_read() flag
3834b88c807SRodney W. Grimes 		 */
3844b88c807SRodney W. Grimes 		switch(ftent->fts_info) {
3854b88c807SRodney W. Grimes 		case FTS_D:
3864b88c807SRodney W. Grimes 		case FTS_DEFAULT:
3874b88c807SRodney W. Grimes 		case FTS_F:
3884b88c807SRodney W. Grimes 		case FTS_SL:
3894b88c807SRodney W. Grimes 		case FTS_SLNONE:
3904b88c807SRodney W. Grimes 			/*
3914b88c807SRodney W. Grimes 			 * these are all ok
3924b88c807SRodney W. Grimes 			 */
3934b88c807SRodney W. Grimes 			break;
3944b88c807SRodney W. Grimes 		case FTS_DP:
3954b88c807SRodney W. Grimes 			/*
3964b88c807SRodney W. Grimes 			 * already saw this directory. If the user wants file
3974b88c807SRodney W. Grimes 			 * access times reset, we use this to restore the
3984b88c807SRodney W. Grimes 			 * access time for this directory since this is the
3994b88c807SRodney W. Grimes 			 * last time we will see it in this file subtree
4004b88c807SRodney W. Grimes 			 * remember to force the time (this is -t on a read
4014b88c807SRodney W. Grimes 			 * directory, not a created directory).
4024b88c807SRodney W. Grimes 			 */
4034b88c807SRodney W. Grimes #			ifdef NET2_FTS
4044b88c807SRodney W. Grimes 			if (!tflag || (get_atdir(ftent->fts_statb.st_dev,
4054b88c807SRodney W. Grimes 			    ftent->fts_statb.st_ino, &mtime, &atime) < 0))
4064b88c807SRodney W. Grimes #			else
4074b88c807SRodney W. Grimes 			if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
4084b88c807SRodney W. Grimes 			    ftent->fts_statp->st_ino, &mtime, &atime) < 0))
4094b88c807SRodney W. Grimes #			endif
4104b88c807SRodney W. Grimes 				continue;
4114b88c807SRodney W. Grimes 			set_ftime(ftent->fts_path, mtime, atime, 1);
4124b88c807SRodney W. Grimes 			continue;
4134b88c807SRodney W. Grimes 		case FTS_DC:
4144b88c807SRodney W. Grimes 			/*
4154b88c807SRodney W. Grimes 			 * fts claims a file system cycle
4164b88c807SRodney W. Grimes 			 */
417778766feSKris Kennaway 			paxwarn(1,"File system cycle found at %s",ftent->fts_path);
4184b88c807SRodney W. Grimes 			continue;
4194b88c807SRodney W. Grimes 		case FTS_DNR:
4204b88c807SRodney W. Grimes #			ifdef NET2_FTS
421778766feSKris Kennaway 			syswarn(1, errno,
4224b88c807SRodney W. Grimes #			else
423778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4244b88c807SRodney W. Grimes #			endif
4254b88c807SRodney W. Grimes 			    "Unable to read directory %s", ftent->fts_path);
4264b88c807SRodney W. Grimes 			continue;
4274b88c807SRodney W. Grimes 		case FTS_ERR:
4284b88c807SRodney W. Grimes #			ifdef NET2_FTS
429778766feSKris Kennaway 			syswarn(1, errno,
4304b88c807SRodney W. Grimes #			else
431778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4324b88c807SRodney W. Grimes #			endif
4334b88c807SRodney W. Grimes 			    "File system traversal error");
4344b88c807SRodney W. Grimes 			continue;
4354b88c807SRodney W. Grimes 		case FTS_NS:
4364b88c807SRodney W. Grimes 		case FTS_NSOK:
4374b88c807SRodney W. Grimes #			ifdef NET2_FTS
438778766feSKris Kennaway 			syswarn(1, errno,
4394b88c807SRodney W. Grimes #			else
440778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4414b88c807SRodney W. Grimes #			endif
4424b88c807SRodney W. Grimes 			    "Unable to access %s", ftent->fts_path);
4434b88c807SRodney W. Grimes 			continue;
4444b88c807SRodney W. Grimes 		}
4454b88c807SRodney W. Grimes 
4464b88c807SRodney W. Grimes 		/*
4474b88c807SRodney W. Grimes 		 * ok got a file tree node to process. copy info into arcn
4484b88c807SRodney W. Grimes 		 * structure (initialize as required)
4494b88c807SRodney W. Grimes 		 */
4504b88c807SRodney W. Grimes 		arcn->skip = 0;
4514b88c807SRodney W. Grimes 		arcn->pad = 0;
4524b88c807SRodney W. Grimes 		arcn->ln_nlen = 0;
4534b88c807SRodney W. Grimes 		arcn->ln_name[0] = '\0';
4544b88c807SRodney W. Grimes #		ifdef NET2_FTS
4554b88c807SRodney W. Grimes 		arcn->sb = ftent->fts_statb;
4564b88c807SRodney W. Grimes #		else
4574b88c807SRodney W. Grimes 		arcn->sb = *(ftent->fts_statp);
4584b88c807SRodney W. Grimes #		endif
4594b88c807SRodney W. Grimes 
4604b88c807SRodney W. Grimes 		/*
4614b88c807SRodney W. Grimes 		 * file type based set up and copy into the arcn struct
4624b88c807SRodney W. Grimes 		 * SIDE NOTE:
4634b88c807SRodney W. Grimes 		 * we try to reset the access time on all files and directories
4644b88c807SRodney W. Grimes 		 * we may read when the -t flag is specified. files are reset
4654b88c807SRodney W. Grimes 		 * when we close them after copying. we reset the directories
4664b88c807SRodney W. Grimes 		 * when we are done with their file tree (we also clean up at
4674b88c807SRodney W. Grimes 		 * end in case we cut short a file tree traversal). However
4684b88c807SRodney W. Grimes 		 * there is no way to reset access times on symlinks.
4694b88c807SRodney W. Grimes 		 */
4704b88c807SRodney W. Grimes 		switch(S_IFMT & arcn->sb.st_mode) {
4714b88c807SRodney W. Grimes 		case S_IFDIR:
4724b88c807SRodney W. Grimes 			arcn->type = PAX_DIR;
4734b88c807SRodney W. Grimes 			if (!tflag)
4744b88c807SRodney W. Grimes 				break;
4754b88c807SRodney W. Grimes 			add_atdir(ftent->fts_path, arcn->sb.st_dev,
4764b88c807SRodney W. Grimes 			    arcn->sb.st_ino, arcn->sb.st_mtime,
4774b88c807SRodney W. Grimes 			    arcn->sb.st_atime);
4784b88c807SRodney W. Grimes 			break;
4794b88c807SRodney W. Grimes 		case S_IFCHR:
4804b88c807SRodney W. Grimes 			arcn->type = PAX_CHR;
4814b88c807SRodney W. Grimes 			break;
4824b88c807SRodney W. Grimes 		case S_IFBLK:
4834b88c807SRodney W. Grimes 			arcn->type = PAX_BLK;
4844b88c807SRodney W. Grimes 			break;
4854b88c807SRodney W. Grimes 		case S_IFREG:
4864b88c807SRodney W. Grimes 			/*
4874b88c807SRodney W. Grimes 			 * only regular files with have data to store on the
4884b88c807SRodney W. Grimes 			 * archive. all others will store a zero length skip.
4894b88c807SRodney W. Grimes 			 * the skip field is used by pax for actual data it has
4904b88c807SRodney W. Grimes 			 * to read (or skip over).
4914b88c807SRodney W. Grimes 			 */
4924b88c807SRodney W. Grimes 			arcn->type = PAX_REG;
4934b88c807SRodney W. Grimes 			arcn->skip = arcn->sb.st_size;
4944b88c807SRodney W. Grimes 			break;
4954b88c807SRodney W. Grimes 		case S_IFLNK:
4964b88c807SRodney W. Grimes 			arcn->type = PAX_SLK;
4974b88c807SRodney W. Grimes 			/*
4984b88c807SRodney W. Grimes 			 * have to read the symlink path from the file
4994b88c807SRodney W. Grimes 			 */
5004b88c807SRodney W. Grimes 			if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
501e00e592aSWarner Losh 			    PAXPATHLEN - 1)) < 0) {
502778766feSKris Kennaway 				syswarn(1, errno, "Unable to read symlink %s",
5034b88c807SRodney W. Grimes 				    ftent->fts_path);
5044b88c807SRodney W. Grimes 				continue;
5054b88c807SRodney W. Grimes 			}
5064b88c807SRodney W. Grimes 			/*
5074b88c807SRodney W. Grimes 			 * set link name length, watch out readlink does not
50846be34b9SKris Kennaway 			 * always NUL terminate the link path
5094b88c807SRodney W. Grimes 			 */
5104b88c807SRodney W. Grimes 			arcn->ln_name[cnt] = '\0';
5114b88c807SRodney W. Grimes 			arcn->ln_nlen = cnt;
5124b88c807SRodney W. Grimes 			break;
5134b88c807SRodney W. Grimes 		case S_IFSOCK:
5144b88c807SRodney W. Grimes 			/*
5154b88c807SRodney W. Grimes 			 * under BSD storing a socket is senseless but we will
5164b88c807SRodney W. Grimes 			 * let the format specific write function make the
5174b88c807SRodney W. Grimes 			 * decision of what to do with it.
5184b88c807SRodney W. Grimes 			 */
5194b88c807SRodney W. Grimes 			arcn->type = PAX_SCK;
5204b88c807SRodney W. Grimes 			break;
5214b88c807SRodney W. Grimes 		case S_IFIFO:
5224b88c807SRodney W. Grimes 			arcn->type = PAX_FIF;
5234b88c807SRodney W. Grimes 			break;
5244b88c807SRodney W. Grimes 		}
5254b88c807SRodney W. Grimes 		break;
5264b88c807SRodney W. Grimes 	}
5274b88c807SRodney W. Grimes 
5284b88c807SRodney W. Grimes 	/*
5294b88c807SRodney W. Grimes 	 * copy file name, set file name length
5304b88c807SRodney W. Grimes 	 */
531b1787decSKris Kennaway 	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, sizeof(arcn->name) - 1);
5324b88c807SRodney W. Grimes 	arcn->name[arcn->nlen] = '\0';
5334b88c807SRodney W. Grimes 	arcn->org_name = ftent->fts_path;
5344b88c807SRodney W. Grimes 	return(0);
5354b88c807SRodney W. Grimes }
536