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