ftree.c revision 21673
1266733Speter/*- 2266733Speter * Copyright (c) 1992 Keith Muller. 3266733Speter * Copyright (c) 1992, 1993 4266733Speter * The Regents of the University of California. All rights reserved. 5266733Speter * 6266733Speter * This code is derived from software contributed to Berkeley by 7266733Speter * Keith Muller of the University of California, San Diego. 8266733Speter * 9266733Speter * Redistribution and use in source and binary forms, with or without 10266733Speter * modification, are permitted provided that the following conditions 11266733Speter * are met: 12266733Speter * 1. Redistributions of source code must retain the above copyright 13266733Speter * notice, this list of conditions and the following disclaimer. 14266733Speter * 2. Redistributions in binary form must reproduce the above copyright 15266733Speter * notice, this list of conditions and the following disclaimer in the 16266733Speter * documentation and/or other materials provided with the distribution. 17266733Speter * 3. All advertising materials mentioning features or use of this software 18266733Speter * must display the following acknowledgement: 19266733Speter * This product includes software developed by the University of 20266733Speter * California, Berkeley and its contributors. 21266733Speter * 4. Neither the name of the University nor the names of its contributors 22266733Speter * may be used to endorse or promote products derived from this software 23266733Speter * without specific prior written permission. 24266733Speter * 25266733Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26266733Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27266733Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28266733Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29266733Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30266733Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31266733Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32266733Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33266733Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34266733Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35266733Speter * SUCH DAMAGE. 36266733Speter * 37266733Speter * $FreeBSD: head/bin/pax/ftree.c 21673 1997-01-14 07:20:47Z jkh $ 38266733Speter */ 39266733Speter 40266733Speter#ifndef lint 41266733Speterstatic char const sccsid[] = "@(#)ftree.c 8.2 (Berkeley) 4/18/94"; 42266733Speter#endif /* not lint */ 43266733Speter 44266733Speter#include <sys/types.h> 45266733Speter#include <sys/time.h> 46266733Speter#include <sys/stat.h> 47266733Speter#include <sys/param.h> 48266733Speter#include <unistd.h> 49266733Speter#include <string.h> 50266733Speter#include <stdio.h> 51266733Speter#include <errno.h> 52266733Speter#include <stdlib.h> 53266733Speter#include <fts.h> 54266733Speter#include "pax.h" 55266733Speter#include "ftree.h" 56266733Speter#include "extern.h" 57266733Speter 58266733Speter/* 59266733Speter * routines to interface with the fts library function. 60266733Speter * 61266733Speter * file args supplied to pax are stored on a single linked list (of type FTREE) 62266733Speter * and given to fts to be processed one at a time. pax "selects" files from 63266733Speter * the expansion of each arg into the corresponding file tree (if the arg is a 64266733Speter * directory, otherwise the node itself is just passed to pax). The selection 65266733Speter * is modified by the -n and -u flags. The user is informed when a specific 66266733Speter * file arg does not generate any selected files. -n keeps expanding the file 67266733Speter * tree arg until one of its files is selected, then skips to the next file 68266733Speter * arg. when the user does not supply the file trees as command line args to 69266733Speter * pax, they are read from stdin 70266733Speter */ 71266733Speter 72266733Speterstatic FTS *ftsp = NULL; /* curent FTS handle */ 73266733Speterstatic int ftsopts; /* options to be used on fts_open */ 74266733Speterstatic char *farray[2]; /* array for passing each arg to fts */ 75266733Speterstatic FTREE *fthead = NULL; /* head of linked list of file args */ 76266733Speterstatic FTREE *fttail = NULL; /* tail of linked list of file args */ 77266733Speterstatic FTREE *ftcur = NULL; /* current file arg being processed */ 78266733Speterstatic FTSENT *ftent = NULL; /* current file tree entry */ 79266733Speterstatic int ftree_skip; /* when set skip to next file arg */ 80266733Speter 81266733Speterstatic int ftree_arg __P((void)); 82266733Speter 83266733Speter/* 84266733Speter * ftree_start() 85266733Speter * initialize the options passed to fts_open() during this run of pax 86266733Speter * options are based on the selection of pax options by the user 87266733Speter * fts_start() also calls fts_arg() to open the first valid file arg. We 88266733Speter * also attempt to reset directory access times when -t (tflag) is set. 89266733Speter * Return: 90266733Speter * 0 if there is at least one valid file arg to process, -1 otherwise 91266733Speter */ 92266733Speter 93266733Speter#if __STDC__ 94266733Speterint 95266733Speterftree_start(void) 96266733Speter#else 97266733Speterint 98266733Speterftree_start() 99266733Speter#endif 100266733Speter{ 101266733Speter /* 102266733Speter * set up the operation mode of fts, open the first file arg. We must 103266733Speter * use FTS_NOCHDIR, as the user may have to open multiple archives and 104266733Speter * if fts did a chdir off into the boondocks, we may create an archive 105266733Speter * volume in an place where the user did not expect to. 106266733Speter */ 107266733Speter ftsopts = FTS_NOCHDIR; 108266733Speter 109266733Speter /* 110266733Speter * optional user flags that effect file traversal 111266733Speter * -H command line symlink follow only (half follow) 112266733Speter * -L follow sylinks (logical) 113266733Speter * -P do not follow sylinks (physical). This is the default. 114266733Speter * -X do not cross over mount points 115266733Speter * -t preserve access times on files read. 116266733Speter * -n select only the first member of a file tree when a match is found 117266733Speter * -d do not extract subtrees rooted at a directory arg. 118266733Speter */ 119266733Speter if (Lflag) 120266733Speter ftsopts |= FTS_LOGICAL; 121266733Speter else 122266733Speter ftsopts |= FTS_PHYSICAL; 123266733Speter if (Hflag) 124266733Speter# ifdef NET2_FTS 125266733Speter warn(0, "The -H flag is not supported on this version"); 126266733Speter# else 127266733Speter ftsopts |= FTS_COMFOLLOW; 128266733Speter# endif 129266733Speter if (Xflag) 130266733Speter ftsopts |= FTS_XDEV; 131266733Speter 132266733Speter if ((fthead == NULL) && ((farray[0] = malloc(PAXPATHLEN+2)) == NULL)) { 133266733Speter warn(1, "Unable to allocate memory for file name buffer"); 134266733Speter return(-1); 135266733Speter } 136266733Speter 137266733Speter if (ftree_arg() < 0) 138266733Speter return(-1); 139266733Speter if (tflag && (atdir_start() < 0)) 140266733Speter return(-1); 141266733Speter return(0); 142266733Speter} 143266733Speter 144266733Speter/* 145266733Speter * ftree_add() 146266733Speter * add the arg to the linked list of files to process. Each will be 147266733Speter * processed by fts one at a time 148266733Speter * Return: 149266733Speter * 0 if added to the linked list, -1 if failed 150266733Speter */ 151266733Speter 152266733Speter#if __STDC__ 153266733Speterint 154266733Speterftree_add(register char *str) 155266733Speter#else 156266733Speterint 157266733Speterftree_add(str) 158266733Speter register char *str; 159266733Speter#endif 160266733Speter{ 161266733Speter register FTREE *ft; 162266733Speter register int len; 163266733Speter 164266733Speter /* 165266733Speter * simple check for bad args 166266733Speter */ 167266733Speter if ((str == NULL) || (*str == '\0')) { 168266733Speter warn(0, "Invalid file name arguement"); 169266733Speter return(-1); 170266733Speter } 171266733Speter 172266733Speter /* 173266733Speter * allocate FTREE node and add to the end of the linked list (args are 174266733Speter * processed in the same order they were passed to pax). Get rid of any 175266733Speter * trailing / the user may pass us. (watch out for / by itself). 176266733Speter */ 177266733Speter if ((ft = (FTREE *)malloc(sizeof(FTREE))) == NULL) { 178266733Speter warn(0, "Unable to allocate memory for filename"); 179266733Speter return(-1); 180266733Speter } 181266733Speter 182266733Speter if (((len = strlen(str) - 1) > 0) && (str[len] == '/')) 183266733Speter str[len] = '\0'; 184266733Speter ft->fname = str; 185266733Speter ft->refcnt = 0; 186266733Speter ft->fow = NULL; 187266733Speter if (fthead == NULL) { 188266733Speter fttail = fthead = ft; 189266733Speter return(0); 190266733Speter } 191266733Speter fttail->fow = ft; 192266733Speter fttail = ft; 193266733Speter return(0); 194266733Speter} 195266733Speter 196266733Speter/* 197266733Speter * ftree_sel() 198266733Speter * this entry has been selected by pax. bump up reference count and handle 199266733Speter * -n and -d processing. 200266733Speter */ 201266733Speter 202266733Speter#if __STDC__ 203266733Spetervoid 204266733Speterftree_sel(register ARCHD *arcn) 205266733Speter#else 206266733Spetervoid 207266733Speterftree_sel(arcn) 208266733Speter register ARCHD *arcn; 209266733Speter#endif 210266733Speter{ 211266733Speter /* 212266733Speter * set reference bit for this pattern. This linked list is only used 213266733Speter * when file trees are supplied pax as args. The list is not used when 214266733Speter * the trees are read from stdin. 215266733Speter */ 216266733Speter if (ftcur != NULL) 217266733Speter ftcur->refcnt = 1; 218266733Speter 219266733Speter /* 220266733Speter * if -n we are done with this arg, force a skip to the next arg when 221266733Speter * pax asks for the next file in next_file(). 222266733Speter * if -d we tell fts only to match the directory (if the arg is a dir) 223266733Speter * and not the entire file tree rooted at that point. 224266733Speter */ 225266733Speter if (nflag) 226266733Speter ftree_skip = 1; 227266733Speter 228266733Speter if (!dflag || (arcn->type != PAX_DIR)) 229266733Speter return; 230266733Speter 231266733Speter if (ftent != NULL) 232266733Speter (void)fts_set(ftsp, ftent, FTS_SKIP); 233266733Speter} 234266733Speter 235266733Speter/* 236266733Speter * ftree_chk() 237266733Speter * called at end on pax execution. Prints all those file args that did not 238266733Speter * have a selected member (reference count still 0) 239266733Speter */ 240266733Speter 241266733Speter#if __STDC__ 242266733Spetervoid 243266733Speterftree_chk(void) 244266733Speter#else 245266733Spetervoid 246266733Speterftree_chk() 247266733Speter#endif 248266733Speter{ 249266733Speter register FTREE *ft; 250266733Speter register int wban = 0; 251266733Speter 252266733Speter /* 253266733Speter * make sure all dir access times were reset. 254266733Speter */ 255266733Speter if (tflag) 256266733Speter atdir_end(); 257266733Speter 258266733Speter /* 259266733Speter * walk down list and check reference count. Print out those members 260266733Speter * that never had a match 261266733Speter */ 262266733Speter for (ft = fthead; ft != NULL; ft = ft->fow) { 263266733Speter if (ft->refcnt > 0) 264266733Speter continue; 265266733Speter if (wban == 0) { 266266733Speter warn(1,"WARNING! These file names were not selected:"); 267266733Speter ++wban; 268266733Speter } 269266733Speter (void)fprintf(stderr, "%s\n", ft->fname); 270266733Speter } 271266733Speter} 272266733Speter 273266733Speter/* 274266733Speter * ftree_arg() 275266733Speter * Get the next file arg for fts to process. Can be from either the linked 276266733Speter * list or read from stdin when the user did not them as args to pax. Each 277266733Speter * arg is processed until the first successful fts_open(). 278266733Speter * Return: 279266733Speter * 0 when the next arg is ready to go, -1 if out of file args (or EOF on 280266733Speter * stdin). 281266733Speter */ 282266733Speter 283266733Speter#if __STDC__ 284266733Speterstatic int 285266733Speterftree_arg(void) 286266733Speter#else 287266733Speterstatic int 288266733Speterftree_arg() 289266733Speter#endif 290266733Speter{ 291266733Speter register char *pt; 292266733Speter 293266733Speter /* 294266733Speter * close off the current file tree 295266733Speter */ 296266733Speter if (ftsp != NULL) { 297266733Speter (void)fts_close(ftsp); 298266733Speter ftsp = NULL; 299266733Speter } 300266733Speter 301266733Speter /* 302266733Speter * keep looping until we get a valid file tree to process. Stop when we 303266733Speter * reach the end of the list (or get an eof on stdin) 304266733Speter */ 305266733Speter for(;;) { 306266733Speter if (fthead == NULL) { 307266733Speter /* 308266733Speter * the user didn't supply any args, get the file trees 309266733Speter * to process from stdin; 310266733Speter */ 311266733Speter if (fgets(farray[0], PAXPATHLEN+1, stdin) == NULL) 312266733Speter return(-1); 313266733Speter if ((pt = strchr(farray[0], '\n')) != NULL) 314266733Speter *pt = '\0'; 315266733Speter } else { 316266733Speter /* 317266733Speter * the user supplied the file args as arguements to pax 318266733Speter */ 319266733Speter if (ftcur == NULL) 320266733Speter ftcur = fthead; 321266733Speter else if ((ftcur = ftcur->fow) == NULL) 322266733Speter return(-1); 323266733Speter farray[0] = ftcur->fname; 324266733Speter } 325266733Speter 326266733Speter /* 327266733Speter * watch it, fts wants the file arg stored in a array of char 328266733Speter * ptrs, with the last one a null. we use a two element array 329266733Speter * and set farray[0] to point at the buffer with the file name 330266733Speter * in it. We cannnot pass all the file args to fts at one shot 331266733Speter * as we need to keep a handle on which file arg generates what 332266733Speter * files (the -n and -d flags need this). If the open is 333266733Speter * successful, return a 0. 334266733Speter */ 335266733Speter if ((ftsp = fts_open(farray, ftsopts, NULL)) != NULL) 336266733Speter break; 337266733Speter } 338266733Speter return(0); 339266733Speter} 340266733Speter 341266733Speter/* 342266733Speter * next_file() 343266733Speter * supplies the next file to process in the supplied archd structure. 344266733Speter * Return: 345266733Speter * 0 when contents of arcn have been set with the next file, -1 when done. 346266733Speter */ 347266733Speter 348266733Speter#if __STDC__ 349266733Speterint 350266733Speternext_file(register ARCHD *arcn) 351266733Speter#else 352266733Speterint 353266733Speternext_file(arcn) 354266733Speter register ARCHD *arcn; 355266733Speter#endif 356266733Speter{ 357266733Speter register int cnt; 358266733Speter time_t atime; 359266733Speter time_t mtime; 360266733Speter 361266733Speter /* 362266733Speter * ftree_sel() might have set the ftree_skip flag if the user has the 363266733Speter * -n option and a file was selected from this file arg tree. (-n says 364266733Speter * only one member is matched for each pattern) ftree_skip being 1 365266733Speter * forces us to go to the next arg now. 366266733Speter */ 367266733Speter if (ftree_skip) { 368266733Speter /* 369266733Speter * clear and go to next arg 370266733Speter */ 371266733Speter ftree_skip = 0; 372266733Speter if (ftree_arg() < 0) 373266733Speter return(-1); 374266733Speter } 375266733Speter 376266733Speter /* 377266733Speter * loop until we get a valid file to process 378266733Speter */ 379266733Speter for(;;) { 380266733Speter if ((ftent = fts_read(ftsp)) == NULL) { 381266733Speter /* 382266733Speter * out of files in this tree, go to next arg, if none 383266733Speter * we are done 384266733Speter */ 385266733Speter if (ftree_arg() < 0) 386266733Speter return(-1); 387266733Speter continue; 388266733Speter } 389266733Speter 390266733Speter /* 391266733Speter * handle each type of fts_read() flag 392266733Speter */ 393266733Speter switch(ftent->fts_info) { 394266733Speter case FTS_D: 395266733Speter case FTS_DEFAULT: 396266733Speter case FTS_F: 397266733Speter case FTS_SL: 398266733Speter case FTS_SLNONE: 399266733Speter /* 400266733Speter * these are all ok 401266733Speter */ 402266733Speter break; 403266733Speter case FTS_DP: 404266733Speter /* 405266733Speter * already saw this directory. If the user wants file 406266733Speter * access times reset, we use this to restore the 407266733Speter * access time for this directory since this is the 408266733Speter * last time we will see it in this file subtree 409266733Speter * remember to force the time (this is -t on a read 410266733Speter * directory, not a created directory). 411266733Speter */ 412266733Speter# ifdef NET2_FTS 413266733Speter if (!tflag || (get_atdir(ftent->fts_statb.st_dev, 414266733Speter ftent->fts_statb.st_ino, &mtime, &atime) < 0)) 415266733Speter# else 416266733Speter if (!tflag || (get_atdir(ftent->fts_statp->st_dev, 417266733Speter ftent->fts_statp->st_ino, &mtime, &atime) < 0)) 418266733Speter# endif 419266733Speter continue; 420266733Speter set_ftime(ftent->fts_path, mtime, atime, 1); 421266733Speter continue; 422266733Speter case FTS_DC: 423266733Speter /* 424266733Speter * fts claims a file system cycle 425266733Speter */ 426266733Speter warn(1,"File system cycle found at %s",ftent->fts_path); 427266733Speter continue; 428266733Speter case FTS_DNR: 429266733Speter# ifdef NET2_FTS 430266733Speter syswarn(1, errno, 431266733Speter# else 432266733Speter syswarn(1, ftent->fts_errno, 433266733Speter# endif 434266733Speter "Unable to read directory %s", ftent->fts_path); 435266733Speter continue; 436266733Speter case FTS_ERR: 437266733Speter# ifdef NET2_FTS 438266733Speter syswarn(1, errno, 439266733Speter# else 440266733Speter syswarn(1, ftent->fts_errno, 441266733Speter# endif 442266733Speter "File system traversal error"); 443266733Speter continue; 444266733Speter case FTS_NS: 445266733Speter case FTS_NSOK: 446266733Speter# ifdef NET2_FTS 447266733Speter syswarn(1, errno, 448266733Speter# else 449266733Speter syswarn(1, ftent->fts_errno, 450266733Speter# endif 451266733Speter "Unable to access %s", ftent->fts_path); 452266733Speter continue; 453266733Speter } 454266733Speter 455266733Speter /* 456266733Speter * ok got a file tree node to process. copy info into arcn 457266733Speter * structure (initialize as required) 458266733Speter */ 459266733Speter arcn->skip = 0; 460266733Speter arcn->pad = 0; 461266733Speter arcn->ln_nlen = 0; 462266733Speter arcn->ln_name[0] = '\0'; 463266733Speter# ifdef NET2_FTS 464266733Speter arcn->sb = ftent->fts_statb; 465266733Speter# else 466266733Speter arcn->sb = *(ftent->fts_statp); 467266733Speter# endif 468266733Speter 469266733Speter /* 470266733Speter * file type based set up and copy into the arcn struct 471266733Speter * SIDE NOTE: 472266733Speter * we try to reset the access time on all files and directories 473266733Speter * we may read when the -t flag is specified. files are reset 474266733Speter * when we close them after copying. we reset the directories 475266733Speter * when we are done with their file tree (we also clean up at 476266733Speter * end in case we cut short a file tree traversal). However 477266733Speter * there is no way to reset access times on symlinks. 478266733Speter */ 479266733Speter switch(S_IFMT & arcn->sb.st_mode) { 480266733Speter case S_IFDIR: 481266733Speter arcn->type = PAX_DIR; 482266733Speter if (!tflag) 483266733Speter break; 484266733Speter add_atdir(ftent->fts_path, arcn->sb.st_dev, 485266733Speter arcn->sb.st_ino, arcn->sb.st_mtime, 486266733Speter arcn->sb.st_atime); 487266733Speter break; 488266733Speter case S_IFCHR: 489266733Speter arcn->type = PAX_CHR; 490266733Speter break; 491266733Speter case S_IFBLK: 492266733Speter arcn->type = PAX_BLK; 493266733Speter break; 494266733Speter case S_IFREG: 495266733Speter /* 496266733Speter * only regular files with have data to store on the 497266733Speter * archive. all others will store a zero length skip. 498266733Speter * the skip field is used by pax for actual data it has 499266733Speter * to read (or skip over). 500266733Speter */ 501266733Speter arcn->type = PAX_REG; 502266733Speter arcn->skip = arcn->sb.st_size; 503266733Speter break; 504266733Speter case S_IFLNK: 505266733Speter arcn->type = PAX_SLK; 506266733Speter /* 507266733Speter * have to read the symlink path from the file 508266733Speter */ 509266733Speter if ((cnt = readlink(ftent->fts_path, arcn->ln_name, 510266733Speter PAXPATHLEN)) < 0) { 511266733Speter syswarn(1, errno, "Unable to read symlink %s", 512266733Speter ftent->fts_path); 513266733Speter continue; 514266733Speter } 515266733Speter /* 516266733Speter * set link name length, watch out readlink does not 517266733Speter * allways null terminate the link path 518266733Speter */ 519266733Speter arcn->ln_name[cnt] = '\0'; 520266733Speter arcn->ln_nlen = cnt; 521266733Speter break; 522266733Speter case S_IFSOCK: 523266733Speter /* 524266733Speter * under BSD storing a socket is senseless but we will 525266733Speter * let the format specific write function make the 526266733Speter * decision of what to do with it. 527266733Speter */ 528266733Speter arcn->type = PAX_SCK; 529266733Speter break; 530266733Speter case S_IFIFO: 531266733Speter arcn->type = PAX_FIF; 532266733Speter break; 533266733Speter } 534266733Speter break; 535266733Speter } 536266733Speter 537266733Speter /* 538266733Speter * copy file name, set file name length 539266733Speter */ 540266733Speter arcn->nlen = l_strncpy(arcn->name, ftent->fts_path, PAXPATHLEN+1); 541266733Speter arcn->name[arcn->nlen] = '\0'; 542266733Speter arcn->org_name = ftent->fts_path; 543266733Speter return(0); 544266733Speter} 545266733Speter