ftree.c revision 1.37 1 /* $NetBSD: ftree.c,v 1.37 2008/02/18 15:54:48 simonb 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.37 2008/02/18 15:54:48 simonb 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 ftent = NULL;
352 }
353
354 /*
355 * keep looping until we get a valid file tree to process. Stop when we
356 * reach the end of the list (or get an eof on stdin)
357 */
358 for(;;) {
359 if (fthead == NULL) {
360 int i, c = EOF;
361 /*
362 * the user didn't supply any args, get the file trees
363 * to process from stdin;
364 */
365 for (i = 0; i < PAXPATHLEN + 2; i++) {
366 c = getchar();
367 if (c == EOF)
368 break;
369 else if (c == sep) {
370 if (i != 0)
371 break;
372 } else
373 farray[0][i] = c;
374 }
375 if (i == 0)
376 return -1;
377 farray[0][i] = '\0';
378 } else {
379 /*
380 * the user supplied the file args as arguments to pax
381 */
382 if (ftcur == NULL)
383 ftcur = fthead;
384 else if ((ftcur = ftcur->fow) == NULL)
385 return -1;
386
387 if (ftcur->refcnt < 0) {
388 /*
389 * chdir entry.
390 * Change directory and retry loop.
391 */
392 if (ar_dochdir(ftcur->fname))
393 return (-1);
394 continue;
395 }
396 farray[0] = ftcur->fname;
397 }
398
399 /*
400 * watch it, fts wants the file arg stored in a array of char
401 * ptrs, with the last one a null. we use a two element array
402 * and set farray[0] to point at the buffer with the file name
403 * in it. We cannot pass all the file args to fts at one shot
404 * as we need to keep a handle on which file arg generates what
405 * files (the -n and -d flags need this). If the open is
406 * successful, return a 0.
407 */
408 if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
409 break;
410 }
411 return 0;
412 }
413
414 /*
415 * next_file()
416 * supplies the next file to process in the supplied archd structure.
417 * Return:
418 * 0 when contents of arcn have been set with the next file, -1 when done.
419 */
420
421 int
422 next_file(ARCHD *arcn)
423 {
424 #ifndef SMALL
425 static char curdir[PAXPATHLEN+2], curpath[PAXPATHLEN+2];
426 static int curdirlen;
427
428 struct stat statbuf;
429 FTSENT Mftent;
430 #endif /* SMALL */
431 int cnt;
432 time_t atime, mtime;
433 char *curlink;
434 #define MFTENT_DUMMY_DEV UINT_MAX
435
436 curlink = NULL;
437 #ifndef SMALL
438 /*
439 * if parsing an mtree(8) specfile, build up `dummy' ftsent
440 * from specfile info, and jump below to complete setup of arcn.
441 */
442 if (Mflag) {
443 int skipoptional;
444
445 next_ftnode:
446 skipoptional = 0;
447 if (ftnode == NULL) /* tree is empty */
448 return (-1);
449
450 /* get current name */
451 if (snprintf(curpath, sizeof(curpath), "%s%s%s",
452 curdir, curdirlen ? "/" : "", ftnode->name)
453 >= sizeof(curpath)) {
454 tty_warn(1, "line %lu: %s: %s", (u_long)ftnode->lineno,
455 curdir, strerror(ENAMETOOLONG));
456 return (-1);
457 }
458 ftnode->flags |= F_VISIT; /* mark node visited */
459
460 /* construct dummy FTSENT */
461 Mftent.fts_path = curpath;
462 Mftent.fts_statp = &statbuf;
463 Mftent.fts_pointer = ftnode;
464 ftent = &Mftent;
465 /* look for existing file */
466 if (lstat(Mftent.fts_path, &statbuf) == -1) {
467 if (ftnode->flags & F_OPT)
468 skipoptional = 1;
469
470 /* missing: fake up stat info */
471 memset(&statbuf, 0, sizeof(statbuf));
472 statbuf.st_dev = MFTENT_DUMMY_DEV;
473 statbuf.st_ino = ftnode->lineno;
474 statbuf.st_size = 0;
475 #define NODETEST(t, m) \
476 if (!(t)) { \
477 tty_warn(1, "line %lu: %s: %s not specified", \
478 (u_long)ftnode->lineno, \
479 ftent->fts_path, m); \
480 return -1; \
481 }
482 statbuf.st_mode = nodetoino(ftnode->type);
483 NODETEST(ftnode->flags & F_TYPE, "type");
484 NODETEST(ftnode->flags & F_MODE, "mode");
485 if (!(ftnode->flags & F_TIME))
486 statbuf.st_mtime = starttime;
487 NODETEST(ftnode->flags & (F_GID | F_GNAME), "group");
488 NODETEST(ftnode->flags & (F_UID | F_UNAME), "user");
489 if (ftnode->type == F_BLOCK || ftnode->type == F_CHAR)
490 NODETEST(ftnode->flags & F_DEV,
491 "device number");
492 if (ftnode->type == F_LINK)
493 NODETEST(ftnode->flags & F_SLINK, "symlink");
494 /* don't require F_FLAGS or F_SIZE */
495 #undef NODETEST
496 } else {
497 if (ftnode->flags & F_TYPE && nodetoino(ftnode->type)
498 != (statbuf.st_mode & S_IFMT)) {
499 tty_warn(1,
500 "line %lu: %s: type mismatch: specfile %s, tree %s",
501 (u_long)ftnode->lineno, ftent->fts_path,
502 inotype(nodetoino(ftnode->type)),
503 inotype(statbuf.st_mode));
504 return -1;
505 }
506 if (ftnode->type == F_DIR && (ftnode->flags & F_OPT))
507 skipoptional = 1;
508 }
509 /*
510 * override settings with those from specfile
511 */
512 if (ftnode->flags & F_MODE) {
513 statbuf.st_mode &= ~ALLPERMS;
514 statbuf.st_mode |= (ftnode->st_mode & ALLPERMS);
515 }
516 if (ftnode->flags & (F_GID | F_GNAME))
517 statbuf.st_gid = ftnode->st_gid;
518 if (ftnode->flags & (F_UID | F_UNAME))
519 statbuf.st_uid = ftnode->st_uid;
520 #if HAVE_STRUCT_STAT_ST_FLAGS
521 if (ftnode->flags & F_FLAGS)
522 statbuf.st_flags = ftnode->st_flags;
523 #endif
524 if (ftnode->flags & F_TIME)
525 #if BSD4_4 && !HAVE_NBTOOL_CONFIG_H
526 statbuf.st_mtimespec = ftnode->st_mtimespec;
527 #else
528 statbuf.st_mtime = ftnode->st_mtimespec.tv_sec;
529 #endif
530 if (ftnode->flags & F_DEV)
531 statbuf.st_rdev = ftnode->st_rdev;
532 if (ftnode->flags & F_SLINK)
533 curlink = ftnode->slink;
534 /* ignore F_SIZE */
535
536 /*
537 * find next node
538 */
539 if (ftnode->type == F_DIR && ftnode->child != NULL) {
540 /* directory with unseen child */
541 ftnode = ftnode->child;
542 curdirlen = strlcpy(curdir, curpath, sizeof(curdir));
543 } else do {
544 if (ftnode->next != NULL) {
545 /* next node at current level */
546 ftnode = ftnode->next;
547 } else { /* move back to parent */
548 /* reset time only on first cd.. */
549 if (Mftent.fts_pointer == ftnode && tflag &&
550 (get_atdir(MFTENT_DUMMY_DEV, ftnode->lineno,
551 &mtime, &atime) == 0)) {
552 set_ftime(ftent->fts_path,
553 mtime, atime, 1, 0);
554 }
555 ftnode = ftnode->parent;
556 if (ftnode->parent == ftnode)
557 ftnode = NULL;
558 else {
559 curdirlen -= strlen(ftnode->name) + 1;
560 curdir[curdirlen] = '\0';
561 }
562 }
563 } while (ftnode != NULL && ftnode->flags & F_VISIT);
564 if (skipoptional) /* skip optional entries */
565 goto next_ftnode;
566 goto got_ftent;
567 }
568 #endif /* SMALL */
569
570 /*
571 * ftree_sel() might have set the ftree_skip flag if the user has the
572 * -n option and a file was selected from this file arg tree. (-n says
573 * only one member is matched for each pattern) ftree_skip being 1
574 * forces us to go to the next arg now.
575 */
576 if (ftree_skip) {
577 /*
578 * clear and go to next arg
579 */
580 ftree_skip = 0;
581 if (ftree_arg() < 0)
582 return -1;
583 }
584
585 if (ftsp == NULL)
586 return -1;
587 /*
588 * loop until we get a valid file to process
589 */
590 for(;;) {
591 if ((ftent = fts_read(ftsp)) == NULL) {
592 /*
593 * out of files in this tree, go to next arg, if none
594 * we are done
595 */
596 if (ftree_arg() < 0)
597 return -1;
598 continue;
599 }
600
601 /*
602 * handle each type of fts_read() flag
603 */
604 switch(ftent->fts_info) {
605 case FTS_D:
606 case FTS_DEFAULT:
607 case FTS_F:
608 case FTS_SL:
609 case FTS_SLNONE:
610 /*
611 * these are all ok
612 */
613 break;
614 case FTS_DP:
615 /*
616 * already saw this directory. If the user wants file
617 * access times reset, we use this to restore the
618 * access time for this directory since this is the
619 * last time we will see it in this file subtree
620 * remember to force the time (this is -t on a read
621 * directory, not a created directory).
622 */
623 if (!tflag || (get_atdir(
624 #ifdef NET2_FTS
625 ftent->fts_statb.st_dev, ftent->fts_statb.st_ino,
626 #else
627 ftent->fts_statp->st_dev, ftent->fts_statp->st_ino,
628 #endif
629 &mtime, &atime) < 0))
630 continue;
631 set_ftime(ftent->fts_path, mtime, atime, 1, 0);
632 continue;
633 case FTS_DC:
634 /*
635 * fts claims a file system cycle
636 */
637 tty_warn(1,"File system cycle found at %s",
638 ftent->fts_path);
639 continue;
640 case FTS_DNR:
641 syswarn(1, FTS_ERRNO(ftent),
642 "Unable to read directory %s", ftent->fts_path);
643 continue;
644 case FTS_ERR:
645 syswarn(1, FTS_ERRNO(ftent),
646 "File system traversal error");
647 continue;
648 case FTS_NS:
649 case FTS_NSOK:
650 syswarn(1, FTS_ERRNO(ftent),
651 "Unable to access %s", ftent->fts_path);
652 continue;
653 }
654
655 #ifndef SMALL
656 got_ftent:
657 #endif /* SMALL */
658 /*
659 * ok got a file tree node to process. copy info into arcn
660 * structure (initialize as required)
661 */
662 arcn->skip = 0;
663 arcn->pad = 0;
664 arcn->ln_nlen = 0;
665 arcn->ln_name[0] = '\0';
666 #ifdef NET2_FTS
667 arcn->sb = ftent->fts_statb;
668 #else
669 arcn->sb = *(ftent->fts_statp);
670 #endif
671
672 /*
673 * file type based set up and copy into the arcn struct
674 * SIDE NOTE:
675 * we try to reset the access time on all files and directories
676 * we may read when the -t flag is specified. files are reset
677 * when we close them after copying. we reset the directories
678 * when we are done with their file tree (we also clean up at
679 * end in case we cut short a file tree traversal). However
680 * there is no way to reset access times on symlinks.
681 */
682 switch(S_IFMT & arcn->sb.st_mode) {
683 case S_IFDIR:
684 arcn->type = PAX_DIR;
685 if (!tflag)
686 break;
687 add_atdir(ftent->fts_path, arcn->sb.st_dev,
688 arcn->sb.st_ino, arcn->sb.st_mtime,
689 arcn->sb.st_atime);
690 break;
691 case S_IFCHR:
692 arcn->type = PAX_CHR;
693 break;
694 case S_IFBLK:
695 arcn->type = PAX_BLK;
696 break;
697 case S_IFREG:
698 /*
699 * only regular files with have data to store on the
700 * archive. all others will store a zero length skip.
701 * the skip field is used by pax for actual data it has
702 * to read (or skip over).
703 */
704 arcn->type = PAX_REG;
705 arcn->skip = arcn->sb.st_size;
706 break;
707 case S_IFLNK:
708 arcn->type = PAX_SLK;
709 if (curlink != NULL) {
710 cnt = strlcpy(arcn->ln_name, curlink,
711 sizeof(arcn->ln_name));
712 /*
713 * have to read the symlink path from the file
714 */
715 } else if ((cnt =
716 readlink(ftent->fts_path, arcn->ln_name,
717 sizeof(arcn->ln_name) - 1)) < 0) {
718 syswarn(1, errno, "Unable to read symlink %s",
719 ftent->fts_path);
720 continue;
721 }
722 /*
723 * set link name length, watch out readlink does not
724 * always null terminate the link path
725 */
726 arcn->ln_name[cnt] = '\0';
727 arcn->ln_nlen = cnt;
728 break;
729 #ifdef S_IFSOCK
730 case S_IFSOCK:
731 /*
732 * under BSD storing a socket is senseless but we will
733 * let the format specific write function make the
734 * decision of what to do with it.
735 */
736 arcn->type = PAX_SCK;
737 break;
738 #endif
739 case S_IFIFO:
740 arcn->type = PAX_FIF;
741 break;
742 }
743 break;
744 }
745
746 /*
747 * copy file name, set file name length
748 */
749 arcn->nlen = strlcpy(arcn->name, ftent->fts_path, sizeof(arcn->name));
750 arcn->org_name = arcn->fts_name;
751 strlcpy(arcn->fts_name, ftent->fts_path, sizeof arcn->fts_name);
752 if (strcmp(NM_CPIO, argv0) == 0) {
753 /*
754 * cpio does *not* descend directories listed in the
755 * arguments, unlike pax/tar, so needs special handling
756 * here. failure to do so results in massive amounts
757 * of duplicated files in the output. We kill fts after
758 * the first name is extracted, what a waste.
759 */
760 ftcur->refcnt = 1;
761 (void)ftree_arg();
762 }
763 return 0;
764 }
765