]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/ftree.c
1 /* $OpenBSD: ftree.c,v 1.28 2008/05/06 06:54:28 henning Exp $ */
2 /* $NetBSD: ftree.c,v 1.4 1995/03/21 09:07:21 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid
[] = "@(#)ftree.c 8.2 (Berkeley) 4/18/94";
41 static const char rcsid
[] = "$OpenBSD: ftree.c,v 1.28 2008/05/06 06:54:28 henning Exp $";
45 #include <sys/types.h>
48 #include <sys/param.h>
60 * routines to interface with the fts library function.
62 * file args supplied to pax are stored on a single linked list (of type FTREE)
63 * and given to fts to be processed one at a time. pax "selects" files from
64 * the expansion of each arg into the corresponding file tree (if the arg is a
65 * directory, otherwise the node itself is just passed to pax). The selection
66 * is modified by the -n and -u flags. The user is informed when a specific
67 * file arg does not generate any selected files. -n keeps expanding the file
68 * tree arg until one of its files is selected, then skips to the next file
69 * arg. when the user does not supply the file trees as command line args to
70 * pax, they are read from stdin
73 static FTS
*ftsp
= NULL
; /* current FTS handle */
74 static int ftsopts
; /* options to be used on fts_open */
75 static char *farray
[2]; /* array for passing each arg to fts */
76 static FTREE
*fthead
= NULL
; /* head of linked list of file args */
77 static FTREE
*fttail
= NULL
; /* tail of linked list of file args */
78 static FTREE
*ftcur
= NULL
; /* current file arg being processed */
79 static FTSENT
*ftent
= NULL
; /* current file tree entry */
80 static int ftree_skip
; /* when set skip to next file arg */
82 static int ftree_arg(void);
83 static char *getpathname(char *, int);
87 * initialize the options passed to fts_open() during this run of pax
88 * options are based on the selection of pax options by the user
89 * fts_start() also calls fts_arg() to open the first valid file arg. We
90 * also attempt to reset directory access times when -t (tflag) is set.
92 * 0 if there is at least one valid file arg to process, -1 otherwise
99 * set up the operation mode of fts, open the first file arg. We must
100 * use FTS_NOCHDIR, as the user may have to open multiple archives and
101 * if fts did a chdir off into the boondocks, we may create an archive
102 * volume in an place where the user did not expect to.
104 ftsopts
= FTS_NOCHDIR
;
107 * optional user flags that effect file traversal
108 * -H command line symlink follow only (half follow)
109 * -L follow sylinks (logical)
110 * -P do not follow sylinks (physical). This is the default.
111 * -X do not cross over mount points
112 * -t preserve access times on files read.
113 * -n select only the first member of a file tree when a match is found
114 * -d do not extract subtrees rooted at a directory arg.
117 ftsopts
|= FTS_LOGICAL
;
119 ftsopts
|= FTS_PHYSICAL
;
121 ftsopts
|= FTS_COMFOLLOW
;
125 if ((fthead
== NULL
) && ((farray
[0] = malloc(PAXPATHLEN
+2)) == NULL
)) {
126 paxwarn(1, "Unable to allocate memory for file name buffer");
132 if (tflag
&& (atdir_start() < 0))
139 * add the arg to the linked list of files to process. Each will be
140 * processed by fts one at a time
142 * 0 if added to the linked list, -1 if failed
146 ftree_add(char *str
, int chflg
)
152 * simple check for bad args
154 if ((str
== NULL
) || (*str
== '\0')) {
155 paxwarn(0, "Invalid file name argument");
160 * allocate FTREE node and add to the end of the linked list (args are
161 * processed in the same order they were passed to pax). Get rid of any
162 * trailing / the user may pass us. (watch out for / by itself).
164 if ((ft
= (FTREE
*)malloc(sizeof(FTREE
))) == NULL
) {
165 paxwarn(0, "Unable to allocate memory for filename");
169 if (((len
= strlen(str
) - 1) > 0) && (str
[len
] == '/'))
176 if (fthead
== NULL
) {
177 fttail
= fthead
= ft
;
187 * this entry has been selected by pax. bump up reference count and handle
188 * -n and -d processing.
192 ftree_sel(ARCHD
*arcn
)
195 * set reference bit for this pattern. This linked list is only used
196 * when file trees are supplied pax as args. The list is not used when
197 * the trees are read from stdin.
203 * if -n we are done with this arg, force a skip to the next arg when
204 * pax asks for the next file in next_file().
205 * if -d we tell fts only to match the directory (if the arg is a dir)
206 * and not the entire file tree rooted at that point.
211 if (!dflag
|| (arcn
->type
!= PAX_DIR
))
215 (void)fts_set(ftsp
, ftent
, FTS_SKIP
);
220 * this entry has not been selected by pax.
227 (void)fts_set(ftsp
, ftent
, FTS_SKIP
);
231 * ftree_skipped_newer()
232 * file has been skipped because a newer file exists and -u/-D given
236 ftree_skipped_newer(ARCHD
*arcn
)
238 /* skipped due to -u/-D, mark accordingly */
245 * called at end on pax execution. Prints all those file args that did not
246 * have a selected member (reference count still 0)
256 * make sure all dir access times were reset.
262 * walk down list and check reference count. Print out those members
263 * that never had a match
265 for (ft
= fthead
; ft
!= NULL
; ft
= ft
->fow
) {
266 if ((ft
->refcnt
> 0) || ft
->newercnt
> 0 || ft
->chflg
)
269 paxwarn(1,"WARNING! These file names were not selected:");
272 (void)fprintf(stderr
, "%s\n", ft
->fname
);
278 * Get the next file arg for fts to process. Can be from either the linked
279 * list or read from stdin when the user did not them as args to pax. Each
280 * arg is processed until the first successful fts_open().
282 * 0 when the next arg is ready to go, -1 if out of file args (or EOF on
291 * close off the current file tree
294 (void)fts_close(ftsp
);
299 * keep looping until we get a valid file tree to process. Stop when we
300 * reach the end of the list (or get an eof on stdin)
303 if (fthead
== NULL
) {
305 * the user didn't supply any args, get the file trees
306 * to process from stdin;
308 if (getpathname(farray
[0], PAXPATHLEN
+1) == NULL
)
312 * the user supplied the file args as arguments to pax
316 else if ((ftcur
= ftcur
->fow
) == NULL
)
319 /* First fchdir() back... */
320 if (fdochdir(cwdfd
) < 0) {
322 "Can't fchdir to starting directory");
325 if (dochdir(ftcur
->fname
) < 0) {
326 syswarn(1, errno
, "Can't chdir to %s",
332 farray
[0] = ftcur
->fname
;
336 * watch it, fts wants the file arg stored in a array of char
337 * ptrs, with the last one a null. we use a two element array
338 * and set farray[0] to point at the buffer with the file name
339 * in it. We cannot pass all the file args to fts at one shot
340 * as we need to keep a handle on which file arg generates what
341 * files (the -n and -d flags need this). If the open is
342 * successful, return a 0.
344 if ((ftsp
= fts_open(farray
, ftsopts
, NULL
)) != NULL
)
352 * supplies the next file to process in the supplied archd structure.
354 * 0 when contents of arcn have been set with the next file, -1 when done.
358 next_file(ARCHD
*arcn
)
365 * ftree_sel() might have set the ftree_skip flag if the user has the
366 * -n option and a file was selected from this file arg tree. (-n says
367 * only one member is matched for each pattern) ftree_skip being 1
368 * forces us to go to the next arg now.
372 * clear and go to next arg
380 * loop until we get a valid file to process
383 if ((ftent
= fts_read(ftsp
)) == NULL
) {
385 syswarn(1, errno
, "next_file");
387 * out of files in this tree, go to next arg, if none
396 * handle each type of fts_read() flag
398 switch (ftent
->fts_info
) {
407 case FTS_SLNONE
: /* was same as above cases except Unix
408 conformance requires this error check */
409 if (Hflag
|| Lflag
) { /* -H or -L was specified */
410 if (ftent
->fts_errno
)
412 ftent
->fts_name
, strerror(ftent
->fts_errno
));
417 * already saw this directory. If the user wants file
418 * access times reset, we use this to restore the
419 * access time for this directory since this is the
420 * last time we will see it in this file subtree
421 * remember to force the time (this is -t on a read
422 * directory, not a created directory).
424 if (!tflag
|| (get_atdir(ftent
->fts_statp
->st_dev
,
425 ftent
->fts_statp
->st_ino
, &mtime
, &atime
) < 0))
427 set_ftime(ftent
->fts_path
, mtime
, atime
, 1);
431 * fts claims a file system cycle
433 paxwarn(1,"File system cycle found at %s",ftent
->fts_path
);
436 syswarn(1, ftent
->fts_errno
,
437 "Unable to read directory %s", ftent
->fts_path
);
440 syswarn(1, ftent
->fts_errno
,
441 "File system traversal error");
445 syswarn(1, ftent
->fts_errno
,
446 "Unable to access %s", ftent
->fts_path
);
451 * ok got a file tree node to process. copy info into arcn
452 * structure (initialize as required)
457 arcn
->ln_name
[0] = '\0';
458 memcpy(&arcn
->sb
, ftent
->fts_statp
, sizeof(arcn
->sb
));
461 * file type based set up and copy into the arcn struct
463 * we try to reset the access time on all files and directories
464 * we may read when the -t flag is specified. files are reset
465 * when we close them after copying. we reset the directories
466 * when we are done with their file tree (we also clean up at
467 * end in case we cut short a file tree traversal). However
468 * there is no way to reset access times on symlinks.
470 switch (S_IFMT
& arcn
->sb
.st_mode
) {
472 arcn
->type
= PAX_DIR
;
475 add_atdir(ftent
->fts_path
, arcn
->sb
.st_dev
,
476 arcn
->sb
.st_ino
, arcn
->sb
.st_mtime
,
480 arcn
->type
= PAX_CHR
;
483 arcn
->type
= PAX_BLK
;
487 * only regular files with have data to store on the
488 * archive. all others will store a zero length skip.
489 * the skip field is used by pax for actual data it has
490 * to read (or skip over).
492 arcn
->type
= PAX_REG
;
493 arcn
->skip
= arcn
->sb
.st_size
;
496 arcn
->type
= PAX_SLK
;
498 * have to read the symlink path from the file
500 if ((cnt
= readlink(ftent
->fts_path
, arcn
->ln_name
,
502 syswarn(1, errno
, "Unable to read symlink %s",
507 * set link name length, watch out readlink does not
508 * always NUL terminate the link path
510 arcn
->ln_name
[cnt
] = '\0';
515 * under BSD storing a socket is senseless but we will
516 * let the format specific write function make the
517 * decision of what to do with it.
519 arcn
->type
= PAX_SCK
;
522 arcn
->type
= PAX_FIF
;
529 * copy file name, set file name length
531 arcn
->nlen
= strlcpy(arcn
->name
, ftent
->fts_path
, sizeof(arcn
->name
));
532 if (arcn
->nlen
>= sizeof(arcn
->name
))
533 arcn
->nlen
= sizeof(arcn
->name
) - 1; /* XXX truncate? */
534 arcn
->org_name
= ftent
->fts_path
;
540 * Reads a pathname from stdin, handling NUL- or newline-termination.
542 * NULL at end of file, otherwise the NUL-terminated buffer.
546 getpathname(char *buf
, int buflen
)
553 * Read a NUL-terminated pathname, being especially
554 * paranoid about proper termination and pathname length.
556 for (bp
= buf
, ep
= buf
+ buflen
; bp
< ep
; bp
++) {
557 if ((ch
= getchar()) == EOF
) {
559 paxwarn(1, "Ignoring unterminated "
563 if ((*bp
= ch
) == '\0')
566 /* Too long - skip this path */
570 if (fgets(buf
, buflen
, stdin
) == NULL
)
572 if ((bp
= strchr(buf
, '\n')) != NULL
|| feof(stdin
)) {
577 /* Too long - skip this path */
580 while ((ch
= getchar()) != term
&& ch
!= EOF
)
582 paxwarn(1, "Ignoring too-long pathname: %s", buf
);