iwm: Fix S:N reporting in ifconfig(8)
[dragonfly.git] / bin / pax / ftree.c
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  * 3. 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  * @(#)ftree.c  8.2 (Berkeley) 4/18/94
34  * $FreeBSD: src/bin/pax/ftree.c,v 1.13.2.1 2001/08/01 05:03:11 obrien Exp $
35  * $DragonFly: src/bin/pax/ftree.c,v 1.7 2006/09/27 19:18:00 pavalos Exp $
36  */
37
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <fts.h>
47 #include "pax.h"
48 #include "ftree.h"
49 #include "extern.h"
50
51 /*
52  * routines to interface with the fts library function.
53  *
54  * file args supplied to pax are stored on a single linked list (of type FTREE)
55  * and given to fts to be processed one at a time. pax "selects" files from
56  * the expansion of each arg into the corresponding file tree (if the arg is a
57  * directory, otherwise the node itself is just passed to pax). The selection
58  * is modified by the -n and -u flags. The user is informed when a specific
59  * file arg does not generate any selected files. -n keeps expanding the file
60  * tree arg until one of its files is selected, then skips to the next file
61  * arg. when the user does not supply the file trees as command line args to
62  * pax, they are read from stdin
63  */
64
65 static FTS *ftsp = NULL;                /* current FTS handle */
66 static int ftsopts;                     /* options to be used on fts_open */
67 static char *farray[2];                 /* array for passing each arg to fts */
68 static FTREE *fthead = NULL;            /* head of linked list of file args */
69 static FTREE *fttail = NULL;            /* tail of linked list of file args */
70 static FTREE *ftcur = NULL;             /* current file arg being processed */
71 static FTSENT *ftent = NULL;            /* current file tree entry */
72 static int ftree_skip;                  /* when set skip to next file arg */
73
74 static int ftree_arg (void);
75
76 /*
77  * ftree_start()
78  *      initialize the options passed to fts_open() during this run of pax
79  *      options are based on the selection of pax options by the user
80  *      fts_start() also calls fts_arg() to open the first valid file arg. We
81  *      also attempt to reset directory access times when -t (tflag) is set.
82  * Return:
83  *      0 if there is at least one valid file arg to process, -1 otherwise
84  */
85
86 int
87 ftree_start(void)
88 {
89         /*
90          * set up the operation mode of fts, open the first file arg. We must
91          * use FTS_NOCHDIR, as the user may have to open multiple archives and
92          * if fts did a chdir off into the boondocks, we may create an archive
93          * volume in an place where the user did not expect to.
94          */
95         ftsopts = FTS_NOCHDIR;
96
97         /*
98          * optional user flags that effect file traversal
99          * -H command line symlink follow only (half follow)
100          * -L follow sylinks (logical)
101          * -P do not follow sylinks (physical). This is the default.
102          * -X do not cross over mount points
103          * -t preserve access times on files read.
104          * -n select only the first member of a file tree when a match is found
105          * -d do not extract subtrees rooted at a directory arg.
106          */
107         if (Lflag)
108                 ftsopts |= FTS_LOGICAL;
109         else
110                 ftsopts |= FTS_PHYSICAL;
111         if (Hflag)
112                 ftsopts |= FTS_COMFOLLOW;
113         if (Xflag)
114                 ftsopts |= FTS_XDEV;
115
116         if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) {
117                 paxwarn(1, "Unable to allocate memory for file name buffer");
118                 return(-1);
119         }
120
121         if (ftree_arg() < 0)
122                 return(-1);
123         if (tflag && (atdir_start() < 0))
124                 return(-1);
125         return(0);
126 }
127
128 /*
129  * ftree_add()
130  *      add the arg to the linked list of files to process. Each will be
131  *      processed by fts one at a time
132  * Return:
133  *      0 if added to the linked list, -1 if failed
134  */
135
136 int
137 ftree_add(char *str, int chflg)
138 {
139         FTREE *ft;
140         int len;
141
142         /*
143          * simple check for bad args
144          */
145         if ((str == NULL) || (*str == '\0')) {
146                 paxwarn(0, "Invalid file name argument");
147                 return(-1);
148         }
149
150         /*
151          * allocate FTREE node and add to the end of the linked list (args are
152          * processed in the same order they were passed to pax). Get rid of any
153          * trailing / the user may pass us. (watch out for / by itself).
154          */
155         if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) {
156                 paxwarn(0, "Unable to allocate memory for filename");
157                 return(-1);
158         }
159
160         if (((len = strlen(str) - 1) > 0) && (str[len] == '/'))
161                 str[len] = '\0';
162         ft->fname = str;
163         ft->refcnt = 0;
164         ft->chflg = chflg;
165         ft->fow = NULL;
166         if (fthead == NULL) {
167                 fttail = fthead = ft;
168                 return(0);
169         }
170         fttail->fow = ft;
171         fttail = ft;
172         return(0);
173 }
174
175 /*
176  * ftree_sel()
177  *      this entry has been selected by pax. bump up reference count and handle
178  *      -n and -d processing.
179  */
180
181 void
182 ftree_sel(ARCHD *arcn)
183 {
184         /*
185          * set reference bit for this pattern. This linked list is only used
186          * when file trees are supplied pax as args. The list is not used when
187          * the trees are read from stdin.
188          */
189         if (ftcur != NULL)
190                 ftcur->refcnt = 1;
191
192         /*
193          * if -n we are done with this arg, force a skip to the next arg when
194          * pax asks for the next file in next_file().
195          * if -d we tell fts only to match the directory (if the arg is a dir)
196          * and not the entire file tree rooted at that point.
197          */
198         if (nflag)
199                 ftree_skip = 1;
200
201         if (!dflag || (arcn->type != PAX_DIR))
202                 return;
203
204         if (ftent != NULL)
205                 fts_set(ftsp, ftent, FTS_SKIP);
206 }
207
208 /*
209  * ftree_chk()
210  *      called at end on pax execution. Prints all those file args that did not
211  *      have a selected member (reference count still 0)
212  */
213
214 void
215 ftree_chk(void)
216 {
217         FTREE *ft;
218         int wban = 0;
219
220         /*
221          * make sure all dir access times were reset.
222          */
223         if (tflag)
224                 atdir_end();
225
226         /*
227          * walk down list and check reference count. Print out those members
228          * that never had a match
229          */
230         for (ft = fthead; ft != NULL; ft = ft->fow) {
231                 if ((ft->refcnt > 0) || ft->chflg)
232                         continue;
233                 if (wban == 0) {
234                         paxwarn(1,"WARNING! These file names were not selected:");
235                         ++wban;
236                 }
237                 fprintf(stderr, "%s\n", ft->fname);
238         }
239 }
240
241 /*
242  * ftree_arg()
243  *      Get the next file arg for fts to process. Can be from either the linked
244  *      list or read from stdin when the user did not them as args to pax. Each
245  *      arg is processed until the first successful fts_open().
246  * Return:
247  *      0 when the next arg is ready to go, -1 if out of file args (or EOF on
248  *      stdin).
249  */
250
251 static int
252 ftree_arg(void)
253 {
254         char *pt;
255
256         /*
257          * close off the current file tree
258          */
259         if (ftsp != NULL) {
260                 fts_close(ftsp);
261                 ftsp = NULL;
262         }
263
264         /*
265          * keep looping until we get a valid file tree to process. Stop when we
266          * reach the end of the list (or get an eof on stdin)
267          */
268         for(;;) {
269                 if (fthead == NULL) {
270                         /*
271                          * the user didn't supply any args, get the file trees
272                          * to process from stdin;
273                          */
274                         if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL)
275                                 return(-1);
276                         if ((pt = strchr(farray[0], '\n')) != NULL)
277                                 *pt = '\0';
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
325 int
326 next_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                         /*
353                          * out of files in this tree, go to next arg, if none
354                          * we are done
355                          */
356                         if (ftree_arg() < 0)
357                                 return(-1);
358                         continue;
359                 }
360
361                 /*
362                  * handle each type of fts_read() flag
363                  */
364                 switch(ftent->fts_info) {
365                 case FTS_D:
366                 case FTS_DEFAULT:
367                 case FTS_F:
368                 case FTS_SL:
369                 case FTS_SLNONE:
370                         /*
371                          * these are all ok
372                          */
373                         break;
374                 case FTS_DP:
375                         /*
376                          * already saw this directory. If the user wants file
377                          * access times reset, we use this to restore the
378                          * access time for this directory since this is the
379                          * last time we will see it in this file subtree
380                          * remember to force the time (this is -t on a read
381                          * directory, not a created directory).
382                          */
383                         if (!tflag || (get_atdir(ftent->fts_statp->st_dev,
384                             ftent->fts_statp->st_ino, &mtime, &atime) < 0))
385                                 continue;
386                         set_ftime(ftent->fts_path, mtime, atime, 1);
387                         continue;
388                 case FTS_DC:
389                         /*
390                          * fts claims a file system cycle
391                          */
392                         paxwarn(1,"File system cycle found at %s",ftent->fts_path);
393                         continue;
394                 case FTS_DNR:
395                         syswarn(1, ftent->fts_errno,
396                             "Unable to read directory %s", ftent->fts_path);
397                         continue;
398                 case FTS_ERR:
399                         syswarn(1, ftent->fts_errno,
400                             "File system traversal error");
401                         continue;
402                 case FTS_NS:
403                 case FTS_NSOK:
404                         syswarn(1, ftent->fts_errno,
405                             "Unable to access %s", ftent->fts_path);
406                         continue;
407                 }
408
409                 /*
410                  * ok got a file tree node to process. copy info into arcn
411                  * structure (initialize as required)
412                  */
413                 arcn->skip = 0;
414                 arcn->pad = 0;
415                 arcn->ln_nlen = 0;
416                 arcn->ln_name[0] = '\0';
417                 arcn->sb = *(ftent->fts_statp);
418
419                 /*
420                  * file type based set up and copy into the arcn struct
421                  * SIDE NOTE:
422                  * we try to reset the access time on all files and directories
423                  * we may read when the -t flag is specified. files are reset
424                  * when we close them after copying. we reset the directories
425                  * when we are done with their file tree (we also clean up at
426                  * end in case we cut short a file tree traversal). However
427                  * there is no way to reset access times on symlinks.
428                  */
429                 switch(S_IFMT & arcn->sb.st_mode) {
430                 case S_IFDIR:
431                         arcn->type = PAX_DIR;
432                         if (!tflag)
433                                 break;
434                         add_atdir(ftent->fts_path, arcn->sb.st_dev,
435                             arcn->sb.st_ino, arcn->sb.st_mtime,
436                             arcn->sb.st_atime);
437                         break;
438                 case S_IFCHR:
439                         arcn->type = PAX_CHR;
440                         break;
441                 case S_IFBLK:
442                         arcn->type = PAX_BLK;
443                         break;
444                 case S_IFREG:
445                         /*
446                          * only regular files with have data to store on the
447                          * archive. all others will store a zero length skip.
448                          * the skip field is used by pax for actual data it has
449                          * to read (or skip over).
450                          */
451                         arcn->type = PAX_REG;
452                         arcn->skip = arcn->sb.st_size;
453                         break;
454                 case S_IFLNK:
455                         arcn->type = PAX_SLK;
456                         /*
457                          * have to read the symlink path from the file
458                          */
459                         if ((cnt = readlink(ftent->fts_path, arcn->ln_name,
460                             PAXPATHLEN - 1)) < 0) {
461                                 syswarn(1, errno, "Unable to read symlink %s",
462                                     ftent->fts_path);
463                                 continue;
464                         }
465                         /*
466                          * set link name length, watch out readlink does not
467                          * always NUL terminate the link path
468                          */
469                         arcn->ln_name[cnt] = '\0';
470                         arcn->ln_nlen = cnt;
471                         break;
472                 case S_IFSOCK:
473                         /*
474                          * under BSD storing a socket is senseless but we will
475                          * let the format specific write function make the
476                          * decision of what to do with it.
477                          */
478                         arcn->type = PAX_SCK;
479                         break;
480                 case S_IFIFO:
481                         arcn->type = PAX_FIF;
482                         break;
483                 }
484                 break;
485         }
486
487         /*
488          * copy file name, set file name length
489          */
490         arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, sizeof(arcn->name) - 1);
491         arcn->name[arcn->nlen] = '\0';
492         arcn->org_name = ftent->fts_path;
493         return(0);
494 }