1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 1996-2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <locale.h> 30*0Sstevel@tonic-gate #include <sys/types.h> 31*0Sstevel@tonic-gate #include <sys/param.h> 32*0Sstevel@tonic-gate #include <stdio.h> 33*0Sstevel@tonic-gate #include <string.h> 34*0Sstevel@tonic-gate #include <errno.h> 35*0Sstevel@tonic-gate #include <search.h> 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include <dirent.h> 38*0Sstevel@tonic-gate #include <fnmatch.h> 39*0Sstevel@tonic-gate #include <sys/stat.h> 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include "rules.h" 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate extern char *mstrdup(const char *); 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * The do_base_dir() function is called when a BASE command is encountered 47*0Sstevel@tonic-gate * in the input directives or end-of-file is reach on the input directive 48*0Sstevel@tonic-gate * file. This function causes the commands associated with the previous 49*0Sstevel@tonic-gate * BASE command to be executed. for example, 50*0Sstevel@tonic-gate * 51*0Sstevel@tonic-gate * BASE a/b/c 52*0Sstevel@tonic-gate * LIST ... 53*0Sstevel@tonic-gate * IGNORE ... 54*0Sstevel@tonic-gate * BASE d/e/f (this command will cause do_base_dir() to be called for 55*0Sstevel@tonic-gate * base directory a/b/c) 56*0Sstevel@tonic-gate * 57*0Sstevel@tonic-gate * Input arguments: 58*0Sstevel@tonic-gate * dirpath - the BASE directory being operated on 59*0Sstevel@tonic-gate * incld_lst - A list of strings to be matched obtained from the 60*0Sstevel@tonic-gate * LIST commands associated with the BASE directory. 61*0Sstevel@tonic-gate * excld_lst - A list of strings to be matched obtained from the 62*0Sstevel@tonic-gate * IGNORE commands associated with the BASE directory. 63*0Sstevel@tonic-gate * func - A function to be called for each matched file. The 64*0Sstevel@tonic-gate * functions allow files to be packed, unpacked, 65*0Sstevel@tonic-gate * examined and filenames printed. 66*0Sstevel@tonic-gate */ 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate void 69*0Sstevel@tonic-gate do_base_dir(char *dirpath, struct item *incld_lst, struct item *excld_lst, 70*0Sstevel@tonic-gate int (*func)(char *, char *, DIR *, int)) 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate struct item *iitem; 73*0Sstevel@tonic-gate int err; 74*0Sstevel@tonic-gate int files_processed = 0; 75*0Sstevel@tonic-gate struct item symlk_hd, *symlk, *symlk_sv; 76*0Sstevel@tonic-gate struct stat64 statbuf; 77*0Sstevel@tonic-gate char linkbuf[MAXPATHLEN]; 78*0Sstevel@tonic-gate int sz; 79*0Sstevel@tonic-gate char *s; 80*0Sstevel@tonic-gate char *mk_base_dir(char *, char *); 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate #ifdef DEBUG 83*0Sstevel@tonic-gate prtitem("Global IGNOREs", &gign_hd); 84*0Sstevel@tonic-gate prtitem("LIST cmds", &list_hd); 85*0Sstevel@tonic-gate prtitem("Local IGNOREs", &lign_hd); 86*0Sstevel@tonic-gate #endif /* DEBUG */ 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate symlk = &symlk_hd; 90*0Sstevel@tonic-gate symlk_hd.i_next = (struct item *)0; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate iitem = incld_lst->i_next; 93*0Sstevel@tonic-gate if (iitem == (struct item *)0) 94*0Sstevel@tonic-gate return; 95*0Sstevel@tonic-gate while (iitem != (struct item *)0) { 96*0Sstevel@tonic-gate #ifdef DEBUG 97*0Sstevel@tonic-gate printf("do_base_dir: iitem->i_str = %s iitem->i_flag = %x\n", 98*0Sstevel@tonic-gate iitem->i_str, iitem->i_flag); 99*0Sstevel@tonic-gate fflush(stdout); 100*0Sstevel@tonic-gate #endif /* DEBUG */ 101*0Sstevel@tonic-gate err = do_list_item(dirpath, iitem->i_str, 102*0Sstevel@tonic-gate iitem->i_flag, excld_lst, 0, &symlk, func); 103*0Sstevel@tonic-gate if (err == 0) { 104*0Sstevel@tonic-gate fprintf(stderr, 105*0Sstevel@tonic-gate gettext("cachefspack: basedir = %s"), 106*0Sstevel@tonic-gate dirpath); 107*0Sstevel@tonic-gate fprintf(stderr, 108*0Sstevel@tonic-gate gettext(" %s - no file(s) selected\n"), 109*0Sstevel@tonic-gate iitem->i_str); 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate iitem = iitem->i_next; 112*0Sstevel@tonic-gate }; 113*0Sstevel@tonic-gate /* 114*0Sstevel@tonic-gate * Invoke 'func' for each component of the BASE 115*0Sstevel@tonic-gate * directory. 116*0Sstevel@tonic-gate */ 117*0Sstevel@tonic-gate func_dir_path(dirpath, func); 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate if (lstat64(dirpath, &statbuf) < 0) { 120*0Sstevel@tonic-gate perror(gettext("Can't stat base directory")); 121*0Sstevel@tonic-gate } else { 122*0Sstevel@tonic-gate if (S_ISLNK(statbuf.st_mode)) { 123*0Sstevel@tonic-gate sz = readlink(dirpath, linkbuf, MAXPATHLEN-1); 124*0Sstevel@tonic-gate if (sz > 0) { 125*0Sstevel@tonic-gate linkbuf[sz] = '\0'; 126*0Sstevel@tonic-gate s = mk_base_dir(dirpath, linkbuf); 127*0Sstevel@tonic-gate if (s != (char *)0) { 128*0Sstevel@tonic-gate func_dir_path(s, func); 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate #ifdef DEBUG 135*0Sstevel@tonic-gate prtitem("Symbolic Links", &symlk_hd); 136*0Sstevel@tonic-gate #endif /* DEBUG */ 137*0Sstevel@tonic-gate iitem = symlk_hd.i_next; 138*0Sstevel@tonic-gate if (iitem == (struct item *)0) 139*0Sstevel@tonic-gate return; 140*0Sstevel@tonic-gate while (iitem != (struct item *)0) { 141*0Sstevel@tonic-gate #ifdef DEBUG 142*0Sstevel@tonic-gate printf("do_bas sl: iitem->i_str = %s iitem->i_flag = %x\n", 143*0Sstevel@tonic-gate iitem->i_str, iitem->i_flag); 144*0Sstevel@tonic-gate fflush(stdout); 145*0Sstevel@tonic-gate #endif /* DEBUG */ 146*0Sstevel@tonic-gate files_processed = do_list_item(iitem->i_str, "*", 147*0Sstevel@tonic-gate (LF_SYMLINK | LF_REGEX), excld_lst, 0, &symlk, func); 148*0Sstevel@tonic-gate if (files_processed) { 149*0Sstevel@tonic-gate /* 150*0Sstevel@tonic-gate * Invoke 'func' for each component of the BASE 151*0Sstevel@tonic-gate * directory. 152*0Sstevel@tonic-gate */ 153*0Sstevel@tonic-gate func_dir_path(iitem->i_str, func); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate symlk_sv = iitem; 156*0Sstevel@tonic-gate iitem = iitem->i_next; 157*0Sstevel@tonic-gate symlk_hd.i_next = iitem; 158*0Sstevel@tonic-gate free(symlk_sv); 159*0Sstevel@tonic-gate #ifdef DEBUG 160*0Sstevel@tonic-gate prtitem("Symbolic Links loop", &symlk_hd); 161*0Sstevel@tonic-gate #endif /* DEBUG */ 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate /* 166*0Sstevel@tonic-gate * The do_list_item() function is called for each LIST item associated with 167*0Sstevel@tonic-gate * a BASE directory. It does the work of descending directories and matching 168*0Sstevel@tonic-gate * filenames. 169*0Sstevel@tonic-gate * 170*0Sstevel@tonic-gate * Input arguments: 171*0Sstevel@tonic-gate * dirpath - the BASE directory being operated on 172*0Sstevel@tonic-gate * pat - The argument from the LIST command to match 173*0Sstevel@tonic-gate * flags - Flags which affect how patterns are matched: 174*0Sstevel@tonic-gate * LF_STRIP_DOTSLASH - means strip off "." and/or "/" at the 175*0Sstevel@tonic-gate * beginning of the pattern to match. 176*0Sstevel@tonic-gate * LF_REGEX - Means match the pattern as a regular expression. 177*0Sstevel@tonic-gate * Otherwise, an exact match of characters is required. 178*0Sstevel@tonic-gate * excld_lst - A list of strings to be matched obtained from the 179*0Sstevel@tonic-gate * IGNORE commands associated with the BASE directory. 180*0Sstevel@tonic-gate * func - A function to be called for each matched file. The 181*0Sstevel@tonic-gate * functions allow files to be packed, unpacked, 182*0Sstevel@tonic-gate * examined and filenames printed. 183*0Sstevel@tonic-gate * 184*0Sstevel@tonic-gate * Return values: 185*0Sstevel@tonic-gate * 0 - 'func' NOT invoked for any file 186*0Sstevel@tonic-gate * 1 - 'func' invoked for at least 1 file 187*0Sstevel@tonic-gate */ 188*0Sstevel@tonic-gate int 189*0Sstevel@tonic-gate do_list_item(char *dirpath, char *pat, int flags, struct item *excld_lst, 190*0Sstevel@tonic-gate DIR *pdir, struct item **symlk_lst, int (*func)(char *, char *, DIR *, int)) 191*0Sstevel@tonic-gate { 192*0Sstevel@tonic-gate static char statnam[MAXPATHLEN]; 193*0Sstevel@tonic-gate static int glastpos = 0; 194*0Sstevel@tonic-gate static int basedir_lastpos; 195*0Sstevel@tonic-gate static int depth = 0; 196*0Sstevel@tonic-gate static int unwind = 0; 197*0Sstevel@tonic-gate static int do_dir = 0; 198*0Sstevel@tonic-gate static int sl_cnt; 199*0Sstevel@tonic-gate static int retval; 200*0Sstevel@tonic-gate static char linkbuf[MAXPATHLEN]; 201*0Sstevel@tonic-gate DIR *dir, *parent_dir; 202*0Sstevel@tonic-gate struct dirent64 *dirent; 203*0Sstevel@tonic-gate int match; 204*0Sstevel@tonic-gate int err; 205*0Sstevel@tonic-gate struct stat64 statbuf; 206*0Sstevel@tonic-gate int llastpos; 207*0Sstevel@tonic-gate struct item *eitem; 208*0Sstevel@tonic-gate int excld_flag; 209*0Sstevel@tonic-gate char *p; 210*0Sstevel@tonic-gate int diropn; 211*0Sstevel@tonic-gate int len; 212*0Sstevel@tonic-gate int sz; 213*0Sstevel@tonic-gate void process_symlk(); 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate strcpy(&statnam[glastpos], dirpath); 216*0Sstevel@tonic-gate len = strlen(statnam) - 1; 217*0Sstevel@tonic-gate if (statnam[len] != '/') { 218*0Sstevel@tonic-gate strcat(statnam, "/"); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate parent_dir = pdir; 221*0Sstevel@tonic-gate llastpos = glastpos; 222*0Sstevel@tonic-gate glastpos = strlen(statnam); 223*0Sstevel@tonic-gate if (depth == 0) { 224*0Sstevel@tonic-gate basedir_lastpos = glastpos; 225*0Sstevel@tonic-gate sl_cnt = slash_cnt(pat); 226*0Sstevel@tonic-gate retval = 0; 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate depth++; 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate diropn = 0; 231*0Sstevel@tonic-gate dir = opendir(statnam); 232*0Sstevel@tonic-gate if (dir == NULL) { 233*0Sstevel@tonic-gate fprintf(stderr, gettext("\ncachefspack: %s - "), statnam); 234*0Sstevel@tonic-gate perror(gettext("Can't open directory")); 235*0Sstevel@tonic-gate goto out; 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate diropn = 1; 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate while (1) { 240*0Sstevel@tonic-gate dirent = readdir64(dir); 241*0Sstevel@tonic-gate if (dirent == NULL) { /* EOF */ 242*0Sstevel@tonic-gate if ((depth-1) > do_dir) { 243*0Sstevel@tonic-gate do_dir = depth - 1; 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate break; 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate /* 248*0Sstevel@tonic-gate * If file is '..' skip it 249*0Sstevel@tonic-gate */ 250*0Sstevel@tonic-gate if (strcmp(dirent->d_name, "..") == 0) { 251*0Sstevel@tonic-gate continue; 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate /* 254*0Sstevel@tonic-gate * Apply excludes if this is not a LISTed directory 255*0Sstevel@tonic-gate * NOTE: names from IGNORE commands are matched against the 256*0Sstevel@tonic-gate * component name(a name between '/' marks), not the 257*0Sstevel@tonic-gate * whole pathname. 258*0Sstevel@tonic-gate */ 259*0Sstevel@tonic-gate if (flags & LF_SYMLINK) { 260*0Sstevel@tonic-gate match = ((depth-1) >= sl_cnt); 261*0Sstevel@tonic-gate } else { 262*0Sstevel@tonic-gate match = ((depth-1) > sl_cnt); 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate if (match) { 265*0Sstevel@tonic-gate eitem = excld_lst->i_next; 266*0Sstevel@tonic-gate excld_flag = 0; 267*0Sstevel@tonic-gate while (eitem != (struct item *)0) { 268*0Sstevel@tonic-gate match = gmatch(dirent->d_name, eitem->i_str); 269*0Sstevel@tonic-gate if (match == 1) { 270*0Sstevel@tonic-gate excld_flag = 1; 271*0Sstevel@tonic-gate break; 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate eitem = eitem->i_next; 274*0Sstevel@tonic-gate } 275*0Sstevel@tonic-gate if (excld_flag == 1) { 276*0Sstevel@tonic-gate continue; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate strcpy(&statnam[glastpos], dirent->d_name); 280*0Sstevel@tonic-gate err = lstat64(statnam, &statbuf); 281*0Sstevel@tonic-gate if (err < 0) { 282*0Sstevel@tonic-gate fprintf(stderr, 283*0Sstevel@tonic-gate gettext("cachefspack: %s - stat failed"), 284*0Sstevel@tonic-gate statnam); 285*0Sstevel@tonic-gate perror(gettext(" ")); 286*0Sstevel@tonic-gate continue; 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate p = pat; 289*0Sstevel@tonic-gate if (flags & LF_STRIP_DOTSLASH) { 290*0Sstevel@tonic-gate if (strncmp(p, "./", 2) == 0) { 291*0Sstevel@tonic-gate p += 2; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate if (S_ISDIR(statbuf.st_mode)) { 295*0Sstevel@tonic-gate #ifdef DEBUG 296*0Sstevel@tonic-gate printf("directory: &statnam[basedir_lastpos] = %s\n", 297*0Sstevel@tonic-gate &statnam[basedir_lastpos]); 298*0Sstevel@tonic-gate printf("statbuf.st_mode = %o\n", statbuf.st_mode); 299*0Sstevel@tonic-gate printf("depth = %d sl_cnt = %d\n", depth, sl_cnt); 300*0Sstevel@tonic-gate fflush(stdout); 301*0Sstevel@tonic-gate #endif /* DEBUG */ 302*0Sstevel@tonic-gate if ((depth-1) == sl_cnt) { 303*0Sstevel@tonic-gate if (flags & LF_REGEX) { 304*0Sstevel@tonic-gate match = 305*0Sstevel@tonic-gate gmatch(&statnam[basedir_lastpos], 306*0Sstevel@tonic-gate p); 307*0Sstevel@tonic-gate } else { 308*0Sstevel@tonic-gate match = 309*0Sstevel@tonic-gate (strcmp(&statnam[basedir_lastpos], 310*0Sstevel@tonic-gate p) == 0); 311*0Sstevel@tonic-gate } 312*0Sstevel@tonic-gate if (match) { 313*0Sstevel@tonic-gate /* 314*0Sstevel@tonic-gate * Don't descend '.' directory 315*0Sstevel@tonic-gate * but match it 316*0Sstevel@tonic-gate */ 317*0Sstevel@tonic-gate if (strcmp(dirent->d_name, ".") != 0) { 318*0Sstevel@tonic-gate do_list_item(dirent->d_name, 319*0Sstevel@tonic-gate "*", flags, excld_lst, 320*0Sstevel@tonic-gate dir, symlk_lst, func); 321*0Sstevel@tonic-gate } else { 322*0Sstevel@tonic-gate if ((depth-1) > do_dir) { 323*0Sstevel@tonic-gate do_dir = depth - 1; 324*0Sstevel@tonic-gate } 325*0Sstevel@tonic-gate (void) func(statnam, 326*0Sstevel@tonic-gate dirent->d_name, 327*0Sstevel@tonic-gate dir, depth); 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate retval = 1; 330*0Sstevel@tonic-gate if (unwind = discont_srch(flags, p)) { 331*0Sstevel@tonic-gate goto out; 332*0Sstevel@tonic-gate } 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate continue; 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate /* 337*0Sstevel@tonic-gate * Don't descend '.' directory 338*0Sstevel@tonic-gate */ 339*0Sstevel@tonic-gate if (strcmp(dirent->d_name, ".") != 0) { 340*0Sstevel@tonic-gate do_list_item(dirent->d_name, p, flags, 341*0Sstevel@tonic-gate excld_lst, dir, symlk_lst, func); 342*0Sstevel@tonic-gate } 343*0Sstevel@tonic-gate if (unwind) { 344*0Sstevel@tonic-gate goto out; 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate continue; 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate if (S_ISLNK(statbuf.st_mode)) { 349*0Sstevel@tonic-gate if (flags & LF_SYMLINK) 350*0Sstevel@tonic-gate continue; 351*0Sstevel@tonic-gate #ifdef DEBUG 352*0Sstevel@tonic-gate printf("sym link : &statnam[basedir_lastpos] = %s\n", 353*0Sstevel@tonic-gate &statnam[basedir_lastpos]); 354*0Sstevel@tonic-gate printf("statbuf.st_mode = %o\n", statbuf.st_mode); 355*0Sstevel@tonic-gate printf("statnam = %s\n", statnam); 356*0Sstevel@tonic-gate #endif /* DEBUG */ 357*0Sstevel@tonic-gate /* 358*0Sstevel@tonic-gate * Symbolic link was explicitly specified or matches a 359*0Sstevel@tonic-gate * regular expression in a LIST item. Thus we follow 360*0Sstevel@tonic-gate * the link. Otherwise, just call 'func' for the link 361*0Sstevel@tonic-gate * name. 362*0Sstevel@tonic-gate */ 363*0Sstevel@tonic-gate #ifdef DEBUG 364*0Sstevel@tonic-gate printf("depth = %d sl_cnt = %d\n", depth, sl_cnt); 365*0Sstevel@tonic-gate fflush(stdout); 366*0Sstevel@tonic-gate #endif /* DEBUG */ 367*0Sstevel@tonic-gate if ((depth-1) == sl_cnt) { 368*0Sstevel@tonic-gate if (flags & LF_REGEX) { 369*0Sstevel@tonic-gate match = 370*0Sstevel@tonic-gate gmatch(&statnam[basedir_lastpos], 371*0Sstevel@tonic-gate p); 372*0Sstevel@tonic-gate } else { 373*0Sstevel@tonic-gate match = 374*0Sstevel@tonic-gate (strcmp(&statnam[basedir_lastpos], 375*0Sstevel@tonic-gate p) == 0); 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate #ifdef DEBUG 378*0Sstevel@tonic-gate printf("match = %d\n", match); 379*0Sstevel@tonic-gate fflush(stdout); 380*0Sstevel@tonic-gate #endif /* DEBUG */ 381*0Sstevel@tonic-gate if (match) { 382*0Sstevel@tonic-gate if ((depth-1) > do_dir) { 383*0Sstevel@tonic-gate do_dir = depth - 1; 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate retval = 1; 386*0Sstevel@tonic-gate (void) func(statnam, dirent->d_name, 387*0Sstevel@tonic-gate dir, depth); 388*0Sstevel@tonic-gate sz = readlink( 389*0Sstevel@tonic-gate statnam, linkbuf, MAXPATHLEN-1); 390*0Sstevel@tonic-gate #ifdef DEBUG 391*0Sstevel@tonic-gate printf("linkbuf = %s\n", linkbuf); 392*0Sstevel@tonic-gate printf("sz = %d\n", sz); 393*0Sstevel@tonic-gate fflush(stdout); 394*0Sstevel@tonic-gate #endif /* DEBUG */ 395*0Sstevel@tonic-gate if (sz < 0) { 396*0Sstevel@tonic-gate continue; 397*0Sstevel@tonic-gate } 398*0Sstevel@tonic-gate linkbuf[sz] = '\0'; 399*0Sstevel@tonic-gate process_symlk(linkbuf, statnam, 400*0Sstevel@tonic-gate glastpos, symlk_lst, func); 401*0Sstevel@tonic-gate if (unwind = discont_srch(flags, p)) { 402*0Sstevel@tonic-gate goto out; 403*0Sstevel@tonic-gate } 404*0Sstevel@tonic-gate } 405*0Sstevel@tonic-gate } 406*0Sstevel@tonic-gate if ((depth-1) > sl_cnt) { 407*0Sstevel@tonic-gate if ((depth-1) > do_dir) { 408*0Sstevel@tonic-gate do_dir = depth - 1; 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate retval = 1; 411*0Sstevel@tonic-gate (void) func(statnam, dirent->d_name, dir, 412*0Sstevel@tonic-gate depth); 413*0Sstevel@tonic-gate sz = readlink(statnam, linkbuf, MAXPATHLEN-1); 414*0Sstevel@tonic-gate #ifdef DEBUG 415*0Sstevel@tonic-gate printf("linkbuf = %s\n", linkbuf); 416*0Sstevel@tonic-gate printf("sz = %d\n", sz); 417*0Sstevel@tonic-gate fflush(stdout); 418*0Sstevel@tonic-gate #endif /* DEBUG */ 419*0Sstevel@tonic-gate if (sz < 0) { 420*0Sstevel@tonic-gate continue; 421*0Sstevel@tonic-gate } 422*0Sstevel@tonic-gate linkbuf[sz] = '\0'; 423*0Sstevel@tonic-gate process_symlk(linkbuf, statnam, glastpos, 424*0Sstevel@tonic-gate symlk_lst, func); 425*0Sstevel@tonic-gate if (unwind = discont_srch(flags, p)) { 426*0Sstevel@tonic-gate goto out; 427*0Sstevel@tonic-gate } 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate continue; 430*0Sstevel@tonic-gate } 431*0Sstevel@tonic-gate /* 432*0Sstevel@tonic-gate * File must be a regular file - 433*0Sstevel@tonic-gate * Does it match the specified pattern? 434*0Sstevel@tonic-gate */ 435*0Sstevel@tonic-gate #ifdef DEBUG 436*0Sstevel@tonic-gate printf("reg file : &statnam[basedir_lastpos] = %s p = %s\n", 437*0Sstevel@tonic-gate &statnam[basedir_lastpos], p); 438*0Sstevel@tonic-gate printf("statbuf.st_mode = %o\n", statbuf.st_mode); 439*0Sstevel@tonic-gate fflush(stdout); 440*0Sstevel@tonic-gate #endif /* DEBUG */ 441*0Sstevel@tonic-gate if (flags & LF_REGEX) { 442*0Sstevel@tonic-gate match = gmatch(&statnam[basedir_lastpos], p); 443*0Sstevel@tonic-gate } else { 444*0Sstevel@tonic-gate match = (strcmp(&statnam[basedir_lastpos], p) == 0); 445*0Sstevel@tonic-gate } 446*0Sstevel@tonic-gate if (!match) { 447*0Sstevel@tonic-gate continue; 448*0Sstevel@tonic-gate } 449*0Sstevel@tonic-gate if ((depth - 1) > do_dir) { 450*0Sstevel@tonic-gate do_dir = depth - 1; 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate retval = 1; 453*0Sstevel@tonic-gate (void) func(statnam, dirent->d_name, dir, depth); 454*0Sstevel@tonic-gate /* 455*0Sstevel@tonic-gate * If the file is an executable, check to see if shared 456*0Sstevel@tonic-gate * libraries need to be packed. 457*0Sstevel@tonic-gate */ 458*0Sstevel@tonic-gate if (statbuf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { 459*0Sstevel@tonic-gate process_executable(statnam, func); 460*0Sstevel@tonic-gate } 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate if (unwind = discont_srch(flags, p)) { 463*0Sstevel@tonic-gate goto out; 464*0Sstevel@tonic-gate } 465*0Sstevel@tonic-gate } 466*0Sstevel@tonic-gate out: 467*0Sstevel@tonic-gate depth--; 468*0Sstevel@tonic-gate if (depth == 0) { 469*0Sstevel@tonic-gate unwind = 0; 470*0Sstevel@tonic-gate } 471*0Sstevel@tonic-gate statnam[glastpos] = '\0'; 472*0Sstevel@tonic-gate if (do_dir) { 473*0Sstevel@tonic-gate do_dir--; 474*0Sstevel@tonic-gate #ifdef DEBUG 475*0Sstevel@tonic-gate printf("out: call func\n"); 476*0Sstevel@tonic-gate fflush(stdout); 477*0Sstevel@tonic-gate printf("out: statnam = %s\n", statnam); 478*0Sstevel@tonic-gate fflush(stdout); 479*0Sstevel@tonic-gate printf("out: &statnam[llastpos] = %s\n", &statnam[llastpos]); 480*0Sstevel@tonic-gate fflush(stdout); 481*0Sstevel@tonic-gate #endif /* DEBUG */ 482*0Sstevel@tonic-gate if (func(statnam, &statnam[llastpos], parent_dir, depth) < 0) { 483*0Sstevel@tonic-gate do_dir = 0; 484*0Sstevel@tonic-gate } 485*0Sstevel@tonic-gate } 486*0Sstevel@tonic-gate glastpos = llastpos; 487*0Sstevel@tonic-gate if (diropn) 488*0Sstevel@tonic-gate closedir(dir); 489*0Sstevel@tonic-gate return (retval); 490*0Sstevel@tonic-gate } 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate /* 493*0Sstevel@tonic-gate * Count all the '/' characters in the string except for those 494*0Sstevel@tonic-gate * in the first character position and last character position 495*0Sstevel@tonic-gate * of the string. 496*0Sstevel@tonic-gate */ 497*0Sstevel@tonic-gate int 498*0Sstevel@tonic-gate slash_cnt(char * str) 499*0Sstevel@tonic-gate { 500*0Sstevel@tonic-gate char *p = str; 501*0Sstevel@tonic-gate int len; 502*0Sstevel@tonic-gate int i; 503*0Sstevel@tonic-gate int count = 0; 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate #ifdef DEBUG 506*0Sstevel@tonic-gate printf("slash_cnt: str = %s", str); 507*0Sstevel@tonic-gate #endif /* DEBUG */ 508*0Sstevel@tonic-gate /* 509*0Sstevel@tonic-gate * NOTE //a, /a and ./a are the same 510*0Sstevel@tonic-gate */ 511*0Sstevel@tonic-gate if (*p == '.') 512*0Sstevel@tonic-gate p++; 513*0Sstevel@tonic-gate while (*p == '/') 514*0Sstevel@tonic-gate p++; 515*0Sstevel@tonic-gate len = strlen(str) - 1; 516*0Sstevel@tonic-gate for (i = 0; i < len; i++) { 517*0Sstevel@tonic-gate if (*p == '/') { 518*0Sstevel@tonic-gate count++; 519*0Sstevel@tonic-gate i--; 520*0Sstevel@tonic-gate while (*p == '/') { 521*0Sstevel@tonic-gate p++; 522*0Sstevel@tonic-gate i++; 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate } else { 525*0Sstevel@tonic-gate p++; 526*0Sstevel@tonic-gate } 527*0Sstevel@tonic-gate } 528*0Sstevel@tonic-gate #ifdef DEBUG 529*0Sstevel@tonic-gate printf(" count = %d\n", count); 530*0Sstevel@tonic-gate fflush(stdout); 531*0Sstevel@tonic-gate #endif /* DEBUG */ 532*0Sstevel@tonic-gate return (count); 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate /* 536*0Sstevel@tonic-gate * For each directory in the path name, call 'func'. 537*0Sstevel@tonic-gate */ 538*0Sstevel@tonic-gate int 539*0Sstevel@tonic-gate func_dir_path(char *path, int (*func)(char *, char *, DIR *, int)) 540*0Sstevel@tonic-gate { 541*0Sstevel@tonic-gate char *dnam; 542*0Sstevel@tonic-gate char *fnam; 543*0Sstevel@tonic-gate char *pathtmp; 544*0Sstevel@tonic-gate DIR *dir; 545*0Sstevel@tonic-gate char *get_fname(char *); 546*0Sstevel@tonic-gate char *get_dirname(char *); 547*0Sstevel@tonic-gate ENTRY hitem, *hitemp; 548*0Sstevel@tonic-gate 549*0Sstevel@tonic-gate #ifdef DEBUG 550*0Sstevel@tonic-gate printf("func_dir_path: path = %s\n", path); 551*0Sstevel@tonic-gate fflush(stdout); 552*0Sstevel@tonic-gate #endif /* DEBUG */ 553*0Sstevel@tonic-gate fnam = path; 554*0Sstevel@tonic-gate dnam = path; 555*0Sstevel@tonic-gate pathtmp = mstrdup(path); 556*0Sstevel@tonic-gate while (fnam != NULL) { 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gate fnam = get_fname(dnam); 559*0Sstevel@tonic-gate dnam = get_dirname(dnam); 560*0Sstevel@tonic-gate if (fnam != (char *)0) { 561*0Sstevel@tonic-gate if (strcmp(fnam, "..") == 0) { 562*0Sstevel@tonic-gate free(pathtmp); 563*0Sstevel@tonic-gate pathtmp = mstrdup(dnam); 564*0Sstevel@tonic-gate continue; 565*0Sstevel@tonic-gate } 566*0Sstevel@tonic-gate } 567*0Sstevel@tonic-gate #ifdef DEBUG 568*0Sstevel@tonic-gate if (fnam != (char *)0) { 569*0Sstevel@tonic-gate printf("func_dir_path: fnam = %s\n", fnam); 570*0Sstevel@tonic-gate } 571*0Sstevel@tonic-gate printf("func_dir_path: dnam = %s pathtmp = %s\n", 572*0Sstevel@tonic-gate dnam, pathtmp); 573*0Sstevel@tonic-gate fflush(stdout); 574*0Sstevel@tonic-gate #endif /* DEBUG */ 575*0Sstevel@tonic-gate 576*0Sstevel@tonic-gate hitem.key = mstrdup(pathtmp); 577*0Sstevel@tonic-gate hitem.data = 0; 578*0Sstevel@tonic-gate hitemp = hsearch(hitem, FIND); 579*0Sstevel@tonic-gate if (hitemp != NULL) { 580*0Sstevel@tonic-gate /* 581*0Sstevel@tonic-gate * If hash item data is 0, item has not been packed. 582*0Sstevel@tonic-gate * If hash item data is 1, item has been packed. 583*0Sstevel@tonic-gate */ 584*0Sstevel@tonic-gate #ifdef DEBUG 585*0Sstevel@tonic-gate printf("func_dir_path: key = %s hitemp->data = %x\n", 586*0Sstevel@tonic-gate hitemp->key, hitemp->data); 587*0Sstevel@tonic-gate fflush(stdout); 588*0Sstevel@tonic-gate #endif /* DEBUG */ 589*0Sstevel@tonic-gate if (hitemp->data == (char *)1) 590*0Sstevel@tonic-gate break; 591*0Sstevel@tonic-gate hitemp->data = (char *)1; 592*0Sstevel@tonic-gate } else { 593*0Sstevel@tonic-gate hitem.key = mstrdup(pathtmp); 594*0Sstevel@tonic-gate hitem.data = (char *)1; 595*0Sstevel@tonic-gate if (hsearch(hitem, ENTER) == NULL) { 596*0Sstevel@tonic-gate fprintf(stderr, 597*0Sstevel@tonic-gate gettext("cachefspack: hash table full\n")); 598*0Sstevel@tonic-gate } 599*0Sstevel@tonic-gate } 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gate dir = opendir(dnam); 602*0Sstevel@tonic-gate if (dir != NULL) { 603*0Sstevel@tonic-gate if (func(pathtmp, fnam, dir, 0) < 0) { 604*0Sstevel@tonic-gate #ifdef DEBUG 605*0Sstevel@tonic-gate printf("func_dir_path: errno = %d\n", errno); 606*0Sstevel@tonic-gate fflush(stdout); 607*0Sstevel@tonic-gate #endif /* DEBUG */ 608*0Sstevel@tonic-gate closedir(dir); 609*0Sstevel@tonic-gate return (-1); 610*0Sstevel@tonic-gate } 611*0Sstevel@tonic-gate closedir(dir); 612*0Sstevel@tonic-gate } else { 613*0Sstevel@tonic-gate printf(gettext("cachefspack: error opening dir -")); 614*0Sstevel@tonic-gate printf("%s\n", dnam); 615*0Sstevel@tonic-gate fflush(stdout); 616*0Sstevel@tonic-gate } 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gate free(pathtmp); 619*0Sstevel@tonic-gate pathtmp = mstrdup(dnam); 620*0Sstevel@tonic-gate } 621*0Sstevel@tonic-gate free(pathtmp); 622*0Sstevel@tonic-gate return (0); 623*0Sstevel@tonic-gate } 624*0Sstevel@tonic-gate void 625*0Sstevel@tonic-gate process_symlk(char *lkpath, char *relpath, int rel_lastpos, 626*0Sstevel@tonic-gate struct item **symlk, int (*func)(char *, char *, DIR *, int)) 627*0Sstevel@tonic-gate { 628*0Sstevel@tonic-gate struct stat64 lstatbuf; 629*0Sstevel@tonic-gate char *l; 630*0Sstevel@tonic-gate struct item *add_item(struct item *, char *, int); 631*0Sstevel@tonic-gate int len; 632*0Sstevel@tonic-gate 633*0Sstevel@tonic-gate /* 634*0Sstevel@tonic-gate * if the link has a relative pathname, append the name to 635*0Sstevel@tonic-gate * current path. 636*0Sstevel@tonic-gate */ 637*0Sstevel@tonic-gate if (*lkpath != '/') { 638*0Sstevel@tonic-gate len = strlen(lkpath); 639*0Sstevel@tonic-gate if ((len + rel_lastpos + 2) > MAXPATHLEN) { 640*0Sstevel@tonic-gate fprintf(stderr, gettext("can't process sym link - %s"), 641*0Sstevel@tonic-gate lkpath); 642*0Sstevel@tonic-gate return; 643*0Sstevel@tonic-gate } 644*0Sstevel@tonic-gate strcpy(&relpath[rel_lastpos], lkpath); 645*0Sstevel@tonic-gate l = relpath; 646*0Sstevel@tonic-gate } else { 647*0Sstevel@tonic-gate l = lkpath; 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate #ifdef DEBUG 650*0Sstevel@tonic-gate printf("process_symlk: lkpath = %s\n", lkpath); 651*0Sstevel@tonic-gate printf("process_symlk: l = %s\n", l); 652*0Sstevel@tonic-gate printf("lstatbuf.st_mode = %o\n", lstatbuf.st_mode); 653*0Sstevel@tonic-gate fflush(stdout); 654*0Sstevel@tonic-gate #endif /* DEBUG */ 655*0Sstevel@tonic-gate if (lstat64(l, &lstatbuf) < 0) { 656*0Sstevel@tonic-gate fprintf(stderr, gettext("Can't lstat sym link - %s"), l); 657*0Sstevel@tonic-gate perror(" "); 658*0Sstevel@tonic-gate return; 659*0Sstevel@tonic-gate } 660*0Sstevel@tonic-gate if (S_ISDIR(lstatbuf.st_mode)) { 661*0Sstevel@tonic-gate *symlk = add_item(*symlk, l, 0); 662*0Sstevel@tonic-gate } 663*0Sstevel@tonic-gate if (S_ISREG(lstatbuf.st_mode)) { 664*0Sstevel@tonic-gate func_dir_path(l, func); 665*0Sstevel@tonic-gate } 666*0Sstevel@tonic-gate } 667*0Sstevel@tonic-gate 668*0Sstevel@tonic-gate discont_srch(int flags, char *pat) 669*0Sstevel@tonic-gate { 670*0Sstevel@tonic-gate char *wild; 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate #ifdef DEBUG 673*0Sstevel@tonic-gate printf("discont_srch: flags = %x pat = %s\n", flags, pat); 674*0Sstevel@tonic-gate fflush(stdout); 675*0Sstevel@tonic-gate #endif /* DEBUG */ 676*0Sstevel@tonic-gate 677*0Sstevel@tonic-gate /* 678*0Sstevel@tonic-gate * if patterns are NOT being matched as regular expressions 679*0Sstevel@tonic-gate * we can have at most 1 match. We got it so quit. 680*0Sstevel@tonic-gate */ 681*0Sstevel@tonic-gate if ((flags & LF_REGEX) != LF_REGEX) { 682*0Sstevel@tonic-gate #ifdef DEBUG 683*0Sstevel@tonic-gate printf("discont_srch: ! LF_REGEX\n"); 684*0Sstevel@tonic-gate fflush(stdout); 685*0Sstevel@tonic-gate #endif /* DEBUG */ 686*0Sstevel@tonic-gate return (1); 687*0Sstevel@tonic-gate } 688*0Sstevel@tonic-gate /* 689*0Sstevel@tonic-gate * if the pattern does not contain wildcard characters and 690*0Sstevel@tonic-gate * we have found a match we are done. 691*0Sstevel@tonic-gate */ 692*0Sstevel@tonic-gate if (WILDCARD(wild, pat) == NULL) { 693*0Sstevel@tonic-gate #ifdef DEBUG 694*0Sstevel@tonic-gate printf("discont_srch: wild = %x\n", wild); 695*0Sstevel@tonic-gate fflush(stdout); 696*0Sstevel@tonic-gate #endif /* DEBUG */ 697*0Sstevel@tonic-gate return (1); 698*0Sstevel@tonic-gate } 699*0Sstevel@tonic-gate return (0); 700*0Sstevel@tonic-gate } 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gate #ifdef DEBUG 703*0Sstevel@tonic-gate prtitem(char * str, struct item *hd) 704*0Sstevel@tonic-gate { 705*0Sstevel@tonic-gate struct item *p = hd->i_next; 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gate printf("\n%s\n\n", str); 708*0Sstevel@tonic-gate while (p != (struct item *)0) { 709*0Sstevel@tonic-gate printf("str = %s\n", p->i_str); 710*0Sstevel@tonic-gate p = p->i_next; 711*0Sstevel@tonic-gate } 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate #endif /* DEBUG */ 714