source: freewrt/tools/paxmirabilis/src/ftree.c@ 3784d08

freewrt_1_0 freewrt_2_0
Last change on this file since 3784d08 was c41ecbb, checked in by Thorsten Glaser <tg@…>, 17 years ago

FreeWRT trunk:

  • merge new MirCPIO upstream, changes: – allow multiple -v options not only for tar but also for cpio, pax – add code to work around broken archives such as the CPIO archive inside

Fedora Core 4 glibc-common-2.3.6-3.i386.rpm which carry the actual data
of hardlinks not in the first but a later (here, the last) occurence of
the file in question: iff hardlinking succeeds (no cross-device!), size
of the linked files is 0, size of the archive member is greater than 0,
we are extracting, but not to stdout, proceed writing out the data.

  • add mircpio, mirpax, mirtar links in order to prevent interfering with native tools when both are in $PATH (this has worked since the last update, can be used now)

FreeWRT 1.0-stable:

  • merge paxmirabilis complete from trunk

note: untested

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

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