Home | History | Annotate | Line # | Download | only in pax
ftree.c revision 1.36
      1 /*	$NetBSD: ftree.c,v 1.36 2008/01/10 04:24:51 tls 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. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 /*-
     37  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to The NetBSD Foundation
     41  * by Luke Mewburn of Wasabi Systems.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *        This product includes software developed by the NetBSD
     54  *        Foundation, Inc. and its contributors.
     55  * 4. Neither the name of The NetBSD Foundation nor the names of its
     56  *    contributors may be used to endorse or promote products derived
     57  *    from this software without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     60  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 #if HAVE_NBTOOL_CONFIG_H
     73 #include "nbtool_config.h"
     74 #endif
     75 
     76 #include <sys/cdefs.h>
     77 #if !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.36 2008/01/10 04:24:51 tls 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()
    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 argument");
    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 	/*
    346 	 * close off the current file tree
    347 	 */
    348 	if (ftsp != NULL) {
    349 		(void)fts_close(ftsp);
    350 		ftsp = NULL;
    351 	}
    352 
    353 	/*
    354 	 * keep looping until we get a valid file tree to process. Stop when we
    355 	 * reach the end of the list (or get an eof on stdin)
    356 	 */
    357 	for(;;) {
    358 		if (fthead == NULL) {
    359 			int i, c = EOF;
    360 			/*
    361 			 * the user didn't supply any args, get the file trees
    362 			 * to process from stdin;
    363 			 */
    364 			for (i = 0; i < PAXPATHLEN + 2; i++) {
    365 				c = getchar();
    366 				if (c == EOF)
    367 					break;
    368 				else if (c == sep) {
    369 					if (i != 0)
    370 						break;
    371 				} else
    372 					farray[0][i] = c;
    373 			}
    374 			if (i == 0)
    375 				return -1;
    376 			farray[0][i] = '\0';
    377 		} else {
    378 			/*
    379 			 * the user supplied the file args as arguments to pax
    380 			 */
    381 			if (ftcur == NULL)
    382 				ftcur = fthead;
    383 			else if ((ftcur = ftcur->fow) == NULL)
    384 				return -1;
    385 
    386 			if (ftcur->refcnt < 0) {
    387 				/*
    388 				 * chdir entry.
    389 				 * Change directory and retry loop.
    390 				 */
    391 				if (ar_dochdir(ftcur->fname))
    392 					return (-1);
    393 				continue;
    394 			}
    395 			farray[0] = ftcur->fname;
    396 		}
    397 
    398 		/*
    399 		 * watch it, fts wants the file arg stored in a array of char
    400 		 * ptrs, with the last one a null. we use a two element array
    401 		 * and set farray[0] to point at the buffer with the file name
    402 		 * in it. We cannot pass all the file args to fts at one shot
    403 		 * as we need to keep a handle on which file arg generates what
    404 		 * files (the -n and -d flags need this). If the open is
    405 		 * successful, return a 0.
    406 		 */
    407 		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
    408 			break;
    409 	}
    410 	return 0;
    411 }
    412 
    413 /*
    414  * next_file()
    415  *	supplies the next file to process in the supplied archd structure.
    416  * Return:
    417  *	0 when contents of arcn have been set with the next file, -1 when done.
    418  */
    419 
    420 int
    421 next_file(ARCHD *arcn)
    422 {
    423 #ifndef SMALL
    424 	static	char	curdir[PAXPATHLEN+2], curpath[PAXPATHLEN+2];
    425 	static	int	curdirlen;
    426 
    427 	struct stat	statbuf;
    428 	FTSENT		Mftent;
    429 #endif	/* SMALL */
    430 	int		cnt;
    431 	time_t		atime, mtime;
    432 	char		*curlink;
    433 #define MFTENT_DUMMY_DEV	UINT_MAX
    434 
    435 	curlink = NULL;
    436 #ifndef SMALL
    437 	/*
    438 	 * if parsing an mtree(8) specfile, build up `dummy' ftsent
    439 	 * from specfile info, and jump below to complete setup of arcn.
    440 	 */
    441 	if (Mflag) {
    442 		int	skipoptional;
    443 
    444  next_ftnode:
    445 		skipoptional = 0;
    446 		if (ftnode == NULL)		/* tree is empty */
    447 			return (-1);
    448 
    449 						/* get current name */
    450 		if (snprintf(curpath, sizeof(curpath), "%s%s%s",
    451 		    curdir, curdirlen ? "/" : "", ftnode->name)
    452 		    >= sizeof(curpath)) {
    453 			tty_warn(1, "line %lu: %s: %s", (u_long)ftnode->lineno,
    454 			    curdir, strerror(ENAMETOOLONG));
    455 			return (-1);
    456 		}
    457 		ftnode->flags |= F_VISIT;	/* mark node visited */
    458 
    459 						/* construct dummy FTSENT */
    460 		Mftent.fts_path = curpath;
    461 		Mftent.fts_statp = &statbuf;
    462 		Mftent.fts_pointer = ftnode;
    463 		ftent = &Mftent;
    464 						/* look for existing file */
    465 		if (lstat(Mftent.fts_path, &statbuf) == -1) {
    466 			if (ftnode->flags & F_OPT)
    467 				skipoptional = 1;
    468 
    469 						/* missing: fake up stat info */
    470 			memset(&statbuf, 0, sizeof(statbuf));
    471 			statbuf.st_dev = MFTENT_DUMMY_DEV;
    472 			statbuf.st_ino = ftnode->lineno;
    473 			statbuf.st_size = 0;
    474 #define NODETEST(t, m)							\
    475 			if (!(t)) {					\
    476 				tty_warn(1, "line %lu: %s: %s not specified", \
    477 				    (u_long)ftnode->lineno,		\
    478 				    ftent->fts_path, m);		\
    479 				return -1;				\
    480 			}
    481 			statbuf.st_mode = nodetoino(ftnode->type);
    482 			NODETEST(ftnode->flags & F_TYPE, "type");
    483 			NODETEST(ftnode->flags & F_MODE, "mode");
    484 			if (!(ftnode->flags & F_TIME))
    485 				statbuf.st_mtime = starttime;
    486 			NODETEST(ftnode->flags & (F_GID | F_GNAME), "group");
    487 			NODETEST(ftnode->flags & (F_UID | F_UNAME), "user");
    488 			if (ftnode->type == F_BLOCK || ftnode->type == F_CHAR)
    489 				NODETEST(ftnode->flags & F_DEV,
    490 				    "device number");
    491 			if (ftnode->type == F_LINK)
    492 				NODETEST(ftnode->flags & F_SLINK, "symlink");
    493 			/* don't require F_FLAGS or F_SIZE */
    494 #undef NODETEST
    495 		} else {
    496 			if (ftnode->flags & F_TYPE && nodetoino(ftnode->type)
    497 			    != (statbuf.st_mode & S_IFMT)) {
    498 				tty_warn(1,
    499 			    "line %lu: %s: type mismatch: specfile %s, tree %s",
    500 				    (u_long)ftnode->lineno, ftent->fts_path,
    501 				    inotype(nodetoino(ftnode->type)),
    502 				    inotype(statbuf.st_mode));
    503 				return -1;
    504 			}
    505 			if (ftnode->type == F_DIR && (ftnode->flags & F_OPT))
    506 				skipoptional = 1;
    507 		}
    508 		/*
    509 		 * override settings with those from specfile
    510 		 */
    511 		if (ftnode->flags & F_MODE) {
    512 			statbuf.st_mode &= ~ALLPERMS;
    513 			statbuf.st_mode |= (ftnode->st_mode & ALLPERMS);
    514 		}
    515 		if (ftnode->flags & (F_GID | F_GNAME))
    516 			statbuf.st_gid = ftnode->st_gid;
    517 		if (ftnode->flags & (F_UID | F_UNAME))
    518 			statbuf.st_uid = ftnode->st_uid;
    519 #if HAVE_STRUCT_STAT_ST_FLAGS
    520 		if (ftnode->flags & F_FLAGS)
    521 			statbuf.st_flags = ftnode->st_flags;
    522 #endif
    523 		if (ftnode->flags & F_TIME)
    524 #if BSD4_4 && !HAVE_NBTOOL_CONFIG_H
    525 			statbuf.st_mtimespec = ftnode->st_mtimespec;
    526 #else
    527 			statbuf.st_mtime = ftnode->st_mtimespec.tv_sec;
    528 #endif
    529 		if (ftnode->flags & F_DEV)
    530 			statbuf.st_rdev = ftnode->st_rdev;
    531 		if (ftnode->flags & F_SLINK)
    532 			curlink = ftnode->slink;
    533 				/* ignore F_SIZE */
    534 
    535 		/*
    536 		 * find next node
    537 		 */
    538 		if (ftnode->type == F_DIR && ftnode->child != NULL) {
    539 					/* directory with unseen child */
    540 			ftnode = ftnode->child;
    541 			curdirlen = strlcpy(curdir, curpath, sizeof(curdir));
    542 		} else do {
    543 			if (ftnode->next != NULL) {
    544 					/* next node at current level */
    545 				ftnode = ftnode->next;
    546 			} else {	/* move back to parent */
    547 					/* reset time only on first cd.. */
    548 				if (Mftent.fts_pointer == ftnode && tflag &&
    549 				    (get_atdir(MFTENT_DUMMY_DEV, ftnode->lineno,
    550 				    &mtime, &atime) == 0)) {
    551 					set_ftime(ftent->fts_path,
    552 					    mtime, atime, 1, 0);
    553 				}
    554 				ftnode = ftnode->parent;
    555 				if (ftnode->parent == ftnode)
    556 					ftnode = NULL;
    557 				else {
    558 					curdirlen -= strlen(ftnode->name) + 1;
    559 					curdir[curdirlen] = '\0';
    560 				}
    561 			}
    562 		} while (ftnode != NULL && ftnode->flags & F_VISIT);
    563 		if (skipoptional)	/* skip optional entries */
    564 			goto next_ftnode;
    565 		goto got_ftent;
    566 	}
    567 #endif	/* SMALL */
    568 
    569 	/*
    570 	 * ftree_sel() might have set the ftree_skip flag if the user has the
    571 	 * -n option and a file was selected from this file arg tree. (-n says
    572 	 * only one member is matched for each pattern) ftree_skip being 1
    573 	 * forces us to go to the next arg now.
    574 	 */
    575 	if (ftree_skip) {
    576 		/*
    577 		 * clear and go to next arg
    578 		 */
    579 		ftree_skip = 0;
    580 		if (ftree_arg() < 0)
    581 			return -1;
    582 	}
    583 
    584 	if (ftsp == NULL)
    585 		return -1;
    586 	/*
    587 	 * loop until we get a valid file to process
    588 	 */
    589 	for(;;) {
    590 		if ((ftent = fts_read(ftsp)) == NULL) {
    591 			/*
    592 			 * out of files in this tree, go to next arg, if none
    593 			 * we are done
    594 			 */
    595 			if (ftree_arg() < 0)
    596 				return -1;
    597 			continue;
    598 		}
    599 
    600 		/*
    601 		 * handle each type of fts_read() flag
    602 		 */
    603 		switch(ftent->fts_info) {
    604 		case FTS_D:
    605 		case FTS_DEFAULT:
    606 		case FTS_F:
    607 		case FTS_SL:
    608 		case FTS_SLNONE:
    609 			/*
    610 			 * these are all ok
    611 			 */
    612 			break;
    613 		case FTS_DP:
    614 			/*
    615 			 * already saw this directory. If the user wants file
    616 			 * access times reset, we use this to restore the
    617 			 * access time for this directory since this is the
    618 			 * last time we will see it in this file subtree
    619 			 * remember to force the time (this is -t on a read
    620 			 * directory, not a created directory).
    621 			 */
    622 			if (!tflag || (get_atdir(
    623 #ifdef NET2_FTS
    624 			    ftent->fts_statb.st_dev, ftent->fts_statb.st_ino,
    625 #else
    626 			    ftent->fts_statp->st_dev, ftent->fts_statp->st_ino,
    627 #endif
    628 			    &mtime, &atime) < 0))
    629 				continue;
    630 			set_ftime(ftent->fts_path, mtime, atime, 1, 0);
    631 			continue;
    632 		case FTS_DC:
    633 			/*
    634 			 * fts claims a file system cycle
    635 			 */
    636 			tty_warn(1,"File system cycle found at %s",
    637 			    ftent->fts_path);
    638 			continue;
    639 		case FTS_DNR:
    640 			syswarn(1, FTS_ERRNO(ftent),
    641 			    "Unable to read directory %s", ftent->fts_path);
    642 			continue;
    643 		case FTS_ERR:
    644 			syswarn(1, FTS_ERRNO(ftent),
    645 			    "File system traversal error");
    646 			continue;
    647 		case FTS_NS:
    648 		case FTS_NSOK:
    649 			syswarn(1, FTS_ERRNO(ftent),
    650 			    "Unable to access %s", ftent->fts_path);
    651 			continue;
    652 		}
    653 
    654 #ifndef SMALL
    655  got_ftent:
    656 #endif	/* SMALL */
    657 		/*
    658 		 * ok got a file tree node to process. copy info into arcn
    659 		 * structure (initialize as required)
    660 		 */
    661 		arcn->skip = 0;
    662 		arcn->pad = 0;
    663 		arcn->ln_nlen = 0;
    664 		arcn->ln_name[0] = '\0';
    665 #ifdef NET2_FTS
    666 		arcn->sb = ftent->fts_statb;
    667 #else
    668 		arcn->sb = *(ftent->fts_statp);
    669 #endif
    670 
    671 		/*
    672 		 * file type based set up and copy into the arcn struct
    673 		 * SIDE NOTE:
    674 		 * we try to reset the access time on all files and directories
    675 		 * we may read when the -t flag is specified. files are reset
    676 		 * when we close them after copying. we reset the directories
    677 		 * when we are done with their file tree (we also clean up at
    678 		 * end in case we cut short a file tree traversal). However
    679 		 * there is no way to reset access times on symlinks.
    680 		 */
    681 		switch(S_IFMT & arcn->sb.st_mode) {
    682 		case S_IFDIR:
    683 			arcn->type = PAX_DIR;
    684 			if (!tflag)
    685 				break;
    686 			add_atdir(ftent->fts_path, arcn->sb.st_dev,
    687 			    arcn->sb.st_ino, arcn->sb.st_mtime,
    688 			    arcn->sb.st_atime);
    689 			break;
    690 		case S_IFCHR:
    691 			arcn->type = PAX_CHR;
    692 			break;
    693 		case S_IFBLK:
    694 			arcn->type = PAX_BLK;
    695 			break;
    696 		case S_IFREG:
    697 			/*
    698 			 * only regular files with have data to store on the
    699 			 * archive. all others will store a zero length skip.
    700 			 * the skip field is used by pax for actual data it has
    701 			 * to read (or skip over).
    702 			 */
    703 			arcn->type = PAX_REG;
    704 			arcn->skip = arcn->sb.st_size;
    705 			break;
    706 		case S_IFLNK:
    707 			arcn->type = PAX_SLK;
    708 			if (curlink != NULL) {
    709 				cnt = strlcpy(arcn->ln_name, curlink,
    710 				    sizeof(arcn->ln_name));
    711 			/*
    712 			 * have to read the symlink path from the file
    713 			 */
    714 			} else if ((cnt =
    715 			    readlink(ftent->fts_path, arcn->ln_name,
    716 			    sizeof(arcn->ln_name) - 1)) < 0) {
    717 				syswarn(1, errno, "Unable to read symlink %s",
    718 				    ftent->fts_path);
    719 				continue;
    720 			}
    721 			/*
    722 			 * set link name length, watch out readlink does not
    723 			 * always null terminate the link path
    724 			 */
    725 			arcn->ln_name[cnt] = '\0';
    726 			arcn->ln_nlen = cnt;
    727 			break;
    728 #ifdef S_IFSOCK
    729 		case S_IFSOCK:
    730 			/*
    731 			 * under BSD storing a socket is senseless but we will
    732 			 * let the format specific write function make the
    733 			 * decision of what to do with it.
    734 			 */
    735 			arcn->type = PAX_SCK;
    736 			break;
    737 #endif
    738 		case S_IFIFO:
    739 			arcn->type = PAX_FIF;
    740 			break;
    741 		}
    742 		break;
    743 	}
    744 
    745 	/*
    746 	 * copy file name, set file name length
    747 	 */
    748 	arcn->nlen = strlcpy(arcn->name, ftent->fts_path, sizeof(arcn->name));
    749 	arcn->org_name = arcn->fts_name;
    750 	strlcpy(arcn->fts_name, ftent->fts_path, sizeof arcn->fts_name);
    751 	if (strcmp(NM_CPIO, argv0) == 0) {
    752 		/*
    753 		 * cpio does *not* descend directories listed in the
    754 		 * arguments, unlike pax/tar, so needs special handling
    755 		 * here.  failure to do so results in massive amounts
    756 		 * of duplicated files in the output. We kill fts after
    757 		 * the first name is extracted, what a waste.
    758 		 */
    759 		ftcur->refcnt = 1;
    760 		(void)ftree_arg();
    761 	}
    762 	return 0;
    763 }
    764