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