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