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