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