ftree.c revision 127958
172365Snik/*-
272365Snik * Copyright (c) 1992 Keith Muller.
372365Snik * Copyright (c) 1992, 1993
472365Snik *	The Regents of the University of California.  All rights reserved.
572365Snik *
672365Snik * This code is derived from software contributed to Berkeley by
772365Snik * Keith Muller of the University of California, San Diego.
872365Snik *
972365Snik * Redistribution and use in source and binary forms, with or without
1072365Snik * modification, are permitted provided that the following conditions
1172365Snik * are met:
1272365Snik * 1. Redistributions of source code must retain the above copyright
1372365Snik *    notice, this list of conditions and the following disclaimer.
1472365Snik * 2. Redistributions in binary form must reproduce the above copyright
1572365Snik *    notice, this list of conditions and the following disclaimer in the
1672365Snik *    documentation and/or other materials provided with the distribution.
1772365Snik * 4. Neither the name of the University nor the names of its contributors
18280394Spfg *    may be used to endorse or promote products derived from this software
1972414Sru *    without specific prior written permission.
2072414Sru *
2172365Snik * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2272414Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2372414Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2472414Sru * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2572414Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2672414Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2772414Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2872414Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2972414Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3072414Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3172365Snik * SUCH DAMAGE.
3272365Snik */
3372365Snik
3484306Sru#ifndef lint
3584306Sru#if 0
3672365Snikstatic char sccsid[] = "@(#)ftree.c	8.2 (Berkeley) 4/18/94";
3772365Snik#endif
3872365Snik#endif /* not lint */
3972365Snik#include <sys/cdefs.h>
4072365Snik__FBSDID("$FreeBSD: head/bin/pax/ftree.c 127958 2004-04-06 20:06:54Z markm $");
4172365Snik
4272365Snik#include <sys/types.h>
4372365Snik#include <sys/time.h>
4472365Snik#include <sys/stat.h>
4572365Snik#include <unistd.h>
4672365Snik#include <string.h>
4772365Snik#include <stdio.h>
4872365Snik#include <errno.h>
4972365Snik#include <stdlib.h>
5072365Snik#include <fts.h>
5172365Snik#include "pax.h"
5272365Snik#include "ftree.h"
5372365Snik#include "extern.h"
5472365Snik
5572365Snik/*
5672365Snik * routines to interface with the fts library function.
5772365Snik *
5872414Sru * file args supplied to pax are stored on a single linked list (of type FTREE)
5972414Sru * and given to fts to be processed one at a time. pax "selects" files from
6072414Sru * the expansion of each arg into the corresponding file tree (if the arg is a
6172414Sru * directory, otherwise the node itself is just passed to pax). The selection
6272365Snik * is modified by the -n and -u flags. The user is informed when a specific
6372414Sru * file arg does not generate any selected files. -n keeps expanding the file
6472414Sru * tree arg until one of its files is selected, then skips to the next file
6572365Snik * arg. when the user does not supply the file trees as command line args to
66119893Sru * pax, they are read from stdin
6772365Snik */
6872365Snik
6972414Srustatic FTS *ftsp = NULL;		/* current FTS handle */
7072414Srustatic int ftsopts;			/* options to be used on fts_open */
7172365Snikstatic char *farray[2];			/* array for passing each arg to fts */
7272365Snikstatic FTREE *fthead = NULL;		/* head of linked list of file args */
7372414Srustatic FTREE *fttail = NULL;		/* tail of linked list of file args */
74108037Srustatic FTREE *ftcur = NULL;		/* current file arg being processed */
7572365Snikstatic FTSENT *ftent = NULL;		/* current file tree entry */
76108037Srustatic int ftree_skip;			/* when set skip to next file arg */
7772365Snik
78108087Srustatic int ftree_arg(void);
7972414Sru
80108087Sru/*
8172414Sru * ftree_start()
8272365Snik *	initialize the options passed to fts_open() during this run of pax
8372414Sru *	options are based on the selection of pax options by the user
8472414Sru *	fts_start() also calls fts_arg() to open the first valid file arg. We
8572414Sru *	also attempt to reset directory access times when -t (tflag) is set.
8672365Snik * Return:
8772365Snik *	0 if there is at least one valid file arg to process, -1 otherwise
8872414Sru */
8972365Snik
9072414Sruint
91108087Sruftree_start(void)
9272414Sru{
9372414Sru	/*
9472414Sru	 * Set up the operation mode of fts, open the first file arg. We must
95108087Sru	 * use FTS_NOCHDIR, as the user may have to open multiple archives and
9672414Sru	 * if fts did a chdir off into the boondocks, we may create an archive
9772414Sru	 * volume in a place where the user did not expect to.
9881773Sru	 */
9972365Snik	ftsopts = FTS_NOCHDIR;
10072414Sru
10172365Snik	/*
10272365Snik	 * optional user flags that effect file traversal
10372414Sru	 * -H command line symlink follow only (half follow)
10472414Sru	 * -L follow sylinks (logical)
10572365Snik	 * -P do not follow sylinks (physical). This is the default.
10672414Sru	 * -X do not cross over mount points
10772365Snik	 * -t preserve access times on files read.
10872365Snik	 * -n select only the first member of a file tree when a match is found
10972365Snik	 * -d do not extract subtrees rooted at a directory arg.
11072365Snik	 */
11172365Snik	if (Lflag)
112108037Sru		ftsopts |= FTS_LOGICAL;
11372414Sru	else
114108037Sru		ftsopts |= FTS_PHYSICAL;
11572414Sru	if (Hflag)
11672414Sru#	ifdef NET2_FTS
11772414Sru		paxwarn(0, "The -H flag is not supported on this version");
11872365Snik#	else
11972414Sru		ftsopts |= FTS_COMFOLLOW;
12072414Sru#	endif
121108037Sru	if (Xflag)
12272365Snik		ftsopts |= FTS_XDEV;
123108037Sru
12472365Snik	if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
12572414Sru		paxwarn(1, "Unable to allocate memory for file name buffer");
126108037Sru		return(-1);
12772365Snik	}
128108037Sru
12972414Sru	if (ftree_arg() < 0)
130108087Sru		return(-1);
13172414Sru	if (tflag && (atdir_start() < 0))
132108087Sru		return(-1);
13372414Sru	return(0);
13472414Sru}
13572414Sru
13672414Sru/*
13772414Sru * ftree_add()
13872414Sru *	add the arg to the linked list of files to process. Each will be
13972365Snik *	processed by fts one at a time
14072414Sru * Return:
14172365Snik *	0 if added to the linked list, -1 if failed
14272414Sru */
14372414Sru
14472414Sruint
145108037Sruftree_add(char *str, int chflg)
14672365Snik{
147108037Sru	FTREE *ft;
14878686Sdd	int len;
14972414Sru
15072365Snik	/*
15172365Snik	 * simple check for bad args
15272414Sru	 */
15372365Snik	if ((str == NULL) || (*str == '\0')) {
15472365Snik		paxwarn(0, "Invalid file name argument");
15572365Snik		return(-1);
15672365Snik	}
15772365Snik
15872414Sru	/*
159108037Sru	 * allocate FTREE node and add to the end of the linked list (args are
16072365Snik	 * processed in the same order they were passed to pax). Get rid of any
161108037Sru	 * trailing / the user may pass us. (watch out for / by itself).
16272414Sru	 */
16372414Sru	if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
16472365Snik		paxwarn(0, "Unable to allocate memory for filename");
16572414Sru		return(-1);
16672414Sru	}
16772414Sru
16872414Sru	if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
169108037Sru		str[len] = '\0';
17072365Snik	ft->fname = str;
171108037Sru	ft->refcnt = 0;
17272365Snik	ft->chflg = chflg;
17372414Sru	ft->fow = NULL;
174108037Sru	if (fthead == NULL) {
17572365Snik		fttail = fthead = ft;
176108037Sru		return(0);
177280394Spfg	}
17872414Sru	fttail->fow = ft;
17972365Snik	fttail = ft;
18072414Sru	return(0);
181108037Sru}
18272365Snik
183108037Sru/*
18472365Snik * ftree_sel()
185108037Sru *	this entry has been selected by pax. bump up reference count and handle
18672365Snik *	-n and -d processing.
187108037Sru */
18872414Sru
189108037Sruvoid
19072414Sruftree_sel(ARCHD *arcn)
191108037Sru{
19272414Sru	/*
19372414Sru	 * set reference bit for this pattern. This linked list is only used
19472365Snik	 * when file trees are supplied pax as args. The list is not used when
19572365Snik	 * the trees are read from stdin.
196108037Sru	 */
19772365Snik	if (ftcur != NULL)
198108037Sru		ftcur->refcnt = 1;
19972414Sru
20072414Sru	/*
20172365Snik	 * if -n we are done with this arg, force a skip to the next arg when
20272414Sru	 * pax asks for the next file in next_file().
203108037Sru	 * if -d we tell fts only to match the directory (if the arg is a dir)
20472365Snik	 * and not the entire file tree rooted at that point.
205108037Sru	 */
20672414Sru	if (nflag)
20772365Snik		ftree_skip = 1;
20872365Snik
209108037Sru	if (!dflag || (arcn->type != PAX_DIR))
21072365Snik		return;
211108037Sru
21272365Snik	if (ftent != NULL)
21372414Sru		(void)fts_set(ftsp, ftent, FTS_SKIP);
214108037Sru}
21572365Snik
216108037Sru/*
21772365Snik * ftree_chk()
21872365Snik *	called at end on pax execution. Prints all those file args that did not
21972414Sru *	have a selected member (reference count still 0)
22072414Sru */
22172414Sru
22272365Snikvoid
22372414Sruftree_chk(void)
22472414Sru{
22572414Sru	FTREE *ft;
22672414Sru	int wban = 0;
227
228	/*
229	 * make sure all dir access times were reset.
230	 */
231	if (tflag)
232		atdir_end();
233
234	/*
235	 * walk down list and check reference count. Print out those members
236	 * that never had a match
237	 */
238	for (ft = fthead; ft != NULL; ft = ft->fow) {
239		if ((ft->refcnt > 0) || ft->chflg)
240			continue;
241		if (wban == 0) {
242			paxwarn(1,"WARNING! These file names were not selected:");
243			++wban;
244		}
245		(void)fprintf(stderr, "%s\n", ft->fname);
246	}
247}
248
249/*
250 * ftree_arg()
251 *	Get the next file arg for fts to process. Can be from either the linked
252 *	list or read from stdin when the user did not them as args to pax. Each
253 *	arg is processed until the first successful fts_open().
254 * Return:
255 *	0 when the next arg is ready to go, -1 if out of file args (or EOF on
256 *	stdin).
257 */
258
259static int
260ftree_arg(void)
261{
262	char *pt;
263
264	/*
265	 * close off the current file tree
266	 */
267	if (ftsp != NULL) {
268		(void)fts_close(ftsp);
269		ftsp = NULL;
270	}
271
272	/*
273	 * keep looping until we get a valid file tree to process. Stop when we
274	 * reach the end of the list (or get an eof on stdin)
275	 */
276	for(;;) {
277		if (fthead == NULL) {
278			/*
279			 * the user didn't supply any args, get the file trees
280			 * to process from stdin;
281			 */
282			if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
283				return(-1);
284			if ((pt = strchr(farray[0], '\n')) != NULL)
285				*pt = '\0';
286		} else {
287			/*
288			 * the user supplied the file args as arguments to pax
289			 */
290			if (ftcur == NULL)
291				ftcur = fthead;
292			else if ((ftcur = ftcur->fow) == NULL)
293				return(-1);
294			if (ftcur->chflg) {
295				/* First fchdir() back... */
296				if (fchdir(cwdfd) < 0) {
297					syswarn(1, errno,
298					  "Can't fchdir to starting directory");
299					return(-1);
300				}
301				if (chdir(ftcur->fname) < 0) {
302					syswarn(1, errno, "Can't chdir to %s",
303					    ftcur->fname);
304					return(-1);
305				}
306				continue;
307			} else
308				farray[0] = ftcur->fname;
309		}
310
311		/*
312		 * Watch it, fts wants the file arg stored in an array of char
313		 * ptrs, with the last one a null. We use a two element array
314		 * and set farray[0] to point at the buffer with the file name
315		 * in it. We cannot pass all the file args to fts at one shot
316		 * as we need to keep a handle on which file arg generates what
317		 * files (the -n and -d flags need this). If the open is
318		 * successful, return a 0.
319		 */
320		if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
321			break;
322	}
323	return(0);
324}
325
326/*
327 * next_file()
328 *	supplies the next file to process in the supplied archd structure.
329 * Return:
330 *	0 when contents of arcn have been set with the next file, -1 when done.
331 */
332
333int
334next_file(ARCHD *arcn)
335{
336	int cnt;
337	time_t atime;
338	time_t mtime;
339
340	/*
341	 * ftree_sel() might have set the ftree_skip flag if the user has the
342	 * -n option and a file was selected from this file arg tree. (-n says
343	 * only one member is matched for each pattern) ftree_skip being 1
344	 * forces us to go to the next arg now.
345	 */
346	if (ftree_skip) {
347		/*
348		 * clear and go to next arg
349		 */
350		ftree_skip = 0;
351		if (ftree_arg() < 0)
352			return(-1);
353	}
354
355	/*
356	 * loop until we get a valid file to process
357	 */
358	for(;;) {
359		if ((ftent = fts_read(ftsp)) == NULL) {
360			/*
361			 * out of files in this tree, go to next arg, if none
362			 * we are done
363			 */
364			if (ftree_arg() < 0)
365				return(-1);
366			continue;
367		}
368
369		/*
370		 * handle each type of fts_read() flag
371		 */
372		switch(ftent->fts_info) {
373		case FTS_D:
374		case FTS_DEFAULT:
375		case FTS_F:
376		case FTS_SL:
377		case FTS_SLNONE:
378			/*
379			 * these are all ok
380			 */
381			break;
382		case FTS_DP:
383			/*
384			 * already saw this directory. If the user wants file
385			 * access times reset, we use this to restore the
386			 * access time for this directory since this is the
387			 * last time we will see it in this file subtree
388			 * remember to force the time (this is -t on a read
389			 * directory, not a created directory).
390			 */
391#			ifdef NET2_FTS
392			if (!tflag || (get_atdir(ftent->fts_statb.st_dev,
393			    ftent->fts_statb.st_ino, &mtime, &atime) < 0))
394#			else
395			if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
396			    ftent->fts_statp->st_ino, &mtime, &atime) < 0))
397#			endif
398				continue;
399			set_ftime(ftent->fts_path, mtime, atime, 1);
400			continue;
401		case FTS_DC:
402			/*
403			 * fts claims a file system cycle
404			 */
405			paxwarn(1,"File system cycle found at %s",ftent->fts_path);
406			continue;
407		case FTS_DNR:
408#			ifdef NET2_FTS
409			syswarn(1, errno,
410#			else
411			syswarn(1, ftent->fts_errno,
412#			endif
413			    "Unable to read directory %s", ftent->fts_path);
414			continue;
415		case FTS_ERR:
416#			ifdef NET2_FTS
417			syswarn(1, errno,
418#			else
419			syswarn(1, ftent->fts_errno,
420#			endif
421			    "File system traversal error");
422			continue;
423		case FTS_NS:
424		case FTS_NSOK:
425#			ifdef NET2_FTS
426			syswarn(1, errno,
427#			else
428			syswarn(1, ftent->fts_errno,
429#			endif
430			    "Unable to access %s", ftent->fts_path);
431			continue;
432		}
433
434		/*
435		 * ok got a file tree node to process. copy info into arcn
436		 * structure (initialize as required)
437		 */
438		arcn->skip = 0;
439		arcn->pad = 0;
440		arcn->ln_nlen = 0;
441		arcn->ln_name[0] = '\0';
442#		ifdef NET2_FTS
443		arcn->sb = ftent->fts_statb;
444#		else
445		arcn->sb = *(ftent->fts_statp);
446#		endif
447
448		/*
449		 * file type based set up and copy into the arcn struct
450		 * SIDE NOTE:
451		 * we try to reset the access time on all files and directories
452		 * we may read when the -t flag is specified. files are reset
453		 * when we close them after copying. we reset the directories
454		 * when we are done with their file tree (we also clean up at
455		 * end in case we cut short a file tree traversal). However
456		 * there is no way to reset access times on symlinks.
457		 */
458		switch(S_IFMT & arcn->sb.st_mode) {
459		case S_IFDIR:
460			arcn->type = PAX_DIR;
461			if (!tflag)
462				break;
463			add_atdir(ftent->fts_path, arcn->sb.st_dev,
464			    arcn->sb.st_ino, arcn->sb.st_mtime,
465			    arcn->sb.st_atime);
466			break;
467		case S_IFCHR:
468			arcn->type = PAX_CHR;
469			break;
470		case S_IFBLK:
471			arcn->type = PAX_BLK;
472			break;
473		case S_IFREG:
474			/*
475			 * only regular files with have data to store on the
476			 * archive. all others will store a zero length skip.
477			 * the skip field is used by pax for actual data it has
478			 * to read (or skip over).
479			 */
480			arcn->type = PAX_REG;
481			arcn->skip = arcn->sb.st_size;
482			break;
483		case S_IFLNK:
484			arcn->type = PAX_SLK;
485			/*
486			 * have to read the symlink path from the file
487			 */
488			if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
489			    PAXPATHLEN - 1)) < 0) {
490				syswarn(1, errno, "Unable to read symlink %s",
491				    ftent->fts_path);
492				continue;
493			}
494			/*
495			 * set link name length, watch out readlink does not
496			 * always NUL terminate the link path
497			 */
498			arcn->ln_name[cnt] = '\0';
499			arcn->ln_nlen = cnt;
500			break;
501		case S_IFSOCK:
502			/*
503			 * under BSD storing a socket is senseless but we will
504			 * let the format specific write function make the
505			 * decision of what to do with it.
506			 */
507			arcn->type = PAX_SCK;
508			break;
509		case S_IFIFO:
510			arcn->type = PAX_FIF;
511			break;
512		}
513		break;
514	}
515
516	/*
517	 * copy file name, set file name length
518	 */
519	arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, sizeof(arcn->name) - 1);
520	arcn->name[arcn->nlen] = '\0';
521	arcn->org_name = ftent->fts_path;
522	return(0);
523}
524