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