ftree.c revision 1.10 1 /* $NetBSD: ftree.c,v 1.10 2000/02/17 03:12:24 itohy 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.10 2000/02/17 03:12:24 itohy 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 __P((void));
88
89 /*
90 * ftree_start()
91 * initialize the options passed to fts_open() during this run of pax
92 * options are based on the selection of pax options by the user
93 * fts_start() also calls fts_arg() to open the first valid file arg. We
94 * also attempt to reset directory access times when -t (tflag) is set.
95 * Return:
96 * 0 if there is at least one valid file arg to process, -1 otherwise
97 */
98
99 #if __STDC__
100 int
101 ftree_start(void)
102 #else
103 int
104 ftree_start()
105 #endif
106 {
107 /*
108 * set up the operation mode of fts, open the first file arg. We must
109 * use FTS_NOCHDIR, as the user may have to open multiple archives and
110 * if fts did a chdir off into the boondocks, we may create an archive
111 * volume in an place where the user did not expect to.
112 */
113 ftsopts = FTS_NOCHDIR;
114
115 /*
116 * optional user flags that effect file traversal
117 * -H command line symlink follow only (half follow)
118 * -L follow sylinks (logical)
119 * -P do not follow sylinks (physical). This is the default.
120 * -X do not cross over mount points
121 * -t preserve access times on files read.
122 * -n select only the first member of a file tree when a match is found
123 * -d do not extract subtrees rooted at a directory arg.
124 */
125 if (Lflag)
126 ftsopts |= FTS_LOGICAL;
127 else
128 ftsopts |= FTS_PHYSICAL;
129 if (Hflag)
130 # ifdef NET2_FTS
131 tty_warn(0, "The -H flag is not supported on this version");
132 # else
133 ftsopts |= FTS_COMFOLLOW;
134 # endif
135 if (Xflag)
136 ftsopts |= FTS_XDEV;
137
138 if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
139 tty_warn(1, "Unable to allocate memory for file name buffer");
140 return(-1);
141 }
142
143 if (ftree_arg() < 0)
144 return(-1);
145 if (tflag && (atdir_start() < 0))
146 return(-1);
147 return(0);
148 }
149
150 /*
151 * ftree_add()
152 * add the arg to the linked list of files to process. Each will be
153 * processed by fts one at a time
154 * Return:
155 * 0 if added to the linked list, -1 if failed
156 */
157
158 #if __STDC__
159 int
160 ftree_add(char *str, int isdir)
161 #else
162 int
163 ftree_add(str, isdir)
164 char *str;
165 int isdir;
166 #endif
167 {
168 FTREE *ft;
169 int len;
170
171 /*
172 * simple check for bad args
173 */
174 if ((str == NULL) || (*str == '\0')) {
175 tty_warn(0, "Invalid file name arguement");
176 return(-1);
177 }
178
179 /*
180 * allocate FTREE node and add to the end of the linked list (args are
181 * processed in the same order they were passed to pax). Get rid of any
182 * trailing / the user may pass us. (watch out for / by itself).
183 */
184 if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
185 tty_warn(0, "Unable to allocate memory for filename");
186 return(-1);
187 }
188
189 if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
190 str[len] = '\0';
191 ft->fname = str;
192 ft->refcnt = -isdir;
193 ft->fow = NULL;
194 if (fthead == NULL) {
195 fttail = fthead = ft;
196 return(0);
197 }
198 fttail->fow = ft;
199 fttail = ft;
200 return(0);
201 }
202
203 /*
204 * ftree_sel()
205 * this entry has been selected by pax. bump up reference count and handle
206 * -n and -d processing.
207 */
208
209 #if __STDC__
210 void
211 ftree_sel(ARCHD *arcn)
212 #else
213 void
214 ftree_sel(arcn)
215 ARCHD *arcn;
216 #endif
217 {
218 /*
219 * set reference bit for this pattern. This linked list is only used
220 * when file trees are supplied pax as args. The list is not used when
221 * the trees are read from stdin.
222 */
223 if (ftcur != NULL)
224 ftcur->refcnt = 1;
225
226 /*
227 * if -n we are done with this arg, force a skip to the next arg when
228 * pax asks for the next file in next_file().
229 * if -d we tell fts only to match the directory (if the arg is a dir)
230 * and not the entire file tree rooted at that point.
231 */
232 if (nflag)
233 ftree_skip = 1;
234
235 if (!dflag || (arcn->type != PAX_DIR))
236 return;
237
238 if (ftent != NULL)
239 (void)fts_set(ftsp, ftent, FTS_SKIP);
240 }
241
242 /*
243 * ftree_chk()
244 * called at end on pax execution. Prints all those file args that did not
245 * have a selected member (reference count still 0)
246 */
247
248 #if __STDC__
249 void
250 ftree_chk(void)
251 #else
252 void
253 ftree_chk()
254 #endif
255 {
256 FTREE *ft;
257 int wban = 0;
258
259 /*
260 * make sure all dir access times were reset.
261 */
262 if (tflag)
263 atdir_end();
264
265 /*
266 * walk down list and check reference count. Print out those members
267 * that never had a match
268 */
269 for (ft = fthead; ft != NULL; ft = ft->fow) {
270 if (ft->refcnt != 0)
271 continue;
272 if (wban == 0) {
273 tty_warn(1,
274 "WARNING! These file names were not selected:");
275 ++wban;
276 }
277 (void)fprintf(stderr, "%s\n", ft->fname);
278 }
279 }
280
281 /*
282 * ftree_arg()
283 * Get the next file arg for fts to process. Can be from either the linked
284 * list or read from stdin when the user did not them as args to pax. Each
285 * arg is processed until the first successful fts_open().
286 * Return:
287 * 0 when the next arg is ready to go, -1 if out of file args (or EOF on
288 * stdin).
289 */
290
291 #if __STDC__
292 static int
293 ftree_arg(void)
294 #else
295 static int
296 ftree_arg()
297 #endif
298 {
299 char *pt;
300
301 /*
302 * close off the current file tree
303 */
304 if (ftsp != NULL) {
305 (void)fts_close(ftsp);
306 ftsp = NULL;
307 }
308
309 /*
310 * keep looping until we get a valid file tree to process. Stop when we
311 * reach the end of the list (or get an eof on stdin)
312 */
313 for(;;) {
314 if (fthead == NULL) {
315 /*
316 * the user didn't supply any args, get the file trees
317 * to process from stdin;
318 */
319 if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
320 return(-1);
321 if ((pt = strchr(farray[0], '\n')) != NULL)
322 *pt = '\0';
323 } else {
324 /*
325 * the user supplied the file args as arguements to pax
326 */
327 if (ftcur == NULL)
328 ftcur = fthead;
329 else if ((ftcur = ftcur->fow) == NULL)
330 return(-1);
331
332 if (ftcur->refcnt < 0) {
333 /*
334 * chdir entry.
335 * Change directory and retry loop.
336 */
337 if (ar_dochdir(ftcur->fname))
338 return (-1);
339 continue;
340 }
341 farray[0] = ftcur->fname;
342 }
343
344 /*
345 * watch it, fts wants the file arg stored in a array of char
346 * ptrs, with the last one a null. we use a two element array
347 * and set farray[0] to point at the buffer with the file name
348 * in it. We cannot pass all the file args to fts at one shot
349 * as we need to keep a handle on which file arg generates what
350 * files (the -n and -d flags need this). If the open is
351 * successful, return a 0.
352 */
353 if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL)
354 break;
355 }
356 return(0);
357 }
358
359 /*
360 * next_file()
361 * supplies the next file to process in the supplied archd structure.
362 * Return:
363 * 0 when contents of arcn have been set with the next file, -1 when done.
364 */
365
366 #if __STDC__
367 int
368 next_file(ARCHD *arcn)
369 #else
370 int
371 next_file(arcn)
372 ARCHD *arcn;
373 #endif
374 {
375 int cnt;
376 time_t atime;
377 time_t mtime;
378
379 /*
380 * ftree_sel() might have set the ftree_skip flag if the user has the
381 * -n option and a file was selected from this file arg tree. (-n says
382 * only one member is matched for each pattern) ftree_skip being 1
383 * forces us to go to the next arg now.
384 */
385 if (ftree_skip) {
386 /*
387 * clear and go to next arg
388 */
389 ftree_skip = 0;
390 if (ftree_arg() < 0)
391 return(-1);
392 }
393
394 /*
395 * loop until we get a valid file to process
396 */
397 for(;;) {
398 if ((ftent = fts_read(ftsp)) == NULL) {
399 /*
400 * out of files in this tree, go to next arg, if none
401 * we are done
402 */
403 if (ftree_arg() < 0)
404 return(-1);
405 continue;
406 }
407
408 /*
409 * handle each type of fts_read() flag
410 */
411 switch(ftent->fts_info) {
412 case FTS_D:
413 /*
414 * cpio does *not* decend directories listed in the
415 * arguments, unlike pax/tar, so needs special handling
416 * here. failure to do so results in massive amounts
417 * of duplicated files in the output.
418 */
419 if (cpio_mode)
420 continue;
421 /* FALLTHROUGH */
422 case FTS_DEFAULT:
423 case FTS_F:
424 case FTS_SL:
425 case FTS_SLNONE:
426 /*
427 * these are all ok
428 */
429 break;
430 case FTS_DP:
431 /*
432 * already saw this directory. If the user wants file
433 * access times reset, we use this to restore the
434 * access time for this directory since this is the
435 * last time we will see it in this file subtree
436 * remember to force the time (this is -t on a read
437 * directory, not a created directory).
438 */
439 # ifdef NET2_FTS
440 if (!tflag || (get_atdir(ftent->fts_statb.st_dev,
441 ftent->fts_statb.st_ino, &mtime, &atime) < 0))
442 # else
443 if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
444 ftent->fts_statp->st_ino, &mtime, &atime) < 0))
445 # endif
446 continue;
447 set_ftime(ftent->fts_path, mtime, atime, 1);
448 continue;
449 case FTS_DC:
450 /*
451 * fts claims a file system cycle
452 */
453 tty_warn(1,"File system cycle found at %s",
454 ftent->fts_path);
455 continue;
456 case FTS_DNR:
457 # ifdef NET2_FTS
458 syswarn(1, errno,
459 # else
460 syswarn(1, ftent->fts_errno,
461 # endif
462 "Unable to read directory %s", ftent->fts_path);
463 continue;
464 case FTS_ERR:
465 # ifdef NET2_FTS
466 syswarn(1, errno,
467 # else
468 syswarn(1, ftent->fts_errno,
469 # endif
470 "File system traversal error");
471 continue;
472 case FTS_NS:
473 case FTS_NSOK:
474 # ifdef NET2_FTS
475 syswarn(1, errno,
476 # else
477 syswarn(1, ftent->fts_errno,
478 # endif
479 "Unable to access %s", ftent->fts_path);
480 continue;
481 }
482
483 /*
484 * ok got a file tree node to process. copy info into arcn
485 * structure (initialize as required)
486 */
487 arcn->skip = 0;
488 arcn->pad = 0;
489 arcn->ln_nlen = 0;
490 arcn->ln_name[0] = '\0';
491 # ifdef NET2_FTS
492 arcn->sb = ftent->fts_statb;
493 # else
494 arcn->sb = *(ftent->fts_statp);
495 # endif
496
497 /*
498 * file type based set up and copy into the arcn struct
499 * SIDE NOTE:
500 * we try to reset the access time on all files and directories
501 * we may read when the -t flag is specified. files are reset
502 * when we close them after copying. we reset the directories
503 * when we are done with their file tree (we also clean up at
504 * end in case we cut short a file tree traversal). However
505 * there is no way to reset access times on symlinks.
506 */
507 switch(S_IFMT & arcn->sb.st_mode) {
508 case S_IFDIR:
509 arcn->type = PAX_DIR;
510 if (!tflag)
511 break;
512 add_atdir(ftent->fts_path, arcn->sb.st_dev,
513 arcn->sb.st_ino, arcn->sb.st_mtime,
514 arcn->sb.st_atime);
515 break;
516 case S_IFCHR:
517 arcn->type = PAX_CHR;
518 break;
519 case S_IFBLK:
520 arcn->type = PAX_BLK;
521 break;
522 case S_IFREG:
523 /*
524 * only regular files with have data to store on the
525 * archive. all others will store a zero length skip.
526 * the skip field is used by pax for actual data it has
527 * to read (or skip over).
528 */
529 arcn->type = PAX_REG;
530 arcn->skip = arcn->sb.st_size;
531 break;
532 case S_IFLNK:
533 arcn->type = PAX_SLK;
534 /*
535 * have to read the symlink path from the file
536 */
537 if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
538 PAXPATHLEN)) < 0) {
539 syswarn(1, errno, "Unable to read symlink %s",
540 ftent->fts_path);
541 continue;
542 }
543 /*
544 * set link name length, watch out readlink does not
545 * allways null terminate the link path
546 */
547 arcn->ln_name[cnt] = '\0';
548 arcn->ln_nlen = cnt;
549 break;
550 case S_IFSOCK:
551 /*
552 * under BSD storing a socket is senseless but we will
553 * let the format specific write function make the
554 * decision of what to do with it.
555 */
556 arcn->type = PAX_SCK;
557 break;
558 case S_IFIFO:
559 arcn->type = PAX_FIF;
560 break;
561 }
562 break;
563 }
564
565 /*
566 * copy file name, set file name length
567 */
568 arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, PAXPATHLEN+1);
569 arcn->name[arcn->nlen] = '\0';
570 arcn->org_name = ftent->fts_path;
571 return(0);
572 }
573