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