source: freewrt/tools/paxmirabilis/src/ftree.c@ fdd4f59

freewrt_1_0 freewrt_2_0
Last change on this file since fdd4f59 was bceb42b, checked in by Thorsten Glaser <tg@…>, 19 years ago

wbx@ asked me a few seconds too late to use an src/ subdirectory

git-svn-id: svn://www.freewrt.org/trunk/freewrt@204 afb5a338-a214-0410-bd46-81f09a774fd1

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