xref: /freebsd/bin/pax/ftree.c (revision 778766fe4d194c8ea0e580bc2906a93e7318faf8)
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  * 3. All advertising materials mentioning features or use of this software
184b88c807SRodney W. Grimes  *    must display the following acknowledgement:
194b88c807SRodney W. Grimes  *	This product includes software developed by the University of
204b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
214b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
224b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
234b88c807SRodney W. Grimes  *    without specific prior written permission.
244b88c807SRodney W. Grimes  *
254b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354b88c807SRodney W. Grimes  * SUCH DAMAGE.
364b88c807SRodney W. Grimes  */
374b88c807SRodney W. Grimes 
384b88c807SRodney W. Grimes #ifndef lint
39c9a8d1f4SPhilippe Charnier #if 0
40c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ftree.c	8.2 (Berkeley) 4/18/94";
41c9a8d1f4SPhilippe Charnier #endif
42c9a8d1f4SPhilippe Charnier static const char rcsid[] =
432a456239SPeter Wemm   "$FreeBSD$";
444b88c807SRodney W. Grimes #endif /* not lint */
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include <sys/types.h>
474b88c807SRodney W. Grimes #include <sys/time.h>
484b88c807SRodney W. Grimes #include <sys/stat.h>
494b88c807SRodney W. Grimes #include <unistd.h>
504b88c807SRodney W. Grimes #include <string.h>
514b88c807SRodney W. Grimes #include <stdio.h>
524b88c807SRodney W. Grimes #include <errno.h>
534b88c807SRodney W. Grimes #include <stdlib.h>
544b88c807SRodney W. Grimes #include <fts.h>
554b88c807SRodney W. Grimes #include "pax.h"
564b88c807SRodney W. Grimes #include "ftree.h"
574b88c807SRodney W. Grimes #include "extern.h"
584b88c807SRodney W. Grimes 
594b88c807SRodney W. Grimes /*
604b88c807SRodney W. Grimes  * routines to interface with the fts library function.
614b88c807SRodney W. Grimes  *
624b88c807SRodney W. Grimes  * file args supplied to pax are stored on a single linked list (of type FTREE)
634b88c807SRodney W. Grimes  * and given to fts to be processed one at a time. pax "selects" files from
644b88c807SRodney W. Grimes  * the expansion of each arg into the corresponding file tree (if the arg is a
654b88c807SRodney W. Grimes  * directory, otherwise the node itself is just passed to pax). The selection
664b88c807SRodney W. Grimes  * is modified by the -n and -u flags. The user is informed when a specific
674b88c807SRodney W. Grimes  * file arg does not generate any selected files. -n keeps expanding the file
684b88c807SRodney W. Grimes  * tree arg until one of its files is selected, then skips to the next file
694b88c807SRodney W. Grimes  * arg. when the user does not supply the file trees as command line args to
704b88c807SRodney W. Grimes  * pax, they are read from stdin
714b88c807SRodney W. Grimes  */
724b88c807SRodney W. Grimes 
7346be34b9SKris Kennaway static FTS *ftsp = NULL;		/* current FTS handle */
744b88c807SRodney W. Grimes static int ftsopts;			/* options to be used on fts_open */
754b88c807SRodney W. Grimes static char *farray[2];			/* array for passing each arg to fts */
764b88c807SRodney W. Grimes static FTREE *fthead = NULL;		/* head of linked list of file args */
774b88c807SRodney W. Grimes static FTREE *fttail = NULL;		/* tail of linked list of file args */
784b88c807SRodney W. Grimes static FTREE *ftcur = NULL;		/* current file arg being processed */
794b88c807SRodney W. Grimes static FTSENT *ftent = NULL;		/* current file tree entry */
804b88c807SRodney W. Grimes static int ftree_skip;			/* when set skip to next file arg */
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes static int ftree_arg __P((void));
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes /*
854b88c807SRodney W. Grimes  * ftree_start()
864b88c807SRodney W. Grimes  *	initialize the options passed to fts_open() during this run of pax
874b88c807SRodney W. Grimes  *	options are based on the selection of pax options by the user
884b88c807SRodney W. Grimes  *	fts_start() also calls fts_arg() to open the first valid file arg. We
894b88c807SRodney W. Grimes  *	also attempt to reset directory access times when -t (tflag) is set.
904b88c807SRodney W. Grimes  * Return:
914b88c807SRodney W. Grimes  *	0 if there is at least one valid file arg to process, -1 otherwise
924b88c807SRodney W. Grimes  */
934b88c807SRodney W. Grimes 
94778766feSKris Kennaway #ifdef __STDC__
954b88c807SRodney W. Grimes int
964b88c807SRodney W. Grimes ftree_start(void)
974b88c807SRodney W. Grimes #else
984b88c807SRodney W. Grimes int
994b88c807SRodney W. Grimes ftree_start()
1004b88c807SRodney W. Grimes #endif
1014b88c807SRodney W. Grimes {
1024b88c807SRodney W. Grimes 	/*
1034b88c807SRodney W. Grimes 	 * set up the operation mode of fts, open the first file arg. We must
1044b88c807SRodney W. Grimes 	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
1054b88c807SRodney W. Grimes 	 * if fts did a chdir off into the boondocks, we may create an archive
1064b88c807SRodney W. Grimes 	 * volume in an place where the user did not expect to.
1074b88c807SRodney W. Grimes 	 */
1084b88c807SRodney W. Grimes 	ftsopts = FTS_NOCHDIR;
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes 	/*
1114b88c807SRodney W. Grimes 	 * optional user flags that effect file traversal
1124b88c807SRodney W. Grimes 	 * -H command line symlink follow only (half follow)
1134b88c807SRodney W. Grimes 	 * -L follow sylinks (logical)
1144b88c807SRodney W. Grimes 	 * -P do not follow sylinks (physical). This is the default.
1154b88c807SRodney W. Grimes 	 * -X do not cross over mount points
1164b88c807SRodney W. Grimes 	 * -t preserve access times on files read.
1174b88c807SRodney W. Grimes 	 * -n select only the first member of a file tree when a match is found
1184b88c807SRodney W. Grimes 	 * -d do not extract subtrees rooted at a directory arg.
1194b88c807SRodney W. Grimes 	 */
1204b88c807SRodney W. Grimes 	if (Lflag)
1214b88c807SRodney W. Grimes 		ftsopts |= FTS_LOGICAL;
1224b88c807SRodney W. Grimes 	else
1234b88c807SRodney W. Grimes 		ftsopts |= FTS_PHYSICAL;
1244b88c807SRodney W. Grimes 	if (Hflag)
1254b88c807SRodney W. Grimes #	ifdef NET2_FTS
126778766feSKris Kennaway 		paxwarn(0, "The -H flag is not supported on this version");
1274b88c807SRodney W. Grimes #	else
1284b88c807SRodney W. Grimes 		ftsopts |= FTS_COMFOLLOW;
1294b88c807SRodney W. Grimes #	endif
1304b88c807SRodney W. Grimes 	if (Xflag)
1314b88c807SRodney W. Grimes 		ftsopts |= FTS_XDEV;
1324b88c807SRodney W. Grimes 
1334b88c807SRodney W. Grimes 	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
134778766feSKris Kennaway 		paxwarn(1, "Unable to allocate memory for file name buffer");
1354b88c807SRodney W. Grimes 		return(-1);
1364b88c807SRodney W. Grimes 	}
1374b88c807SRodney W. Grimes 
1384b88c807SRodney W. Grimes 	if (ftree_arg() < 0)
1394b88c807SRodney W. Grimes 		return(-1);
1404b88c807SRodney W. Grimes 	if (tflag && (atdir_start() < 0))
1414b88c807SRodney W. Grimes 		return(-1);
1424b88c807SRodney W. Grimes 	return(0);
1434b88c807SRodney W. Grimes }
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes /*
1464b88c807SRodney W. Grimes  * ftree_add()
1474b88c807SRodney W. Grimes  *	add the arg to the linked list of files to process. Each will be
1484b88c807SRodney W. Grimes  *	processed by fts one at a time
1494b88c807SRodney W. Grimes  * Return:
1504b88c807SRodney W. Grimes  *	0 if added to the linked list, -1 if failed
1514b88c807SRodney W. Grimes  */
1524b88c807SRodney W. Grimes 
153778766feSKris Kennaway #ifdef __STDC__
1544b88c807SRodney W. Grimes int
1554b88c807SRodney W. Grimes ftree_add(register char *str)
1564b88c807SRodney W. Grimes #else
1574b88c807SRodney W. Grimes int
1584b88c807SRodney W. Grimes ftree_add(str)
1594b88c807SRodney W. Grimes 	register char *str;
1604b88c807SRodney W. Grimes #endif
1614b88c807SRodney W. Grimes {
1624b88c807SRodney W. Grimes 	register FTREE *ft;
1634b88c807SRodney W. Grimes 	register int len;
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes 	/*
1664b88c807SRodney W. Grimes 	 * simple check for bad args
1674b88c807SRodney W. Grimes 	 */
1684b88c807SRodney W. Grimes 	if ((str == NULL) || (*str == '\0')) {
169778766feSKris Kennaway 		paxwarn(0, "Invalid file name argument");
1704b88c807SRodney W. Grimes 		return(-1);
1714b88c807SRodney W. Grimes 	}
1724b88c807SRodney W. Grimes 
1734b88c807SRodney W. Grimes 	/*
1744b88c807SRodney W. Grimes 	 * allocate FTREE node and add to the end of the linked list (args are
1754b88c807SRodney W. Grimes 	 * processed in the same order they were passed to pax). Get rid of any
1764b88c807SRodney W. Grimes 	 * trailing / the user may pass us. (watch out for / by itself).
1774b88c807SRodney W. Grimes 	 */
1784b88c807SRodney W. Grimes 	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
179778766feSKris Kennaway 		paxwarn(0, "Unable to allocate memory for filename");
1804b88c807SRodney W. Grimes 		return(-1);
1814b88c807SRodney W. Grimes 	}
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes 	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
1844b88c807SRodney W. Grimes 		str[len] = '\0';
1854b88c807SRodney W. Grimes 	ft->fname = str;
1864b88c807SRodney W. Grimes 	ft->refcnt = 0;
1874b88c807SRodney W. Grimes 	ft->fow = NULL;
1884b88c807SRodney W. Grimes 	if (fthead == NULL) {
1894b88c807SRodney W. Grimes 		fttail = fthead = ft;
1904b88c807SRodney W. Grimes 		return(0);
1914b88c807SRodney W. Grimes 	}
1924b88c807SRodney W. Grimes 	fttail->fow = ft;
1934b88c807SRodney W. Grimes 	fttail = ft;
1944b88c807SRodney W. Grimes 	return(0);
1954b88c807SRodney W. Grimes }
1964b88c807SRodney W. Grimes 
1974b88c807SRodney W. Grimes /*
1984b88c807SRodney W. Grimes  * ftree_sel()
1994b88c807SRodney W. Grimes  *	this entry has been selected by pax. bump up reference count and handle
2004b88c807SRodney W. Grimes  *	-n and -d processing.
2014b88c807SRodney W. Grimes  */
2024b88c807SRodney W. Grimes 
203778766feSKris Kennaway #ifdef __STDC__
2044b88c807SRodney W. Grimes void
2054b88c807SRodney W. Grimes ftree_sel(register ARCHD *arcn)
2064b88c807SRodney W. Grimes #else
2074b88c807SRodney W. Grimes void
2084b88c807SRodney W. Grimes ftree_sel(arcn)
2094b88c807SRodney W. Grimes 	register ARCHD *arcn;
2104b88c807SRodney W. Grimes #endif
2114b88c807SRodney W. Grimes {
2124b88c807SRodney W. Grimes 	/*
2134b88c807SRodney W. Grimes 	 * set reference bit for this pattern. This linked list is only used
2144b88c807SRodney W. Grimes 	 * when file trees are supplied pax as args. The list is not used when
2154b88c807SRodney W. Grimes 	 * the trees are read from stdin.
2164b88c807SRodney W. Grimes 	 */
2174b88c807SRodney W. Grimes 	if (ftcur != NULL)
2184b88c807SRodney W. Grimes 		ftcur->refcnt = 1;
2194b88c807SRodney W. Grimes 
2204b88c807SRodney W. Grimes 	/*
2214b88c807SRodney W. Grimes 	 * if -n we are done with this arg, force a skip to the next arg when
2224b88c807SRodney W. Grimes 	 * pax asks for the next file in next_file().
2234b88c807SRodney W. Grimes 	 * if -d we tell fts only to match the directory (if the arg is a dir)
2244b88c807SRodney W. Grimes 	 * and not the entire file tree rooted at that point.
2254b88c807SRodney W. Grimes 	 */
2264b88c807SRodney W. Grimes 	if (nflag)
2274b88c807SRodney W. Grimes 		ftree_skip = 1;
2284b88c807SRodney W. Grimes 
2294b88c807SRodney W. Grimes 	if (!dflag || (arcn->type != PAX_DIR))
2304b88c807SRodney W. Grimes 		return;
2314b88c807SRodney W. Grimes 
2324b88c807SRodney W. Grimes 	if (ftent != NULL)
2334b88c807SRodney W. Grimes 		(void)fts_set(ftsp, ftent, FTS_SKIP);
2344b88c807SRodney W. Grimes }
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes /*
2374b88c807SRodney W. Grimes  * ftree_chk()
2384b88c807SRodney W. Grimes  *	called at end on pax execution. Prints all those file args that did not
2394b88c807SRodney W. Grimes  *	have a selected member (reference count still 0)
2404b88c807SRodney W. Grimes  */
2414b88c807SRodney W. Grimes 
242778766feSKris Kennaway #ifdef __STDC__
2434b88c807SRodney W. Grimes void
2444b88c807SRodney W. Grimes ftree_chk(void)
2454b88c807SRodney W. Grimes #else
2464b88c807SRodney W. Grimes void
2474b88c807SRodney W. Grimes ftree_chk()
2484b88c807SRodney W. Grimes #endif
2494b88c807SRodney W. Grimes {
2504b88c807SRodney W. Grimes 	register FTREE *ft;
2514b88c807SRodney W. Grimes 	register int wban = 0;
2524b88c807SRodney W. Grimes 
2534b88c807SRodney W. Grimes 	/*
2544b88c807SRodney W. Grimes 	 * make sure all dir access times were reset.
2554b88c807SRodney W. Grimes 	 */
2564b88c807SRodney W. Grimes 	if (tflag)
2574b88c807SRodney W. Grimes 		atdir_end();
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes 	/*
2604b88c807SRodney W. Grimes 	 * walk down list and check reference count. Print out those members
2614b88c807SRodney W. Grimes 	 * that never had a match
2624b88c807SRodney W. Grimes 	 */
2634b88c807SRodney W. Grimes 	for (ft = fthead; ft != NULL; ft = ft->fow) {
2644b88c807SRodney W. Grimes 		if (ft->refcnt > 0)
2654b88c807SRodney W. Grimes 			continue;
2664b88c807SRodney W. Grimes 		if (wban == 0) {
267778766feSKris Kennaway 			paxwarn(1,"WARNING! These file names were not selected:");
2684b88c807SRodney W. Grimes 			++wban;
2694b88c807SRodney W. Grimes 		}
2704b88c807SRodney W. Grimes 		(void)fprintf(stderr, "%s\n", ft->fname);
2714b88c807SRodney W. Grimes 	}
2724b88c807SRodney W. Grimes }
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes /*
2754b88c807SRodney W. Grimes  * ftree_arg()
2764b88c807SRodney W. Grimes  *	Get the next file arg for fts to process. Can be from either the linked
2774b88c807SRodney W. Grimes  *	list or read from stdin when the user did not them as args to pax. Each
2784b88c807SRodney W. Grimes  *	arg is processed until the first successful fts_open().
2794b88c807SRodney W. Grimes  * Return:
2804b88c807SRodney W. Grimes  *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
2814b88c807SRodney W. Grimes  *	stdin).
2824b88c807SRodney W. Grimes  */
2834b88c807SRodney W. Grimes 
284778766feSKris Kennaway #ifdef __STDC__
2854b88c807SRodney W. Grimes static int
2864b88c807SRodney W. Grimes ftree_arg(void)
2874b88c807SRodney W. Grimes #else
2884b88c807SRodney W. Grimes static int
2894b88c807SRodney W. Grimes ftree_arg()
2904b88c807SRodney W. Grimes #endif
2914b88c807SRodney W. Grimes {
2924b88c807SRodney W. Grimes 	register char *pt;
2934b88c807SRodney W. Grimes 
2944b88c807SRodney W. Grimes 	/*
2954b88c807SRodney W. Grimes 	 * close off the current file tree
2964b88c807SRodney W. Grimes 	 */
2974b88c807SRodney W. Grimes 	if (ftsp != NULL) {
2984b88c807SRodney W. Grimes 		(void)fts_close(ftsp);
2994b88c807SRodney W. Grimes 		ftsp = NULL;
3004b88c807SRodney W. Grimes 	}
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes 	/*
3034b88c807SRodney W. Grimes 	 * keep looping until we get a valid file tree to process. Stop when we
3044b88c807SRodney W. Grimes 	 * reach the end of the list (or get an eof on stdin)
3054b88c807SRodney W. Grimes 	 */
3064b88c807SRodney W. Grimes 	for(;;) {
3074b88c807SRodney W. Grimes 		if (fthead == NULL) {
3084b88c807SRodney W. Grimes 			/*
3094b88c807SRodney W. Grimes 			 * the user didn't supply any args, get the file trees
3104b88c807SRodney W. Grimes 			 * to process from stdin;
3114b88c807SRodney W. Grimes 			 */
3124b88c807SRodney W. Grimes 			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
3134b88c807SRodney W. Grimes 				return(-1);
3144b88c807SRodney W. Grimes 			if ((pt = strchr(farray[0], '\n')) != NULL)
3154b88c807SRodney W. Grimes 				*pt = '\0';
3164b88c807SRodney W. Grimes 		} else {
3174b88c807SRodney W. Grimes 			/*
318778766feSKris Kennaway 			 * the user supplied the file args as arguments to pax
3194b88c807SRodney W. Grimes 			 */
3204b88c807SRodney W. Grimes 			if (ftcur == NULL)
3214b88c807SRodney W. Grimes 				ftcur = fthead;
3224b88c807SRodney W. Grimes 			else if ((ftcur = ftcur->fow) == NULL)
3234b88c807SRodney W. Grimes 				return(-1);
3244b88c807SRodney W. Grimes 			farray[0] = ftcur->fname;
3254b88c807SRodney W. Grimes 		}
3264b88c807SRodney W. Grimes 
3274b88c807SRodney W. Grimes 		/*
3284b88c807SRodney W. Grimes 		 * watch it, fts wants the file arg stored in a array of char
3294b88c807SRodney W. Grimes 		 * ptrs, with the last one a null. we use a two element array
3304b88c807SRodney W. Grimes 		 * and set farray[0] to point at the buffer with the file name
33146be34b9SKris Kennaway 		 * in it. We cannot pass all the file args to fts at one shot
3324b88c807SRodney W. Grimes 		 * as we need to keep a handle on which file arg generates what
3334b88c807SRodney W. Grimes 		 * files (the -n and -d flags need this). If the open is
3344b88c807SRodney W. Grimes 		 * successful, return a 0.
3354b88c807SRodney W. Grimes 		 */
3364b88c807SRodney W. Grimes 		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
3374b88c807SRodney W. Grimes 			break;
3384b88c807SRodney W. Grimes 	}
3394b88c807SRodney W. Grimes 	return(0);
3404b88c807SRodney W. Grimes }
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes /*
3434b88c807SRodney W. Grimes  * next_file()
3444b88c807SRodney W. Grimes  *	supplies the next file to process in the supplied archd structure.
3454b88c807SRodney W. Grimes  * Return:
3464b88c807SRodney W. Grimes  *	0 when contents of arcn have been set with the next file, -1 when done.
3474b88c807SRodney W. Grimes  */
3484b88c807SRodney W. Grimes 
349778766feSKris Kennaway #ifdef __STDC__
3504b88c807SRodney W. Grimes int
3514b88c807SRodney W. Grimes next_file(register ARCHD *arcn)
3524b88c807SRodney W. Grimes #else
3534b88c807SRodney W. Grimes int
3544b88c807SRodney W. Grimes next_file(arcn)
3554b88c807SRodney W. Grimes 	register ARCHD *arcn;
3564b88c807SRodney W. Grimes #endif
3574b88c807SRodney W. Grimes {
3584b88c807SRodney W. Grimes 	register int cnt;
3594b88c807SRodney W. Grimes 	time_t atime;
3604b88c807SRodney W. Grimes 	time_t mtime;
3614b88c807SRodney W. Grimes 
3624b88c807SRodney W. Grimes 	/*
3634b88c807SRodney W. Grimes 	 * ftree_sel() might have set the ftree_skip flag if the user has the
3644b88c807SRodney W. Grimes 	 * -n option and a file was selected from this file arg tree. (-n says
3654b88c807SRodney W. Grimes 	 * only one member is matched for each pattern) ftree_skip being 1
3664b88c807SRodney W. Grimes 	 * forces us to go to the next arg now.
3674b88c807SRodney W. Grimes 	 */
3684b88c807SRodney W. Grimes 	if (ftree_skip) {
3694b88c807SRodney W. Grimes 		/*
3704b88c807SRodney W. Grimes 		 * clear and go to next arg
3714b88c807SRodney W. Grimes 		 */
3724b88c807SRodney W. Grimes 		ftree_skip = 0;
3734b88c807SRodney W. Grimes 		if (ftree_arg() < 0)
3744b88c807SRodney W. Grimes 			return(-1);
3754b88c807SRodney W. Grimes 	}
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes 	/*
3784b88c807SRodney W. Grimes 	 * loop until we get a valid file to process
3794b88c807SRodney W. Grimes 	 */
3804b88c807SRodney W. Grimes 	for(;;) {
3814b88c807SRodney W. Grimes 		if ((ftent = fts_read(ftsp)) == NULL) {
3824b88c807SRodney W. Grimes 			/*
3834b88c807SRodney W. Grimes 			 * out of files in this tree, go to next arg, if none
3844b88c807SRodney W. Grimes 			 * we are done
3854b88c807SRodney W. Grimes 			 */
3864b88c807SRodney W. Grimes 			if (ftree_arg() < 0)
3874b88c807SRodney W. Grimes 				return(-1);
3884b88c807SRodney W. Grimes 			continue;
3894b88c807SRodney W. Grimes 		}
3904b88c807SRodney W. Grimes 
3914b88c807SRodney W. Grimes 		/*
3924b88c807SRodney W. Grimes 		 * handle each type of fts_read() flag
3934b88c807SRodney W. Grimes 		 */
3944b88c807SRodney W. Grimes 		switch(ftent->fts_info) {
3954b88c807SRodney W. Grimes 		case FTS_D:
3964b88c807SRodney W. Grimes 		case FTS_DEFAULT:
3974b88c807SRodney W. Grimes 		case FTS_F:
3984b88c807SRodney W. Grimes 		case FTS_SL:
3994b88c807SRodney W. Grimes 		case FTS_SLNONE:
4004b88c807SRodney W. Grimes 			/*
4014b88c807SRodney W. Grimes 			 * these are all ok
4024b88c807SRodney W. Grimes 			 */
4034b88c807SRodney W. Grimes 			break;
4044b88c807SRodney W. Grimes 		case FTS_DP:
4054b88c807SRodney W. Grimes 			/*
4064b88c807SRodney W. Grimes 			 * already saw this directory. If the user wants file
4074b88c807SRodney W. Grimes 			 * access times reset, we use this to restore the
4084b88c807SRodney W. Grimes 			 * access time for this directory since this is the
4094b88c807SRodney W. Grimes 			 * last time we will see it in this file subtree
4104b88c807SRodney W. Grimes 			 * remember to force the time (this is -t on a read
4114b88c807SRodney W. Grimes 			 * directory, not a created directory).
4124b88c807SRodney W. Grimes 			 */
4134b88c807SRodney W. Grimes #			ifdef NET2_FTS
4144b88c807SRodney W. Grimes 			if (!tflag || (get_atdir(ftent->fts_statb.st_dev,
4154b88c807SRodney W. Grimes 			    ftent->fts_statb.st_ino, &mtime, &atime) < 0))
4164b88c807SRodney W. Grimes #			else
4174b88c807SRodney W. Grimes 			if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
4184b88c807SRodney W. Grimes 			    ftent->fts_statp->st_ino, &mtime, &atime) < 0))
4194b88c807SRodney W. Grimes #			endif
4204b88c807SRodney W. Grimes 				continue;
4214b88c807SRodney W. Grimes 			set_ftime(ftent->fts_path, mtime, atime, 1);
4224b88c807SRodney W. Grimes 			continue;
4234b88c807SRodney W. Grimes 		case FTS_DC:
4244b88c807SRodney W. Grimes 			/*
4254b88c807SRodney W. Grimes 			 * fts claims a file system cycle
4264b88c807SRodney W. Grimes 			 */
427778766feSKris Kennaway 			paxwarn(1,"File system cycle found at %s",ftent->fts_path);
4284b88c807SRodney W. Grimes 			continue;
4294b88c807SRodney W. Grimes 		case FTS_DNR:
4304b88c807SRodney W. Grimes #			ifdef NET2_FTS
431778766feSKris Kennaway 			syswarn(1, errno,
4324b88c807SRodney W. Grimes #			else
433778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4344b88c807SRodney W. Grimes #			endif
4354b88c807SRodney W. Grimes 			    "Unable to read directory %s", ftent->fts_path);
4364b88c807SRodney W. Grimes 			continue;
4374b88c807SRodney W. Grimes 		case FTS_ERR:
4384b88c807SRodney W. Grimes #			ifdef NET2_FTS
439778766feSKris Kennaway 			syswarn(1, errno,
4404b88c807SRodney W. Grimes #			else
441778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4424b88c807SRodney W. Grimes #			endif
4434b88c807SRodney W. Grimes 			    "File system traversal error");
4444b88c807SRodney W. Grimes 			continue;
4454b88c807SRodney W. Grimes 		case FTS_NS:
4464b88c807SRodney W. Grimes 		case FTS_NSOK:
4474b88c807SRodney W. Grimes #			ifdef NET2_FTS
448778766feSKris Kennaway 			syswarn(1, errno,
4494b88c807SRodney W. Grimes #			else
450778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4514b88c807SRodney W. Grimes #			endif
4524b88c807SRodney W. Grimes 			    "Unable to access %s", ftent->fts_path);
4534b88c807SRodney W. Grimes 			continue;
4544b88c807SRodney W. Grimes 		}
4554b88c807SRodney W. Grimes 
4564b88c807SRodney W. Grimes 		/*
4574b88c807SRodney W. Grimes 		 * ok got a file tree node to process. copy info into arcn
4584b88c807SRodney W. Grimes 		 * structure (initialize as required)
4594b88c807SRodney W. Grimes 		 */
4604b88c807SRodney W. Grimes 		arcn->skip = 0;
4614b88c807SRodney W. Grimes 		arcn->pad = 0;
4624b88c807SRodney W. Grimes 		arcn->ln_nlen = 0;
4634b88c807SRodney W. Grimes 		arcn->ln_name[0] = '\0';
4644b88c807SRodney W. Grimes #		ifdef NET2_FTS
4654b88c807SRodney W. Grimes 		arcn->sb = ftent->fts_statb;
4664b88c807SRodney W. Grimes #		else
4674b88c807SRodney W. Grimes 		arcn->sb = *(ftent->fts_statp);
4684b88c807SRodney W. Grimes #		endif
4694b88c807SRodney W. Grimes 
4704b88c807SRodney W. Grimes 		/*
4714b88c807SRodney W. Grimes 		 * file type based set up and copy into the arcn struct
4724b88c807SRodney W. Grimes 		 * SIDE NOTE:
4734b88c807SRodney W. Grimes 		 * we try to reset the access time on all files and directories
4744b88c807SRodney W. Grimes 		 * we may read when the -t flag is specified. files are reset
4754b88c807SRodney W. Grimes 		 * when we close them after copying. we reset the directories
4764b88c807SRodney W. Grimes 		 * when we are done with their file tree (we also clean up at
4774b88c807SRodney W. Grimes 		 * end in case we cut short a file tree traversal). However
4784b88c807SRodney W. Grimes 		 * there is no way to reset access times on symlinks.
4794b88c807SRodney W. Grimes 		 */
4804b88c807SRodney W. Grimes 		switch(S_IFMT & arcn->sb.st_mode) {
4814b88c807SRodney W. Grimes 		case S_IFDIR:
4824b88c807SRodney W. Grimes 			arcn->type = PAX_DIR;
4834b88c807SRodney W. Grimes 			if (!tflag)
4844b88c807SRodney W. Grimes 				break;
4854b88c807SRodney W. Grimes 			add_atdir(ftent->fts_path, arcn->sb.st_dev,
4864b88c807SRodney W. Grimes 			    arcn->sb.st_ino, arcn->sb.st_mtime,
4874b88c807SRodney W. Grimes 			    arcn->sb.st_atime);
4884b88c807SRodney W. Grimes 			break;
4894b88c807SRodney W. Grimes 		case S_IFCHR:
4904b88c807SRodney W. Grimes 			arcn->type = PAX_CHR;
4914b88c807SRodney W. Grimes 			break;
4924b88c807SRodney W. Grimes 		case S_IFBLK:
4934b88c807SRodney W. Grimes 			arcn->type = PAX_BLK;
4944b88c807SRodney W. Grimes 			break;
4954b88c807SRodney W. Grimes 		case S_IFREG:
4964b88c807SRodney W. Grimes 			/*
4974b88c807SRodney W. Grimes 			 * only regular files with have data to store on the
4984b88c807SRodney W. Grimes 			 * archive. all others will store a zero length skip.
4994b88c807SRodney W. Grimes 			 * the skip field is used by pax for actual data it has
5004b88c807SRodney W. Grimes 			 * to read (or skip over).
5014b88c807SRodney W. Grimes 			 */
5024b88c807SRodney W. Grimes 			arcn->type = PAX_REG;
5034b88c807SRodney W. Grimes 			arcn->skip = arcn->sb.st_size;
5044b88c807SRodney W. Grimes 			break;
5054b88c807SRodney W. Grimes 		case S_IFLNK:
5064b88c807SRodney W. Grimes 			arcn->type = PAX_SLK;
5074b88c807SRodney W. Grimes 			/*
5084b88c807SRodney W. Grimes 			 * have to read the symlink path from the file
5094b88c807SRodney W. Grimes 			 */
5104b88c807SRodney W. Grimes 			if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
511e00e592aSWarner Losh 			    PAXPATHLEN - 1)) < 0) {
512778766feSKris Kennaway 				syswarn(1, errno, "Unable to read symlink %s",
5134b88c807SRodney W. Grimes 				    ftent->fts_path);
5144b88c807SRodney W. Grimes 				continue;
5154b88c807SRodney W. Grimes 			}
5164b88c807SRodney W. Grimes 			/*
5174b88c807SRodney W. Grimes 			 * set link name length, watch out readlink does not
51846be34b9SKris Kennaway 			 * always NUL terminate the link path
5194b88c807SRodney W. Grimes 			 */
5204b88c807SRodney W. Grimes 			arcn->ln_name[cnt] = '\0';
5214b88c807SRodney W. Grimes 			arcn->ln_nlen = cnt;
5224b88c807SRodney W. Grimes 			break;
5234b88c807SRodney W. Grimes 		case S_IFSOCK:
5244b88c807SRodney W. Grimes 			/*
5254b88c807SRodney W. Grimes 			 * under BSD storing a socket is senseless but we will
5264b88c807SRodney W. Grimes 			 * let the format specific write function make the
5274b88c807SRodney W. Grimes 			 * decision of what to do with it.
5284b88c807SRodney W. Grimes 			 */
5294b88c807SRodney W. Grimes 			arcn->type = PAX_SCK;
5304b88c807SRodney W. Grimes 			break;
5314b88c807SRodney W. Grimes 		case S_IFIFO:
5324b88c807SRodney W. Grimes 			arcn->type = PAX_FIF;
5334b88c807SRodney W. Grimes 			break;
5344b88c807SRodney W. Grimes 		}
5354b88c807SRodney W. Grimes 		break;
5364b88c807SRodney W. Grimes 	}
5374b88c807SRodney W. Grimes 
5384b88c807SRodney W. Grimes 	/*
5394b88c807SRodney W. Grimes 	 * copy file name, set file name length
5404b88c807SRodney W. Grimes 	 */
5414b88c807SRodney W. Grimes 	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, PAXPATHLEN+1);
5424b88c807SRodney W. Grimes 	arcn->name[arcn->nlen] = '\0';
5434b88c807SRodney W. Grimes 	arcn->org_name = ftent->fts_path;
5444b88c807SRodney W. Grimes 	return(0);
5454b88c807SRodney W. Grimes }
546