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