Home | History | Annotate | Line # | Download | only in pax
ftree.c revision 1.13
      1 /*	$NetBSD: ftree.c,v 1.13 2001/10/25 13:54:54 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992 Keith Muller.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Keith Muller of the University of California, San Diego.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 /*-
     41  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     42  * All rights reserved.
     43  *
     44  * This code is derived from software contributed to The NetBSD Foundation
     45  * by Luke Mewburn of Wasabi Systems.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *        This product includes software developed by the NetBSD
     58  *        Foundation, Inc. and its contributors.
     59  * 4. Neither the name of The NetBSD Foundation nor the names of its
     60  *    contributors may be used to endorse or promote products derived
     61  *    from this software without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     64  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     65  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     66  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     67  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     68  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     69  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     70  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     71  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     72  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     73  * POSSIBILITY OF SUCH DAMAGE.
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 #ifndef lint
     78 #if 0
     79 static char sccsid[] = "@(#)ftree.c	8.2 (Berkeley) 4/18/94";
     80 #else
     81 __RCSID("$NetBSD: ftree.c,v 1.13 2001/10/25 13:54:54 lukem Exp $");
     82 #endif
     83 #endif /* not lint */
     84 
     85 #include <sys/types.h>
     86 #include <sys/time.h>
     87 #include <sys/stat.h>
     88 #include <sys/param.h>
     89 #include <unistd.h>
     90 #include <string.h>
     91 #include <stdio.h>
     92 #include <ctype.h>
     93 #include <errno.h>
     94 #include <stdlib.h>
     95 #include <fts.h>
     96 #include "pax.h"
     97 #include "ftree.h"
     98 #include "extern.h"
     99 #include "mtree.h"
    100 
    101 /*
    102  * routines to interface with the fts library function.
    103  *
    104  * file args supplied to pax are stored on a single linked list (of type FTREE)
    105  * and given to fts to be processed one at a time. pax "selects" files from
    106  * the expansion of each arg into the corresponding file tree (if the arg is a
    107  * directory, otherwise the node itself is just passed to pax). The selection
    108  * is modified by the -n and -u flags. The user is informed when a specific
    109  * file arg does not generate any selected files. -n keeps expanding the file
    110  * tree arg until one of its files is selected, then skips to the next file
    111  * arg. when the user does not supply the file trees as command line args to
    112  * pax, they are read from stdin
    113  */
    114 
    115 static FTS *ftsp = NULL;		/* current FTS handle */
    116 static int ftsopts;			/* options to be used on fts_open */
    117 static char *farray[2];			/* array for passing each arg to fts */
    118 static FTREE *fthead = NULL;		/* head of linked list of file args */
    119 static FTREE *fttail = NULL;		/* tail of linked list of file args */
    120 static FTREE *ftcur = NULL;		/* current file arg being processed */
    121 static FTSENT *ftent = NULL;		/* current file tree entry */
    122 static int ftree_skip;			/* when set skip to next file arg */
    123 static NODE *ftnode = NULL;		/* mtree(8) specfile; used by -M */
    124 
    125 static int ftree_arg(void);
    126 
    127 #ifdef NET2_FTS
    128 #define	FTS_ERRNO(x)	errno
    129 #else
    130 #define	FTS_ERRNO(x)	(x)->fts_errno
    131 #endif
    132 
    133 /*
    134  * ftree_start()
    135  *	initialize the options passed to fts_open() during this run of pax
    136  *	options are based on the selection of pax options by the user
    137  *	fts_start() also calls fts_arg() to open the first valid file arg. We
    138  *	also attempt to reset directory access times when -t (tflag) is set.
    139  * Return:
    140  *	0 if there is at least one valid file arg to process, -1 otherwise
    141  */
    142 
    143 int
    144 ftree_start(void)
    145 {
    146 
    147 	/*
    148 	 * if -M is given, the list of filenames on stdin is actually
    149 	 * an mtree(8) specfile, so parse the specfile into a NODE *
    150 	 * tree at ftnode, for use by next_file()
    151 	 */
    152 	if (Mflag) {
    153 		if (fthead != NULL) {
    154 			tty_warn(1,
    155 	    "The -M flag is only supported when reading file list from stdin");
    156 			return(-1);
    157 		}
    158 		ftnode = spec(stdin);
    159 		if (ftnode != NULL &&
    160 		    (ftnode->type != F_DIR || strcmp(ftnode->name, ".") != 0)) {
    161 			tty_warn(1,
    162 			    "First node of specfile is not `.' directory");
    163 			return(-1);
    164 		}
    165 		return(0);
    166 	}
    167 
    168 	/*
    169 	 * set up the operation mode of fts, open the first file arg. We must
    170 	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
    171 	 * if fts did a chdir off into the boondocks, we may create an archive
    172 	 * volume in an place where the user did not expect to.
    173 	 */
    174 	ftsopts = FTS_NOCHDIR;
    175 
    176 	/*
    177 	 * optional user flags that effect file traversal
    178 	 * -H command line symlink follow only (half follow)
    179 	 * -L follow sylinks (logical)
    180 	 * -P do not follow sylinks (physical). This is the default.
    181 	 * -X do not cross over mount points
    182 	 * -t preserve access times on files read.
    183 	 * -n select only the first member of a file tree when a match is found
    184 	 * -d do not extract subtrees rooted at a directory arg.
    185 	 */
    186 	if (Lflag)
    187 		ftsopts |= FTS_LOGICAL;
    188 	else
    189 		ftsopts |= FTS_PHYSICAL;
    190 	if (Hflag)
    191 #ifdef NET2_FTS
    192 		tty_warn(0, "The -H flag is not supported on this version");
    193 #else
    194 		ftsopts |= FTS_COMFOLLOW;
    195 #endif
    196 	if (Xflag)
    197 		ftsopts |= FTS_XDEV;
    198 
    199 	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
    200 		tty_warn(1, "Unable to allocate memory for file name buffer");
    201 		return(-1);
    202 	}
    203 
    204 	if (ftree_arg() < 0)
    205 		return(-1);
    206 	if (tflag && (atdir_start() < 0))
    207 		return(-1);
    208 	return(0);
    209 }
    210 
    211 /*
    212  * ftree_add()
    213  *	add the arg to the linked list of files to process. Each will be
    214  *	processed by fts one at a time
    215  * Return:
    216  *	0 if added to the linked list, -1 if failed
    217  */
    218 
    219 int
    220 ftree_add(char *str, int isdir)
    221 {
    222 	FTREE *ft;
    223 	int len;
    224 
    225 	/*
    226 	 * simple check for bad args
    227 	 */
    228 	if ((str == NULL) || (*str == '\0')) {
    229 		tty_warn(0, "Invalid file name arguement");
    230 		return(-1);
    231 	}
    232 
    233 	/*
    234 	 * allocate FTREE node and add to the end of the linked list (args are
    235 	 * processed in the same order they were passed to pax). Get rid of any
    236 	 * trailing / the user may pass us. (watch out for / by itself).
    237 	 */
    238 	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
    239 		tty_warn(0, "Unable to allocate memory for filename");
    240 		return(-1);
    241 	}
    242 
    243 	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
    244 		str[len] = '\0';
    245 	ft->fname = str;
    246 	ft->refcnt = -isdir;
    247 	ft->fow = NULL;
    248 	if (fthead == NULL) {
    249 		fttail = fthead = ft;
    250 		return(0);
    251 	}
    252 	fttail->fow = ft;
    253 	fttail = ft;
    254 	return(0);
    255 }
    256 
    257 /*
    258  * ftree_sel()
    259  *	this entry has been selected by pax. bump up reference count and handle
    260  *	-n and -d processing.
    261  */
    262 
    263 void
    264 ftree_sel(ARCHD *arcn)
    265 {
    266 	/*
    267 	 * set reference bit for this pattern. This linked list is only used
    268 	 * when file trees are supplied pax as args. The list is not used when
    269 	 * the trees are read from stdin.
    270 	 */
    271 	if (ftcur != NULL)
    272 		ftcur->refcnt = 1;
    273 
    274 	/*
    275 	 * if -n we are done with this arg, force a skip to the next arg when
    276 	 * pax asks for the next file in next_file().
    277 	 * if -M we don't use fts(3), so the rest of this function is moot.
    278 	 * if -d we tell fts only to match the directory (if the arg is a dir)
    279 	 * and not the entire file tree rooted at that point.
    280 	 */
    281 	if (nflag)
    282 		ftree_skip = 1;
    283 
    284 	if (Mflag || !dflag || (arcn->type != PAX_DIR))
    285 		return;
    286 
    287 	if (ftent != NULL)
    288 		(void)fts_set(ftsp, ftent, FTS_SKIP);
    289 }
    290 
    291 /*
    292  * ftree_chk()
    293  *	called at end on pax execution. Prints all those file args that did not
    294  *	have a selected member (reference count still 0)
    295  */
    296 
    297 void
    298 ftree_chk(void)
    299 {
    300 	FTREE *ft;
    301 	int wban = 0;
    302 
    303 	/*
    304 	 * make sure all dir access times were reset.
    305 	 */
    306 	if (tflag)
    307 		atdir_end();
    308 
    309 	/*
    310 	 * walk down list and check reference count. Print out those members
    311 	 * that never had a match
    312 	 */
    313 	for (ft = fthead; ft != NULL; ft = ft->fow) {
    314 		if (ft->refcnt != 0)
    315 			continue;
    316 		if (wban == 0) {
    317 			tty_warn(1,
    318 			    "WARNING! These file names were not selected:");
    319 			++wban;
    320 		}
    321 		(void)fprintf(stderr, "%s\n", ft->fname);
    322 	}
    323 }
    324 
    325 /*
    326  * ftree_arg()
    327  *	Get the next file arg for fts to process. Can be from either the linked
    328  *	list or read from stdin when the user did not them as args to pax. Each
    329  *	arg is processed until the first successful fts_open().
    330  * Return:
    331  *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
    332  *	stdin).
    333  */
    334 
    335 static int
    336 ftree_arg(void)
    337 {
    338 	char *pt;
    339 
    340 	/*
    341 	 * close off the current file tree
    342 	 */
    343 	if (ftsp != NULL) {
    344 		(void)fts_close(ftsp);
    345 		ftsp = NULL;
    346 	}
    347 
    348 	/*
    349 	 * keep looping until we get a valid file tree to process. Stop when we
    350 	 * reach the end of the list (or get an eof on stdin)
    351 	 */
    352 	for(;;) {
    353 		if (fthead == NULL) {
    354 			/*
    355 			 * the user didn't supply any args, get the file trees
    356 			 * to process from stdin;
    357 			 */
    358 			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
    359 				return(-1);
    360 			if ((pt = strchr(farray[0], '\n')) != NULL)
    361 				*pt = '\0';
    362 		} else {
    363 			/*
    364 			 * the user supplied the file args as arguements to pax
    365 			 */
    366 			if (ftcur == NULL)
    367 				ftcur = fthead;
    368 			else if ((ftcur = ftcur->fow) == NULL)
    369 				return(-1);
    370 
    371 			if (ftcur->refcnt < 0) {
    372 				/*
    373 				 * chdir entry.
    374 				 * Change directory and retry loop.
    375 				 */
    376 				if (ar_dochdir(ftcur->fname))
    377 					return (-1);
    378 				continue;
    379 			}
    380 			farray[0] = ftcur->fname;
    381 		}
    382 
    383 		/*
    384 		 * watch it, fts wants the file arg stored in a array of char
    385 		 * ptrs, with the last one a null. we use a two element array
    386 		 * and set farray[0] to point at the buffer with the file name
    387 		 * in it. We cannot pass all the file args to fts at one shot
    388 		 * as we need to keep a handle on which file arg generates what
    389 		 * files (the -n and -d flags need this). If the open is
    390 		 * successful, return a 0.
    391 		 */
    392 		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
    393 			break;
    394 	}
    395 	return(0);
    396 }
    397 
    398 /*
    399  * next_file()
    400  *	supplies the next file to process in the supplied archd structure.
    401  * Return:
    402  *	0 when contents of arcn have been set with the next file, -1 when done.
    403  */
    404 
    405 int
    406 next_file(ARCHD *arcn)
    407 {
    408 	static	char	curdir[PAXPATHLEN+2], curpath[PAXPATHLEN+2];
    409 	static	int	curdirlen;
    410 
    411 	struct stat	statbuf;
    412 	FTSENT		Mftent;
    413 	int		cnt;
    414 	time_t		atime, mtime;
    415 	char		*curlink;
    416 #define MFTENT_DUMMY_DEV	UINT_MAX
    417 
    418 	curlink = NULL;
    419 	/*
    420 	 * if parsing an mtree(8) specfile, build up `dummy' ftsent
    421 	 * from specfile info, and jump below to complete setup of arcn.
    422 	 */
    423 	if (Mflag) {
    424 		if (ftnode == NULL)		/* tree is empty */
    425 			return (-1);
    426 
    427 						/* get current name */
    428 		if (snprintf(curpath, sizeof(curpath), "%s%s%s",
    429 		    curdir, curdirlen ? "/" : "", ftnode->name)
    430 		    >= sizeof(curpath)) {
    431 			tty_warn(1, "line %d: %s: %s",
    432 			    ftnode->lineno, curdir, strerror(ENAMETOOLONG));
    433 			return (-1);
    434 		}
    435 		ftnode->flags |= F_VISIT;	/* mark node visited */
    436 
    437 						/* construct dummy FTSENT */
    438 		Mftent.fts_path = curpath;
    439 		Mftent.fts_statp = &statbuf;
    440 		Mftent.fts_pointer = ftnode;
    441 		ftent = &Mftent;
    442 						/* stat existing file */
    443 		if (lstat(Mftent.fts_path, &statbuf) == -1) {
    444 						/* fake up stat buffer */
    445 			memset(&statbuf, 0, sizeof(statbuf));
    446 			statbuf.st_dev = MFTENT_DUMMY_DEV;
    447 			statbuf.st_ino = ftnode->lineno;
    448 			statbuf.st_size = 0;
    449 #define NODETEST(t, m)							\
    450 			if (!(t)) {					\
    451 				tty_warn(1, "line %d: %s: %s not specified", \
    452 				    ftnode->lineno, ftent->fts_path, m); \
    453 				return(-1);				\
    454 			}
    455 			statbuf.st_mode = nodetoino(ftnode->type);
    456 			NODETEST(ftnode->flags & F_TYPE, "type");
    457 			NODETEST(ftnode->flags & F_MODE, "mode");
    458 			if (!(ftnode->flags & F_TIME))
    459 				statbuf.st_mtime = starttime;
    460 			NODETEST(ftnode->flags & (F_GID | F_GNAME), "group");
    461 			NODETEST(ftnode->flags & (F_UID | F_UNAME), "user");
    462 			if (ftnode->type == F_BLOCK || ftnode->type == F_CHAR)
    463 				NODETEST(ftnode->flags & F_DEV,
    464 				    "device number");
    465 			if (ftnode->type == F_LINK)
    466 				NODETEST(ftnode->flags & F_SLINK, "symlink");
    467 			/* don't require F_FLAGS or F_SIZE */
    468 #undef NODETEST
    469 		} else if (ftnode->flags & F_TYPE &&
    470 		    nodetoino(ftnode->type) != (statbuf.st_mode & S_IFMT)) {
    471 			tty_warn(1,
    472 			    "line %d: %s: type mismatch: specfile %s, tree %s",
    473 			    ftnode->lineno, ftent->fts_path,
    474 			    inotype(nodetoino(ftnode->type)),
    475 			    inotype(statbuf.st_mode));
    476 			return(-1);
    477 		}
    478 		/*
    479 		 * override settings with those from specfile
    480 		 */
    481 		if (ftnode->flags & F_MODE) {
    482 			statbuf.st_mode &= ~ALLPERMS;
    483 			statbuf.st_mode |= (ftnode->st_mode & ALLPERMS);
    484 		}
    485 		if (ftnode->flags & (F_GID | F_GNAME))
    486 			statbuf.st_gid = ftnode->st_gid;
    487 		if (ftnode->flags & (F_UID | F_UNAME))
    488 			statbuf.st_uid = ftnode->st_uid;
    489 		if (ftnode->flags & F_FLAGS)
    490 			statbuf.st_flags = ftnode->st_flags;
    491 		if (ftnode->flags & F_TIME)
    492 			statbuf.st_mtimespec = ftnode->st_mtimespec;
    493 		if (ftnode->flags & F_DEV)
    494 			statbuf.st_rdev = ftnode->st_rdev;
    495 		if (ftnode->flags & F_SLINK)
    496 			curlink = ftnode->slink;
    497 				/* ignore F_SIZE */
    498 
    499 		/*
    500 		 * find next node
    501 		 */
    502 		if (ftnode->type == F_DIR && ftnode->child != NULL) {
    503 					/* directory with unseen child */
    504 			ftnode = ftnode->child;
    505 			curdirlen = l_strncpy(curdir, curpath, sizeof(curdir));
    506 		} else do {
    507 			if (ftnode->next != NULL) {
    508 					/* next node at current level */
    509 				ftnode = ftnode->next;
    510 			} else {	/* move back to parent */
    511 					/* reset time only on first cd.. */
    512 				if (Mftent.fts_pointer == ftnode && tflag &&
    513 				    (get_atdir(MFTENT_DUMMY_DEV, ftnode->lineno,
    514 				    &mtime, &atime) == 0)) {
    515 					set_ftime(ftent->fts_path,
    516 					    mtime, atime, 1);
    517 				}
    518 				if (ftnode->parent == ftnode)
    519 					ftnode = NULL;
    520 				else
    521 					ftnode = ftnode->parent;
    522 				if (ftnode != NULL) {
    523 					curdirlen -= strlen(ftnode->name) + 1;
    524 					curdir[curdirlen] = '\0';
    525 				}
    526 			}
    527 		} while (ftnode != NULL && ftnode->flags & F_VISIT);
    528 		goto got_ftent;
    529 	}
    530 
    531 	/*
    532 	 * ftree_sel() might have set the ftree_skip flag if the user has the
    533 	 * -n option and a file was selected from this file arg tree. (-n says
    534 	 * only one member is matched for each pattern) ftree_skip being 1
    535 	 * forces us to go to the next arg now.
    536 	 */
    537 	if (ftree_skip) {
    538 		/*
    539 		 * clear and go to next arg
    540 		 */
    541 		ftree_skip = 0;
    542 		if (ftree_arg() < 0)
    543 			return(-1);
    544 	}
    545 
    546 	/*
    547 	 * loop until we get a valid file to process
    548 	 */
    549 	for(;;) {
    550 		if ((ftent = fts_read(ftsp)) == NULL) {
    551 			/*
    552 			 * out of files in this tree, go to next arg, if none
    553 			 * we are done
    554 			 */
    555 			if (ftree_arg() < 0)
    556 				return(-1);
    557 			continue;
    558 		}
    559 
    560 		/*
    561 		 * handle each type of fts_read() flag
    562 		 */
    563 		switch(ftent->fts_info) {
    564 		case FTS_D:
    565 			/*
    566 			 * cpio does *not* decend directories listed in the
    567 			 * arguments, unlike pax/tar, so needs special handling
    568 			 * here.  failure to do so results in massive amounts
    569 			 * of duplicated files in the output.
    570 			 */
    571 			if (cpio_mode)
    572 				continue;
    573 			/* FALLTHROUGH */
    574 		case FTS_DEFAULT:
    575 		case FTS_F:
    576 		case FTS_SL:
    577 		case FTS_SLNONE:
    578 			/*
    579 			 * these are all ok
    580 			 */
    581 			break;
    582 		case FTS_DP:
    583 			/*
    584 			 * already saw this directory. If the user wants file
    585 			 * access times reset, we use this to restore the
    586 			 * access time for this directory since this is the
    587 			 * last time we will see it in this file subtree
    588 			 * remember to force the time (this is -t on a read
    589 			 * directory, not a created directory).
    590 			 */
    591 			if (!tflag || (get_atdir(
    592 #ifdef NET2_FTS
    593 			    ftent->fts_statb.st_dev, ftent->fts_statb.st_ino,
    594 #else
    595 			    ftent->fts_statp->st_dev, ftent->fts_statp->st_ino,
    596 #endif
    597 			    &mtime, &atime) < 0))
    598 				continue;
    599 			set_ftime(ftent->fts_path, mtime, atime, 1);
    600 			continue;
    601 		case FTS_DC:
    602 			/*
    603 			 * fts claims a file system cycle
    604 			 */
    605 			tty_warn(1,"File system cycle found at %s",
    606 			    ftent->fts_path);
    607 			continue;
    608 		case FTS_DNR:
    609 			syswarn(1, FTS_ERRNO(ftent),
    610 			    "Unable to read directory %s", ftent->fts_path);
    611 			continue;
    612 		case FTS_ERR:
    613 			syswarn(1, FTS_ERRNO(ftent),
    614 			    "File system traversal error");
    615 			continue;
    616 		case FTS_NS:
    617 		case FTS_NSOK:
    618 			syswarn(1, FTS_ERRNO(ftent),
    619 			    "Unable to access %s", ftent->fts_path);
    620 			continue;
    621 		}
    622 
    623  got_ftent:
    624 		/*
    625 		 * ok got a file tree node to process. copy info into arcn
    626 		 * structure (initialize as required)
    627 		 */
    628 		arcn->skip = 0;
    629 		arcn->pad = 0;
    630 		arcn->ln_nlen = 0;
    631 		arcn->ln_name[0] = '\0';
    632 #ifdef NET2_FTS
    633 		arcn->sb = ftent->fts_statb;
    634 #else
    635 		arcn->sb = *(ftent->fts_statp);
    636 #endif
    637 
    638 		/*
    639 		 * file type based set up and copy into the arcn struct
    640 		 * SIDE NOTE:
    641 		 * we try to reset the access time on all files and directories
    642 		 * we may read when the -t flag is specified. files are reset
    643 		 * when we close them after copying. we reset the directories
    644 		 * when we are done with their file tree (we also clean up at
    645 		 * end in case we cut short a file tree traversal). However
    646 		 * there is no way to reset access times on symlinks.
    647 		 */
    648 		switch(S_IFMT & arcn->sb.st_mode) {
    649 		case S_IFDIR:
    650 			arcn->type = PAX_DIR;
    651 			if (!tflag)
    652 				break;
    653 			add_atdir(ftent->fts_path, arcn->sb.st_dev,
    654 			    arcn->sb.st_ino, arcn->sb.st_mtime,
    655 			    arcn->sb.st_atime);
    656 			break;
    657 		case S_IFCHR:
    658 			arcn->type = PAX_CHR;
    659 			break;
    660 		case S_IFBLK:
    661 			arcn->type = PAX_BLK;
    662 			break;
    663 		case S_IFREG:
    664 			/*
    665 			 * only regular files with have data to store on the
    666 			 * archive. all others will store a zero length skip.
    667 			 * the skip field is used by pax for actual data it has
    668 			 * to read (or skip over).
    669 			 */
    670 			arcn->type = PAX_REG;
    671 			arcn->skip = arcn->sb.st_size;
    672 			break;
    673 		case S_IFLNK:
    674 			arcn->type = PAX_SLK;
    675 			if (curlink != NULL) {
    676 				cnt = l_strncpy(arcn->ln_name, curlink,
    677 				    sizeof(arcn->ln_name));
    678 			/*
    679 			 * have to read the symlink path from the file
    680 			 */
    681 			} else if ((cnt =
    682 			    readlink(ftent->fts_path, arcn->ln_name,
    683 			    PAXPATHLEN)) < 0) {
    684 				syswarn(1, errno, "Unable to read symlink %s",
    685 				    ftent->fts_path);
    686 				continue;
    687 			}
    688 			/*
    689 			 * set link name length, watch out readlink does not
    690 			 * allways null terminate the link path
    691 			 */
    692 			arcn->ln_name[cnt] = '\0';
    693 			arcn->ln_nlen = cnt;
    694 			break;
    695 		case S_IFSOCK:
    696 			/*
    697 			 * under BSD storing a socket is senseless but we will
    698 			 * let the format specific write function make the
    699 			 * decision of what to do with it.
    700 			 */
    701 			arcn->type = PAX_SCK;
    702 			break;
    703 		case S_IFIFO:
    704 			arcn->type = PAX_FIF;
    705 			break;
    706 		}
    707 		break;
    708 	}
    709 
    710 	/*
    711 	 * copy file name, set file name length
    712 	 */
    713 	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, PAXPATHLEN+1);
    714 	arcn->name[arcn->nlen] = '\0';
    715 	arcn->org_name = ftent->fts_path;
    716 	return(0);
    717 }
    718