17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*cdf0c1d5Smjnelson * Common Development and Distribution License (the "License"). 6*cdf0c1d5Smjnelson * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 22*cdf0c1d5Smjnelson /* 23*cdf0c1d5Smjnelson * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24*cdf0c1d5Smjnelson * Use is subject to license terms. 25*cdf0c1d5Smjnelson */ 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Finds all unreferenced files in a source tree that do not match a list of 307c478bd9Sstevel@tonic-gate * permitted pathnames. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <ctype.h> 347c478bd9Sstevel@tonic-gate #include <errno.h> 357c478bd9Sstevel@tonic-gate #include <fnmatch.h> 367c478bd9Sstevel@tonic-gate #include <ftw.h> 377c478bd9Sstevel@tonic-gate #include <stdarg.h> 387c478bd9Sstevel@tonic-gate #include <stdio.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate #include <time.h> 427c478bd9Sstevel@tonic-gate #include <unistd.h> 437c478bd9Sstevel@tonic-gate #include <sys/param.h> 447c478bd9Sstevel@tonic-gate #include <sys/stat.h> 457c478bd9Sstevel@tonic-gate #include <sys/types.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* 487c478bd9Sstevel@tonic-gate * Pathname set: a simple datatype for storing pathname pattern globs and 497c478bd9Sstevel@tonic-gate * for checking whether a given pathname is matched by a pattern glob in 507c478bd9Sstevel@tonic-gate * the set. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate typedef struct { 537c478bd9Sstevel@tonic-gate char **paths; 547c478bd9Sstevel@tonic-gate unsigned int npath; 557c478bd9Sstevel@tonic-gate unsigned int maxpaths; 567c478bd9Sstevel@tonic-gate } pnset_t; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate static int pnset_add(pnset_t *, const char *); 597c478bd9Sstevel@tonic-gate static int pnset_check(const pnset_t *, const char *); 607c478bd9Sstevel@tonic-gate static void pnset_empty(pnset_t *); 617c478bd9Sstevel@tonic-gate static int checkpath(const char *, const struct stat *, int, struct FTW *); 627c478bd9Sstevel@tonic-gate static pnset_t *make_exset(const char *); 637c478bd9Sstevel@tonic-gate static void warn(const char *, ...); 647c478bd9Sstevel@tonic-gate static void die(const char *, ...); 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static time_t tstamp; /* timestamp to compare files to */ 677c478bd9Sstevel@tonic-gate static pnset_t *exsetp; /* pathname globs to ignore */ 687c478bd9Sstevel@tonic-gate static const char *progname; 697c478bd9Sstevel@tonic-gate static boolean_t allfiles = B_FALSE; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate int 727c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 737c478bd9Sstevel@tonic-gate { 747c478bd9Sstevel@tonic-gate int c; 757c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 767c478bd9Sstevel@tonic-gate char subtree[MAXPATHLEN] = "./"; 777c478bd9Sstevel@tonic-gate char *tstampfile = ".build.tstamp"; 787c478bd9Sstevel@tonic-gate struct stat tsstat; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate progname = strrchr(argv[0], '/'); 817c478bd9Sstevel@tonic-gate if (progname == NULL) 827c478bd9Sstevel@tonic-gate progname = argv[0]; 837c478bd9Sstevel@tonic-gate else 847c478bd9Sstevel@tonic-gate progname++; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "as:t:")) != EOF) { 877c478bd9Sstevel@tonic-gate switch (c) { 887c478bd9Sstevel@tonic-gate case 'a': 897c478bd9Sstevel@tonic-gate allfiles = B_TRUE; 907c478bd9Sstevel@tonic-gate break; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate case 's': 937c478bd9Sstevel@tonic-gate (void) strlcat(subtree, optarg, MAXPATHLEN); 947c478bd9Sstevel@tonic-gate break; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate case 't': 977c478bd9Sstevel@tonic-gate tstampfile = optarg; 987c478bd9Sstevel@tonic-gate break; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate default: 1017c478bd9Sstevel@tonic-gate case '?': 1027c478bd9Sstevel@tonic-gate goto usage; 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate argc -= optind; 1077c478bd9Sstevel@tonic-gate argv += optind; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate if (argc != 2) { 1107c478bd9Sstevel@tonic-gate usage: (void) fprintf(stderr, "usage: %s [-a] [-s subtree] " 1117c478bd9Sstevel@tonic-gate "[-t tstampfile] srcroot exceptfile\n", progname); 1127c478bd9Sstevel@tonic-gate return (EXIT_FAILURE); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate /* 1167c478bd9Sstevel@tonic-gate * Interpret a relative timestamp path as relative to srcroot. 1177c478bd9Sstevel@tonic-gate */ 1187c478bd9Sstevel@tonic-gate if (tstampfile[0] == '/') 1197c478bd9Sstevel@tonic-gate (void) strlcpy(path, tstampfile, MAXPATHLEN); 1207c478bd9Sstevel@tonic-gate else 1217c478bd9Sstevel@tonic-gate (void) snprintf(path, MAXPATHLEN, "%s/%s", argv[0], tstampfile); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if (stat(path, &tsstat) == -1) 1247c478bd9Sstevel@tonic-gate die("cannot stat timestamp file \"%s\"", path); 1257c478bd9Sstevel@tonic-gate tstamp = tsstat.st_mtime; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Create the exception pathname set. 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate exsetp = make_exset(argv[1]); 1317c478bd9Sstevel@tonic-gate if (exsetp == NULL) 1327c478bd9Sstevel@tonic-gate die("cannot make exception pathname set\n"); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate /* 1357c478bd9Sstevel@tonic-gate * Walk the specified subtree of the tree rooted at argv[0]. 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate (void) chdir(argv[0]); 1387c478bd9Sstevel@tonic-gate if (nftw(subtree, checkpath, 100, FTW_PHYS) != 0) 1397c478bd9Sstevel@tonic-gate die("cannot walk tree rooted at \"%s\"\n", argv[0]); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate pnset_empty(exsetp); 1427c478bd9Sstevel@tonic-gate return (EXIT_SUCCESS); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* 1467c478bd9Sstevel@tonic-gate * Using `exceptfile' and a built-in list of exceptions, build and return a 1477c478bd9Sstevel@tonic-gate * pnset_t consisting of all of the pathnames globs which are allowed to be 1487c478bd9Sstevel@tonic-gate * unreferenced in the source tree. 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate static pnset_t * 1517c478bd9Sstevel@tonic-gate make_exset(const char *exceptfile) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate FILE *fp; 1547c478bd9Sstevel@tonic-gate char line[MAXPATHLEN]; 1557c478bd9Sstevel@tonic-gate char *newline; 1567c478bd9Sstevel@tonic-gate pnset_t *pnsetp; 1577c478bd9Sstevel@tonic-gate unsigned int i; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate pnsetp = calloc(sizeof (pnset_t), 1); 1607c478bd9Sstevel@tonic-gate if (pnsetp == NULL) 1617c478bd9Sstevel@tonic-gate return (NULL); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* 1647c478bd9Sstevel@tonic-gate * Add any exceptions from the file. 1657c478bd9Sstevel@tonic-gate */ 1667c478bd9Sstevel@tonic-gate fp = fopen(exceptfile, "r"); 1677c478bd9Sstevel@tonic-gate if (fp == NULL) { 1687c478bd9Sstevel@tonic-gate warn("cannot open exception file \"%s\"", exceptfile); 1697c478bd9Sstevel@tonic-gate goto fail; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) != NULL) { 1737c478bd9Sstevel@tonic-gate newline = strrchr(line, '\n'); 1747c478bd9Sstevel@tonic-gate if (newline != NULL) 1757c478bd9Sstevel@tonic-gate *newline = '\0'; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate for (i = 0; isspace(line[i]); i++) 1787c478bd9Sstevel@tonic-gate ; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate if (line[i] == '#' || line[i] == '\0') 1817c478bd9Sstevel@tonic-gate continue; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (pnset_add(pnsetp, line) == 0) { 1847c478bd9Sstevel@tonic-gate (void) fclose(fp); 1857c478bd9Sstevel@tonic-gate goto fail; 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate (void) fclose(fp); 1907c478bd9Sstevel@tonic-gate return (pnsetp); 1917c478bd9Sstevel@tonic-gate fail: 1927c478bd9Sstevel@tonic-gate pnset_empty(pnsetp); 1937c478bd9Sstevel@tonic-gate free(pnsetp); 1947c478bd9Sstevel@tonic-gate return (NULL); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* 1987c478bd9Sstevel@tonic-gate * FTW callback: print `path' if it's older than `tstamp' and not in `exsetp'. 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate static int 2017c478bd9Sstevel@tonic-gate checkpath(const char *path, const struct stat *statp, int type, 2027c478bd9Sstevel@tonic-gate struct FTW *ftwp) 2037c478bd9Sstevel@tonic-gate { 2047c478bd9Sstevel@tonic-gate char sccspath[MAXPATHLEN]; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate switch (type) { 2077c478bd9Sstevel@tonic-gate case FTW_F: 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * Skip if the file is referenced or in the exception list. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate if (statp->st_atime >= tstamp || pnset_check(exsetp, path)) 2127c478bd9Sstevel@tonic-gate return (0); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * If not explicitly checking all files, restrict ourselves 2167c478bd9Sstevel@tonic-gate * to unreferenced files under SCCS control. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate if (!allfiles) { 2197c478bd9Sstevel@tonic-gate (void) snprintf(sccspath, MAXPATHLEN, "%.*s/SCCS/s.%s", 2207c478bd9Sstevel@tonic-gate ftwp->base, path, path + ftwp->base); 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if (access(sccspath, F_OK) == -1) 2237c478bd9Sstevel@tonic-gate return (0); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate (void) puts(path); 2277c478bd9Sstevel@tonic-gate return (0); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate case FTW_D: 2307c478bd9Sstevel@tonic-gate /* 2317c478bd9Sstevel@tonic-gate * Prune any directories in the exception list. 2327c478bd9Sstevel@tonic-gate */ 2337c478bd9Sstevel@tonic-gate if (pnset_check(exsetp, path)) 2347c478bd9Sstevel@tonic-gate ftwp->quit = FTW_PRUNE; 2357c478bd9Sstevel@tonic-gate return (0); 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate case FTW_DNR: 2387c478bd9Sstevel@tonic-gate warn("cannot read \"%s\"", path); 2397c478bd9Sstevel@tonic-gate return (0); 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate case FTW_NS: 2427c478bd9Sstevel@tonic-gate warn("cannot stat \"%s\"", path); 2437c478bd9Sstevel@tonic-gate return (0); 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate default: 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate return (0); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * Add `path' to the pnset_t pointed to by `pnsetp'. 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate static int 2567c478bd9Sstevel@tonic-gate pnset_add(pnset_t *pnsetp, const char *path) 2577c478bd9Sstevel@tonic-gate { 2587c478bd9Sstevel@tonic-gate char **newpaths; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate if (pnsetp->npath == pnsetp->maxpaths) { 2617c478bd9Sstevel@tonic-gate newpaths = realloc(pnsetp->paths, sizeof (const char *) * 2627c478bd9Sstevel@tonic-gate (pnsetp->maxpaths + 15)); 2637c478bd9Sstevel@tonic-gate if (newpaths == NULL) 2647c478bd9Sstevel@tonic-gate return (0); 2657c478bd9Sstevel@tonic-gate pnsetp->paths = newpaths; 2667c478bd9Sstevel@tonic-gate pnsetp->maxpaths += 15; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate pnsetp->paths[pnsetp->npath] = strdup(path); 2707c478bd9Sstevel@tonic-gate if (pnsetp->paths[pnsetp->npath] == NULL) 2717c478bd9Sstevel@tonic-gate return (0); 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate pnsetp->npath++; 2747c478bd9Sstevel@tonic-gate return (1); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate /* 2787c478bd9Sstevel@tonic-gate * Check `path' against the pnset_t pointed to by `pnsetp'. 2797c478bd9Sstevel@tonic-gate */ 2807c478bd9Sstevel@tonic-gate static int 2817c478bd9Sstevel@tonic-gate pnset_check(const pnset_t *pnsetp, const char *path) 2827c478bd9Sstevel@tonic-gate { 2837c478bd9Sstevel@tonic-gate unsigned int i; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate for (i = 0; i < pnsetp->npath; i++) { 2867c478bd9Sstevel@tonic-gate if (fnmatch(pnsetp->paths[i], path, 0) == 0) 2877c478bd9Sstevel@tonic-gate return (1); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate return (0); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* 2937c478bd9Sstevel@tonic-gate * Empty the pnset_t pointed to by `pnsetp'. 2947c478bd9Sstevel@tonic-gate */ 2957c478bd9Sstevel@tonic-gate static void 2967c478bd9Sstevel@tonic-gate pnset_empty(pnset_t *pnsetp) 2977c478bd9Sstevel@tonic-gate { 2987c478bd9Sstevel@tonic-gate while (pnsetp->npath-- != 0) 2997c478bd9Sstevel@tonic-gate free(pnsetp->paths[pnsetp->npath]); 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate free(pnsetp->paths); 3027c478bd9Sstevel@tonic-gate pnsetp->maxpaths = 0; 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3067c478bd9Sstevel@tonic-gate static void 3077c478bd9Sstevel@tonic-gate warn(const char *format, ...) 3087c478bd9Sstevel@tonic-gate { 3097c478bd9Sstevel@tonic-gate va_list alist; 3107c478bd9Sstevel@tonic-gate char *errstr = strerror(errno); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (errstr == NULL) 3137c478bd9Sstevel@tonic-gate errstr = "<unknown error>"; 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate va_start(alist, format); 3187c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 3197c478bd9Sstevel@tonic-gate va_end(alist); 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate if (strrchr(format, '\n') == NULL) 3227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", errstr); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 3267c478bd9Sstevel@tonic-gate static void 3277c478bd9Sstevel@tonic-gate die(const char *format, ...) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate va_list alist; 3307c478bd9Sstevel@tonic-gate char *errstr = strerror(errno); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate if (errstr == NULL) 3337c478bd9Sstevel@tonic-gate errstr = "<unknown error>"; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: fatal: ", progname); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate va_start(alist, format); 3387c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 3397c478bd9Sstevel@tonic-gate va_end(alist); 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate if (strrchr(format, '\n') == NULL) 3427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", errstr); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 3457c478bd9Sstevel@tonic-gate } 346