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