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