Annotation of src/usr.bin/cvs/file.c, Revision 1.11
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",
1.8 jfb 77: ".#*",
1.1 jfb 78: #ifdef OLD_SMELLY_CRUFT
79: "RCSLOG",
80: "tags",
81: "TAGS",
82: "RCS",
83: "SCCS",
84: "#*",
85: ",*",
86: #endif
87: };
88:
89:
1.11 ! jfb 90: /*
! 91: * Entries in the CVS/Entries file with a revision of '0' have only been
! 92: * added. Compare against this revision to see if this is the case
! 93: */
1.4 jfb 94: static RCSNUM *cvs_addedrev;
95:
96:
1.1 jfb 97: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
98:
99:
1.6 jfb 100: static int cvs_file_getdir (struct cvs_file *, int);
101: static void cvs_file_freedir (struct cvs_dir *);
102: static int cvs_file_sort (struct cvs_flist *);
103: static int cvs_file_cmp (const void *, const void *);
104: static CVSFILE* cvs_file_alloc (const char *, u_int);
1.3 jfb 105:
106:
107:
1.1 jfb 108: /*
109: * cvs_file_init()
110: *
111: */
112:
113: int
114: cvs_file_init(void)
115: {
116: int i;
117: size_t len;
118: char path[MAXPATHLEN], buf[MAXNAMLEN];
119: FILE *ifp;
120: struct passwd *pwd;
121:
122: TAILQ_INIT(&cvs_ign_pats);
123:
1.4 jfb 124: cvs_addedrev = rcsnum_alloc();
125: rcsnum_aton("0", NULL, cvs_addedrev);
126:
1.1 jfb 127: /* standard patterns to ignore */
1.10 jfb 128: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.1 jfb 129: cvs_file_ignore(cvs_ign_std[i]);
130:
131: /* read the cvsignore file in the user's home directory, if any */
132: pwd = getpwuid(getuid());
133: if (pwd != NULL) {
134: snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
135: ifp = fopen(path, "r");
136: if (ifp == NULL) {
137: if (errno != ENOENT)
138: cvs_log(LP_ERRNO, "failed to open `%s'", path);
139: }
140: else {
141: while (fgets(buf, sizeof(buf), ifp) != NULL) {
142: len = strlen(buf);
143: if (len == 0)
144: continue;
145: if (buf[len - 1] != '\n') {
146: cvs_log(LP_ERR, "line too long in `%s'",
147: path);
148: }
149: buf[--len] = '\0';
150: cvs_file_ignore(buf);
151: }
152: (void)fclose(ifp);
153: }
154: }
155:
156: return (0);
157: }
158:
159:
160: /*
161: * cvs_file_ignore()
162: *
163: * Add the pattern <pat> to the list of patterns for files to ignore.
164: * Returns 0 on success, or -1 on failure.
165: */
166:
167: int
168: cvs_file_ignore(const char *pat)
169: {
170: char *cp;
171: struct cvs_ignpat *ip;
172:
173: ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
174: if (ip == NULL) {
175: cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
176: return (-1);
177: }
178:
179: strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
180:
181: /* check if we will need globbing for that pattern */
182: ip->ip_flags = CVS_IGN_STATIC;
183: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
184: if (CVS_CHAR_ISMETA(*cp)) {
185: ip->ip_flags &= ~CVS_IGN_STATIC;
186: break;
187: }
188: }
189:
190: TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
191:
192: return (0);
193: }
194:
195:
196: /*
1.5 jfb 197: * cvs_file_chkign()
1.1 jfb 198: *
199: * Returns 1 if the filename <file> is matched by one of the ignore
200: * patterns, or 0 otherwise.
201: */
202:
203: int
1.5 jfb 204: cvs_file_chkign(const char *file)
1.1 jfb 205: {
206: struct cvs_ignpat *ip;
207:
208: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
209: if (ip->ip_flags & CVS_IGN_STATIC) {
210: if (strcmp(file, ip->ip_pat) == 0)
211: return (1);
212: }
213: else if (fnmatch(ip->ip_pat, file, FNM_PERIOD) == 0)
214: return (1);
215: }
216:
217: return (0);
218: }
219:
220:
221: /*
1.6 jfb 222: * cvs_file_create()
1.1 jfb 223: *
1.6 jfb 224: * Create a new file whose path is specified in <path> and of type <type>.
1.1 jfb 225: */
226:
1.6 jfb 227: CVSFILE*
228: cvs_file_create(const char *path, u_int type, mode_t mode)
1.1 jfb 229: {
1.6 jfb 230: int fd;
231: CVSFILE *cfp;
1.1 jfb 232:
1.6 jfb 233: cfp = cvs_file_alloc(path, type);
234: if (cfp == NULL)
235: return (NULL);
236: cfp->cf_type = type;
1.1 jfb 237:
1.6 jfb 238: if (type == DT_DIR) {
239: if (mkdir(path, mode) == -1) {
240: cvs_file_free(cfp);
241: return (NULL);
242: }
1.1 jfb 243: }
1.6 jfb 244: else {
245: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
246: if (fd == -1) {
247: cvs_file_free(cfp);
1.1 jfb 248: return (NULL);
249: }
1.6 jfb 250: (void)close(fd);
1.1 jfb 251: }
252:
1.6 jfb 253: return (cfp);
1.3 jfb 254: }
255:
256:
257: /*
258: * cvs_file_get()
259: *
260: * Load a cvs_file structure with all the information pertaining to the file
261: * <path>.
1.4 jfb 262: * The <flags> parameter specifies various flags that alter the behaviour of
263: * the function. The CF_STAT flag is used to keep stat information of the
264: * file in the structure after it is used (it is lost otherwise). The
265: * CF_RECURSE flag causes the function to recursively load subdirectories
266: * when <path> is a directory. The CF_SORT flag causes the files to be
267: * sorted in alphabetical order upon loading.
268: * The special case of "." as a path specification generates recursion for
269: * a single level and is equivalent to calling cvs_file_get() on all files
270: * of that directory.
1.3 jfb 271: * Returns a pointer to the cvs file structure, which must later be freed
272: * with cvs_file_free().
273: */
274:
275: struct cvs_file*
276: cvs_file_get(const char *path, int flags)
277: {
1.4 jfb 278: int cwd;
1.9 jfb 279: size_t len;
1.5 jfb 280: char buf[32];
1.3 jfb 281: struct stat st;
1.5 jfb 282: struct tm lmtm;
1.3 jfb 283: struct cvs_file *cfp;
1.4 jfb 284: struct cvs_ent *ent;
1.3 jfb 285:
1.4 jfb 286: if (strcmp(path, ".") == 0)
287: cwd = 1;
288: else
289: cwd = 0;
290:
1.3 jfb 291: if (stat(path, &st) == -1) {
1.4 jfb 292: cvs_log(LP_ERRNO, "failed to stat %s", path);
1.3 jfb 293: return (NULL);
294: }
295:
1.6 jfb 296: cfp = cvs_file_alloc(path, IFTODT(st.st_mode));
1.3 jfb 297: if (cfp == NULL) {
298: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
299: return (NULL);
300: }
301:
1.4 jfb 302: ent = cvs_ent_getent(path);
303: if (ent == NULL)
304: cfp->cf_cvstat = (cwd == 1) ?
305: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
306: else {
1.9 jfb 307: /* always show directories as up-to-date */
308: if (ent->ce_type == CVS_ENT_DIR)
309: cfp->cf_cvstat = CVS_FST_UPTODATE;
310: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1.4 jfb 311: cfp->cf_cvstat = CVS_FST_ADDED;
1.5 jfb 312: else {
313: /* check last modified time */
1.9 jfb 314: if ((gmtime_r((time_t *)&(st.st_mtime), &lmtm) == NULL) ||
1.5 jfb 315: (asctime_r(&lmtm, buf) == NULL)) {
316: cvs_log(LP_ERR,
317: "failed to generate file timestamp");
318: /* fake an up to date file */
319: strlcpy(buf, ent->ce_timestamp, sizeof(buf));
320: }
1.9 jfb 321: len = strlen(buf);
322: if ((len > 0) && (buf[len - 1] == '\n'))
323: buf[--len] = '\0';
324:
1.5 jfb 325: if (strcmp(buf, ent->ce_timestamp) == 0)
326: cfp->cf_cvstat = CVS_FST_UPTODATE;
327: else
328: cfp->cf_cvstat = CVS_FST_MODIFIED;
329: }
1.4 jfb 330:
331: cvs_ent_free(ent);
332: }
333:
1.3 jfb 334: /* convert from stat mode to dirent values */
335: cfp->cf_type = IFTODT(st.st_mode);
1.4 jfb 336: if ((cfp->cf_type == DT_DIR) && ((flags & CF_RECURSE) || cwd)) {
1.6 jfb 337: if ((flags & CF_KNOWN) && (cfp->cf_cvstat == CVS_FST_UNKNOWN)) {
338: free(cfp->cf_ddat);
1.4 jfb 339: cfp->cf_ddat = NULL;
1.6 jfb 340: }
341: else if (cvs_file_getdir(cfp, flags) < 0) {
342: cvs_file_free(cfp);
343: return (NULL);
1.3 jfb 344: }
345: }
346:
347: if (flags & CF_STAT) {
348: cfp->cf_stat = (struct stat *)malloc(sizeof(struct stat));
349: if (cfp->cf_stat == NULL) {
350: cvs_log(LP_ERRNO, "failed to allocate stat structure");
351: cvs_file_free(cfp);
352: return (NULL);
353: }
354:
355: memcpy(cfp->cf_stat, &st, sizeof(struct stat));
1.9 jfb 356: }
357:
358: return (cfp);
359: }
360:
361:
362: /*
363: * cvs_file_getspec()
364: *
365: * Load a specific set of files whose paths are given in the vector <fspec>,
366: * whose size is given in <fsn>.
367: * Returns a pointer to the lowest common subdirectory to all specified
368: * files.
369: */
370:
371: CVSFILE*
372: cvs_file_getspec(char **fspec, int fsn, int flags)
373: {
374: int i, c;
375: char common[MAXPATHLEN];
376: struct cvs_file *cfp;
377:
378: /* first find the common subdir */
379: strlcpy(common, fspec[0], sizeof(common));
380: for (i = 1; i < fsn; i++) {
381: for (c = 0; ; c++) {
382: if (common[c] != fspec[i][c]) {
383: printf("backtracking!\n");
384: /* go back to last dir */
385: while ((c > 0) && (common[--c] != '/'))
386: common[c] = '\0';
387: break;
388: }
389: }
390: }
391: printf("common part = `%s'\n", common);
392: if (*common == '\0')
393: strlcpy(common, ".", sizeof(common));
394:
395: /* first load the common subdirectory */
396: cfp = cvs_file_get(common, flags);
397: for (i = 0; i < fsn; i++) {
1.3 jfb 398: }
399:
400: return (cfp);
401: }
402:
403:
404: /*
405: * cvs_file_getdir()
406: *
407: * Get a cvs directory structure for the directory whose path is <dir>.
408: */
409:
1.6 jfb 410: static int
1.3 jfb 411: cvs_file_getdir(struct cvs_file *cf, int flags)
412: {
413: int nf, ret, fd;
414: long base;
415: void *dp, *ep, *tmp;
1.11 ! jfb 416: char fbuf[2048], pbuf[MAXPATHLEN];
1.3 jfb 417: struct dirent *ent;
418: struct cvs_file *cfp;
419: struct cvs_dir *cdp;
1.10 jfb 420: struct cvs_flist dirs;
1.3 jfb 421:
1.10 jfb 422: TAILQ_INIT(&dirs);
1.7 jfb 423: cdp = cf->cf_ddat;
424:
1.4 jfb 425: if (cvs_readrepo(cf->cf_path, pbuf, sizeof(pbuf)) == 0) {
426: cdp->cd_repo = strdup(pbuf);
427: if (cdp->cd_repo == NULL) {
428: free(cdp);
1.6 jfb 429: return (-1);
1.4 jfb 430: }
1.3 jfb 431: }
432:
433: cdp->cd_root = cvsroot_get(cf->cf_path);
434: if (cdp->cd_root == NULL) {
435: cvs_file_freedir(cdp);
1.6 jfb 436: return (-1);
1.3 jfb 437: }
438:
439: fd = open(cf->cf_path, O_RDONLY);
440: if (fd == -1) {
441: cvs_log(LP_ERRNO, "failed to open `%s'", cf->cf_path);
442: cvs_file_freedir(cdp);
1.6 jfb 443: return (-1);
1.3 jfb 444: }
445:
1.11 ! jfb 446: do {
! 447: ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
! 448: if (ret == -1) {
! 449: cvs_log(LP_ERRNO, "failed to get directory entries");
! 450: (void)close(fd);
! 451: cvs_file_freedir(cdp);
! 452: return (-1);
! 453: }
1.10 jfb 454:
1.11 ! jfb 455: dp = fbuf;
! 456: ep = fbuf + (size_t)ret;
! 457: while (dp < ep) {
! 458: ent = (struct dirent *)dp;
! 459: dp += ent->d_reclen;
! 460:
! 461: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
! 462: continue;
! 463:
! 464: snprintf(pbuf, sizeof(pbuf), "%s/%s",
! 465: cf->cf_path, ent->d_name);
! 466: cfp = cvs_file_get(pbuf, flags);
! 467: if (cfp != NULL) {
! 468: cfp->cf_parent = cf;
! 469: if (cfp->cf_type == DT_DIR)
! 470: TAILQ_INSERT_HEAD(&dirs, cfp, cf_list);
! 471: else
! 472: TAILQ_INSERT_HEAD(&(cdp->cd_files), cfp,
! 473: cf_list);
! 474: }
1.4 jfb 475: }
1.11 ! jfb 476: } while (ret > 0);
1.3 jfb 477:
1.10 jfb 478: if (flags & CF_SORT) {
1.3 jfb 479: cvs_file_sort(&(cdp->cd_files));
1.10 jfb 480: cvs_file_sort(&dirs);
481: }
482: TAILQ_FOREACH(cfp, &dirs, cf_list)
483: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.3 jfb 484:
485: (void)close(fd);
1.6 jfb 486: cf->cf_ddat = cdp;
1.3 jfb 487:
1.6 jfb 488: return (0);
1.3 jfb 489: }
490:
491:
492: /*
493: * cvs_file_free()
494: *
495: * Free a cvs_file structure and its contents.
496: */
497:
498: void
499: cvs_file_free(struct cvs_file *cf)
500: {
501: if (cf->cf_path != NULL)
502: free(cf->cf_path);
503: if (cf->cf_stat != NULL)
504: free(cf->cf_stat);
505: if (cf->cf_ddat != NULL)
506: cvs_file_freedir(cf->cf_ddat);
507: free(cf);
1.5 jfb 508: }
509:
510:
511: /*
512: * cvs_file_examine()
513: *
514: * Examine the contents of the CVS file structure <cf> with the function
515: * <exam>. The function is called for all subdirectories and files of the
516: * root file.
517: */
518:
519: int
520: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
521: {
522: int ret;
523: struct cvs_file *fp;
524:
525: if (cf->cf_type == DT_DIR) {
526: ret = (*exam)(cf, arg);
1.10 jfb 527: TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5 jfb 528: ret = cvs_file_examine(fp, exam, arg);
529: if (ret == -1)
530: return (-1);
531: }
532: }
533: else
534: return (*exam)(cf, arg);
1.3 jfb 535: }
536:
537:
538: /*
539: * cvs_file_freedir()
540: *
541: * Free a cvs_dir structure and its contents.
542: */
543:
544: static void
545: cvs_file_freedir(struct cvs_dir *cd)
546: {
547: struct cvs_file *cfp;
548:
549: if (cd->cd_root != NULL)
550: cvsroot_free(cd->cd_root);
551: if (cd->cd_repo != NULL)
552: free(cd->cd_repo);
553:
1.10 jfb 554: while (!TAILQ_EMPTY(&(cd->cd_files))) {
555: cfp = TAILQ_FIRST(&(cd->cd_files));
556: TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3 jfb 557: cvs_file_free(cfp);
558: }
559: }
560:
561:
562: /*
563: * cvs_file_sort()
564: *
565: * Sort a list of cvs file structures according to their filename.
566: */
567:
568: static int
569: cvs_file_sort(struct cvs_flist *flp)
570: {
571: int i;
572: size_t nb;
573: struct cvs_file *cf, *cfvec[256];
574:
575: i = 0;
1.10 jfb 576: TAILQ_FOREACH(cf, flp, cf_list) {
1.3 jfb 577: cfvec[i++] = cf;
578: if (i == sizeof(cfvec)/sizeof(struct cvs_file *)) {
579: cvs_log(LP_WARN, "too many files to sort");
580: return (-1);
581: }
582:
583: /* now unlink it from the list,
584: * we'll put it back in order later
585: */
1.10 jfb 586: TAILQ_REMOVE(flp, cf, cf_list);
1.3 jfb 587: }
588:
589: /* clear the list just in case */
1.10 jfb 590: TAILQ_INIT(flp);
1.3 jfb 591: nb = (size_t)i;
592:
593: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
594:
595: /* rebuild the list from the bottom up */
596: for (i = (int)nb - 1; i >= 0; i--)
1.10 jfb 597: TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 598:
599: return (0);
600: }
601:
602:
603: static int
604: cvs_file_cmp(const void *f1, const void *f2)
605: {
606: struct cvs_file *cf1, *cf2;
607: cf1 = *(struct cvs_file **)f1;
608: cf2 = *(struct cvs_file **)f2;
609: return strcmp(cf1->cf_name, cf2->cf_name);
1.6 jfb 610: }
611:
612:
613: CVSFILE*
614: cvs_file_alloc(const char *path, u_int type)
615: {
616: size_t len;
617: char pbuf[MAXPATHLEN];
618: CVSFILE *cfp;
619: struct cvs_dir *ddat;
620:
621: cfp = (struct cvs_file *)malloc(sizeof(*cfp));
622: if (cfp == NULL) {
623: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
624: return (NULL);
625: }
626: memset(cfp, 0, sizeof(*cfp));
627:
628: /* ditch trailing slashes */
629: strlcpy(pbuf, path, sizeof(pbuf));
630: len = strlen(pbuf);
631: while (pbuf[len - 1] == '/')
632: pbuf[--len] = '\0';
633:
634: cfp->cf_path = strdup(pbuf);
635: if (cfp->cf_path == NULL) {
636: free(cfp);
637: return (NULL);
638: }
639:
640: cfp->cf_name = strrchr(cfp->cf_path, '/');
641: if (cfp->cf_name == NULL)
642: cfp->cf_name = cfp->cf_path;
643: else
644: cfp->cf_name++;
645:
646: cfp->cf_type = type;
647: cfp->cf_cvstat = CVS_FST_UNKNOWN;
648:
649: if (type == DT_DIR) {
650: ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
651: if (ddat == NULL) {
652: cvs_file_free(cfp);
653: return (NULL);
654: }
655: memset(ddat, 0, sizeof(*ddat));
1.10 jfb 656: TAILQ_INIT(&(ddat->cd_files));
1.6 jfb 657: cfp->cf_ddat = ddat;
658: }
659: return (cfp);
1.1 jfb 660: }