Annotation of src/usr.bin/cvs/file.c, Revision 1.85
1.85 ! joris 1: /* $OpenBSD: file.c,v 1.84 2005/06/01 14:03:14 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26 jfb 4: * All rights reserved.
1.1 jfb 5: *
1.26 jfb 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.26 jfb 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.26 jfb 13: * derived from this software without specific prior written permission.
1.1 jfb 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
1.26 jfb 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28: #include <sys/queue.h>
29: #include <sys/stat.h>
30:
1.83 xsa 31: #include <dirent.h>
1.1 jfb 32: #include <errno.h>
33: #include <fcntl.h>
1.83 xsa 34: #include <fnmatch.h>
1.51 jfb 35: #include <libgen.h>
1.83 xsa 36: #include <pwd.h>
37: #include <stdio.h>
1.1 jfb 38: #include <stdlib.h>
1.83 xsa 39: #include <string.h>
1.1 jfb 40: #include <unistd.h>
41:
42: #include "cvs.h"
1.83 xsa 43: #include "file.h"
1.1 jfb 44: #include "log.h"
1.57 jfb 45: #include "strtab.h"
1.1 jfb 46:
47:
48: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
49:
50: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
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: static const char *cvs_ign_std[] = {
65: ".",
66: "..",
67: "*.o",
68: "*.so",
1.45 xsa 69: "*.a",
1.1 jfb 70: "*.bak",
71: "*.orig",
72: "*.rej",
1.45 xsa 73: "*.old",
1.1 jfb 74: "*.exe",
75: "*.depend",
1.45 xsa 76: "*.obj",
77: "*.elc",
78: "*.ln",
79: "*.olb",
1.1 jfb 80: "CVS",
81: "core",
1.49 jfb 82: "*.core",
1.8 jfb 83: ".#*",
1.45 xsa 84: "*~",
85: "_$*",
86: "*$",
1.1 jfb 87: #ifdef OLD_SMELLY_CRUFT
88: "RCSLOG",
89: "tags",
90: "TAGS",
91: "RCS",
92: "SCCS",
1.45 xsa 93: "cvslog.*", /* to ignore CVS_CLIENT_LOG output */
1.1 jfb 94: "#*",
95: ",*",
96: #endif
97: };
98:
99:
1.11 jfb 100: /*
101: * Entries in the CVS/Entries file with a revision of '0' have only been
102: * added. Compare against this revision to see if this is the case
103: */
1.4 jfb 104: static RCSNUM *cvs_addedrev;
105:
106:
1.1 jfb 107: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
108:
109:
1.73 joris 110: static int cvs_file_getdir(CVSFILE *, int, char *, int (*)(CVSFILE *, void *), void *);
1.69 joris 111: static int cvs_load_dirinfo (CVSFILE *, int);
1.62 jfb 112: static int cvs_file_sort (struct cvs_flist *, u_int);
113: static int cvs_file_cmp (const void *, const void *);
114: static int cvs_file_cmpname (const char *, const char *);
115: static CVSFILE* cvs_file_alloc (const char *, u_int);
116: static CVSFILE* cvs_file_lget (const char *, int, CVSFILE *, struct cvs_ent *);
1.3 jfb 117:
118:
119:
1.1 jfb 120: /*
121: * cvs_file_init()
122: *
123: */
124: int
125: cvs_file_init(void)
126: {
1.61 xsa 127: int i, l;
1.1 jfb 128: size_t len;
129: char path[MAXPATHLEN], buf[MAXNAMLEN];
130: FILE *ifp;
131: struct passwd *pwd;
132:
133: TAILQ_INIT(&cvs_ign_pats);
134:
1.53 jfb 135: if ((cvs_addedrev = rcsnum_parse("0")) == NULL)
1.46 jfb 136: return (-1);
1.4 jfb 137:
1.1 jfb 138: /* standard patterns to ignore */
1.10 jfb 139: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26 jfb 140: cvs_file_ignore(cvs_ign_std[i]);
1.1 jfb 141:
142: /* read the cvsignore file in the user's home directory, if any */
143: pwd = getpwuid(getuid());
144: if (pwd != NULL) {
1.61 xsa 145: l = snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
146: if (l == -1 || l >= (int)sizeof(path)) {
147: errno = ENAMETOOLONG;
148: cvs_log(LP_ERRNO, "%s", path);
149: return (-1);
150: }
151:
1.1 jfb 152: ifp = fopen(path, "r");
153: if (ifp == NULL) {
154: if (errno != ENOENT)
1.34 jfb 155: cvs_log(LP_ERRNO,
156: "failed to open user's cvsignore", path);
1.38 deraadt 157: } else {
1.1 jfb 158: while (fgets(buf, sizeof(buf), ifp) != NULL) {
159: len = strlen(buf);
160: if (len == 0)
161: continue;
162: if (buf[len - 1] != '\n') {
163: cvs_log(LP_ERR, "line too long in `%s'",
164: path);
165: }
166: buf[--len] = '\0';
167: cvs_file_ignore(buf);
168: }
169: (void)fclose(ifp);
170: }
171: }
172:
173: return (0);
174: }
175:
176:
177: /*
178: * cvs_file_ignore()
179: *
180: * Add the pattern <pat> to the list of patterns for files to ignore.
181: * Returns 0 on success, or -1 on failure.
182: */
183: int
184: cvs_file_ignore(const char *pat)
185: {
186: char *cp;
187: struct cvs_ignpat *ip;
188:
189: ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
190: if (ip == NULL) {
191: cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
192: return (-1);
193: }
194:
195: strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
196:
197: /* check if we will need globbing for that pattern */
198: ip->ip_flags = CVS_IGN_STATIC;
199: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
200: if (CVS_CHAR_ISMETA(*cp)) {
201: ip->ip_flags &= ~CVS_IGN_STATIC;
202: break;
203: }
204: }
205:
206: TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
207:
208: return (0);
209: }
210:
211:
212: /*
1.5 jfb 213: * cvs_file_chkign()
1.1 jfb 214: *
215: * Returns 1 if the filename <file> is matched by one of the ignore
216: * patterns, or 0 otherwise.
217: */
218: int
1.5 jfb 219: cvs_file_chkign(const char *file)
1.1 jfb 220: {
1.23 jfb 221: int flags;
1.1 jfb 222: struct cvs_ignpat *ip;
223:
1.23 jfb 224: flags = FNM_PERIOD;
225: if (cvs_nocase)
226: flags |= FNM_CASEFOLD;
227:
1.1 jfb 228: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
229: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 230: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 231: return (1);
1.38 deraadt 232: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 233: return (1);
234: }
235:
236: return (0);
237: }
238:
239:
240: /*
1.6 jfb 241: * cvs_file_create()
1.1 jfb 242: *
1.6 jfb 243: * Create a new file whose path is specified in <path> and of type <type>.
1.26 jfb 244: * If the type is DT_DIR, the CVS administrative repository and files will be
245: * created.
1.25 jfb 246: * Returns the created file on success, or NULL on failure.
1.1 jfb 247: */
1.6 jfb 248: CVSFILE*
1.34 jfb 249: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1 jfb 250: {
1.85 ! joris 251: int fd, l;
1.84 joris 252: int bail;
1.85 ! joris 253: char fp[MAXPATHLEN], repo[MAXPATHLEN];
1.6 jfb 254: CVSFILE *cfp;
1.62 jfb 255: CVSENTRIES *ent;
1.1 jfb 256:
1.6 jfb 257: cfp = cvs_file_alloc(path, type);
258: if (cfp == NULL)
259: return (NULL);
1.26 jfb 260:
1.85 ! joris 261: bail = l = 0;
1.22 jfb 262: cfp->cf_mode = mode;
1.34 jfb 263: cfp->cf_parent = parent;
1.1 jfb 264:
1.34 jfb 265: if (type == DT_DIR) {
1.62 jfb 266: cfp->cf_root = cvsroot_get(path);
1.84 joris 267:
268: /*
269: * If we do not have a valid root for this, try looking at
270: * the parent its root.
271: */
272: if (cfp->cf_root == NULL) {
273: if (parent != NULL && parent->cf_root != NULL) {
274: cfp->cf_root =
275: cvsroot_parse(parent->cf_root->cr_str);
276: if (cfp->cf_root == NULL)
277: bail = 1;
278: } else {
279: bail = 1;
280: }
281: }
282:
283: /* we tried, too bad */
284: if (bail) {
285: cvs_log(LP_ERR, "failed to obtain root info for `%s'",
286: path);
287: return (NULL);
288: }
289:
1.85 ! joris 290: if (cvs_repo_base != NULL) {
! 291: cvs_file_getpath(cfp, fp, sizeof(fp));
! 292: l = snprintf(repo, sizeof(repo), "%s/%s", cvs_repo_base,
! 293: fp);
! 294: } else {
! 295: cvs_file_getpath(cfp, repo, sizeof(repo));
! 296: l = 0;
! 297: }
! 298:
! 299: if (l == -1 || l >= (int)sizeof(repo)) {
! 300: errno = ENAMETOOLONG;
! 301: cvs_log(LP_ERRNO, "%s", repo);
! 302: cvs_file_free(cfp);
! 303: return (NULL);
! 304: }
! 305:
! 306: cfp->cf_repo = strdup(repo);
1.62 jfb 307: if (cfp->cf_repo == NULL) {
1.34 jfb 308: cvs_file_free(cfp);
309: return (NULL);
310: }
1.33 joris 311:
1.79 joris 312: if (((mkdir(path, mode) == -1) && (errno != EEXIST)) ||
1.78 joris 313: (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo) < 0)) {
1.6 jfb 314: cvs_file_free(cfp);
315: return (NULL);
316: }
1.26 jfb 317:
1.62 jfb 318: ent = cvs_ent_open(path, O_RDWR);
319: if (ent != NULL) {
320: cvs_ent_close(ent);
1.50 jfb 321: }
1.38 deraadt 322: } else {
1.6 jfb 323: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
324: if (fd == -1) {
325: cvs_file_free(cfp);
1.1 jfb 326: return (NULL);
327: }
1.6 jfb 328: (void)close(fd);
1.1 jfb 329: }
330:
1.6 jfb 331: return (cfp);
1.3 jfb 332: }
333:
334:
335: /*
1.35 jfb 336: * cvs_file_copy()
337: *
338: * Allocate space to create a copy of the file <orig>. The copy inherits all
339: * of the original's attributes, but does not inherit its children if the
340: * original file is a directory. Note that files copied using this mechanism
341: * are linked to their parent, but the parent has no link to the file. This
342: * is so cvs_file_getpath() works.
343: * Returns the copied file on success, or NULL on failure. The returned
344: * structure should be freed using cvs_file_free().
345: */
346: CVSFILE*
347: cvs_file_copy(CVSFILE *orig)
348: {
349: char path[MAXPATHLEN];
350: CVSFILE *cfp;
351:
352: cvs_file_getpath(orig, path, sizeof(path));
353:
354: cfp = cvs_file_alloc(path, orig->cf_type);
355: if (cfp == NULL)
356: return (NULL);
357:
358: cfp->cf_parent = orig->cf_parent;
359: cfp->cf_mode = orig->cf_mode;
360: cfp->cf_cvstat = orig->cf_cvstat;
361:
1.62 jfb 362: if (orig->cf_type == DT_REG)
363: cfp->cf_mtime = orig->cf_mtime;
364: else if (orig->cf_type == DT_DIR) {
1.35 jfb 365: /* XXX copy CVS directory attributes */
366: }
367:
368: return (cfp);
369: }
370:
371:
372: /*
1.3 jfb 373: * cvs_file_get()
374: *
375: * Load a cvs_file structure with all the information pertaining to the file
376: * <path>.
1.4 jfb 377: * The <flags> parameter specifies various flags that alter the behaviour of
1.21 jfb 378: * the function. The CF_RECURSE flag causes the function to recursively load
379: * subdirectories when <path> is a directory.
380: * The CF_SORT flag causes the files to be sorted in alphabetical order upon
381: * loading. The special case of "." as a path specification generates
382: * recursion for a single level and is equivalent to calling cvs_file_get() on
383: * all files of that directory.
1.3 jfb 384: * Returns a pointer to the cvs file structure, which must later be freed
385: * with cvs_file_free().
386: */
387:
1.14 jfb 388: CVSFILE*
1.73 joris 389: cvs_file_get(const char *path, int flags, int (*cb)(CVSFILE *, void *),
390: void *arg)
1.3 jfb 391: {
1.68 joris 392: char *files[1];
393:
394: files[0] = path;
1.73 joris 395: return cvs_file_getspec(files, 1, flags, cb, arg);
1.9 jfb 396: }
397:
398:
399: /*
400: * cvs_file_getspec()
401: *
402: * Load a specific set of files whose paths are given in the vector <fspec>,
403: * whose size is given in <fsn>.
404: * Returns a pointer to the lowest common subdirectory to all specified
405: * files.
406: */
407: CVSFILE*
1.73 joris 408: cvs_file_getspec(char **fspec, int fsn, int flags, int (*cb)(CVSFILE *, void *),
409: void *arg)
1.9 jfb 410: {
1.26 jfb 411: int i;
1.68 joris 412: int pwd;
1.26 jfb 413: char *sp, *np, pcopy[MAXPATHLEN];
1.68 joris 414: CVSFILE *base, *nf;
415: CVSENTRIES *entfile;
416: struct cvs_ent *ent;
1.26 jfb 417:
1.68 joris 418: entfile = cvs_ent_open(".", O_RDONLY);
419: base = cvs_file_lget(".", 0, NULL, NULL);
1.26 jfb 420: if (base == NULL)
421: return (NULL);
1.85 ! joris 422:
! 423: /*
! 424: * fill in the repository base (needed to construct repo's in
! 425: * cvs_file_create).
! 426: */
! 427: if (base->cf_repo != NULL) {
! 428: cvs_repo_base = strdup(base->cf_repo);
! 429: if (cvs_repo_base == NULL) {
! 430: cvs_log(LP_ERR, "failed to duplicate repository base");
! 431: cvs_file_free(base);
! 432: if (entfile)
! 433: cvs_ent_close(entfile);
! 434: return (NULL);
! 435: }
! 436:
! 437: printf("cvs_repo_base is %s\n", cvs_repo_base);
! 438: }
1.26 jfb 439:
1.73 joris 440: /* XXX - needed for some commands */
441: if (cb != NULL) {
442: if (cb(base, arg) != CVS_EX_OK) {
443: cvs_file_free(base);
444: return (NULL);
445: }
446: }
447:
1.26 jfb 448: for (i = 0; i < fsn; i++) {
449: strlcpy(pcopy, fspec[i], sizeof(pcopy));
450: sp = pcopy;
1.68 joris 451: pwd = (!strcmp(pcopy, "."));
1.26 jfb 452:
1.68 joris 453: np = strchr(sp, '/');
454: if (np != NULL)
455: *np = '\0';
456:
457: if (pwd) {
458: nf = base;
459: } else {
460: nf = cvs_file_find(base, pcopy);
1.26 jfb 461: if (nf == NULL) {
1.68 joris 462: if (entfile != NULL)
463: ent = cvs_ent_get(entfile, pcopy);
464: else
465: ent = NULL;
466: nf = cvs_file_lget(pcopy, 0, base, ent);
1.26 jfb 467: if (nf == NULL) {
468: cvs_file_free(base);
469: return (NULL);
470: }
471:
1.68 joris 472: if (cvs_file_attach(base, nf) < 0) {
1.59 joris 473: cvs_file_free(base);
474: return (NULL);
475: }
1.26 jfb 476: }
1.68 joris 477: }
1.26 jfb 478:
1.68 joris 479: if (nf->cf_type == DT_DIR) {
480: if (np != NULL)
481: *np++;
482:
1.73 joris 483: if (cvs_file_getdir(nf, flags, np, cb, arg) < 0) {
1.68 joris 484: cvs_file_free(base);
485: return (NULL);
1.26 jfb 486: }
1.73 joris 487: } else {
488: if (cb != NULL) {
1.75 joris 489: if (cb(nf, arg) != CVS_EX_OK) {
490: cvs_file_free(base);
1.73 joris 491: return (NULL);
1.75 joris 492: }
1.73 joris 493: }
1.68 joris 494: }
1.26 jfb 495: }
496:
497: return (base);
1.3 jfb 498: }
499:
500:
501: /*
1.13 jfb 502: * cvs_file_find()
503: *
504: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
505: * The file's pathname <path> must be relative to the base of <hier>.
506: * Returns the entry on success, or NULL on failure.
507: */
508: CVSFILE*
509: cvs_file_find(CVSFILE *hier, const char *path)
510: {
511: char *pp, *sp, pbuf[MAXPATHLEN];
512: CVSFILE *sf, *cf;
513:
514: strlcpy(pbuf, path, sizeof(pbuf));
515:
516: cf = hier;
517: pp = pbuf;
518: do {
519: sp = strchr(pp, '/');
520: if (sp != NULL)
1.24 jfb 521: *(sp++) = '\0';
1.13 jfb 522:
523: /* special case */
524: if (*pp == '.') {
525: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
526: /* request to go back to parent */
527: if (cf->cf_parent == NULL) {
528: cvs_log(LP_NOTICE,
529: "path %s goes back too far", path);
530: return (NULL);
531: }
532: cf = cf->cf_parent;
533: continue;
1.38 deraadt 534: } else if (*(pp + 1) == '\0')
1.13 jfb 535: continue;
536: }
537:
1.62 jfb 538: SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.34 jfb 539: if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13 jfb 540: break;
541: if (sf == NULL)
542: return (NULL);
543:
544: cf = sf;
545: pp = sp;
546: } while (sp != NULL);
547:
1.24 jfb 548: return (cf);
1.13 jfb 549: }
550:
551:
552: /*
1.34 jfb 553: * cvs_file_getpath()
554: *
555: * Get the full path of the file <file> and store it in <buf>, which is of
556: * size <len>. For portability, it is recommended that <buf> always be
557: * at least MAXPATHLEN bytes long.
558: * Returns a pointer to the start of the path on success, or NULL on failure.
559: */
560: char*
561: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
562: {
563: u_int i;
1.77 jfb 564: const char *fp, *namevec[CVS_FILE_MAXDEPTH];
1.34 jfb 565: CVSFILE *top;
566:
567: buf[0] = '\0';
568: i = CVS_FILE_MAXDEPTH;
569: memset(namevec, 0, sizeof(namevec));
570:
571: /* find the top node */
572: for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
573: fp = CVS_FILE_NAME(top);
574:
575: /* skip self-references */
576: if ((fp[0] == '.') && (fp[1] == '\0'))
577: continue;
578: namevec[--i] = fp;
579: }
580:
581: if (i == 0)
582: return (NULL);
583: else if (i == CVS_FILE_MAXDEPTH) {
584: strlcpy(buf, ".", len);
585: return (buf);
586: }
587:
588: while (i < CVS_FILE_MAXDEPTH - 1) {
589: strlcat(buf, namevec[i++], len);
590: strlcat(buf, "/", len);
591: }
592: strlcat(buf, namevec[i], len);
593:
594: return (buf);
595: }
596:
597:
598: /*
1.22 jfb 599: * cvs_file_attach()
600: *
601: * Attach the file <file> as one of the children of parent <parent>, which
602: * has to be a file of type DT_DIR.
603: * Returns 0 on success, or -1 on failure.
604: */
605: int
606: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
607: {
608: if (parent->cf_type != DT_DIR)
609: return (-1);
610:
1.62 jfb 611: SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22 jfb 612: file->cf_parent = parent;
613:
614: return (0);
615: }
616:
617:
618: /*
1.68 joris 619: * Load directory information
1.3 jfb 620: */
1.6 jfb 621: static int
1.69 joris 622: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3 jfb 623: {
1.68 joris 624: char fpath[MAXPATHLEN];
625: char pbuf[MAXPATHLEN];
1.20 jfb 626: struct stat st;
1.68 joris 627: int l;
1.7 jfb 628:
1.34 jfb 629: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.62 jfb 630: cf->cf_root = cvsroot_get(fpath);
631: if (cf->cf_root == NULL)
1.48 jfb 632: return (-1);
633:
1.74 joris 634: if (flags & CF_MKADMIN)
1.78 joris 635: cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL);
1.69 joris 636:
1.74 joris 637: /* if the CVS administrative directory exists, load the info */
638: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
639: if (l == -1 || l >= (int)sizeof(pbuf)) {
640: errno = ENAMETOOLONG;
641: cvs_log(LP_ERRNO, "%s", pbuf);
642: return (-1);
643: }
1.61 xsa 644:
1.74 joris 645: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
646: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
647: cf->cf_repo = strdup(pbuf);
648: if (cf->cf_repo == NULL) {
649: cvs_log(LP_ERRNO,
650: "failed to dup repository string");
651: return (-1);
1.20 jfb 652: }
1.68 joris 653: }
654: }
1.26 jfb 655:
1.68 joris 656: return (0);
657: }
658:
659: /*
660: * cvs_file_getdir()
661: *
662: * Get a cvs directory structure for the directory whose path is <dir>.
663: * This function should not free the directory information on error, as this
664: * is performed by cvs_file_free().
665: */
666: static int
1.73 joris 667: cvs_file_getdir(CVSFILE *cf, int flags, char *path, int (*cb)(CVSFILE *, void *), void *arg)
1.68 joris 668: {
669: int l;
670: int check_entry;
671: u_int ndirs, nfiles;
672: char *cur, *np;
673: char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
674: struct dirent *ent;
1.76 joris 675: CVSFILE *cfp;
1.68 joris 676: struct cvs_ent *cvsent;
677: struct cvs_flist dirs;
678: DIR *dirp;
679: CVSENTRIES *entfile;
680:
1.81 joris 681: check_entry = 1;
682: ndirs = nfiles = 0;
1.68 joris 683: SIMPLEQ_INIT(&dirs);
684:
685: cvs_file_getpath(cf, fpath, sizeof(fpath));
686: entfile = cvs_ent_open(fpath, O_RDONLY);
687:
688: cf->cf_root = cvsroot_get(fpath);
689: if (cf->cf_root == NULL)
690: return (-1);
691:
692: cur = np = NULL;
693: if (path != NULL) {
694: cur = strchr(path, '/');
695: if (cur != NULL) {
696: *cur = '\0';
697: np = cur + 1;
698: if (np != NULL && *np == '\0')
699: np = NULL;
1.20 jfb 700: }
701: }
1.14 jfb 702:
1.56 jfb 703: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 704: return (0);
705:
1.73 joris 706: /* callback for the directory entry */
707: if (cb != NULL) {
708: if (cb(cf, arg) != CVS_EX_OK)
709: return (-1);
710: }
711:
1.68 joris 712: dirp = opendir(fpath);
713: if (dirp == NULL) {
714: cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6 jfb 715: return (-1);
1.3 jfb 716: }
717:
1.68 joris 718: while ((ent = readdir(dirp)) != NULL) {
719: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
720: continue;
1.11 jfb 721:
1.68 joris 722: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
723: continue;
1.24 jfb 724:
1.68 joris 725: if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
726: if (entfile != NULL)
727: (void)cvs_ent_remove(entfile,
728: ent->d_name);
729: continue;
730: }
1.70 joris 731:
732: if ((ent->d_type != DT_DIR) && (flags & CF_NOFILES))
733: continue;
1.56 jfb 734:
1.68 joris 735: if (path != NULL) {
736: if (strcmp(path, ent->d_name))
1.56 jfb 737: continue;
1.68 joris 738: }
1.11 jfb 739:
1.68 joris 740: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
741: ent->d_name);
742: if (l == -1 || l >= (int)sizeof(pbuf)) {
743: errno = ENAMETOOLONG;
744: cvs_log(LP_ERRNO, "%s", pbuf);
745: closedir(dirp);
746: return (-1);
747: }
1.61 xsa 748:
1.68 joris 749: cfp = cvs_file_find(cf, ent->d_name);
750: if (cfp == NULL) {
1.62 jfb 751: if (entfile != NULL)
752: cvsent = cvs_ent_get(entfile, ent->d_name);
1.71 joris 753: else
754: cvsent = NULL;
755:
1.62 jfb 756: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68 joris 757:
1.55 jfb 758: if (cfp == NULL) {
1.68 joris 759: closedir(dirp);
1.55 jfb 760: return (-1);
761: }
1.62 jfb 762: if (entfile != NULL)
763: cvs_ent_remove(entfile, cfp->cf_name);
1.55 jfb 764:
1.68 joris 765: if (cfp->cf_type != DT_DIR) {
1.62 jfb 766: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.68 joris 767: cf_list);
1.62 jfb 768: nfiles++;
1.11 jfb 769: }
1.68 joris 770: } else {
771: cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4 jfb 772: }
1.14 jfb 773:
1.68 joris 774: if (cfp->cf_type == DT_DIR) {
775: ndirs++;
776: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.73 joris 777: } else {
778: /* callback for the file */
779: if (cb != NULL) {
780: if (cb(cfp, arg) != CVS_EX_OK) {
781: closedir(dirp);
782: return (-1);
783: }
784: }
1.68 joris 785: }
786:
787: if (path != NULL) {
788: check_entry = 0;
789: break;
790: }
791: }
792:
793: closedir(dirp);
794:
795: if (entfile != NULL && check_entry) {
1.62 jfb 796: while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68 joris 797: if (path != NULL) {
798: if (strcmp(cvsent->ce_name, path))
799: continue;
800: }
801:
1.61 xsa 802: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44 jfb 803: cvsent->ce_name);
1.61 xsa 804: if (l == -1 || l >= (int)sizeof(pbuf)) {
805: errno = ENAMETOOLONG;
806: cvs_log(LP_ERRNO, "%s", pbuf);
807: return (-1);
808: }
809:
1.68 joris 810: cfp = cvs_file_find(cf, cvsent->ce_name);
811: if (cfp == NULL) {
812: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
813: if (cfp == NULL)
814: continue;
815:
816: if (cfp->cf_type != DT_DIR) {
817: SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
818: cfp, cf_list);
1.62 jfb 819: nfiles++;
1.44 jfb 820: }
1.68 joris 821: } else {
822: cfp->cf_flags |= CVS_GDIR_IGNORE;
823: }
824:
825: if (cfp->cf_type == DT_DIR) {
826: ndirs++;
827: SIMPLEQ_INSERT_TAIL(&dirs, cfp,
828: cf_list);
1.73 joris 829: } else {
830: /* callback for the file */
831: if (cb != NULL) {
832: if (cb(cfp, arg) != CVS_EX_OK)
833: return (-1);
834: }
1.44 jfb 835: }
1.68 joris 836:
837: if (path != NULL)
838: break;
1.44 jfb 839: }
1.62 jfb 840: cvs_ent_close(entfile);
1.40 jfb 841: }
1.34 jfb 842:
1.10 jfb 843: if (flags & CF_SORT) {
1.68 joris 844: if (nfiles > 0)
845: cvs_file_sort(&(cf->cf_files), nfiles);
846: if (ndirs > 0)
847: cvs_file_sort(&dirs, ndirs);
1.10 jfb 848: }
1.26 jfb 849:
1.62 jfb 850: while (!SIMPLEQ_EMPTY(&dirs)) {
851: cfp = SIMPLEQ_FIRST(&dirs);
852: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 853:
854: if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
855: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
856: else
857: cfp->cf_flags &= ~CVS_GDIR_IGNORE;
858:
1.73 joris 859: if (cvs_file_getdir(cfp, flags, np, cb, arg) < 0) {
1.82 xsa 860: cvs_log(LP_ERR, "failed to get %s", CVS_FILE_NAME(cfp));
1.68 joris 861: continue;
862: }
1.26 jfb 863: }
1.3 jfb 864:
1.6 jfb 865: return (0);
1.3 jfb 866: }
867:
868:
869: /*
870: * cvs_file_free()
871: *
872: * Free a cvs_file structure and its contents.
873: */
874: void
1.14 jfb 875: cvs_file_free(CVSFILE *cf)
1.3 jfb 876: {
1.62 jfb 877: CVSFILE *child;
878:
1.47 jfb 879: if (cf->cf_name != NULL)
1.57 jfb 880: cvs_strfree(cf->cf_name);
1.62 jfb 881:
882: if (cf->cf_type == DT_DIR) {
883: if (cf->cf_root != NULL)
884: cvsroot_free(cf->cf_root);
885: if (cf->cf_repo != NULL)
886: free(cf->cf_repo);
887: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
888: child = SIMPLEQ_FIRST(&(cf->cf_files));
889: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
890: cvs_file_free(child);
891: }
1.64 joris 892: } else {
893: if (cf->cf_tag != NULL)
894: cvs_strfree(cf->cf_tag);
1.77 jfb 895: if (cf->cf_opts != NULL)
896: cvs_strfree(cf->cf_opts);
1.62 jfb 897: }
1.64 joris 898:
1.3 jfb 899: free(cf);
1.5 jfb 900: }
901:
902:
903: /*
904: * cvs_file_examine()
905: *
906: * Examine the contents of the CVS file structure <cf> with the function
907: * <exam>. The function is called for all subdirectories and files of the
908: * root file.
909: */
910: int
911: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
912: {
913: int ret;
1.14 jfb 914: CVSFILE *fp;
1.5 jfb 915:
916: if (cf->cf_type == DT_DIR) {
917: ret = (*exam)(cf, arg);
1.62 jfb 918: SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5 jfb 919: ret = cvs_file_examine(fp, exam, arg);
1.60 joris 920: if (ret != 0)
1.13 jfb 921: break;
1.5 jfb 922: }
1.38 deraadt 923: } else
1.13 jfb 924: ret = (*exam)(cf, arg);
925:
926: return (ret);
1.3 jfb 927: }
928:
929: /*
930: * cvs_file_sort()
931: *
1.16 jfb 932: * Sort a list of cvs file structures according to their filename. The list
933: * <flp> is modified according to the sorting algorithm. The number of files
934: * in the list must be given by <nfiles>.
935: * Returns 0 on success, or -1 on failure.
1.3 jfb 936: */
937: static int
1.16 jfb 938: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 939: {
940: int i;
941: size_t nb;
1.16 jfb 942: CVSFILE *cf, **cfvec;
943:
944: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
945: if (cfvec == NULL) {
946: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
947: return (-1);
948: }
1.3 jfb 949:
950: i = 0;
1.62 jfb 951: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 952: if (i == (int)nfiles) {
1.3 jfb 953: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 954: /* rebuild the list and abort sorting */
955: while (--i >= 0)
1.62 jfb 956: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16 jfb 957: free(cfvec);
1.3 jfb 958: return (-1);
959: }
1.16 jfb 960: cfvec[i++] = cf;
1.3 jfb 961:
962: /* now unlink it from the list,
963: * we'll put it back in order later
964: */
1.62 jfb 965: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 966: }
967:
968: /* clear the list just in case */
1.62 jfb 969: SIMPLEQ_INIT(flp);
1.3 jfb 970: nb = (size_t)i;
971:
972: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
973:
974: /* rebuild the list from the bottom up */
975: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 976: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 977:
1.16 jfb 978: free(cfvec);
1.3 jfb 979: return (0);
980: }
981:
982:
983: static int
984: cvs_file_cmp(const void *f1, const void *f2)
985: {
1.41 jfb 986: const CVSFILE *cf1, *cf2;
1.62 jfb 987: cf1 = *(CVSFILE * const *)f1;
988: cf2 = *(CVSFILE * const *)f2;
1.34 jfb 989: return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6 jfb 990: }
991:
992:
1.34 jfb 993: /*
994: * cvs_file_alloc()
995: *
996: * Allocate a CVSFILE structure and initialize its internals.
997: */
1.6 jfb 998: CVSFILE*
999: cvs_file_alloc(const char *path, u_int type)
1000: {
1001: CVSFILE *cfp;
1002:
1.14 jfb 1003: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 1004: if (cfp == NULL) {
1005: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
1006: return (NULL);
1007: }
1008: memset(cfp, 0, sizeof(*cfp));
1009:
1.62 jfb 1010: cfp->cf_type = type;
1011: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1012:
1013: if (type == DT_DIR) {
1014: SIMPLEQ_INIT(&(cfp->cf_files));
1015: }
1016:
1.57 jfb 1017: cfp->cf_name = cvs_strdup(basename(path));
1.34 jfb 1018: if (cfp->cf_name == NULL) {
1.57 jfb 1019: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 1020: cvs_file_free(cfp);
1.6 jfb 1021: return (NULL);
1022: }
1023:
1024: return (cfp);
1.1 jfb 1025: }
1.14 jfb 1026:
1027:
1028: /*
1029: * cvs_file_lget()
1030: *
1031: * Get the file and link it with the parent right away.
1.22 jfb 1032: * Returns a pointer to the created file structure on success, or NULL on
1033: * failure.
1.14 jfb 1034: */
1035: static CVSFILE*
1.62 jfb 1036: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14 jfb 1037: {
1.44 jfb 1038: int ret, cwd;
1039: u_int type;
1.14 jfb 1040: struct stat st;
1041: CVSFILE *cfp;
1042:
1.44 jfb 1043: type = DT_UNKNOWN;
1044: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62 jfb 1045:
1.44 jfb 1046: ret = stat(path, &st);
1047: if (ret == 0)
1048: type = IFTODT(st.st_mode);
1.14 jfb 1049:
1.44 jfb 1050: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 1051: return (NULL);
1052: cfp->cf_parent = parent;
1.54 joris 1053:
1054: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 1055: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 1056:
1.44 jfb 1057: if (ret == 0) {
1058: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 1059: if (cfp->cf_type == DT_REG)
1060: cfp->cf_mtime = st.st_mtime;
1.44 jfb 1061:
1062: if (ent == NULL)
1063: cfp->cf_cvstat = (cwd == 1) ?
1064: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14 jfb 1065: else {
1.44 jfb 1066: /* always show directories as up-to-date */
1067: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 1068: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 1069: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1070: cfp->cf_cvstat = CVS_FST_ADDED;
1071: else {
1072: /* check last modified time */
1.80 jfb 1073: if (ent->ce_mtime == (time_t)st.st_mtime)
1.44 jfb 1074: cfp->cf_cvstat = CVS_FST_UPTODATE;
1075: else
1076: cfp->cf_cvstat = CVS_FST_MODIFIED;
1077: }
1.14 jfb 1078: }
1.44 jfb 1079: } else {
1080: if (ent == NULL) {
1081: cvs_log(LP_ERR, "no Entry and no file for `%s'",
1082: CVS_FILE_NAME(cfp));
1083: cvs_file_free(cfp);
1084: return (NULL);
1.66 joris 1085: } else {
1086: if (ent->ce_type == CVS_ENT_FILE)
1087: cfp->cf_type = DT_REG;
1088: else if (ent->ce_type == CVS_ENT_DIR)
1089: cfp->cf_type = DT_DIR;
1090: else
1091: cvs_log(LP_WARN, "unknown ce_type %d",
1092: ent->ce_type);
1.67 joris 1093:
1094: if (ent->ce_status == CVS_ENT_REMOVED)
1095: cfp->cf_cvstat = CVS_FST_REMOVED;
1096: else
1097: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1098: }
1.14 jfb 1099: }
1.52 jfb 1100:
1.62 jfb 1101: if (ent != NULL) {
1102: /* steal the RCSNUM */
1103: cfp->cf_lrev = ent->ce_rev;
1.72 jfb 1104: ent->ce_rev = NULL;
1105:
1.77 jfb 1106: if (ent->ce_type == CVS_ENT_FILE) {
1107: if (ent->ce_tag[0] != '\0') {
1108: cfp->cf_tag = cvs_strdup(ent->ce_tag);
1109: if (cfp->cf_tag == NULL) {
1110: cvs_file_free(cfp);
1111: return (NULL);
1112: }
1113: }
1114:
1115: if (ent->ce_opts[0] != '\0') {
1116: cfp->cf_opts = cvs_strdup(ent->ce_opts);
1117: if (cfp->cf_opts == NULL) {
1118: cvs_file_free(cfp);
1119: return (NULL);
1120: }
1.65 joris 1121: }
1.62 jfb 1122: }
1123: }
1.14 jfb 1124:
1.69 joris 1125: if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26 jfb 1126: cvs_file_free(cfp);
1127: return (NULL);
1.14 jfb 1128: }
1.74 joris 1129:
1130: if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
1131: !strcmp(cfp->cf_repo, path))
1132: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14 jfb 1133:
1134: return (cfp);
1.23 jfb 1135: }
1136:
1137:
1138: static int
1139: cvs_file_cmpname(const char *name1, const char *name2)
1140: {
1141: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1142: (strcasecmp(name1, name2));
1.14 jfb 1143: }