source: trunk/minix/commands/pax/ftree.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

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