-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindunref.c
626 lines (529 loc) · 13.3 KB
/
findunref.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2018, Joyent, Inc.
* Copyright 2021 Stephen Hahn.
*/
/*
* Finds all unreferenced files in a source tree that do not match a list of
* permitted pathnames.
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <alloca.h>
#include <ctype.h>
#include <errno.h>
#include <fnmatch.h>
#include <ftw.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
/*
* Masquerade Illumos conventions.
*/
typedef bool boolean_t;
#define B_TRUE true
#define B_FALSE false
#define strlcat strncat
#define strlcpy strncpy
/*
* Pathname set: a simple datatype for storing pathname pattern globs and
* for checking whether a given pathname is matched by a pattern glob in
* the set.
*/
typedef struct {
char **paths;
unsigned int npath;
unsigned int maxpaths;
} pnset_t;
/*
* Data associated with the current SCM manifest.
*/
typedef struct scmdata {
pnset_t *manifest;
char metapath[MAXPATHLEN];
char root[MAXPATHLEN];
unsigned int rootlen;
boolean_t rootwarn;
} scmdata_t;
/*
* Hooks used to check if a given unreferenced file is known to an SCM
* (currently Git, Mercurial and TeamWare).
*/
typedef int checkscm_func_t(const char *, const struct FTW *);
typedef void chdirscm_func_t(const char *);
typedef struct {
const char *name;
checkscm_func_t *checkfunc;
chdirscm_func_t *chdirfunc;
} scm_t;
static checkscm_func_t check_tw, check_scmdata;
static chdirscm_func_t chdir_hg, chdir_git;
static int pnset_add(pnset_t *, const char *);
static int pnset_check(const pnset_t *, const char *);
static void pnset_empty(pnset_t *);
static void pnset_free(pnset_t *);
static int checkpath(const char *, const struct stat *, int, struct FTW *);
static pnset_t *make_exset(const char *);
static int asprintf(char **, const char *, ...);
static void warn(const char *, ...);
static void die(const char *, ...);
static const scm_t scms[] = {
{ "hg", check_scmdata, chdir_hg },
{ "mercurial", check_scmdata, chdir_hg },
{ "git", check_scmdata, chdir_git },
{ NULL, NULL, NULL }
};
static const scm_t *scm;
static scmdata_t scmdata;
static time_t tstamp; /* timestamp to compare files to */
static pnset_t *exsetp; /* pathname globs to ignore */
static const char *progname;
int
main(int argc, char *argv[])
{
int c;
int r;
char path[MAXPATHLEN + 1];
char subtree[MAXPATHLEN + 1] = "./";
char *tstampfile = ".build.tstamp";
struct stat tsstat;
progname = strrchr(argv[0], '/');
if (progname == NULL)
progname = argv[0];
else
progname++;
while ((c = getopt(argc, argv, "as:t:S:")) != EOF) {
switch (c) {
case 'a':
/* for compatibility; now the default */
break;
case 's':
(void) strlcat(subtree, optarg, MAXPATHLEN);
break;
case 't':
tstampfile = optarg;
break;
case 'S':
for (scm = scms; scm->name != NULL; scm++) {
if (strcmp(scm->name, optarg) == 0)
break;
}
if (scm->name == NULL)
die("unsupported SCM `%s'\n", optarg);
break;
default:
case '?':
goto usage;
}
}
argc -= optind;
argv += optind;
if (argc != 2) {
usage: (void) fprintf(stderr, "usage: %s [-s <subtree>] "
"[-t <tstampfile>] [-S hg|tw|git] <srcroot> <exceptfile>\n",
progname);
return (EXIT_FAILURE);
}
/*
* Interpret a relative timestamp path as relative to srcroot.
*/
if (tstampfile[0] == '/')
(void) strlcpy(path, tstampfile, MAXPATHLEN);
else
(void) snprintf(path, MAXPATHLEN, "%s/%s", argv[0], tstampfile);
if (stat(path, &tsstat) == -1)
die("cannot stat timestamp file \"%s\"", path);
tstamp = tsstat.st_mtime;
/*
* Create the exception pathname set.
*/
exsetp = make_exset(argv[1]);
if (exsetp == NULL)
die("cannot make exception pathname set\n");
/*
* Walk the specified subtree of the tree rooted at argv[0].
*/
if (chdir(argv[0]) == -1)
die("cannot change directory to \"%s\"", argv[0]);
if ((r = nftw(subtree, checkpath, 100, FTW_PHYS)) != 0
&& r != FTW_SKIP_SUBTREE)
die("cannot walk tree rooted at \"%s\"\n", argv[0]);
pnset_empty(exsetp);
return (EXIT_SUCCESS);
}
/*
* Load and return a pnset for the manifest for the Mercurial repo at `hgroot'.
*/
static pnset_t *
hg_manifest(const char *hgroot)
{
FILE *fp = NULL;
char *hgcmd = NULL;
char *newline;
pnset_t *pnsetp;
char path[MAXPATHLEN];
pnsetp = calloc(1, sizeof (pnset_t));
if (pnsetp == NULL ||
asprintf(&hgcmd, "hg manifest -R %s", hgroot) == -1)
goto fail;
fp = popen(hgcmd, "r");
if (fp == NULL)
goto fail;
while (fgets(path, sizeof (path), fp) != NULL) {
newline = strrchr(path, '\n');
if (newline != NULL)
*newline = '\0';
if (pnset_add(pnsetp, path) == 0)
goto fail;
}
(void) pclose(fp);
return (pnsetp);
fail:
warn("cannot load hg manifest at %s", hgroot);
if (fp != NULL)
(void) pclose(fp);
pnset_free(pnsetp);
return (NULL);
}
/*
* Load and return a pnset for the manifest for the Git repo at `gitroot'.
*/
static pnset_t *
git_manifest(const char *gitroot)
{
FILE *fp = NULL;
char *gitcmd = NULL;
char *newline;
pnset_t *pnsetp;
char path[MAXPATHLEN];
pnsetp = calloc(1, sizeof (pnset_t));
if (pnsetp == NULL ||
asprintf(&gitcmd, "git --git-dir=%s/.git ls-files", gitroot) == -1)
goto fail;
fp = popen(gitcmd, "r");
if (fp == NULL)
goto fail;
while (fgets(path, sizeof (path), fp) != NULL) {
newline = strrchr(path, '\n');
if (newline != NULL)
*newline = '\0';
if (pnset_add(pnsetp, path) == 0)
goto fail;
}
(void) pclose(fp);
return (pnsetp);
fail:
warn("cannot load git manifest at %s", gitroot);
if (fp != NULL)
(void) pclose(fp);
pnset_free(pnsetp);
return (NULL);
}
/*
* If necessary, change our active manifest to be appropriate for `path'.
*/
static void
chdir_scmdata(const char *path, const char *meta,
pnset_t *(*manifest_func)(const char *path))
{
char scmpath[MAXPATHLEN + 1];
char basepath[MAXPATHLEN];
char *slash;
(void) snprintf(scmpath, MAXPATHLEN, "%s/%s", path, meta);
/*
* Change our active manifest if any one of the following is true:
*
* 1. No manifest is loaded. Find the nearest SCM root to load from.
*
* 2. A manifest is loaded, but we've moved into a directory with
* its own metadata directory (e.g., usr/closed). Load from its
* root.
*
* 3. A manifest is loaded, but no longer applies (e.g., the manifest
* under usr/closed is loaded, but we've moved to usr/src).
*/
if (scmdata.manifest == NULL ||
(strcmp(scmpath, scmdata.metapath) != 0 &&
access(scmpath, X_OK) == 0) ||
strncmp(path, scmdata.root, scmdata.rootlen - 1) != 0) {
pnset_free(scmdata.manifest);
scmdata.manifest = NULL;
(void) strlcpy(basepath, path, MAXPATHLEN);
/*
* Walk up the directory tree looking for metadata
* subdirectories.
*/
while (access(scmpath, X_OK) == -1) {
slash = strrchr(basepath, '/');
if (slash == NULL) {
if (!scmdata.rootwarn) {
warn("no metadata directory "
"for \"%s\"\n", path);
scmdata.rootwarn = B_TRUE;
}
return;
}
*slash = '\0';
(void) snprintf(scmpath, MAXPATHLEN + 1, "%s/%s", basepath,
meta);
}
/*
* We found a directory with an SCM metadata directory; record
* it and load its manifest.
*/
(void) strlcpy(scmdata.metapath, scmpath, MAXPATHLEN);
(void) strlcpy(scmdata.root, basepath, MAXPATHLEN);
scmdata.manifest = manifest_func(scmdata.root);
/*
* The logic in check_scmdata() depends on scmdata.root having
* a single trailing slash, so only add it if it's missing.
*/
if (scmdata.root[strlen(scmdata.root) - 1] != '/')
(void) strlcat(scmdata.root, "/", MAXPATHLEN);
scmdata.rootlen = strlen(scmdata.root);
}
}
/*
* If necessary, change our active manifest to be appropriate for `path'.
*/
static void
chdir_git(const char *path)
{
chdir_scmdata(path, ".git", git_manifest);
}
static void
chdir_hg(const char *path)
{
chdir_scmdata(path, ".hg", hg_manifest);
}
/* ARGSUSED */
static int
check_scmdata(const char *path, const struct FTW *ftwp)
{
/*
* The manifest paths are relative to the manifest root; skip past it.
*/
path += scmdata.rootlen;
return (scmdata.manifest != NULL && pnset_check(scmdata.manifest,
path));
}
/*
* Using `exceptfile' and a built-in list of exceptions, build and return a
* pnset_t consisting of all of the pathnames globs which are allowed to be
* unreferenced in the source tree.
*/
static pnset_t *
make_exset(const char *exceptfile)
{
FILE *fp;
char line[MAXPATHLEN];
char *newline;
pnset_t *pnsetp;
unsigned int i;
pnsetp = calloc(1, sizeof (pnset_t));
if (pnsetp == NULL)
return (NULL);
/*
* Add any exceptions from the file.
*/
fp = fopen(exceptfile, "r");
if (fp == NULL) {
warn("cannot open exception file \"%s\"", exceptfile);
goto fail;
}
while (fgets(line, sizeof (line), fp) != NULL) {
newline = strrchr(line, '\n');
if (newline != NULL)
*newline = '\0';
for (i = 0; isspace(line[i]); i++)
;
if (line[i] == '#' || line[i] == '\0')
continue;
if (pnset_add(pnsetp, line) == 0) {
(void) fclose(fp);
goto fail;
}
}
(void) fclose(fp);
return (pnsetp);
fail:
pnset_free(pnsetp);
return (NULL);
}
/*
* FTW callback: print `path' if it's older than `tstamp' and not in `exsetp'.
*/
static int
checkpath(const char *path, const struct stat *statp, int type,
struct FTW *ftwp)
{
switch (type) {
case FTW_F:
/*
* Skip if the file is referenced or in the exception list.
*/
if (statp->st_atime >= tstamp || pnset_check(exsetp, path))
return (0);
/*
* If requested, restrict ourselves to unreferenced files
* under SCM control.
*/
if (scm == NULL || scm->checkfunc(path, ftwp))
(void) puts(path);
return (0);
case FTW_D:
/*
* Prune any directories in the exception list.
*/
if (pnset_check(exsetp, path)) {
return (FTW_SKIP_SUBTREE);
}
/*
* If necessary, advise the SCM logic of our new directory.
*/
if (scm != NULL && scm->chdirfunc != NULL)
scm->chdirfunc(path);
return (0);
case FTW_DNR:
warn("cannot read \"%s\"", path);
return (0);
case FTW_NS:
warn("cannot stat \"%s\"", path);
return (0);
default:
break;
}
return (0);
}
/*
* Add `path' to the pnset_t pointed to by `pnsetp'.
*/
static int
pnset_add(pnset_t *pnsetp, const char *path)
{
char **newpaths;
unsigned int maxpaths;
if (pnsetp->npath == pnsetp->maxpaths) {
maxpaths = (pnsetp->maxpaths == 0) ? 512 : pnsetp->maxpaths * 2;
newpaths = realloc(pnsetp->paths, sizeof (char *) * maxpaths);
if (newpaths == NULL)
return (0);
pnsetp->paths = newpaths;
pnsetp->maxpaths = maxpaths;
}
pnsetp->paths[pnsetp->npath] = strdup(path);
if (pnsetp->paths[pnsetp->npath] == NULL)
return (0);
pnsetp->npath++;
return (1);
}
/*
* Check `path' against the pnset_t pointed to by `pnsetp'.
*/
static int
pnset_check(const pnset_t *pnsetp, const char *path)
{
unsigned int i;
for (i = 0; i < pnsetp->npath; i++) {
if (fnmatch(pnsetp->paths[i], path, 0) == 0)
return (1);
}
return (0);
}
/*
* Empty the pnset_t pointed to by `pnsetp'.
*/
static void
pnset_empty(pnset_t *pnsetp)
{
while (pnsetp->npath-- != 0)
free(pnsetp->paths[pnsetp->npath]);
free(pnsetp->paths);
pnsetp->maxpaths = 0;
}
/*
* Free the pnset_t pointed to by `pnsetp'.
*/
static void
pnset_free(pnset_t *pnsetp)
{
if (pnsetp != NULL) {
pnset_empty(pnsetp);
free(pnsetp);
}
}
/* PRINTFLIKE1 */
static void
warn(const char *format, ...)
{
va_list alist;
char *errstr = strerror(errno);
if (errstr == NULL)
errstr = "<unknown error>";
(void) fprintf(stderr, "%s: ", progname);
va_start(alist, format);
(void) vfprintf(stderr, format, alist);
va_end(alist);
if (strrchr(format, '\n') == NULL)
(void) fprintf(stderr, ": %s\n", errstr);
}
/* PRINTFLIKE1 */
static void
die(const char *format, ...)
{
va_list alist;
char *errstr = strerror(errno);
if (errstr == NULL)
errstr = "<unknown error>";
(void) fprintf(stderr, "%s: fatal: ", progname);
va_start(alist, format);
(void) vfprintf(stderr, format, alist);
va_end(alist);
if (strrchr(format, '\n') == NULL)
(void) fprintf(stderr, ": %s\n", errstr);
exit(EXIT_FAILURE);
}
/* PRINTFLIKE1 */
static int
asprintf(char **dstp, const char *format, ...)
{
va_list alist;
size_t needed;
char *cp;
va_start(alist, format);
needed = vsnprintf(NULL, 0, format, alist);
va_end(alist);
cp = alloca(needed);
va_start(alist, format);
(void) vsnprintf(cp, needed, format, alist);
va_end(alist);
*dstp = cp;
return (0);
}