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

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

even FreeWRT 1.0-stable deserves paxmirabilis-20120216 compiled with LTO ☺

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@3981 afb5a338-a214-0410-bd46-81f09a774fd1

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