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