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