xref: /freebsd/bin/pax/ftree.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
54b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
64b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
94b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/time.h>
384b88c807SRodney W. Grimes #include <sys/stat.h>
394b88c807SRodney W. Grimes #include <unistd.h>
404b88c807SRodney W. Grimes #include <string.h>
414b88c807SRodney W. Grimes #include <stdio.h>
424b88c807SRodney W. Grimes #include <errno.h>
434b88c807SRodney W. Grimes #include <stdlib.h>
444b88c807SRodney W. Grimes #include <fts.h>
454b88c807SRodney W. Grimes #include "pax.h"
464b88c807SRodney W. Grimes #include "ftree.h"
474b88c807SRodney W. Grimes #include "extern.h"
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes /*
504b88c807SRodney W. Grimes  * routines to interface with the fts library function.
514b88c807SRodney W. Grimes  *
524b88c807SRodney W. Grimes  * file args supplied to pax are stored on a single linked list (of type FTREE)
534b88c807SRodney W. Grimes  * and given to fts to be processed one at a time. pax "selects" files from
544b88c807SRodney W. Grimes  * the expansion of each arg into the corresponding file tree (if the arg is a
554b88c807SRodney W. Grimes  * directory, otherwise the node itself is just passed to pax). The selection
564b88c807SRodney W. Grimes  * is modified by the -n and -u flags. The user is informed when a specific
574b88c807SRodney W. Grimes  * file arg does not generate any selected files. -n keeps expanding the file
584b88c807SRodney W. Grimes  * tree arg until one of its files is selected, then skips to the next file
594b88c807SRodney W. Grimes  * arg. when the user does not supply the file trees as command line args to
604b88c807SRodney W. Grimes  * pax, they are read from stdin
614b88c807SRodney W. Grimes  */
624b88c807SRodney W. Grimes 
6346be34b9SKris Kennaway static FTS *ftsp = NULL;		/* current FTS handle */
644b88c807SRodney W. Grimes static int ftsopts;			/* options to be used on fts_open */
654b88c807SRodney W. Grimes static char *farray[2];			/* array for passing each arg to fts */
664b88c807SRodney W. Grimes static FTREE *fthead = NULL;		/* head of linked list of file args */
674b88c807SRodney W. Grimes static FTREE *fttail = NULL;		/* tail of linked list of file args */
684b88c807SRodney W. Grimes static FTREE *ftcur = NULL;		/* current file arg being processed */
694b88c807SRodney W. Grimes static FTSENT *ftent = NULL;		/* current file tree entry */
704b88c807SRodney W. Grimes static int ftree_skip;			/* when set skip to next file arg */
714b88c807SRodney W. Grimes 
7246251ddeSWarner Losh static int ftree_arg(void);
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes /*
754b88c807SRodney W. Grimes  * ftree_start()
764b88c807SRodney W. Grimes  *	initialize the options passed to fts_open() during this run of pax
774b88c807SRodney W. Grimes  *	options are based on the selection of pax options by the user
784b88c807SRodney W. Grimes  *	fts_start() also calls fts_arg() to open the first valid file arg. We
794b88c807SRodney W. Grimes  *	also attempt to reset directory access times when -t (tflag) is set.
804b88c807SRodney W. Grimes  * Return:
814b88c807SRodney W. Grimes  *	0 if there is at least one valid file arg to process, -1 otherwise
824b88c807SRodney W. Grimes  */
834b88c807SRodney W. Grimes 
844b88c807SRodney W. Grimes int
ftree_start(void)854b88c807SRodney W. Grimes ftree_start(void)
864b88c807SRodney W. Grimes {
874b88c807SRodney W. Grimes 	/*
88cd8fb74dSGiorgos Keramidas 	 * Set up the operation mode of fts, open the first file arg. We must
894b88c807SRodney W. Grimes 	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
904b88c807SRodney W. Grimes 	 * if fts did a chdir off into the boondocks, we may create an archive
91cd8fb74dSGiorgos Keramidas 	 * volume in a place where the user did not expect to.
924b88c807SRodney W. Grimes 	 */
934b88c807SRodney W. Grimes 	ftsopts = FTS_NOCHDIR;
944b88c807SRodney W. Grimes 
954b88c807SRodney W. Grimes 	/*
964b88c807SRodney W. Grimes 	 * optional user flags that effect file traversal
974b88c807SRodney W. Grimes 	 * -H command line symlink follow only (half follow)
9801c99176SUlrich Spörlein 	 * -L follow symlinks (logical)
9901c99176SUlrich Spörlein 	 * -P do not follow symlinks (physical). This is the default.
1004b88c807SRodney W. Grimes 	 * -X do not cross over mount points
1014b88c807SRodney W. Grimes 	 * -t preserve access times on files read.
1024b88c807SRodney W. Grimes 	 * -n select only the first member of a file tree when a match is found
1034b88c807SRodney W. Grimes 	 * -d do not extract subtrees rooted at a directory arg.
1044b88c807SRodney W. Grimes 	 */
1054b88c807SRodney W. Grimes 	if (Lflag)
1064b88c807SRodney W. Grimes 		ftsopts |= FTS_LOGICAL;
1074b88c807SRodney W. Grimes 	else
1084b88c807SRodney W. Grimes 		ftsopts |= FTS_PHYSICAL;
1094b88c807SRodney W. Grimes 	if (Hflag)
1104b88c807SRodney W. Grimes 		ftsopts |= FTS_COMFOLLOW;
1114b88c807SRodney W. Grimes 	if (Xflag)
1124b88c807SRodney W. Grimes 		ftsopts |= FTS_XDEV;
1134b88c807SRodney W. Grimes 
1144b88c807SRodney W. Grimes 	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
115778766feSKris Kennaway 		paxwarn(1, "Unable to allocate memory for file name buffer");
1164b88c807SRodney W. Grimes 		return(-1);
1174b88c807SRodney W. Grimes 	}
1184b88c807SRodney W. Grimes 
1194b88c807SRodney W. Grimes 	if (ftree_arg() < 0)
1204b88c807SRodney W. Grimes 		return(-1);
1214b88c807SRodney W. Grimes 	if (tflag && (atdir_start() < 0))
1224b88c807SRodney W. Grimes 		return(-1);
1234b88c807SRodney W. Grimes 	return(0);
1244b88c807SRodney W. Grimes }
1254b88c807SRodney W. Grimes 
1264b88c807SRodney W. Grimes /*
1274b88c807SRodney W. Grimes  * ftree_add()
1284b88c807SRodney W. Grimes  *	add the arg to the linked list of files to process. Each will be
1294b88c807SRodney W. Grimes  *	processed by fts one at a time
1304b88c807SRodney W. Grimes  * Return:
1314b88c807SRodney W. Grimes  *	0 if added to the linked list, -1 if failed
1324b88c807SRodney W. Grimes  */
1334b88c807SRodney W. Grimes 
1344b88c807SRodney W. Grimes int
ftree_add(char * str,int chflg)135f789b261SWarner Losh ftree_add(char *str, int chflg)
1364b88c807SRodney W. Grimes {
137f789b261SWarner Losh 	FTREE *ft;
138f789b261SWarner Losh 	int len;
1394b88c807SRodney W. Grimes 
1404b88c807SRodney W. Grimes 	/*
1414b88c807SRodney W. Grimes 	 * simple check for bad args
1424b88c807SRodney W. Grimes 	 */
1434b88c807SRodney W. Grimes 	if ((str == NULL) || (*str == '\0')) {
144778766feSKris Kennaway 		paxwarn(0, "Invalid file name argument");
1454b88c807SRodney W. Grimes 		return(-1);
1464b88c807SRodney W. Grimes 	}
1474b88c807SRodney W. Grimes 
1484b88c807SRodney W. Grimes 	/*
1494b88c807SRodney W. Grimes 	 * allocate FTREE node and add to the end of the linked list (args are
1504b88c807SRodney W. Grimes 	 * processed in the same order they were passed to pax). Get rid of any
1514b88c807SRodney W. Grimes 	 * trailing / the user may pass us. (watch out for / by itself).
1524b88c807SRodney W. Grimes 	 */
1534b88c807SRodney W. Grimes 	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
154778766feSKris Kennaway 		paxwarn(0, "Unable to allocate memory for filename");
1554b88c807SRodney W. Grimes 		return(-1);
1564b88c807SRodney W. Grimes 	}
1574b88c807SRodney W. Grimes 
1584b88c807SRodney W. Grimes 	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
1594b88c807SRodney W. Grimes 		str[len] = '\0';
1604b88c807SRodney W. Grimes 	ft->fname = str;
1614b88c807SRodney W. Grimes 	ft->refcnt = 0;
162b1787decSKris Kennaway 	ft->chflg = chflg;
1634b88c807SRodney W. Grimes 	ft->fow = NULL;
1644b88c807SRodney W. Grimes 	if (fthead == NULL) {
1654b88c807SRodney W. Grimes 		fttail = fthead = ft;
1664b88c807SRodney W. Grimes 		return(0);
1674b88c807SRodney W. Grimes 	}
1684b88c807SRodney W. Grimes 	fttail->fow = ft;
1694b88c807SRodney W. Grimes 	fttail = ft;
1704b88c807SRodney W. Grimes 	return(0);
1714b88c807SRodney W. Grimes }
1724b88c807SRodney W. Grimes 
1734b88c807SRodney W. Grimes /*
1744b88c807SRodney W. Grimes  * ftree_sel()
1754b88c807SRodney W. Grimes  *	this entry has been selected by pax. bump up reference count and handle
1764b88c807SRodney W. Grimes  *	-n and -d processing.
1774b88c807SRodney W. Grimes  */
1784b88c807SRodney W. Grimes 
1794b88c807SRodney W. Grimes void
ftree_sel(ARCHD * arcn)180f789b261SWarner Losh ftree_sel(ARCHD *arcn)
1814b88c807SRodney W. Grimes {
1824b88c807SRodney W. Grimes 	/*
1834b88c807SRodney W. Grimes 	 * set reference bit for this pattern. This linked list is only used
1844b88c807SRodney W. Grimes 	 * when file trees are supplied pax as args. The list is not used when
1854b88c807SRodney W. Grimes 	 * the trees are read from stdin.
1864b88c807SRodney W. Grimes 	 */
1874b88c807SRodney W. Grimes 	if (ftcur != NULL)
1884b88c807SRodney W. Grimes 		ftcur->refcnt = 1;
1894b88c807SRodney W. Grimes 
1904b88c807SRodney W. Grimes 	/*
1914b88c807SRodney W. Grimes 	 * if -n we are done with this arg, force a skip to the next arg when
1924b88c807SRodney W. Grimes 	 * pax asks for the next file in next_file().
1934b88c807SRodney W. Grimes 	 * if -d we tell fts only to match the directory (if the arg is a dir)
1944b88c807SRodney W. Grimes 	 * and not the entire file tree rooted at that point.
1954b88c807SRodney W. Grimes 	 */
1964b88c807SRodney W. Grimes 	if (nflag)
1974b88c807SRodney W. Grimes 		ftree_skip = 1;
1984b88c807SRodney W. Grimes 
1994b88c807SRodney W. Grimes 	if (!dflag || (arcn->type != PAX_DIR))
2004b88c807SRodney W. Grimes 		return;
2014b88c807SRodney W. Grimes 
2024b88c807SRodney W. Grimes 	if (ftent != NULL)
2034b88c807SRodney W. Grimes 		(void)fts_set(ftsp, ftent, FTS_SKIP);
2044b88c807SRodney W. Grimes }
2054b88c807SRodney W. Grimes 
2064b88c807SRodney W. Grimes /*
2070e32d64eSBrian Somers  * ftree_notsel()
2080e32d64eSBrian Somers  *	this entry has not been selected by pax.
2090e32d64eSBrian Somers  */
2100e32d64eSBrian Somers 
2110e32d64eSBrian Somers void
ftree_notsel(void)2123d65b2f5SEd Schouten ftree_notsel(void)
2130e32d64eSBrian Somers {
2140e32d64eSBrian Somers 	if (ftent != NULL)
2150e32d64eSBrian Somers 		(void)fts_set(ftsp, ftent, FTS_SKIP);
2160e32d64eSBrian Somers }
2170e32d64eSBrian Somers 
2180e32d64eSBrian Somers /*
2194b88c807SRodney W. Grimes  * ftree_chk()
2204b88c807SRodney W. Grimes  *	called at end on pax execution. Prints all those file args that did not
2214b88c807SRodney W. Grimes  *	have a selected member (reference count still 0)
2224b88c807SRodney W. Grimes  */
2234b88c807SRodney W. Grimes 
2244b88c807SRodney W. Grimes void
ftree_chk(void)2254b88c807SRodney W. Grimes ftree_chk(void)
2264b88c807SRodney W. Grimes {
227f789b261SWarner Losh 	FTREE *ft;
228f789b261SWarner Losh 	int wban = 0;
2294b88c807SRodney W. Grimes 
2304b88c807SRodney W. Grimes 	/*
2314b88c807SRodney W. Grimes 	 * make sure all dir access times were reset.
2324b88c807SRodney W. Grimes 	 */
2334b88c807SRodney W. Grimes 	if (tflag)
2344b88c807SRodney W. Grimes 		atdir_end();
2354b88c807SRodney W. Grimes 
2364b88c807SRodney W. Grimes 	/*
2374b88c807SRodney W. Grimes 	 * walk down list and check reference count. Print out those members
2384b88c807SRodney W. Grimes 	 * that never had a match
2394b88c807SRodney W. Grimes 	 */
2404b88c807SRodney W. Grimes 	for (ft = fthead; ft != NULL; ft = ft->fow) {
241b1787decSKris Kennaway 		if ((ft->refcnt > 0) || ft->chflg)
2424b88c807SRodney W. Grimes 			continue;
2434b88c807SRodney W. Grimes 		if (wban == 0) {
244778766feSKris Kennaway 			paxwarn(1,"WARNING! These file names were not selected:");
2454b88c807SRodney W. Grimes 			++wban;
2464b88c807SRodney W. Grimes 		}
2474b88c807SRodney W. Grimes 		(void)fprintf(stderr, "%s\n", ft->fname);
2484b88c807SRodney W. Grimes 	}
2494b88c807SRodney W. Grimes }
2504b88c807SRodney W. Grimes 
2514b88c807SRodney W. Grimes /*
2524b88c807SRodney W. Grimes  * ftree_arg()
2534b88c807SRodney W. Grimes  *	Get the next file arg for fts to process. Can be from either the linked
2544b88c807SRodney W. Grimes  *	list or read from stdin when the user did not them as args to pax. Each
2554b88c807SRodney W. Grimes  *	arg is processed until the first successful fts_open().
2564b88c807SRodney W. Grimes  * Return:
2574b88c807SRodney W. Grimes  *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
2584b88c807SRodney W. Grimes  *	stdin).
2594b88c807SRodney W. Grimes  */
2604b88c807SRodney W. Grimes 
2614b88c807SRodney W. Grimes static int
ftree_arg(void)2624b88c807SRodney W. Grimes ftree_arg(void)
2634b88c807SRodney W. Grimes {
264f789b261SWarner Losh 	char *pt;
2654b88c807SRodney W. Grimes 
2664b88c807SRodney W. Grimes 	/*
2674b88c807SRodney W. Grimes 	 * close off the current file tree
2684b88c807SRodney W. Grimes 	 */
2694b88c807SRodney W. Grimes 	if (ftsp != NULL) {
2704b88c807SRodney W. Grimes 		(void)fts_close(ftsp);
2714b88c807SRodney W. Grimes 		ftsp = NULL;
2724b88c807SRodney W. Grimes 	}
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes 	/*
2754b88c807SRodney W. Grimes 	 * keep looping until we get a valid file tree to process. Stop when we
2764b88c807SRodney W. Grimes 	 * reach the end of the list (or get an eof on stdin)
2774b88c807SRodney W. Grimes 	 */
2784b88c807SRodney W. Grimes 	for(;;) {
2794b88c807SRodney W. Grimes 		if (fthead == NULL) {
2804b88c807SRodney W. Grimes 			/*
2814b88c807SRodney W. Grimes 			 * the user didn't supply any args, get the file trees
2824b88c807SRodney W. Grimes 			 * to process from stdin;
2834b88c807SRodney W. Grimes 			 */
2844b88c807SRodney W. Grimes 			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
2854b88c807SRodney W. Grimes 				return(-1);
2864b88c807SRodney W. Grimes 			if ((pt = strchr(farray[0], '\n')) != NULL)
2874b88c807SRodney W. Grimes 				*pt = '\0';
2884b88c807SRodney W. Grimes 		} else {
2894b88c807SRodney W. Grimes 			/*
290778766feSKris Kennaway 			 * the user supplied the file args as arguments to pax
2914b88c807SRodney W. Grimes 			 */
2924b88c807SRodney W. Grimes 			if (ftcur == NULL)
2934b88c807SRodney W. Grimes 				ftcur = fthead;
2944b88c807SRodney W. Grimes 			else if ((ftcur = ftcur->fow) == NULL)
2954b88c807SRodney W. Grimes 				return(-1);
296b1787decSKris Kennaway 			if (ftcur->chflg) {
297b1787decSKris Kennaway 				/* First fchdir() back... */
298b1787decSKris Kennaway 				if (fchdir(cwdfd) < 0) {
299b1787decSKris Kennaway 					syswarn(1, errno,
300b1787decSKris Kennaway 					  "Can't fchdir to starting directory");
301b1787decSKris Kennaway 					return(-1);
302b1787decSKris Kennaway 				}
303b1787decSKris Kennaway 				if (chdir(ftcur->fname) < 0) {
304b1787decSKris Kennaway 					syswarn(1, errno, "Can't chdir to %s",
305b1787decSKris Kennaway 					    ftcur->fname);
306b1787decSKris Kennaway 					return(-1);
307b1787decSKris Kennaway 				}
308b1787decSKris Kennaway 				continue;
309b1787decSKris Kennaway 			} else
3104b88c807SRodney W. Grimes 				farray[0] = ftcur->fname;
3114b88c807SRodney W. Grimes 		}
3124b88c807SRodney W. Grimes 
3134b88c807SRodney W. Grimes 		/*
3149d5abbddSJens Schweikhardt 		 * Watch it, fts wants the file arg stored in an array of char
3159d5abbddSJens Schweikhardt 		 * ptrs, with the last one a null. We use a two element array
3164b88c807SRodney W. Grimes 		 * and set farray[0] to point at the buffer with the file name
31746be34b9SKris Kennaway 		 * in it. We cannot pass all the file args to fts at one shot
3184b88c807SRodney W. Grimes 		 * as we need to keep a handle on which file arg generates what
3194b88c807SRodney W. Grimes 		 * files (the -n and -d flags need this). If the open is
3204b88c807SRodney W. Grimes 		 * successful, return a 0.
3214b88c807SRodney W. Grimes 		 */
3224b88c807SRodney W. Grimes 		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
3234b88c807SRodney W. Grimes 			break;
3244b88c807SRodney W. Grimes 	}
3254b88c807SRodney W. Grimes 	return(0);
3264b88c807SRodney W. Grimes }
3274b88c807SRodney W. Grimes 
3284b88c807SRodney W. Grimes /*
3294b88c807SRodney W. Grimes  * next_file()
3304b88c807SRodney W. Grimes  *	supplies the next file to process in the supplied archd structure.
3314b88c807SRodney W. Grimes  * Return:
3324b88c807SRodney W. Grimes  *	0 when contents of arcn have been set with the next file, -1 when done.
3334b88c807SRodney W. Grimes  */
3344b88c807SRodney W. Grimes 
3354b88c807SRodney W. Grimes int
next_file(ARCHD * arcn)336f789b261SWarner Losh next_file(ARCHD *arcn)
3374b88c807SRodney W. Grimes {
338f789b261SWarner Losh 	int cnt;
3394b88c807SRodney W. Grimes 	time_t atime;
3404b88c807SRodney W. Grimes 	time_t mtime;
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes 	/*
3434b88c807SRodney W. Grimes 	 * ftree_sel() might have set the ftree_skip flag if the user has the
3444b88c807SRodney W. Grimes 	 * -n option and a file was selected from this file arg tree. (-n says
3454b88c807SRodney W. Grimes 	 * only one member is matched for each pattern) ftree_skip being 1
3464b88c807SRodney W. Grimes 	 * forces us to go to the next arg now.
3474b88c807SRodney W. Grimes 	 */
3484b88c807SRodney W. Grimes 	if (ftree_skip) {
3494b88c807SRodney W. Grimes 		/*
3504b88c807SRodney W. Grimes 		 * clear and go to next arg
3514b88c807SRodney W. Grimes 		 */
3524b88c807SRodney W. Grimes 		ftree_skip = 0;
3534b88c807SRodney W. Grimes 		if (ftree_arg() < 0)
3544b88c807SRodney W. Grimes 			return(-1);
3554b88c807SRodney W. Grimes 	}
3564b88c807SRodney W. Grimes 
3574b88c807SRodney W. Grimes 	/*
3584b88c807SRodney W. Grimes 	 * loop until we get a valid file to process
3594b88c807SRodney W. Grimes 	 */
3604b88c807SRodney W. Grimes 	for(;;) {
3614b88c807SRodney W. Grimes 		if ((ftent = fts_read(ftsp)) == NULL) {
3624b88c807SRodney W. Grimes 			/*
3634b88c807SRodney W. Grimes 			 * out of files in this tree, go to next arg, if none
3644b88c807SRodney W. Grimes 			 * we are done
3654b88c807SRodney W. Grimes 			 */
3664b88c807SRodney W. Grimes 			if (ftree_arg() < 0)
3674b88c807SRodney W. Grimes 				return(-1);
3684b88c807SRodney W. Grimes 			continue;
3694b88c807SRodney W. Grimes 		}
3704b88c807SRodney W. Grimes 
3714b88c807SRodney W. Grimes 		/*
3724b88c807SRodney W. Grimes 		 * handle each type of fts_read() flag
3734b88c807SRodney W. Grimes 		 */
3744b88c807SRodney W. Grimes 		switch(ftent->fts_info) {
3754b88c807SRodney W. Grimes 		case FTS_D:
3764b88c807SRodney W. Grimes 		case FTS_DEFAULT:
3774b88c807SRodney W. Grimes 		case FTS_F:
3784b88c807SRodney W. Grimes 		case FTS_SL:
3794b88c807SRodney W. Grimes 		case FTS_SLNONE:
3804b88c807SRodney W. Grimes 			/*
3814b88c807SRodney W. Grimes 			 * these are all ok
3824b88c807SRodney W. Grimes 			 */
3834b88c807SRodney W. Grimes 			break;
3844b88c807SRodney W. Grimes 		case FTS_DP:
3854b88c807SRodney W. Grimes 			/*
3864b88c807SRodney W. Grimes 			 * already saw this directory. If the user wants file
3874b88c807SRodney W. Grimes 			 * access times reset, we use this to restore the
3884b88c807SRodney W. Grimes 			 * access time for this directory since this is the
3894b88c807SRodney W. Grimes 			 * last time we will see it in this file subtree
3904b88c807SRodney W. Grimes 			 * remember to force the time (this is -t on a read
3914b88c807SRodney W. Grimes 			 * directory, not a created directory).
3924b88c807SRodney W. Grimes 			 */
3934b88c807SRodney W. Grimes 			if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
3944b88c807SRodney W. Grimes 			    ftent->fts_statp->st_ino, &mtime, &atime) < 0))
3954b88c807SRodney W. Grimes 				continue;
3964b88c807SRodney W. Grimes 			set_ftime(ftent->fts_path, mtime, atime, 1);
3974b88c807SRodney W. Grimes 			continue;
3984b88c807SRodney W. Grimes 		case FTS_DC:
3994b88c807SRodney W. Grimes 			/*
4004b88c807SRodney W. Grimes 			 * fts claims a file system cycle
4014b88c807SRodney W. Grimes 			 */
402778766feSKris Kennaway 			paxwarn(1,"File system cycle found at %s",ftent->fts_path);
4034b88c807SRodney W. Grimes 			continue;
4044b88c807SRodney W. Grimes 		case FTS_DNR:
405778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4064b88c807SRodney W. Grimes 			    "Unable to read directory %s", ftent->fts_path);
4074b88c807SRodney W. Grimes 			continue;
4084b88c807SRodney W. Grimes 		case FTS_ERR:
409778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4104b88c807SRodney W. Grimes 			    "File system traversal error");
4114b88c807SRodney W. Grimes 			continue;
4124b88c807SRodney W. Grimes 		case FTS_NS:
4134b88c807SRodney W. Grimes 		case FTS_NSOK:
414778766feSKris Kennaway 			syswarn(1, ftent->fts_errno,
4154b88c807SRodney W. Grimes 			    "Unable to access %s", ftent->fts_path);
4164b88c807SRodney W. Grimes 			continue;
4174b88c807SRodney W. Grimes 		}
4184b88c807SRodney W. Grimes 
4194b88c807SRodney W. Grimes 		/*
4204b88c807SRodney W. Grimes 		 * ok got a file tree node to process. copy info into arcn
4214b88c807SRodney W. Grimes 		 * structure (initialize as required)
4224b88c807SRodney W. Grimes 		 */
4234b88c807SRodney W. Grimes 		arcn->skip = 0;
4244b88c807SRodney W. Grimes 		arcn->pad = 0;
4254b88c807SRodney W. Grimes 		arcn->ln_nlen = 0;
4264b88c807SRodney W. Grimes 		arcn->ln_name[0] = '\0';
4274b88c807SRodney W. Grimes 		arcn->sb = *(ftent->fts_statp);
4284b88c807SRodney W. Grimes 
4294b88c807SRodney W. Grimes 		/*
4304b88c807SRodney W. Grimes 		 * file type based set up and copy into the arcn struct
4314b88c807SRodney W. Grimes 		 * SIDE NOTE:
4324b88c807SRodney W. Grimes 		 * we try to reset the access time on all files and directories
4334b88c807SRodney W. Grimes 		 * we may read when the -t flag is specified. files are reset
4344b88c807SRodney W. Grimes 		 * when we close them after copying. we reset the directories
4354b88c807SRodney W. Grimes 		 * when we are done with their file tree (we also clean up at
4364b88c807SRodney W. Grimes 		 * end in case we cut short a file tree traversal). However
4374b88c807SRodney W. Grimes 		 * there is no way to reset access times on symlinks.
4384b88c807SRodney W. Grimes 		 */
4394b88c807SRodney W. Grimes 		switch(S_IFMT & arcn->sb.st_mode) {
4404b88c807SRodney W. Grimes 		case S_IFDIR:
4414b88c807SRodney W. Grimes 			arcn->type = PAX_DIR;
4424b88c807SRodney W. Grimes 			if (!tflag)
4434b88c807SRodney W. Grimes 				break;
4444b88c807SRodney W. Grimes 			add_atdir(ftent->fts_path, arcn->sb.st_dev,
4454b88c807SRodney W. Grimes 			    arcn->sb.st_ino, arcn->sb.st_mtime,
4464b88c807SRodney W. Grimes 			    arcn->sb.st_atime);
4474b88c807SRodney W. Grimes 			break;
4484b88c807SRodney W. Grimes 		case S_IFCHR:
4494b88c807SRodney W. Grimes 			arcn->type = PAX_CHR;
4504b88c807SRodney W. Grimes 			break;
4514b88c807SRodney W. Grimes 		case S_IFBLK:
4524b88c807SRodney W. Grimes 			arcn->type = PAX_BLK;
4534b88c807SRodney W. Grimes 			break;
4544b88c807SRodney W. Grimes 		case S_IFREG:
4554b88c807SRodney W. Grimes 			/*
4564b88c807SRodney W. Grimes 			 * only regular files with have data to store on the
4574b88c807SRodney W. Grimes 			 * archive. all others will store a zero length skip.
4584b88c807SRodney W. Grimes 			 * the skip field is used by pax for actual data it has
4594b88c807SRodney W. Grimes 			 * to read (or skip over).
4604b88c807SRodney W. Grimes 			 */
4614b88c807SRodney W. Grimes 			arcn->type = PAX_REG;
4624b88c807SRodney W. Grimes 			arcn->skip = arcn->sb.st_size;
4634b88c807SRodney W. Grimes 			break;
4644b88c807SRodney W. Grimes 		case S_IFLNK:
4654b88c807SRodney W. Grimes 			arcn->type = PAX_SLK;
4664b88c807SRodney W. Grimes 			/*
4674b88c807SRodney W. Grimes 			 * have to read the symlink path from the file
4684b88c807SRodney W. Grimes 			 */
4694b88c807SRodney W. Grimes 			if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
470e00e592aSWarner Losh 			    PAXPATHLEN - 1)) < 0) {
471778766feSKris Kennaway 				syswarn(1, errno, "Unable to read symlink %s",
4724b88c807SRodney W. Grimes 				    ftent->fts_path);
4734b88c807SRodney W. Grimes 				continue;
4744b88c807SRodney W. Grimes 			}
4754b88c807SRodney W. Grimes 			/*
4764b88c807SRodney W. Grimes 			 * set link name length, watch out readlink does not
47746be34b9SKris Kennaway 			 * always NUL terminate the link path
4784b88c807SRodney W. Grimes 			 */
4794b88c807SRodney W. Grimes 			arcn->ln_name[cnt] = '\0';
4804b88c807SRodney W. Grimes 			arcn->ln_nlen = cnt;
4814b88c807SRodney W. Grimes 			break;
4824b88c807SRodney W. Grimes 		case S_IFSOCK:
4834b88c807SRodney W. Grimes 			/*
4844b88c807SRodney W. Grimes 			 * under BSD storing a socket is senseless but we will
4854b88c807SRodney W. Grimes 			 * let the format specific write function make the
4864b88c807SRodney W. Grimes 			 * decision of what to do with it.
4874b88c807SRodney W. Grimes 			 */
4884b88c807SRodney W. Grimes 			arcn->type = PAX_SCK;
4894b88c807SRodney W. Grimes 			break;
4904b88c807SRodney W. Grimes 		case S_IFIFO:
4914b88c807SRodney W. Grimes 			arcn->type = PAX_FIF;
4924b88c807SRodney W. Grimes 			break;
4934b88c807SRodney W. Grimes 		}
4944b88c807SRodney W. Grimes 		break;
4954b88c807SRodney W. Grimes 	}
4964b88c807SRodney W. Grimes 
4974b88c807SRodney W. Grimes 	/*
4984b88c807SRodney W. Grimes 	 * copy file name, set file name length
4994b88c807SRodney W. Grimes 	 */
500b1787decSKris Kennaway 	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, sizeof(arcn->name) - 1);
5014b88c807SRodney W. Grimes 	arcn->name[arcn->nlen] = '\0';
5024b88c807SRodney W. Grimes 	arcn->org_name = ftent->fts_path;
5034b88c807SRodney W. Grimes 	return(0);
5044b88c807SRodney W. Grimes }
505