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