6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 *
21 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
23 */
24
25 /*
26 * Finds all unreferenced files in a source tree that do not match a list of
27 * permitted pathnames.
28 */
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <fnmatch.h>
33 #include <ftw.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <unistd.h>
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43
44 /*
45 * Pathname set: a simple datatype for storing pathname pattern globs and
183
184 if (nftw(subtree, checkpath, 100, FTW_PHYS) != 0)
185 die("cannot walk tree rooted at \"%s\"\n", argv[0]);
186
187 pnset_empty(exsetp);
188 return (EXIT_SUCCESS);
189 }
190
191 /*
192 * Load and return a pnset for the manifest for the Mercurial repo at `hgroot'.
193 */
194 static pnset_t *
195 hg_manifest(const char *hgroot)
196 {
197 FILE *fp = NULL;
198 char *hgcmd = NULL;
199 char *newline;
200 pnset_t *pnsetp;
201 char path[MAXPATHLEN];
202
203 pnsetp = calloc(sizeof (pnset_t), 1);
204 if (pnsetp == NULL ||
205 asprintf(&hgcmd, "hg manifest -R %s", hgroot) == -1)
206 goto fail;
207
208 fp = popen(hgcmd, "r");
209 if (fp == NULL)
210 goto fail;
211
212 while (fgets(path, sizeof (path), fp) != NULL) {
213 newline = strrchr(path, '\n');
214 if (newline != NULL)
215 *newline = '\0';
216
217 if (pnset_add(pnsetp, path) == 0)
218 goto fail;
219 }
220
221 (void) pclose(fp);
222 free(hgcmd);
223 return (pnsetp);
225 warn("cannot load hg manifest at %s", hgroot);
226 if (fp != NULL)
227 (void) pclose(fp);
228 free(hgcmd);
229 pnset_free(pnsetp);
230 return (NULL);
231 }
232
233 /*
234 * Load and return a pnset for the manifest for the Git repo at `gitroot'.
235 */
236 static pnset_t *
237 git_manifest(const char *gitroot)
238 {
239 FILE *fp = NULL;
240 char *gitcmd = NULL;
241 char *newline;
242 pnset_t *pnsetp;
243 char path[MAXPATHLEN];
244
245 pnsetp = calloc(sizeof (pnset_t), 1);
246 if (pnsetp == NULL ||
247 asprintf(&gitcmd, "git --git-dir=%s/.git ls-files", gitroot) == -1)
248 goto fail;
249
250 fp = popen(gitcmd, "r");
251 if (fp == NULL)
252 goto fail;
253
254 while (fgets(path, sizeof (path), fp) != NULL) {
255 newline = strrchr(path, '\n');
256 if (newline != NULL)
257 *newline = '\0';
258
259 if (pnset_add(pnsetp, path) == 0)
260 goto fail;
261 }
262
263 (void) pclose(fp);
264 free(gitcmd);
265 return (pnsetp);
383 (void) snprintf(sccspath, MAXPATHLEN, "%.*s/SCCS/s.%s", ftwp->base,
384 path, path + ftwp->base);
385
386 return (access(sccspath, F_OK) == 0);
387 }
388
389 /*
390 * Using `exceptfile' and a built-in list of exceptions, build and return a
391 * pnset_t consisting of all of the pathnames globs which are allowed to be
392 * unreferenced in the source tree.
393 */
394 static pnset_t *
395 make_exset(const char *exceptfile)
396 {
397 FILE *fp;
398 char line[MAXPATHLEN];
399 char *newline;
400 pnset_t *pnsetp;
401 unsigned int i;
402
403 pnsetp = calloc(sizeof (pnset_t), 1);
404 if (pnsetp == NULL)
405 return (NULL);
406
407 /*
408 * Add any exceptions from the file.
409 */
410 fp = fopen(exceptfile, "r");
411 if (fp == NULL) {
412 warn("cannot open exception file \"%s\"", exceptfile);
413 goto fail;
414 }
415
416 while (fgets(line, sizeof (line), fp) != NULL) {
417 newline = strrchr(line, '\n');
418 if (newline != NULL)
419 *newline = '\0';
420
421 for (i = 0; isspace(line[i]); i++)
422 ;
423
|
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 *
21 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
22 * Use is subject to license terms.
23 */
24
25 /*
26 * Copyright (c) 2018, Joyent, Inc.
27 */
28
29 /*
30 * Finds all unreferenced files in a source tree that do not match a list of
31 * permitted pathnames.
32 */
33
34 #include <ctype.h>
35 #include <errno.h>
36 #include <fnmatch.h>
37 #include <ftw.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <unistd.h>
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47
48 /*
49 * Pathname set: a simple datatype for storing pathname pattern globs and
187
188 if (nftw(subtree, checkpath, 100, FTW_PHYS) != 0)
189 die("cannot walk tree rooted at \"%s\"\n", argv[0]);
190
191 pnset_empty(exsetp);
192 return (EXIT_SUCCESS);
193 }
194
195 /*
196 * Load and return a pnset for the manifest for the Mercurial repo at `hgroot'.
197 */
198 static pnset_t *
199 hg_manifest(const char *hgroot)
200 {
201 FILE *fp = NULL;
202 char *hgcmd = NULL;
203 char *newline;
204 pnset_t *pnsetp;
205 char path[MAXPATHLEN];
206
207 pnsetp = calloc(1, sizeof (pnset_t));
208 if (pnsetp == NULL ||
209 asprintf(&hgcmd, "hg manifest -R %s", hgroot) == -1)
210 goto fail;
211
212 fp = popen(hgcmd, "r");
213 if (fp == NULL)
214 goto fail;
215
216 while (fgets(path, sizeof (path), fp) != NULL) {
217 newline = strrchr(path, '\n');
218 if (newline != NULL)
219 *newline = '\0';
220
221 if (pnset_add(pnsetp, path) == 0)
222 goto fail;
223 }
224
225 (void) pclose(fp);
226 free(hgcmd);
227 return (pnsetp);
229 warn("cannot load hg manifest at %s", hgroot);
230 if (fp != NULL)
231 (void) pclose(fp);
232 free(hgcmd);
233 pnset_free(pnsetp);
234 return (NULL);
235 }
236
237 /*
238 * Load and return a pnset for the manifest for the Git repo at `gitroot'.
239 */
240 static pnset_t *
241 git_manifest(const char *gitroot)
242 {
243 FILE *fp = NULL;
244 char *gitcmd = NULL;
245 char *newline;
246 pnset_t *pnsetp;
247 char path[MAXPATHLEN];
248
249 pnsetp = calloc(1, sizeof (pnset_t));
250 if (pnsetp == NULL ||
251 asprintf(&gitcmd, "git --git-dir=%s/.git ls-files", gitroot) == -1)
252 goto fail;
253
254 fp = popen(gitcmd, "r");
255 if (fp == NULL)
256 goto fail;
257
258 while (fgets(path, sizeof (path), fp) != NULL) {
259 newline = strrchr(path, '\n');
260 if (newline != NULL)
261 *newline = '\0';
262
263 if (pnset_add(pnsetp, path) == 0)
264 goto fail;
265 }
266
267 (void) pclose(fp);
268 free(gitcmd);
269 return (pnsetp);
387 (void) snprintf(sccspath, MAXPATHLEN, "%.*s/SCCS/s.%s", ftwp->base,
388 path, path + ftwp->base);
389
390 return (access(sccspath, F_OK) == 0);
391 }
392
393 /*
394 * Using `exceptfile' and a built-in list of exceptions, build and return a
395 * pnset_t consisting of all of the pathnames globs which are allowed to be
396 * unreferenced in the source tree.
397 */
398 static pnset_t *
399 make_exset(const char *exceptfile)
400 {
401 FILE *fp;
402 char line[MAXPATHLEN];
403 char *newline;
404 pnset_t *pnsetp;
405 unsigned int i;
406
407 pnsetp = calloc(1, sizeof (pnset_t));
408 if (pnsetp == NULL)
409 return (NULL);
410
411 /*
412 * Add any exceptions from the file.
413 */
414 fp = fopen(exceptfile, "r");
415 if (fp == NULL) {
416 warn("cannot open exception file \"%s\"", exceptfile);
417 goto fail;
418 }
419
420 while (fgets(line, sizeof (line), fp) != NULL) {
421 newline = strrchr(line, '\n');
422 if (newline != NULL)
423 *newline = '\0';
424
425 for (i = 0; isspace(line[i]); i++)
426 ;
427
|