Home | History | Annotate | Line # | Download | only in pax
ftree.c revision 1.11
      1 /*	$NetBSD: ftree.c,v 1.11 2001/10/25 05:33:33 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. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)ftree.c	8.2 (Berkeley) 4/18/94";
     44 #else
     45 __RCSID("$NetBSD: ftree.c,v 1.11 2001/10/25 05:33:33 lukem Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/time.h>
     51 #include <sys/stat.h>
     52 #include <sys/param.h>
     53 #include <unistd.h>
     54 #include <string.h>
     55 #include <stdio.h>
     56 #include <ctype.h>
     57 #include <errno.h>
     58 #include <stdlib.h>
     59 #include <fts.h>
     60 #include "pax.h"
     61 #include "ftree.h"
     62 #include "extern.h"
     63 
     64 /*
     65  * routines to interface with the fts library function.
     66  *
     67  * file args supplied to pax are stored on a single linked list (of type FTREE)
     68  * and given to fts to be processed one at a time. pax "selects" files from
     69  * the expansion of each arg into the corresponding file tree (if the arg is a
     70  * directory, otherwise the node itself is just passed to pax). The selection
     71  * is modified by the -n and -u flags. The user is informed when a specific
     72  * file arg does not generate any selected files. -n keeps expanding the file
     73  * tree arg until one of its files is selected, then skips to the next file
     74  * arg. when the user does not supply the file trees as command line args to
     75  * pax, they are read from stdin
     76  */
     77 
     78 static FTS *ftsp = NULL;		/* current FTS handle */
     79 static int ftsopts;			/* options to be used on fts_open */
     80 static char *farray[2];			/* array for passing each arg to fts */
     81 static FTREE *fthead = NULL;		/* head of linked list of file args */
     82 static FTREE *fttail = NULL;		/* tail of linked list of file args */
     83 static FTREE *ftcur = NULL;		/* current file arg being processed */
     84 static FTSENT *ftent = NULL;		/* current file tree entry */
     85 static int ftree_skip;			/* when set skip to next file arg */
     86 
     87 static int ftree_arg(void);
     88 
     89 #ifdef NET2_FTS
     90 #define	FTS_ERRNO(x)	errno
     91 #else
     92 #define	FTS_ERRNO(x)	(x)->fts_errno
     93 #endif
     94 
     95 /*
     96  * ftree_start()
     97  *	initialize the options passed to fts_open() during this run of pax
     98  *	options are based on the selection of pax options by the user
     99  *	fts_start() also calls fts_arg() to open the first valid file arg. We
    100  *	also attempt to reset directory access times when -t (tflag) is set.
    101  * Return:
    102  *	0 if there is at least one valid file arg to process, -1 otherwise
    103  */
    104 
    105 int
    106 ftree_start(void)
    107 {
    108 	/*
    109 	 * set up the operation mode of fts, open the first file arg. We must
    110 	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
    111 	 * if fts did a chdir off into the boondocks, we may create an archive
    112 	 * volume in an place where the user did not expect to.
    113 	 */
    114 	ftsopts = FTS_NOCHDIR;
    115 
    116 	/*
    117 	 * optional user flags that effect file traversal
    118 	 * -H command line symlink follow only (half follow)
    119 	 * -L follow sylinks (logical)
    120 	 * -P do not follow sylinks (physical). This is the default.
    121 	 * -X do not cross over mount points
    122 	 * -t preserve access times on files read.
    123 	 * -n select only the first member of a file tree when a match is found
    124 	 * -d do not extract subtrees rooted at a directory arg.
    125 	 */
    126 	if (Lflag)
    127 		ftsopts |= FTS_LOGICAL;
    128 	else
    129 		ftsopts |= FTS_PHYSICAL;
    130 	if (Hflag)
    131 #ifdef NET2_FTS
    132 		tty_warn(0, "The -H flag is not supported on this version");
    133 #else
    134 		ftsopts |= FTS_COMFOLLOW;
    135 #endif
    136 	if (Xflag)
    137 		ftsopts |= FTS_XDEV;
    138 
    139 	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
    140 		tty_warn(1, "Unable to allocate memory for file name buffer");
    141 		return(-1);
    142 	}
    143 
    144 	if (ftree_arg() < 0)
    145 		return(-1);
    146 	if (tflag && (atdir_start() < 0))
    147 		return(-1);
    148 	return(0);
    149 }
    150 
    151 /*
    152  * ftree_add()
    153  *	add the arg to the linked list of files to process. Each will be
    154  *	processed by fts one at a time
    155  * Return:
    156  *	0 if added to the linked list, -1 if failed
    157  */
    158 
    159 int
    160 ftree_add(char *str, int isdir)
    161 {
    162 	FTREE *ft;
    163 	int len;
    164 
    165 	/*
    166 	 * simple check for bad args
    167 	 */
    168 	if ((str == NULL) || (*str == '\0')) {
    169 		tty_warn(0, "Invalid file name arguement");
    170 		return(-1);
    171 	}
    172 
    173 	/*
    174 	 * allocate FTREE node and add to the end of the linked list (args are
    175 	 * processed in the same order they were passed to pax). Get rid of any
    176 	 * trailing / the user may pass us. (watch out for / by itself).
    177 	 */
    178 	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
    179 		tty_warn(0, "Unable to allocate memory for filename");
    180 		return(-1);
    181 	}
    182 
    183 	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
    184 		str[len] = '\0';
    185 	ft->fname = str;
    186 	ft->refcnt = -isdir;
    187 	ft->fow = NULL;
    188 	if (fthead == NULL) {
    189 		fttail = fthead = ft;
    190 		return(0);
    191 	}
    192 	fttail->fow = ft;
    193 	fttail = ft;
    194 	return(0);
    195 }
    196 
    197 /*
    198  * ftree_sel()
    199  *	this entry has been selected by pax. bump up reference count and handle
    200  *	-n and -d processing.
    201  */
    202 
    203 void
    204 ftree_sel(ARCHD *arcn)
    205 {
    206 	/*
    207 	 * set reference bit for this pattern. This linked list is only used
    208 	 * when file trees are supplied pax as args. The list is not used when
    209 	 * the trees are read from stdin.
    210 	 */
    211 	if (ftcur != NULL)
    212 		ftcur->refcnt = 1;
    213 
    214 	/*
    215 	 * if -n we are done with this arg, force a skip to the next arg when
    216 	 * pax asks for the next file in next_file().
    217 	 * if -d we tell fts only to match the directory (if the arg is a dir)
    218 	 * and not the entire file tree rooted at that point.
    219 	 */
    220 	if (nflag)
    221 		ftree_skip = 1;
    222 
    223 	if (!dflag || (arcn->type != PAX_DIR))
    224 		return;
    225 
    226 	if (ftent != NULL)
    227 		(void)fts_set(ftsp, ftent, FTS_SKIP);
    228 }
    229 
    230 /*
    231  * ftree_chk()
    232  *	called at end on pax execution. Prints all those file args that did not
    233  *	have a selected member (reference count still 0)
    234  */
    235 
    236 void
    237 ftree_chk(void)
    238 {
    239 	FTREE *ft;
    240 	int wban = 0;
    241 
    242 	/*
    243 	 * make sure all dir access times were reset.
    244 	 */
    245 	if (tflag)
    246 		atdir_end();
    247 
    248 	/*
    249 	 * walk down list and check reference count. Print out those members
    250 	 * that never had a match
    251 	 */
    252 	for (ft = fthead; ft != NULL; ft = ft->fow) {
    253 		if (ft->refcnt != 0)
    254 			continue;
    255 		if (wban == 0) {
    256 			tty_warn(1,
    257 			    "WARNING! These file names were not selected:");
    258 			++wban;
    259 		}
    260 		(void)fprintf(stderr, "%s\n", ft->fname);
    261 	}
    262 }
    263 
    264 /*
    265  * ftree_arg()
    266  *	Get the next file arg for fts to process. Can be from either the linked
    267  *	list or read from stdin when the user did not them as args to pax. Each
    268  *	arg is processed until the first successful fts_open().
    269  * Return:
    270  *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
    271  *	stdin).
    272  */
    273 
    274 static int
    275 ftree_arg(void)
    276 {
    277 	char *pt;
    278 
    279 	/*
    280 	 * close off the current file tree
    281 	 */
    282 	if (ftsp != NULL) {
    283 		(void)fts_close(ftsp);
    284 		ftsp = NULL;
    285 	}
    286 
    287 	/*
    288 	 * keep looping until we get a valid file tree to process. Stop when we
    289 	 * reach the end of the list (or get an eof on stdin)
    290 	 */
    291 	for(;;) {
    292 		if (fthead == NULL) {
    293 			/*
    294 			 * the user didn't supply any args, get the file trees
    295 			 * to process from stdin;
    296 			 */
    297 			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
    298 				return(-1);
    299 			if ((pt = strchr(farray[0], '\n')) != NULL)
    300 				*pt = '\0';
    301 		} else {
    302 			/*
    303 			 * the user supplied the file args as arguements to pax
    304 			 */
    305 			if (ftcur == NULL)
    306 				ftcur = fthead;
    307 			else if ((ftcur = ftcur->fow) == NULL)
    308 				return(-1);
    309 
    310 			if (ftcur->refcnt < 0) {
    311 				/*
    312 				 * chdir entry.
    313 				 * Change directory and retry loop.
    314 				 */
    315 				if (ar_dochdir(ftcur->fname))
    316 					return (-1);
    317 				continue;
    318 			}
    319 			farray[0] = ftcur->fname;
    320 		}
    321 
    322 		/*
    323 		 * watch it, fts wants the file arg stored in a array of char
    324 		 * ptrs, with the last one a null. we use a two element array
    325 		 * and set farray[0] to point at the buffer with the file name
    326 		 * in it. We cannot pass all the file args to fts at one shot
    327 		 * as we need to keep a handle on which file arg generates what
    328 		 * files (the -n and -d flags need this). If the open is
    329 		 * successful, return a 0.
    330 		 */
    331 		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
    332 			break;
    333 	}
    334 	return(0);
    335 }
    336 
    337 /*
    338  * next_file()
    339  *	supplies the next file to process in the supplied archd structure.
    340  * Return:
    341  *	0 when contents of arcn have been set with the next file, -1 when done.
    342  */
    343 
    344 int
    345 next_file(ARCHD *arcn)
    346 {
    347 	int cnt;
    348 	time_t atime;
    349 	time_t mtime;
    350 
    351 	/*
    352 	 * ftree_sel() might have set the ftree_skip flag if the user has the
    353 	 * -n option and a file was selected from this file arg tree. (-n says
    354 	 * only one member is matched for each pattern) ftree_skip being 1
    355 	 * forces us to go to the next arg now.
    356 	 */
    357 	if (ftree_skip) {
    358 		/*
    359 		 * clear and go to next arg
    360 		 */
    361 		ftree_skip = 0;
    362 		if (ftree_arg() < 0)
    363 			return(-1);
    364 	}
    365 
    366 	/*
    367 	 * loop until we get a valid file to process
    368 	 */
    369 	for(;;) {
    370 		if ((ftent = fts_read(ftsp)) == NULL) {
    371 			/*
    372 			 * out of files in this tree, go to next arg, if none
    373 			 * we are done
    374 			 */
    375 			if (ftree_arg() < 0)
    376 				return(-1);
    377 			continue;
    378 		}
    379 
    380 		/*
    381 		 * handle each type of fts_read() flag
    382 		 */
    383 		switch(ftent->fts_info) {
    384 		case FTS_D:
    385 			/*
    386 			 * cpio does *not* decend directories listed in the
    387 			 * arguments, unlike pax/tar, so needs special handling
    388 			 * here.  failure to do so results in massive amounts
    389 			 * of duplicated files in the output.
    390 			 */
    391 			if (cpio_mode)
    392 				continue;
    393 			/* FALLTHROUGH */
    394 		case FTS_DEFAULT:
    395 		case FTS_F:
    396 		case FTS_SL:
    397 		case FTS_SLNONE:
    398 			/*
    399 			 * these are all ok
    400 			 */
    401 			break;
    402 		case FTS_DP:
    403 			/*
    404 			 * already saw this directory. If the user wants file
    405 			 * access times reset, we use this to restore the
    406 			 * access time for this directory since this is the
    407 			 * last time we will see it in this file subtree
    408 			 * remember to force the time (this is -t on a read
    409 			 * directory, not a created directory).
    410 			 */
    411 			if (!tflag || (get_atdir(
    412 #ifdef NET2_FTS
    413 			    ftent->fts_statb.st_dev, ftent->fts_statb.st_ino,
    414 #else
    415 			    ftent->fts_statp->st_dev, ftent->fts_statp->st_ino,
    416 #endif
    417 			    &mtime, &atime) < 0))
    418 				continue;
    419 			set_ftime(ftent->fts_path, mtime, atime, 1);
    420 			continue;
    421 		case FTS_DC:
    422 			/*
    423 			 * fts claims a file system cycle
    424 			 */
    425 			tty_warn(1,"File system cycle found at %s",
    426 			    ftent->fts_path);
    427 			continue;
    428 		case FTS_DNR:
    429 			syswarn(1, FTS_ERRNO(ftent),
    430 			    "Unable to read directory %s", ftent->fts_path);
    431 			continue;
    432 		case FTS_ERR:
    433 			syswarn(1, FTS_ERRNO(ftent),
    434 			    "File system traversal error");
    435 			continue;
    436 		case FTS_NS:
    437 		case FTS_NSOK:
    438 			syswarn(1, FTS_ERRNO(ftent),
    439 			    "Unable to access %s", ftent->fts_path);
    440 			continue;
    441 		}
    442 
    443 		/*
    444 		 * ok got a file tree node to process. copy info into arcn
    445 		 * structure (initialize as required)
    446 		 */
    447 		arcn->skip = 0;
    448 		arcn->pad = 0;
    449 		arcn->ln_nlen = 0;
    450 		arcn->ln_name[0] = '\0';
    451 #ifdef NET2_FTS
    452 		arcn->sb = ftent->fts_statb;
    453 #else
    454 		arcn->sb = *(ftent->fts_statp);
    455 #endif
    456 
    457 		/*
    458 		 * file type based set up and copy into the arcn struct
    459 		 * SIDE NOTE:
    460 		 * we try to reset the access time on all files and directories
    461 		 * we may read when the -t flag is specified. files are reset
    462 		 * when we close them after copying. we reset the directories
    463 		 * when we are done with their file tree (we also clean up at
    464 		 * end in case we cut short a file tree traversal). However
    465 		 * there is no way to reset access times on symlinks.
    466 		 */
    467 		switch(S_IFMT & arcn->sb.st_mode) {
    468 		case S_IFDIR:
    469 			arcn->type = PAX_DIR;
    470 			if (!tflag)
    471 				break;
    472 			add_atdir(ftent->fts_path, arcn->sb.st_dev,
    473 			    arcn->sb.st_ino, arcn->sb.st_mtime,
    474 			    arcn->sb.st_atime);
    475 			break;
    476 		case S_IFCHR:
    477 			arcn->type = PAX_CHR;
    478 			break;
    479 		case S_IFBLK:
    480 			arcn->type = PAX_BLK;
    481 			break;
    482 		case S_IFREG:
    483 			/*
    484 			 * only regular files with have data to store on the
    485 			 * archive. all others will store a zero length skip.
    486 			 * the skip field is used by pax for actual data it has
    487 			 * to read (or skip over).
    488 			 */
    489 			arcn->type = PAX_REG;
    490 			arcn->skip = arcn->sb.st_size;
    491 			break;
    492 		case S_IFLNK:
    493 			arcn->type = PAX_SLK;
    494 			/*
    495 			 * have to read the symlink path from the file
    496 			 */
    497 			if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
    498 			    PAXPATHLEN)) < 0) {
    499 				syswarn(1, errno, "Unable to read symlink %s",
    500 				    ftent->fts_path);
    501 				continue;
    502 			}
    503 			/*
    504 			 * set link name length, watch out readlink does not
    505 			 * allways null terminate the link path
    506 			 */
    507 			arcn->ln_name[cnt] = '\0';
    508 			arcn->ln_nlen = cnt;
    509 			break;
    510 		case S_IFSOCK:
    511 			/*
    512 			 * under BSD storing a socket is senseless but we will
    513 			 * let the format specific write function make the
    514 			 * decision of what to do with it.
    515 			 */
    516 			arcn->type = PAX_SCK;
    517 			break;
    518 		case S_IFIFO:
    519 			arcn->type = PAX_FIF;
    520 			break;
    521 		}
    522 		break;
    523 	}
    524 
    525 	/*
    526 	 * copy file name, set file name length
    527 	 */
    528 	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, PAXPATHLEN+1);
    529 	arcn->name[arcn->nlen] = '\0';
    530 	arcn->org_name = ftent->fts_path;
    531 	return(0);
    532 }
    533