2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)ftree.c 8.2 (Berkeley) 4/18/94";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
56 * routines to interface with the fts library function.
58 * file args supplied to pax are stored on a single linked list (of type FTREE)
59 * and given to fts to be processed one at a time. pax "selects" files from
60 * the expansion of each arg into the corresponding file tree (if the arg is a
61 * directory, otherwise the node itself is just passed to pax). The selection
62 * is modified by the -n and -u flags. The user is informed when a specific
63 * file arg does not generate any selected files. -n keeps expanding the file
64 * tree arg until one of its files is selected, then skips to the next file
65 * arg. when the user does not supply the file trees as command line args to
66 * pax, they are read from stdin
69 static FTS *ftsp = NULL; /* current FTS handle */
70 static int ftsopts; /* options to be used on fts_open */
71 static char *farray[2]; /* array for passing each arg to fts */
72 static FTREE *fthead = NULL; /* head of linked list of file args */
73 static FTREE *fttail = NULL; /* tail of linked list of file args */
74 static FTREE *ftcur = NULL; /* current file arg being processed */
75 static FTSENT *ftent = NULL; /* current file tree entry */
76 static int ftree_skip; /* when set skip to next file arg */
78 static int ftree_arg(void);
82 * initialize the options passed to fts_open() during this run of pax
83 * options are based on the selection of pax options by the user
84 * fts_start() also calls fts_arg() to open the first valid file arg. We
85 * also attempt to reset directory access times when -t (tflag) is set.
87 * 0 if there is at least one valid file arg to process, -1 otherwise
94 * Set up the operation mode of fts, open the first file arg. We must
95 * use FTS_NOCHDIR, as the user may have to open multiple archives and
96 * if fts did a chdir off into the boondocks, we may create an archive
97 * volume in a place where the user did not expect to.
99 ftsopts = FTS_NOCHDIR;
102 * optional user flags that effect file traversal
103 * -H command line symlink follow only (half follow)
104 * -L follow symlinks (logical)
105 * -P do not follow symlinks (physical). This is the default.
106 * -X do not cross over mount points
107 * -t preserve access times on files read.
108 * -n select only the first member of a file tree when a match is found
109 * -d do not extract subtrees rooted at a directory arg.
112 ftsopts |= FTS_LOGICAL;
114 ftsopts |= FTS_PHYSICAL;
117 paxwarn(0, "The -H flag is not supported on this version");
119 ftsopts |= FTS_COMFOLLOW;
124 if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
125 paxwarn(1, "Unable to allocate memory for file name buffer");
131 if (tflag && (atdir_start() < 0))
138 * add the arg to the linked list of files to process. Each will be
139 * processed by fts one at a time
141 * 0 if added to the linked list, -1 if failed
145 ftree_add(char *str, int chflg)
151 * simple check for bad args
153 if ((str == NULL) || (*str == '\0')) {
154 paxwarn(0, "Invalid file name argument");
159 * allocate FTREE node and add to the end of the linked list (args are
160 * processed in the same order they were passed to pax). Get rid of any
161 * trailing / the user may pass us. (watch out for / by itself).
163 if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
164 paxwarn(0, "Unable to allocate memory for filename");
168 if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
174 if (fthead == NULL) {
175 fttail = fthead = ft;
185 * this entry has been selected by pax. bump up reference count and handle
186 * -n and -d processing.
190 ftree_sel(ARCHD *arcn)
193 * set reference bit for this pattern. This linked list is only used
194 * when file trees are supplied pax as args. The list is not used when
195 * the trees are read from stdin.
201 * if -n we are done with this arg, force a skip to the next arg when
202 * pax asks for the next file in next_file().
203 * if -d we tell fts only to match the directory (if the arg is a dir)
204 * and not the entire file tree rooted at that point.
209 if (!dflag || (arcn->type != PAX_DIR))
213 (void)fts_set(ftsp, ftent, FTS_SKIP);
218 * this entry has not been selected by pax.
225 (void)fts_set(ftsp, ftent, FTS_SKIP);
230 * called at end on pax execution. Prints all those file args that did not
231 * have a selected member (reference count still 0)
241 * make sure all dir access times were reset.
247 * walk down list and check reference count. Print out those members
248 * that never had a match
250 for (ft = fthead; ft != NULL; ft = ft->fow) {
251 if ((ft->refcnt > 0) || ft->chflg)
254 paxwarn(1,"WARNING! These file names were not selected:");
257 (void)fprintf(stderr, "%s\n", ft->fname);
263 * Get the next file arg for fts to process. Can be from either the linked
264 * list or read from stdin when the user did not them as args to pax. Each
265 * arg is processed until the first successful fts_open().
267 * 0 when the next arg is ready to go, -1 if out of file args (or EOF on
277 * close off the current file tree
280 (void)fts_close(ftsp);
285 * keep looping until we get a valid file tree to process. Stop when we
286 * reach the end of the list (or get an eof on stdin)
289 if (fthead == NULL) {
291 * the user didn't supply any args, get the file trees
292 * to process from stdin;
294 if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
296 if ((pt = strchr(farray[0], '\n')) != NULL)
300 * the user supplied the file args as arguments to pax
304 else if ((ftcur = ftcur->fow) == NULL)
307 /* First fchdir() back... */
308 if (fchdir(cwdfd) < 0) {
310 "Can't fchdir to starting directory");
313 if (chdir(ftcur->fname) < 0) {
314 syswarn(1, errno, "Can't chdir to %s",
320 farray[0] = ftcur->fname;
324 * Watch it, fts wants the file arg stored in an array of char
325 * ptrs, with the last one a null. We use a two element array
326 * and set farray[0] to point at the buffer with the file name
327 * in it. We cannot pass all the file args to fts at one shot
328 * as we need to keep a handle on which file arg generates what
329 * files (the -n and -d flags need this). If the open is
330 * successful, return a 0.
332 if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
340 * supplies the next file to process in the supplied archd structure.
342 * 0 when contents of arcn have been set with the next file, -1 when done.
346 next_file(ARCHD *arcn)
353 * ftree_sel() might have set the ftree_skip flag if the user has the
354 * -n option and a file was selected from this file arg tree. (-n says
355 * only one member is matched for each pattern) ftree_skip being 1
356 * forces us to go to the next arg now.
360 * clear and go to next arg
368 * loop until we get a valid file to process
371 if ((ftent = fts_read(ftsp)) == NULL) {
373 * out of files in this tree, go to next arg, if none
382 * handle each type of fts_read() flag
384 switch(ftent->fts_info) {
396 * already saw this directory. If the user wants file
397 * access times reset, we use this to restore the
398 * access time for this directory since this is the
399 * last time we will see it in this file subtree
400 * remember to force the time (this is -t on a read
401 * directory, not a created directory).
404 if (!tflag || (get_atdir(ftent->fts_statb.st_dev,
405 ftent->fts_statb.st_ino, &mtime, &atime) < 0))
407 if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
408 ftent->fts_statp->st_ino, &mtime, &atime) < 0))
411 set_ftime(ftent->fts_path, mtime, atime, 1);
415 * fts claims a file system cycle
417 paxwarn(1,"File system cycle found at %s",ftent->fts_path);
423 syswarn(1, ftent->fts_errno,
425 "Unable to read directory %s", ftent->fts_path);
431 syswarn(1, ftent->fts_errno,
433 "File system traversal error");
440 syswarn(1, ftent->fts_errno,
442 "Unable to access %s", ftent->fts_path);
447 * ok got a file tree node to process. copy info into arcn
448 * structure (initialize as required)
453 arcn->ln_name[0] = '\0';
455 arcn->sb = ftent->fts_statb;
457 arcn->sb = *(ftent->fts_statp);
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,
501 PAXPATHLEN - 1)) < 0) {
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 = l_strncpy(arcn->name, ftent->fts_path, sizeof(arcn->name) - 1);
532 arcn->name[arcn->nlen] = '\0';
533 arcn->org_name = ftent->fts_path;