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