ftree.c revision 76351
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1992 Keith Muller.
31556Srgrimes * Copyright (c) 1992, 1993
41556Srgrimes *	The Regents of the University of California.  All rights reserved.
51556Srgrimes *
61556Srgrimes * This code is derived from software contributed to Berkeley by
71556Srgrimes * Keith Muller of the University of California, San Diego.
81556Srgrimes *
91556Srgrimes * Redistribution and use in source and binary forms, with or without
101556Srgrimes * modification, are permitted provided that the following conditions
111556Srgrimes * are met:
121556Srgrimes * 1. Redistributions of source code must retain the above copyright
131556Srgrimes *    notice, this list of conditions and the following disclaimer.
141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151556Srgrimes *    notice, this list of conditions and the following disclaimer in the
161556Srgrimes *    documentation and/or other materials provided with the distribution.
171556Srgrimes * 3. All advertising materials mentioning features or use of this software
181556Srgrimes *    must display the following acknowledgement:
191556Srgrimes *	This product includes software developed by the University of
201556Srgrimes *	California, Berkeley and its contributors.
211556Srgrimes * 4. Neither the name of the University nor the names of its contributors
221556Srgrimes *    may be used to endorse or promote products derived from this software
231556Srgrimes *    without specific prior written permission.
241556Srgrimes *
251556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351556Srgrimes * SUCH DAMAGE.
361556Srgrimes */
371556Srgrimes
381556Srgrimes#ifndef lint
3936049Scharnier#if 0
4036049Scharnierstatic char sccsid[] = "@(#)ftree.c	8.2 (Berkeley) 4/18/94";
4136049Scharnier#endif
4236049Scharnierstatic const char rcsid[] =
4350471Speter  "$FreeBSD: head/bin/pax/ftree.c 76351 2001-05-08 06:19:06Z kris $";
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <sys/types.h>
471556Srgrimes#include <sys/time.h>
481556Srgrimes#include <sys/stat.h>
491556Srgrimes#include <unistd.h>
501556Srgrimes#include <string.h>
511556Srgrimes#include <stdio.h>
521556Srgrimes#include <errno.h>
531556Srgrimes#include <stdlib.h>
541556Srgrimes#include <fts.h>
551556Srgrimes#include "pax.h"
561556Srgrimes#include "ftree.h"
571556Srgrimes#include "extern.h"
581556Srgrimes
591556Srgrimes/*
601556Srgrimes * routines to interface with the fts library function.
611556Srgrimes *
621556Srgrimes * file args supplied to pax are stored on a single linked list (of type FTREE)
631556Srgrimes * and given to fts to be processed one at a time. pax "selects" files from
641556Srgrimes * the expansion of each arg into the corresponding file tree (if the arg is a
651556Srgrimes * directory, otherwise the node itself is just passed to pax). The selection
661556Srgrimes * is modified by the -n and -u flags. The user is informed when a specific
671556Srgrimes * file arg does not generate any selected files. -n keeps expanding the file
681556Srgrimes * tree arg until one of its files is selected, then skips to the next file
691556Srgrimes * arg. when the user does not supply the file trees as command line args to
701556Srgrimes * pax, they are read from stdin
711556Srgrimes */
721556Srgrimes
7346684Skrisstatic FTS *ftsp = NULL;		/* current FTS handle */
741556Srgrimesstatic int ftsopts;			/* options to be used on fts_open */
751556Srgrimesstatic char *farray[2];			/* array for passing each arg to fts */
761556Srgrimesstatic FTREE *fthead = NULL;		/* head of linked list of file args */
771556Srgrimesstatic FTREE *fttail = NULL;		/* tail of linked list of file args */
781556Srgrimesstatic FTREE *ftcur = NULL;		/* current file arg being processed */
791556Srgrimesstatic FTSENT *ftent = NULL;		/* current file tree entry */
801556Srgrimesstatic int ftree_skip;			/* when set skip to next file arg */
811556Srgrimes
821556Srgrimesstatic int ftree_arg __P((void));
831556Srgrimes
841556Srgrimes/*
851556Srgrimes * ftree_start()
861556Srgrimes *	initialize the options passed to fts_open() during this run of pax
871556Srgrimes *	options are based on the selection of pax options by the user
881556Srgrimes *	fts_start() also calls fts_arg() to open the first valid file arg. We
891556Srgrimes *	also attempt to reset directory access times when -t (tflag) is set.
901556Srgrimes * Return:
911556Srgrimes *	0 if there is at least one valid file arg to process, -1 otherwise
921556Srgrimes */
931556Srgrimes
9476017Skris#ifdef __STDC__
951556Srgrimesint
961556Srgrimesftree_start(void)
971556Srgrimes#else
981556Srgrimesint
991556Srgrimesftree_start()
1001556Srgrimes#endif
1011556Srgrimes{
1021556Srgrimes	/*
1031556Srgrimes	 * set up the operation mode of fts, open the first file arg. We must
1041556Srgrimes	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
1051556Srgrimes	 * if fts did a chdir off into the boondocks, we may create an archive
1061556Srgrimes	 * volume in an place where the user did not expect to.
1071556Srgrimes	 */
1081556Srgrimes	ftsopts = FTS_NOCHDIR;
1091556Srgrimes
1101556Srgrimes	/*
1111556Srgrimes	 * optional user flags that effect file traversal
1121556Srgrimes	 * -H command line symlink follow only (half follow)
1131556Srgrimes	 * -L follow sylinks (logical)
1141556Srgrimes	 * -P do not follow sylinks (physical). This is the default.
1151556Srgrimes	 * -X do not cross over mount points
1161556Srgrimes	 * -t preserve access times on files read.
1171556Srgrimes	 * -n select only the first member of a file tree when a match is found
1181556Srgrimes	 * -d do not extract subtrees rooted at a directory arg.
1191556Srgrimes	 */
1201556Srgrimes	if (Lflag)
1211556Srgrimes		ftsopts |= FTS_LOGICAL;
1221556Srgrimes	else
1231556Srgrimes		ftsopts |= FTS_PHYSICAL;
1241556Srgrimes	if (Hflag)
1251556Srgrimes#	ifdef NET2_FTS
12676017Skris		paxwarn(0, "The -H flag is not supported on this version");
1271556Srgrimes#	else
1281556Srgrimes		ftsopts |= FTS_COMFOLLOW;
1291556Srgrimes#	endif
1301556Srgrimes	if (Xflag)
1311556Srgrimes		ftsopts |= FTS_XDEV;
1321556Srgrimes
1331556Srgrimes	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
13476017Skris		paxwarn(1, "Unable to allocate memory for file name buffer");
1351556Srgrimes		return(-1);
1361556Srgrimes	}
1371556Srgrimes
1381556Srgrimes	if (ftree_arg() < 0)
1391556Srgrimes		return(-1);
1401556Srgrimes	if (tflag && (atdir_start() < 0))
1411556Srgrimes		return(-1);
1421556Srgrimes	return(0);
1431556Srgrimes}
1441556Srgrimes
1451556Srgrimes/*
1461556Srgrimes * ftree_add()
1471556Srgrimes *	add the arg to the linked list of files to process. Each will be
1481556Srgrimes *	processed by fts one at a time
1491556Srgrimes * Return:
1501556Srgrimes *	0 if added to the linked list, -1 if failed
1511556Srgrimes */
1521556Srgrimes
15376017Skris#ifdef __STDC__
1541556Srgrimesint
15576351Skrisftree_add(register char *str, int chflg)
1561556Srgrimes#else
1571556Srgrimesint
15876351Skrisftree_add(str, chflg)
1591556Srgrimes	register char *str;
16076351Skris	int chflg;
1611556Srgrimes#endif
1621556Srgrimes{
1631556Srgrimes	register FTREE *ft;
1641556Srgrimes	register int len;
1651556Srgrimes
1661556Srgrimes	/*
1671556Srgrimes	 * simple check for bad args
1681556Srgrimes	 */
1691556Srgrimes	if ((str == NULL) || (*str == '\0')) {
17076017Skris		paxwarn(0, "Invalid file name argument");
1711556Srgrimes		return(-1);
1721556Srgrimes	}
1731556Srgrimes
1741556Srgrimes	/*
1751556Srgrimes	 * allocate FTREE node and add to the end of the linked list (args are
1761556Srgrimes	 * processed in the same order they were passed to pax). Get rid of any
1771556Srgrimes	 * trailing / the user may pass us. (watch out for / by itself).
1781556Srgrimes	 */
1791556Srgrimes	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
18076017Skris		paxwarn(0, "Unable to allocate memory for filename");
1811556Srgrimes		return(-1);
1821556Srgrimes	}
1831556Srgrimes
1841556Srgrimes	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
1851556Srgrimes		str[len] = '\0';
1861556Srgrimes	ft->fname = str;
1871556Srgrimes	ft->refcnt = 0;
18876351Skris	ft->chflg = chflg;
1891556Srgrimes	ft->fow = NULL;
1901556Srgrimes	if (fthead == NULL) {
1911556Srgrimes		fttail = fthead = ft;
1921556Srgrimes		return(0);
1931556Srgrimes	}
1941556Srgrimes	fttail->fow = ft;
1951556Srgrimes	fttail = ft;
1961556Srgrimes	return(0);
1971556Srgrimes}
1981556Srgrimes
1991556Srgrimes/*
2001556Srgrimes * ftree_sel()
2011556Srgrimes *	this entry has been selected by pax. bump up reference count and handle
2021556Srgrimes *	-n and -d processing.
2031556Srgrimes */
2041556Srgrimes
20576017Skris#ifdef __STDC__
2061556Srgrimesvoid
2071556Srgrimesftree_sel(register ARCHD *arcn)
2081556Srgrimes#else
2091556Srgrimesvoid
2101556Srgrimesftree_sel(arcn)
2111556Srgrimes	register ARCHD *arcn;
2121556Srgrimes#endif
2131556Srgrimes{
2141556Srgrimes	/*
2151556Srgrimes	 * set reference bit for this pattern. This linked list is only used
2161556Srgrimes	 * when file trees are supplied pax as args. The list is not used when
2171556Srgrimes	 * the trees are read from stdin.
2181556Srgrimes	 */
2198855Srgrimes	if (ftcur != NULL)
2201556Srgrimes		ftcur->refcnt = 1;
2211556Srgrimes
2221556Srgrimes	/*
2231556Srgrimes	 * if -n we are done with this arg, force a skip to the next arg when
2241556Srgrimes	 * pax asks for the next file in next_file().
2251556Srgrimes	 * if -d we tell fts only to match the directory (if the arg is a dir)
2261556Srgrimes	 * and not the entire file tree rooted at that point.
2271556Srgrimes	 */
2281556Srgrimes	if (nflag)
2291556Srgrimes		ftree_skip = 1;
2301556Srgrimes
2311556Srgrimes	if (!dflag || (arcn->type != PAX_DIR))
2321556Srgrimes		return;
2331556Srgrimes
2341556Srgrimes	if (ftent != NULL)
2351556Srgrimes		(void)fts_set(ftsp, ftent, FTS_SKIP);
2361556Srgrimes}
2371556Srgrimes
2381556Srgrimes/*
2391556Srgrimes * ftree_chk()
2401556Srgrimes *	called at end on pax execution. Prints all those file args that did not
2411556Srgrimes *	have a selected member (reference count still 0)
2421556Srgrimes */
2431556Srgrimes
24476017Skris#ifdef __STDC__
2451556Srgrimesvoid
2461556Srgrimesftree_chk(void)
2471556Srgrimes#else
2481556Srgrimesvoid
2491556Srgrimesftree_chk()
2501556Srgrimes#endif
2511556Srgrimes{
2521556Srgrimes	register FTREE *ft;
2531556Srgrimes	register int wban = 0;
2541556Srgrimes
2551556Srgrimes	/*
2561556Srgrimes	 * make sure all dir access times were reset.
2571556Srgrimes	 */
2581556Srgrimes	if (tflag)
2591556Srgrimes		atdir_end();
2601556Srgrimes
2611556Srgrimes	/*
2621556Srgrimes	 * walk down list and check reference count. Print out those members
2631556Srgrimes	 * that never had a match
2641556Srgrimes	 */
2651556Srgrimes	for (ft = fthead; ft != NULL; ft = ft->fow) {
26676351Skris		if ((ft->refcnt > 0) || ft->chflg)
2671556Srgrimes			continue;
2681556Srgrimes		if (wban == 0) {
26976017Skris			paxwarn(1,"WARNING! These file names were not selected:");
2701556Srgrimes			++wban;
2711556Srgrimes		}
2721556Srgrimes		(void)fprintf(stderr, "%s\n", ft->fname);
2731556Srgrimes	}
2741556Srgrimes}
2751556Srgrimes
2761556Srgrimes/*
2771556Srgrimes * ftree_arg()
2781556Srgrimes *	Get the next file arg for fts to process. Can be from either the linked
2791556Srgrimes *	list or read from stdin when the user did not them as args to pax. Each
2801556Srgrimes *	arg is processed until the first successful fts_open().
2811556Srgrimes * Return:
2821556Srgrimes *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
2831556Srgrimes *	stdin).
2841556Srgrimes */
2851556Srgrimes
28676017Skris#ifdef __STDC__
2871556Srgrimesstatic int
2881556Srgrimesftree_arg(void)
2891556Srgrimes#else
2901556Srgrimesstatic int
2911556Srgrimesftree_arg()
2921556Srgrimes#endif
2931556Srgrimes{
2941556Srgrimes	register char *pt;
2951556Srgrimes
2961556Srgrimes	/*
2971556Srgrimes	 * close off the current file tree
2981556Srgrimes	 */
2991556Srgrimes	if (ftsp != NULL) {
3001556Srgrimes		(void)fts_close(ftsp);
3011556Srgrimes		ftsp = NULL;
3021556Srgrimes	}
3031556Srgrimes
3041556Srgrimes	/*
3051556Srgrimes	 * keep looping until we get a valid file tree to process. Stop when we
3061556Srgrimes	 * reach the end of the list (or get an eof on stdin)
3071556Srgrimes	 */
3081556Srgrimes	for(;;) {
3091556Srgrimes		if (fthead == NULL) {
3101556Srgrimes			/*
3111556Srgrimes			 * the user didn't supply any args, get the file trees
3128855Srgrimes			 * to process from stdin;
3131556Srgrimes			 */
3141556Srgrimes			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
3151556Srgrimes				return(-1);
3161556Srgrimes			if ((pt = strchr(farray[0], '\n')) != NULL)
3171556Srgrimes				*pt = '\0';
3181556Srgrimes		} else {
3191556Srgrimes			/*
32076017Skris			 * the user supplied the file args as arguments to pax
3211556Srgrimes			 */
3221556Srgrimes			if (ftcur == NULL)
3231556Srgrimes				ftcur = fthead;
3241556Srgrimes			else if ((ftcur = ftcur->fow) == NULL)
3251556Srgrimes				return(-1);
32676351Skris			if (ftcur->chflg) {
32776351Skris				/* First fchdir() back... */
32876351Skris				if (fchdir(cwdfd) < 0) {
32976351Skris					syswarn(1, errno,
33076351Skris					  "Can't fchdir to starting directory");
33176351Skris					return(-1);
33276351Skris				}
33376351Skris				if (chdir(ftcur->fname) < 0) {
33476351Skris					syswarn(1, errno, "Can't chdir to %s",
33576351Skris					    ftcur->fname);
33676351Skris					return(-1);
33776351Skris				}
33876351Skris				continue;
33976351Skris			} else
34076351Skris				farray[0] = ftcur->fname;
3411556Srgrimes		}
3421556Srgrimes
3431556Srgrimes		/*
3441556Srgrimes		 * watch it, fts wants the file arg stored in a array of char
3451556Srgrimes		 * ptrs, with the last one a null. we use a two element array
3461556Srgrimes		 * and set farray[0] to point at the buffer with the file name
34746684Skris		 * in it. We cannot pass all the file args to fts at one shot
3481556Srgrimes		 * as we need to keep a handle on which file arg generates what
3491556Srgrimes		 * files (the -n and -d flags need this). If the open is
3501556Srgrimes		 * successful, return a 0.
3511556Srgrimes		 */
3521556Srgrimes		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
3531556Srgrimes			break;
3541556Srgrimes	}
3551556Srgrimes	return(0);
3561556Srgrimes}
3571556Srgrimes
3581556Srgrimes/*
3591556Srgrimes * next_file()
3601556Srgrimes *	supplies the next file to process in the supplied archd structure.
3611556Srgrimes * Return:
3621556Srgrimes *	0 when contents of arcn have been set with the next file, -1 when done.
3631556Srgrimes */
3641556Srgrimes
36576017Skris#ifdef __STDC__
3661556Srgrimesint
3671556Srgrimesnext_file(register ARCHD *arcn)
3681556Srgrimes#else
3691556Srgrimesint
3701556Srgrimesnext_file(arcn)
3711556Srgrimes	register ARCHD *arcn;
3721556Srgrimes#endif
3731556Srgrimes{
3741556Srgrimes	register int cnt;
3751556Srgrimes	time_t atime;
3761556Srgrimes	time_t mtime;
3771556Srgrimes
3781556Srgrimes	/*
3791556Srgrimes	 * ftree_sel() might have set the ftree_skip flag if the user has the
3801556Srgrimes	 * -n option and a file was selected from this file arg tree. (-n says
3818855Srgrimes	 * only one member is matched for each pattern) ftree_skip being 1
3821556Srgrimes	 * forces us to go to the next arg now.
3831556Srgrimes	 */
3841556Srgrimes	if (ftree_skip) {
3851556Srgrimes		/*
3861556Srgrimes		 * clear and go to next arg
3871556Srgrimes		 */
3881556Srgrimes		ftree_skip = 0;
3891556Srgrimes		if (ftree_arg() < 0)
3901556Srgrimes			return(-1);
3911556Srgrimes	}
3921556Srgrimes
3931556Srgrimes	/*
3941556Srgrimes	 * loop until we get a valid file to process
3951556Srgrimes	 */
3961556Srgrimes	for(;;) {
3971556Srgrimes		if ((ftent = fts_read(ftsp)) == NULL) {
3981556Srgrimes			/*
3991556Srgrimes			 * out of files in this tree, go to next arg, if none
4001556Srgrimes			 * we are done
4011556Srgrimes			 */
4021556Srgrimes			if (ftree_arg() < 0)
4031556Srgrimes				return(-1);
4041556Srgrimes			continue;
4051556Srgrimes		}
4061556Srgrimes
4071556Srgrimes		/*
4081556Srgrimes		 * handle each type of fts_read() flag
4091556Srgrimes		 */
4101556Srgrimes		switch(ftent->fts_info) {
4111556Srgrimes		case FTS_D:
4121556Srgrimes		case FTS_DEFAULT:
4131556Srgrimes		case FTS_F:
4141556Srgrimes		case FTS_SL:
4151556Srgrimes		case FTS_SLNONE:
4161556Srgrimes			/*
4171556Srgrimes			 * these are all ok
4181556Srgrimes			 */
4191556Srgrimes			break;
4201556Srgrimes		case FTS_DP:
4211556Srgrimes			/*
4221556Srgrimes			 * already saw this directory. If the user wants file
4231556Srgrimes			 * access times reset, we use this to restore the
4241556Srgrimes			 * access time for this directory since this is the
4251556Srgrimes			 * last time we will see it in this file subtree
4261556Srgrimes			 * remember to force the time (this is -t on a read
4271556Srgrimes			 * directory, not a created directory).
4281556Srgrimes			 */
4291556Srgrimes#			ifdef NET2_FTS
4301556Srgrimes			if (!tflag || (get_atdir(ftent->fts_statb.st_dev,
4311556Srgrimes			    ftent->fts_statb.st_ino, &mtime, &atime) < 0))
4321556Srgrimes#			else
4331556Srgrimes			if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
4341556Srgrimes			    ftent->fts_statp->st_ino, &mtime, &atime) < 0))
4351556Srgrimes#			endif
4361556Srgrimes				continue;
4371556Srgrimes			set_ftime(ftent->fts_path, mtime, atime, 1);
4381556Srgrimes			continue;
4391556Srgrimes		case FTS_DC:
4401556Srgrimes			/*
4411556Srgrimes			 * fts claims a file system cycle
4421556Srgrimes			 */
44376017Skris			paxwarn(1,"File system cycle found at %s",ftent->fts_path);
4441556Srgrimes			continue;
4451556Srgrimes		case FTS_DNR:
4461556Srgrimes#			ifdef NET2_FTS
44776017Skris			syswarn(1, errno,
4481556Srgrimes#			else
44976017Skris			syswarn(1, ftent->fts_errno,
4501556Srgrimes#			endif
4511556Srgrimes			    "Unable to read directory %s", ftent->fts_path);
4521556Srgrimes			continue;
4531556Srgrimes		case FTS_ERR:
4541556Srgrimes#			ifdef NET2_FTS
45576017Skris			syswarn(1, errno,
4561556Srgrimes#			else
45776017Skris			syswarn(1, ftent->fts_errno,
4581556Srgrimes#			endif
4591556Srgrimes			    "File system traversal error");
4601556Srgrimes			continue;
4611556Srgrimes		case FTS_NS:
4621556Srgrimes		case FTS_NSOK:
4631556Srgrimes#			ifdef NET2_FTS
46476017Skris			syswarn(1, errno,
4651556Srgrimes#			else
46676017Skris			syswarn(1, ftent->fts_errno,
4671556Srgrimes#			endif
4681556Srgrimes			    "Unable to access %s", ftent->fts_path);
4691556Srgrimes			continue;
4701556Srgrimes		}
4711556Srgrimes
4721556Srgrimes		/*
4731556Srgrimes		 * ok got a file tree node to process. copy info into arcn
4741556Srgrimes		 * structure (initialize as required)
4751556Srgrimes		 */
4761556Srgrimes		arcn->skip = 0;
4771556Srgrimes		arcn->pad = 0;
4781556Srgrimes		arcn->ln_nlen = 0;
4791556Srgrimes		arcn->ln_name[0] = '\0';
4801556Srgrimes#		ifdef NET2_FTS
4811556Srgrimes		arcn->sb = ftent->fts_statb;
4821556Srgrimes#		else
4831556Srgrimes		arcn->sb = *(ftent->fts_statp);
4841556Srgrimes#		endif
4851556Srgrimes
4861556Srgrimes		/*
4871556Srgrimes		 * file type based set up and copy into the arcn struct
4881556Srgrimes		 * SIDE NOTE:
4891556Srgrimes		 * we try to reset the access time on all files and directories
4901556Srgrimes		 * we may read when the -t flag is specified. files are reset
4911556Srgrimes		 * when we close them after copying. we reset the directories
4921556Srgrimes		 * when we are done with their file tree (we also clean up at
4931556Srgrimes		 * end in case we cut short a file tree traversal). However
4941556Srgrimes		 * there is no way to reset access times on symlinks.
4951556Srgrimes		 */
4961556Srgrimes		switch(S_IFMT & arcn->sb.st_mode) {
4971556Srgrimes		case S_IFDIR:
4981556Srgrimes			arcn->type = PAX_DIR;
4991556Srgrimes			if (!tflag)
5001556Srgrimes				break;
5011556Srgrimes			add_atdir(ftent->fts_path, arcn->sb.st_dev,
5021556Srgrimes			    arcn->sb.st_ino, arcn->sb.st_mtime,
5031556Srgrimes			    arcn->sb.st_atime);
5041556Srgrimes			break;
5051556Srgrimes		case S_IFCHR:
5061556Srgrimes			arcn->type = PAX_CHR;
5071556Srgrimes			break;
5081556Srgrimes		case S_IFBLK:
5091556Srgrimes			arcn->type = PAX_BLK;
5101556Srgrimes			break;
5111556Srgrimes		case S_IFREG:
5121556Srgrimes			/*
5131556Srgrimes			 * only regular files with have data to store on the
5141556Srgrimes			 * archive. all others will store a zero length skip.
5151556Srgrimes			 * the skip field is used by pax for actual data it has
5161556Srgrimes			 * to read (or skip over).
5171556Srgrimes			 */
5181556Srgrimes			arcn->type = PAX_REG;
5191556Srgrimes			arcn->skip = arcn->sb.st_size;
5201556Srgrimes			break;
5211556Srgrimes		case S_IFLNK:
5221556Srgrimes			arcn->type = PAX_SLK;
5231556Srgrimes			/*
5241556Srgrimes			 * have to read the symlink path from the file
5251556Srgrimes			 */
5261556Srgrimes			if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
52736784Simp			    PAXPATHLEN - 1)) < 0) {
52876017Skris				syswarn(1, errno, "Unable to read symlink %s",
5291556Srgrimes				    ftent->fts_path);
5301556Srgrimes				continue;
5311556Srgrimes			}
5321556Srgrimes			/*
5331556Srgrimes			 * set link name length, watch out readlink does not
53446684Skris			 * always NUL terminate the link path
5351556Srgrimes			 */
5361556Srgrimes			arcn->ln_name[cnt] = '\0';
5371556Srgrimes			arcn->ln_nlen = cnt;
5381556Srgrimes			break;
5391556Srgrimes		case S_IFSOCK:
5401556Srgrimes			/*
5411556Srgrimes			 * under BSD storing a socket is senseless but we will
5421556Srgrimes			 * let the format specific write function make the
5431556Srgrimes			 * decision of what to do with it.
5441556Srgrimes			 */
5451556Srgrimes			arcn->type = PAX_SCK;
5461556Srgrimes			break;
5471556Srgrimes		case S_IFIFO:
5481556Srgrimes			arcn->type = PAX_FIF;
5491556Srgrimes			break;
5501556Srgrimes		}
5511556Srgrimes		break;
5521556Srgrimes	}
5531556Srgrimes
5541556Srgrimes	/*
5551556Srgrimes	 * copy file name, set file name length
5561556Srgrimes	 */
55776351Skris	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, sizeof(arcn->name) - 1);
5581556Srgrimes	arcn->name[arcn->nlen] = '\0';
5591556Srgrimes	arcn->org_name = ftent->fts_path;
5601556Srgrimes	return(0);
5611556Srgrimes}
562