Annotation of src/usr.bin/cvs/file.c, Revision 1.5
1.1 jfb 1: /* $OpenBSD$ */
2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include <sys/types.h>
28: #include <sys/queue.h>
29: #include <sys/stat.h>
30:
31: #include <pwd.h>
32: #include <errno.h>
33: #include <stdio.h>
34: #include <fcntl.h>
35: #include <dirent.h>
36: #include <stdlib.h>
37: #include <unistd.h>
38: #include <string.h>
39: #include <fnmatch.h>
40:
41: #include "cvs.h"
42: #include "log.h"
43:
44:
45: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
46:
47:
48:
49: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
50:
51:
52:
53: /* ignore pattern */
54: struct cvs_ignpat {
55: char ip_pat[MAXNAMLEN];
56: int ip_flags;
57: TAILQ_ENTRY (cvs_ignpat) ip_list;
58: };
59:
60:
61: /*
62: * Standard patterns to ignore.
63: */
64:
65: static const char *cvs_ign_std[] = {
66: ".",
67: "..",
68: "*.o",
69: "*.so",
70: "*.bak",
71: "*.orig",
72: "*.rej",
73: "*.exe",
74: "*.depend",
75: "CVS",
76: "core",
77: #ifdef OLD_SMELLY_CRUFT
78: "RCSLOG",
79: "tags",
80: "TAGS",
81: "RCS",
82: "SCCS",
83: "#*",
84: ".#*",
85: ",*",
86: #endif
87: };
88:
89:
1.4 jfb 90: static RCSNUM *cvs_addedrev;
91:
92:
1.1 jfb 93: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
94:
95:
1.3 jfb 96: static struct cvs_dir* cvs_file_getdir (struct cvs_file *, int);
97: static void cvs_file_freedir (struct cvs_dir *);
98: static int cvs_file_sort (struct cvs_flist *);
99: static int cvs_file_cmp (const void *, const void *);
100:
101:
102:
1.1 jfb 103: /*
104: * cvs_file_init()
105: *
106: */
107:
108: int
109: cvs_file_init(void)
110: {
111: int i;
112: size_t len;
113: char path[MAXPATHLEN], buf[MAXNAMLEN];
114: FILE *ifp;
115: struct passwd *pwd;
116:
117: TAILQ_INIT(&cvs_ign_pats);
118:
1.4 jfb 119: cvs_addedrev = rcsnum_alloc();
120: rcsnum_aton("0", NULL, cvs_addedrev);
121:
1.1 jfb 122: /* standard patterns to ignore */
123: for (i = 0; i < sizeof(cvs_ign_std)/sizeof(char *); i++)
124: cvs_file_ignore(cvs_ign_std[i]);
125:
126: /* read the cvsignore file in the user's home directory, if any */
127: pwd = getpwuid(getuid());
128: if (pwd != NULL) {
129: snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
130: ifp = fopen(path, "r");
131: if (ifp == NULL) {
132: if (errno != ENOENT)
133: cvs_log(LP_ERRNO, "failed to open `%s'", path);
134: }
135: else {
136: while (fgets(buf, sizeof(buf), ifp) != NULL) {
137: len = strlen(buf);
138: if (len == 0)
139: continue;
140: if (buf[len - 1] != '\n') {
141: cvs_log(LP_ERR, "line too long in `%s'",
142: path);
143: }
144: buf[--len] = '\0';
145: cvs_file_ignore(buf);
146: }
147: (void)fclose(ifp);
148: }
149: }
150:
151: return (0);
152: }
153:
154:
155: /*
156: * cvs_file_ignore()
157: *
158: * Add the pattern <pat> to the list of patterns for files to ignore.
159: * Returns 0 on success, or -1 on failure.
160: */
161:
162: int
163: cvs_file_ignore(const char *pat)
164: {
165: char *cp;
166: struct cvs_ignpat *ip;
167:
168: ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
169: if (ip == NULL) {
170: cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
171: return (-1);
172: }
173:
174: strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
175:
176: /* check if we will need globbing for that pattern */
177: ip->ip_flags = CVS_IGN_STATIC;
178: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
179: if (CVS_CHAR_ISMETA(*cp)) {
180: ip->ip_flags &= ~CVS_IGN_STATIC;
181: break;
182: }
183: }
184:
185: TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
186:
187: return (0);
188: }
189:
190:
191: /*
1.5 ! jfb 192: * cvs_file_chkign()
1.1 jfb 193: *
194: * Returns 1 if the filename <file> is matched by one of the ignore
195: * patterns, or 0 otherwise.
196: */
197:
198: int
1.5 ! jfb 199: cvs_file_chkign(const char *file)
1.1 jfb 200: {
201: struct cvs_ignpat *ip;
202:
203: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
204: if (ip->ip_flags & CVS_IGN_STATIC) {
205: if (strcmp(file, ip->ip_pat) == 0)
206: return (1);
207: }
208: else if (fnmatch(ip->ip_pat, file, FNM_PERIOD) == 0)
209: return (1);
210: }
211:
212: return (0);
213: }
214:
215:
216: /*
217: * cvs_file_getv()
218: *
219: * Get a vector of all the files found in the directory <dir> and not
220: * matching any of the ignore patterns. The number of files found is
221: * returned in <nfiles>.
222: * Returns a pointer to a dynamically-allocated string vector on success,
1.2 jfb 223: * or NULL on failure. The returned vector should be freed with
224: * cvs_freeargv().
1.1 jfb 225: */
226:
227: char**
1.3 jfb 228: cvs_file_getv(const char *dir, int *nfiles, int recurse)
1.1 jfb 229: {
230: int nf, ret, fd;
231: long base;
232: void *dp, *ep, *tmp;
233: char fbuf[1024], **fvec;
234: struct dirent *ent;
235:
236: *nfiles = 0;
237: fvec = NULL;
238:
239: fd = open(dir, O_RDONLY);
240: if (fd == -1) {
241: cvs_log(LP_ERRNO, "failed to open `%s'", dir);
242: return (NULL);
243: }
244: ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
245: if (ret == -1) {
246: cvs_log(LP_ERRNO, "failed to get directory entries");
247: (void)close(fd);
248: return (NULL);
249: }
250:
251: dp = fbuf;
252: ep = fbuf + (size_t)ret;
253: while (dp < ep) {
254: ent = (struct dirent *)dp;
255: dp += ent->d_reclen;
256:
1.5 ! jfb 257: if (cvs_file_chkign(ent->d_name))
1.1 jfb 258: continue;
259:
260: tmp = realloc(fvec, (*nfiles + 1) * sizeof(char *));
261: if (tmp == NULL) {
262: cvs_log(LP_ERRNO, "failed to reallocate file vector");
263: (void)close(fd);
264: free(fvec);
265: return (NULL);
266: }
267: fvec[++(*nfiles)] = strdup(ent->d_name);
268:
269: *nfiles++;
270: }
271:
272: (void)close(fd);
273:
274: return (fvec);
1.3 jfb 275: }
276:
277:
278: /*
279: * cvs_file_get()
280: *
281: * Load a cvs_file structure with all the information pertaining to the file
282: * <path>.
1.4 jfb 283: * The <flags> parameter specifies various flags that alter the behaviour of
284: * the function. The CF_STAT flag is used to keep stat information of the
285: * file in the structure after it is used (it is lost otherwise). The
286: * CF_RECURSE flag causes the function to recursively load subdirectories
287: * when <path> is a directory. The CF_SORT flag causes the files to be
288: * sorted in alphabetical order upon loading.
289: * The special case of "." as a path specification generates recursion for
290: * a single level and is equivalent to calling cvs_file_get() on all files
291: * of that directory.
1.3 jfb 292: * Returns a pointer to the cvs file structure, which must later be freed
293: * with cvs_file_free().
294: */
295:
296: struct cvs_file*
297: cvs_file_get(const char *path, int flags)
298: {
1.4 jfb 299: int cwd;
1.3 jfb 300: size_t dlen;
1.5 ! jfb 301: char buf[32];
1.3 jfb 302: struct stat st;
1.5 ! jfb 303: struct tm lmtm;
1.3 jfb 304: struct cvs_file *cfp;
1.4 jfb 305: struct cvs_ent *ent;
1.3 jfb 306:
307: printf("cvs_file_get(%s)\n", path);
308:
1.4 jfb 309: if (strcmp(path, ".") == 0)
310: cwd = 1;
311: else
312: cwd = 0;
313:
1.3 jfb 314: if (stat(path, &st) == -1) {
1.4 jfb 315: cvs_log(LP_ERRNO, "failed to stat %s", path);
1.3 jfb 316: return (NULL);
317: }
318:
319: cfp = (struct cvs_file *)malloc(sizeof(*cfp));
320: if (cfp == NULL) {
321: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
322: return (NULL);
323: }
324: memset(cfp, 0, sizeof(*cfp));
325:
1.4 jfb 326: ent = cvs_ent_getent(path);
327: if (ent == NULL)
328: cfp->cf_cvstat = (cwd == 1) ?
329: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
330: else {
331: if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
332: cfp->cf_cvstat = CVS_FST_ADDED;
1.5 ! jfb 333: else {
! 334: /* check last modified time */
! 335: if ((gmtime_r((time_t *)&(st.st_atime), &lmtm) == NULL) ||
! 336: (asctime_r(&lmtm, buf) == NULL)) {
! 337: cvs_log(LP_ERR,
! 338: "failed to generate file timestamp");
! 339: /* fake an up to date file */
! 340: strlcpy(buf, ent->ce_timestamp, sizeof(buf));
! 341: }
! 342: if (strcmp(buf, ent->ce_timestamp) == 0)
! 343: cfp->cf_cvstat = CVS_FST_UPTODATE;
! 344: else
! 345: cfp->cf_cvstat = CVS_FST_MODIFIED;
! 346: }
1.4 jfb 347:
348: cvs_ent_free(ent);
349: }
350:
1.3 jfb 351: cfp->cf_path = strdup(path);
352: if (cfp->cf_path == NULL) {
1.4 jfb 353: cvs_log(LP_ERRNO, "failed to allocate file path");
1.3 jfb 354: free(cfp);
355: return (NULL);
356: }
357:
358: cfp->cf_name = strrchr(cfp->cf_path, '/');
359: if (cfp->cf_name == NULL)
360: cfp->cf_name = cfp->cf_path;
361: else
362: cfp->cf_name++;
363:
364: /* convert from stat mode to dirent values */
365: cfp->cf_type = IFTODT(st.st_mode);
1.4 jfb 366: if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
367: if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN))
368: cfp->cf_ddat = NULL;
369: else {
370: cfp->cf_ddat = cvs_file_getdir(cfp, flags);
371: if (cfp->cf_ddat == NULL) {
372: cvs_file_free(cfp);
373: return (NULL);
374: }
1.3 jfb 375: }
376: }
377:
378: if (flags & CF_STAT) {
379: cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
380: if (cfp->cf_stat == NULL) {
381: cvs_log(LP_ERRNO, "failed to allocate stat structure");
382: cvs_file_free(cfp);
383: return (NULL);
384: }
385:
386: memcpy(cfp->cf_stat, &st, sizeof(struct stat));
387: }
388:
389: return (cfp);
390: }
391:
392:
393: /*
394: * cvs_file_getdir()
395: *
396: * Get a cvs directory structure for the directory whose path is <dir>.
397: */
398:
399: static struct cvs_dir*
400: cvs_file_getdir(struct cvs_file *cf, int flags)
401: {
402: int nf, ret, fd;
403: long base;
404: void *dp, *ep, *tmp;
405: char fbuf[1024], pbuf[MAXPATHLEN];
406: struct dirent *ent;
407: struct cvs_file *cfp;
408: struct cvs_dir *cdp;
409:
410: cdp = (struct cvs_dir *)malloc(sizeof(*cdp));
411: if (cdp == NULL) {
412: cvs_log(LP_ERRNO, "failed to allocate dir");
413: return (NULL);
414: }
415: memset(cdp, 0, sizeof(*cdp));
416: LIST_INIT(&(cdp->cd_files));
417:
1.4 jfb 418: if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
419: cdp->cd_repo = strdup(pbuf);
420: if (cdp->cd_repo == NULL) {
421: free(cdp);
422: return (NULL);
423: }
1.3 jfb 424: }
425:
426: cdp->cd_root = cvsroot_get(cf->cf_path);
427: if (cdp->cd_root == NULL) {
428: cvs_file_freedir(cdp);
429: return (NULL);
430: }
431:
432: fd = open(cf->cf_path, O_RDONLY);
433: if (fd == -1) {
434: cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
435: cvs_file_freedir(cdp);
436: return (NULL);
437: }
438: ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
439: if (ret == -1) {
440: cvs_log(LP_ERRNO, "failed to get directory entries");
441: (void)close(fd);
442: cvs_file_freedir(cdp);
443: return (NULL);
444: }
445:
446: dp = fbuf;
447: ep = fbuf + (size_t)ret;
448: while (dp < ep) {
449: ent = (struct dirent *)dp;
450: dp += ent->d_reclen;
451:
1.5 ! jfb 452: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.3 jfb 453: continue;
454:
455: snprintf(pbuf, sizeof(pbuf), "%s/%s", cf->cf_path, ent->d_name);
456: cfp = cvs_file_get(pbuf, flags);
1.4 jfb 457: if (cfp != NULL) {
458: cfp->cf_parent = cf;
459: LIST_INSERT_HEAD(&(cdp->cd_files), cfp, cf_list);
460: }
1.3 jfb 461: }
462:
463: if (flags & CF_SORT)
464: cvs_file_sort(&(cdp->cd_files));
465:
466: (void)close(fd);
467:
468: return (cdp);
469: }
470:
471:
472: /*
473: * cvs_file_free()
474: *
475: * Free a cvs_file structure and its contents.
476: */
477:
478: void
479: cvs_file_free(struct cvs_file *cf)
480: {
481: struct cvs_file *cfp;
482: struct cvs_dir *cd;
483:
484: if (cf->cf_path != NULL)
485: free(cf->cf_path);
486: if (cf->cf_stat != NULL)
487: free(cf->cf_stat);
488: if (cf->cf_ddat != NULL)
489: cvs_file_freedir(cf->cf_ddat);
490: free(cf);
1.5 ! jfb 491: }
! 492:
! 493:
! 494: /*
! 495: * cvs_file_examine()
! 496: *
! 497: * Examine the contents of the CVS file structure <cf> with the function
! 498: * <exam>. The function is called for all subdirectories and files of the
! 499: * root file.
! 500: */
! 501:
! 502: int
! 503: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
! 504: {
! 505: int ret;
! 506: struct cvs_file *fp;
! 507:
! 508: if (cf->cf_type == DT_DIR) {
! 509: ret = (*exam)(cf, arg);
! 510: LIST_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
! 511: ret = cvs_file_examine(fp, exam, arg);
! 512: if (ret == -1)
! 513: return (-1);
! 514: }
! 515: }
! 516: else
! 517: return (*exam)(cf, arg);
1.3 jfb 518: }
519:
520:
521: /*
522: * cvs_file_freedir()
523: *
524: * Free a cvs_dir structure and its contents.
525: */
526:
527: static void
528: cvs_file_freedir(struct cvs_dir *cd)
529: {
530: struct cvs_file *cfp;
531:
532: if (cd->cd_root != NULL)
533: cvsroot_free(cd->cd_root);
534: if (cd->cd_repo != NULL)
535: free(cd->cd_repo);
536:
537: while (!LIST_EMPTY(&(cd->cd_files))) {
538: cfp = LIST_FIRST(&(cd->cd_files));
539: LIST_REMOVE(cfp, cf_list);
540: cvs_file_free(cfp);
541: }
542: }
543:
544:
545: /*
546: * cvs_file_sort()
547: *
548: * Sort a list of cvs file structures according to their filename.
549: */
550:
551: static int
552: cvs_file_sort(struct cvs_flist *flp)
553: {
554: int i;
555: size_t nb;
556: struct cvs_file *cf, *cfvec[256];
557:
558: i = 0;
559: LIST_FOREACH(cf, flp, cf_list) {
560: cfvec[i++] = cf;
561: if (i == sizeof(cfvec)/sizeof(struct cvs_file *)) {
562: cvs_log(LP_WARN, "too many files to sort");
563: return (-1);
564: }
565:
566: /* now unlink it from the list,
567: * we'll put it back in order later
568: */
569: LIST_REMOVE(cf, cf_list);
570: }
571:
572: /* clear the list just in case */
573: LIST_INIT(flp);
574: nb = (size_t)i;
575:
576: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
577:
578: /* rebuild the list from the bottom up */
579: for (i = (int)nb - 1; i >= 0; i--)
580: LIST_INSERT_HEAD(flp, cfvec[i], cf_list);
581:
582: return (0);
583: }
584:
585:
586: static int
587: cvs_file_cmp(const void *f1, const void *f2)
588: {
589: struct cvs_file *cf1, *cf2;
590: cf1 = *(struct cvs_file **)f1;
591: cf2 = *(struct cvs_file **)f2;
592: return strcmp(cf1->cf_name, cf2->cf_name);
1.1 jfb 593: }