1 /* $NetBSD: ftree.c,v 1.39 2008/04/28 20:22:51 martin Exp $ */
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
37 * Copyright (c) 2001 The NetBSD Foundation, Inc.
38 * All rights reserved.
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Luke Mewburn of Wasabi Systems.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
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.
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.
65 #if HAVE_NBTOOL_CONFIG_H
66 #include "nbtool_config.h"
69 #include <sys/cdefs.h>
72 static char sccsid
[] = "@(#)ftree.c 8.2 (Berkeley) 4/18/94";
74 __RCSID("$NetBSD: ftree.c,v 1.39 2008/04/28 20:22:51 martin Exp $");
78 #include <sys/types.h>
81 #include <sys/param.h>
98 * routines to interface with the fts library function.
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
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 */
120 static NODE
*ftnode
= NULL
; /* mtree(8) specfile; used by -M */
123 static int ftree_arg(void);
125 #define FTS_ERRNO(x) (x)->fts_errno
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.
134 * 0 if there is at least one valid file arg to process, -1 otherwise
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()
148 if (fthead
!= NULL
) {
150 "The -M flag is only supported when reading file list from stdin");
153 ftnode
= spec(stdin
);
154 if (ftnode
!= NULL
&&
155 (ftnode
->type
!= F_DIR
|| strcmp(ftnode
->name
, ".") != 0)) {
157 "First node of specfile is not `.' directory");
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.
170 ftsopts
= FTS_NOCHDIR
;
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.
183 ftsopts
|= FTS_LOGICAL
;
185 ftsopts
|= FTS_PHYSICAL
;
187 ftsopts
|= FTS_COMFOLLOW
;
191 if ((fthead
== NULL
) && ((farray
[0] = malloc(PAXPATHLEN
+2)) == NULL
)) {
192 tty_warn(1, "Unable to allocate memory for file name buffer");
198 if (tflag
&& (atdir_start() < 0))
205 * add the arg to the linked list of files to process. Each will be
206 * processed by fts one at a time
208 * 0 if added to the linked list, -1 if failed
212 ftree_add(char *str
, int isdir
)
218 * simple check for bad args
220 if ((str
== NULL
) || (*str
== '\0')) {
221 tty_warn(0, "Invalid file name argument");
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).
230 if ((ft
= (FTREE
*)malloc(sizeof(FTREE
))) == NULL
) {
231 tty_warn(0, "Unable to allocate memory for filename");
235 if (((len
= strlen(str
) - 1) > 0) && (str
[len
] == '/'))
240 if (fthead
== NULL
) {
241 fttail
= fthead
= ft
;
251 * this entry has been selected by pax. bump up reference count and handle
252 * -n and -d processing.
256 ftree_sel(ARCHD
*arcn
)
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.
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.
276 if (Mflag
|| !dflag
|| (arcn
->type
!= PAX_DIR
))
280 (void)fts_set(ftsp
, ftent
, FTS_SKIP
);
285 * called at end on pax execution. Prints all those file args that did not
286 * have a selected member (reference count still 0)
296 * make sure all dir access times were reset.
302 * walk down list and check reference count. Print out those members
303 * that never had a match
305 for (ft
= fthead
; ft
!= NULL
; ft
= ft
->fow
) {
310 "WARNING! These file names were not selected:");
313 (void)fprintf(stderr
, "%s\n", ft
->fname
);
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().
323 * 0 when the next arg is ready to go, -1 if out of file args (or EOF on
331 * close off the current file tree
334 (void)fts_close(ftsp
);
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)
344 if (fthead
== NULL
) {
347 * the user didn't supply any args, get the file trees
348 * to process from stdin;
350 for (i
= 0; i
< PAXPATHLEN
+ 2; i
++) {
365 * the user supplied the file args as arguments to pax
369 else if ((ftcur
= ftcur
->fow
) == NULL
)
372 if (ftcur
->refcnt
< 0) {
375 * Change directory and retry loop.
377 if (ar_dochdir(ftcur
->fname
))
381 farray
[0] = ftcur
->fname
;
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.
393 if ((ftsp
= fts_open(farray
, ftsopts
, NULL
)) != NULL
)
401 * supplies the next file to process in the supplied archd structure.
403 * 0 when contents of arcn have been set with the next file, -1 when done.
407 next_file(ARCHD
*arcn
)
410 static char curdir
[PAXPATHLEN
+2], curpath
[PAXPATHLEN
+2];
411 static int curdirlen
;
419 #define MFTENT_DUMMY_DEV UINT_MAX
424 * if parsing an mtree(8) specfile, build up `dummy' ftsent
425 * from specfile info, and jump below to complete setup of arcn.
432 if (ftnode
== NULL
) /* tree is empty */
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
));
443 ftnode
->flags
|= F_VISIT
; /* mark node visited */
445 /* construct dummy FTSENT */
446 Mftent
.fts_path
= curpath
;
447 Mftent
.fts_statp
= &statbuf
;
448 Mftent
.fts_pointer
= ftnode
;
450 /* look for existing file */
451 if (lstat(Mftent
.fts_path
, &statbuf
) == -1) {
452 if (ftnode
->flags
& F_OPT
)
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
;
460 #define NODETEST(t, m) \
462 tty_warn(1, "line %lu: %s: %s not specified", \
463 (u_long)ftnode->lineno, \
464 ftent->fts_path, m); \
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
,
477 if (ftnode
->type
== F_LINK
)
478 NODETEST(ftnode
->flags
& F_SLINK
, "symlink");
479 /* don't require F_FLAGS or F_SIZE */
482 if (ftnode
->flags
& F_TYPE
&& nodetoino(ftnode
->type
)
483 != (statbuf
.st_mode
& S_IFMT
)) {
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
));
491 if (ftnode
->type
== F_DIR
&& (ftnode
->flags
& F_OPT
))
495 * override settings with those from specfile
497 if (ftnode
->flags
& F_MODE
) {
498 statbuf
.st_mode
&= ~ALLPERMS
;
499 statbuf
.st_mode
|= (ftnode
->st_mode
& ALLPERMS
);
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
;
509 if (ftnode
->flags
& F_TIME
)
510 #if BSD4_4 && !HAVE_NBTOOL_CONFIG_H
511 statbuf
.st_mtimespec
= ftnode
->st_mtimespec
;
513 statbuf
.st_mtime
= ftnode
->st_mtimespec
.tv_sec
;
515 if (ftnode
->flags
& F_DEV
)
516 statbuf
.st_rdev
= ftnode
->st_rdev
;
517 if (ftnode
->flags
& F_SLINK
)
518 curlink
= ftnode
->slink
;
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
));
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
,
540 ftnode
= ftnode
->parent
;
541 if (ftnode
->parent
== ftnode
)
544 curdirlen
-= strlen(ftnode
->name
) + 1;
545 curdir
[curdirlen
] = '\0';
548 } while (ftnode
!= NULL
&& ftnode
->flags
& F_VISIT
);
549 if (skipoptional
) /* skip optional entries */
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.
563 * clear and go to next arg
573 * loop until we get a valid file to process
576 if ((ftent
= fts_read(ftsp
)) == NULL
) {
578 * out of files in this tree, go to next arg, if none
587 * handle each type of fts_read() flag
589 switch(ftent
->fts_info
) {
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).
608 if (!tflag
|| (get_atdir(
609 ftent
->fts_statp
->st_dev
, ftent
->fts_statp
->st_ino
,
610 &mtime
, &atime
) < 0))
612 set_ftime(ftent
->fts_path
, mtime
, atime
, 1, 0);
616 * fts claims a file system cycle
618 tty_warn(1,"File system cycle found at %s",
622 syswarn(1, FTS_ERRNO(ftent
),
623 "Unable to read directory %s", ftent
->fts_path
);
626 syswarn(1, FTS_ERRNO(ftent
),
627 "File system traversal error");
631 syswarn(1, FTS_ERRNO(ftent
),
632 "Unable to access %s", ftent
->fts_path
);
640 * ok got a file tree node to process. copy info into arcn
641 * structure (initialize as required)
646 arcn
->ln_name
[0] = '\0';
647 arcn
->sb
= *(ftent
->fts_statp
);
650 * file type based set up and copy into the arcn struct
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.
659 switch(S_IFMT
& arcn
->sb
.st_mode
) {
661 arcn
->type
= PAX_DIR
;
664 add_atdir(ftent
->fts_path
, arcn
->sb
.st_dev
,
665 arcn
->sb
.st_ino
, arcn
->sb
.st_mtime
,
669 arcn
->type
= PAX_CHR
;
672 arcn
->type
= PAX_BLK
;
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).
681 arcn
->type
= PAX_REG
;
682 arcn
->skip
= arcn
->sb
.st_size
;
685 arcn
->type
= PAX_SLK
;
686 if (curlink
!= NULL
) {
687 cnt
= strlcpy(arcn
->ln_name
, curlink
,
688 sizeof(arcn
->ln_name
));
690 * have to read the symlink path from the file
693 readlink(ftent
->fts_path
, arcn
->ln_name
,
694 sizeof(arcn
->ln_name
) - 1)) < 0) {
695 syswarn(1, errno
, "Unable to read symlink %s",
700 * set link name length, watch out readlink does not
701 * always null terminate the link path
703 arcn
->ln_name
[cnt
] = '\0';
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.
713 arcn
->type
= PAX_SCK
;
717 arcn
->type
= PAX_FIF
;
724 * copy file name, set file name length
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) {
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.