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