Annotation of src/usr.bin/cvs/file.c, Revision 1.86
1.86 ! joris 1: /* $OpenBSD: file.c,v 1.85 2005/06/01 15:46:32 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: }
1.26 jfb 437:
1.73 joris 438: /* XXX - needed for some commands */
439: if (cb != NULL) {
440: if (cb(base, arg) != CVS_EX_OK) {
441: cvs_file_free(base);
442: return (NULL);
443: }
444: }
445:
1.26 jfb 446: for (i = 0; i < fsn; i++) {
447: strlcpy(pcopy, fspec[i], sizeof(pcopy));
448: sp = pcopy;
1.68 joris 449: pwd = (!strcmp(pcopy, "."));
1.26 jfb 450:
1.68 joris 451: np = strchr(sp, '/');
452: if (np != NULL)
453: *np = '\0';
454:
455: if (pwd) {
456: nf = base;
457: } else {
458: nf = cvs_file_find(base, pcopy);
1.26 jfb 459: if (nf == NULL) {
1.68 joris 460: if (entfile != NULL)
461: ent = cvs_ent_get(entfile, pcopy);
462: else
463: ent = NULL;
464: nf = cvs_file_lget(pcopy, 0, base, ent);
1.26 jfb 465: if (nf == NULL) {
466: cvs_file_free(base);
467: return (NULL);
468: }
469:
1.68 joris 470: if (cvs_file_attach(base, nf) < 0) {
1.59 joris 471: cvs_file_free(base);
472: return (NULL);
473: }
1.26 jfb 474: }
1.68 joris 475: }
1.26 jfb 476:
1.68 joris 477: if (nf->cf_type == DT_DIR) {
478: if (np != NULL)
479: *np++;
480:
1.73 joris 481: if (cvs_file_getdir(nf, flags, np, cb, arg) < 0) {
1.68 joris 482: cvs_file_free(base);
483: return (NULL);
1.26 jfb 484: }
1.73 joris 485: } else {
486: if (cb != NULL) {
1.75 joris 487: if (cb(nf, arg) != CVS_EX_OK) {
488: cvs_file_free(base);
1.73 joris 489: return (NULL);
1.75 joris 490: }
1.73 joris 491: }
1.68 joris 492: }
1.26 jfb 493: }
494:
495: return (base);
1.3 jfb 496: }
497:
498:
499: /*
1.13 jfb 500: * cvs_file_find()
501: *
502: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
503: * The file's pathname <path> must be relative to the base of <hier>.
504: * Returns the entry on success, or NULL on failure.
505: */
506: CVSFILE*
507: cvs_file_find(CVSFILE *hier, const char *path)
508: {
509: char *pp, *sp, pbuf[MAXPATHLEN];
510: CVSFILE *sf, *cf;
511:
512: strlcpy(pbuf, path, sizeof(pbuf));
513:
514: cf = hier;
515: pp = pbuf;
516: do {
517: sp = strchr(pp, '/');
518: if (sp != NULL)
1.24 jfb 519: *(sp++) = '\0';
1.13 jfb 520:
521: /* special case */
522: if (*pp == '.') {
523: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
524: /* request to go back to parent */
525: if (cf->cf_parent == NULL) {
526: cvs_log(LP_NOTICE,
527: "path %s goes back too far", path);
528: return (NULL);
529: }
530: cf = cf->cf_parent;
531: continue;
1.38 deraadt 532: } else if (*(pp + 1) == '\0')
1.13 jfb 533: continue;
534: }
535:
1.62 jfb 536: SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.34 jfb 537: if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13 jfb 538: break;
539: if (sf == NULL)
540: return (NULL);
541:
542: cf = sf;
543: pp = sp;
544: } while (sp != NULL);
545:
1.24 jfb 546: return (cf);
1.13 jfb 547: }
548:
549:
550: /*
1.34 jfb 551: * cvs_file_getpath()
552: *
553: * Get the full path of the file <file> and store it in <buf>, which is of
554: * size <len>. For portability, it is recommended that <buf> always be
555: * at least MAXPATHLEN bytes long.
556: * Returns a pointer to the start of the path on success, or NULL on failure.
557: */
558: char*
559: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
560: {
561: u_int i;
1.77 jfb 562: const char *fp, *namevec[CVS_FILE_MAXDEPTH];
1.34 jfb 563: CVSFILE *top;
564:
565: buf[0] = '\0';
566: i = CVS_FILE_MAXDEPTH;
567: memset(namevec, 0, sizeof(namevec));
568:
569: /* find the top node */
570: for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
571: fp = CVS_FILE_NAME(top);
572:
573: /* skip self-references */
574: if ((fp[0] == '.') && (fp[1] == '\0'))
575: continue;
576: namevec[--i] = fp;
577: }
578:
579: if (i == 0)
580: return (NULL);
581: else if (i == CVS_FILE_MAXDEPTH) {
582: strlcpy(buf, ".", len);
583: return (buf);
584: }
585:
586: while (i < CVS_FILE_MAXDEPTH - 1) {
587: strlcat(buf, namevec[i++], len);
588: strlcat(buf, "/", len);
589: }
590: strlcat(buf, namevec[i], len);
591:
592: return (buf);
593: }
594:
595:
596: /*
1.22 jfb 597: * cvs_file_attach()
598: *
599: * Attach the file <file> as one of the children of parent <parent>, which
600: * has to be a file of type DT_DIR.
601: * Returns 0 on success, or -1 on failure.
602: */
603: int
604: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
605: {
606: if (parent->cf_type != DT_DIR)
607: return (-1);
608:
1.62 jfb 609: SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22 jfb 610: file->cf_parent = parent;
611:
612: return (0);
613: }
614:
615:
616: /*
1.68 joris 617: * Load directory information
1.3 jfb 618: */
1.6 jfb 619: static int
1.69 joris 620: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3 jfb 621: {
1.68 joris 622: char fpath[MAXPATHLEN];
623: char pbuf[MAXPATHLEN];
1.20 jfb 624: struct stat st;
1.68 joris 625: int l;
1.7 jfb 626:
1.34 jfb 627: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.62 jfb 628: cf->cf_root = cvsroot_get(fpath);
629: if (cf->cf_root == NULL)
1.48 jfb 630: return (-1);
631:
1.74 joris 632: if (flags & CF_MKADMIN)
1.78 joris 633: cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL);
1.69 joris 634:
1.74 joris 635: /* if the CVS administrative directory exists, load the info */
636: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
637: if (l == -1 || l >= (int)sizeof(pbuf)) {
638: errno = ENAMETOOLONG;
639: cvs_log(LP_ERRNO, "%s", pbuf);
640: return (-1);
641: }
1.61 xsa 642:
1.74 joris 643: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
644: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
645: cf->cf_repo = strdup(pbuf);
646: if (cf->cf_repo == NULL) {
647: cvs_log(LP_ERRNO,
648: "failed to dup repository string");
649: return (-1);
1.20 jfb 650: }
1.68 joris 651: }
652: }
1.26 jfb 653:
1.68 joris 654: return (0);
655: }
656:
657: /*
658: * cvs_file_getdir()
659: *
660: * Get a cvs directory structure for the directory whose path is <dir>.
661: * This function should not free the directory information on error, as this
662: * is performed by cvs_file_free().
663: */
664: static int
1.73 joris 665: cvs_file_getdir(CVSFILE *cf, int flags, char *path, int (*cb)(CVSFILE *, void *), void *arg)
1.68 joris 666: {
667: int l;
668: int check_entry;
669: u_int ndirs, nfiles;
670: char *cur, *np;
671: char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
672: struct dirent *ent;
1.76 joris 673: CVSFILE *cfp;
1.68 joris 674: struct cvs_ent *cvsent;
675: struct cvs_flist dirs;
676: DIR *dirp;
677: CVSENTRIES *entfile;
678:
1.81 joris 679: check_entry = 1;
680: ndirs = nfiles = 0;
1.68 joris 681: SIMPLEQ_INIT(&dirs);
682:
683: cvs_file_getpath(cf, fpath, sizeof(fpath));
684: entfile = cvs_ent_open(fpath, O_RDONLY);
685:
686: cf->cf_root = cvsroot_get(fpath);
687: if (cf->cf_root == NULL)
688: return (-1);
689:
690: cur = np = NULL;
691: if (path != NULL) {
692: cur = strchr(path, '/');
693: if (cur != NULL) {
694: *cur = '\0';
695: np = cur + 1;
696: if (np != NULL && *np == '\0')
697: np = NULL;
1.20 jfb 698: }
699: }
1.14 jfb 700:
1.56 jfb 701: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 702: return (0);
703:
1.73 joris 704: /* callback for the directory entry */
705: if (cb != NULL) {
706: if (cb(cf, arg) != CVS_EX_OK)
707: return (-1);
708: }
709:
1.68 joris 710: dirp = opendir(fpath);
711: if (dirp == NULL) {
712: cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6 jfb 713: return (-1);
1.3 jfb 714: }
715:
1.68 joris 716: while ((ent = readdir(dirp)) != NULL) {
717: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
718: continue;
1.11 jfb 719:
1.68 joris 720: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
721: continue;
1.24 jfb 722:
1.68 joris 723: if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
724: if (entfile != NULL)
725: (void)cvs_ent_remove(entfile,
726: ent->d_name);
727: continue;
728: }
1.70 joris 729:
730: if ((ent->d_type != DT_DIR) && (flags & CF_NOFILES))
731: continue;
1.56 jfb 732:
1.68 joris 733: if (path != NULL) {
734: if (strcmp(path, ent->d_name))
1.56 jfb 735: continue;
1.68 joris 736: }
1.11 jfb 737:
1.68 joris 738: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
739: ent->d_name);
740: if (l == -1 || l >= (int)sizeof(pbuf)) {
741: errno = ENAMETOOLONG;
742: cvs_log(LP_ERRNO, "%s", pbuf);
743: closedir(dirp);
744: return (-1);
745: }
1.61 xsa 746:
1.68 joris 747: cfp = cvs_file_find(cf, ent->d_name);
748: if (cfp == NULL) {
1.62 jfb 749: if (entfile != NULL)
750: cvsent = cvs_ent_get(entfile, ent->d_name);
1.71 joris 751: else
752: cvsent = NULL;
753:
1.62 jfb 754: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68 joris 755:
1.55 jfb 756: if (cfp == NULL) {
1.68 joris 757: closedir(dirp);
1.55 jfb 758: return (-1);
759: }
1.62 jfb 760: if (entfile != NULL)
761: cvs_ent_remove(entfile, cfp->cf_name);
1.55 jfb 762:
1.68 joris 763: if (cfp->cf_type != DT_DIR) {
1.62 jfb 764: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.68 joris 765: cf_list);
1.62 jfb 766: nfiles++;
1.11 jfb 767: }
1.68 joris 768: } else {
769: cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4 jfb 770: }
1.14 jfb 771:
1.68 joris 772: if (cfp->cf_type == DT_DIR) {
773: ndirs++;
774: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.73 joris 775: } else {
776: /* callback for the file */
777: if (cb != NULL) {
778: if (cb(cfp, arg) != CVS_EX_OK) {
779: closedir(dirp);
780: return (-1);
781: }
782: }
1.68 joris 783: }
784:
785: if (path != NULL) {
786: check_entry = 0;
787: break;
788: }
789: }
790:
791: closedir(dirp);
792:
793: if (entfile != NULL && check_entry) {
1.62 jfb 794: while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68 joris 795: if (path != NULL) {
796: if (strcmp(cvsent->ce_name, path))
797: continue;
798: }
799:
1.61 xsa 800: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44 jfb 801: cvsent->ce_name);
1.61 xsa 802: if (l == -1 || l >= (int)sizeof(pbuf)) {
803: errno = ENAMETOOLONG;
804: cvs_log(LP_ERRNO, "%s", pbuf);
805: return (-1);
806: }
807:
1.68 joris 808: cfp = cvs_file_find(cf, cvsent->ce_name);
809: if (cfp == NULL) {
810: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
811: if (cfp == NULL)
812: continue;
813:
814: if (cfp->cf_type != DT_DIR) {
815: SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
816: cfp, cf_list);
1.62 jfb 817: nfiles++;
1.44 jfb 818: }
1.68 joris 819: } else {
820: cfp->cf_flags |= CVS_GDIR_IGNORE;
821: }
822:
823: if (cfp->cf_type == DT_DIR) {
824: ndirs++;
825: SIMPLEQ_INSERT_TAIL(&dirs, cfp,
826: cf_list);
1.73 joris 827: } else {
828: /* callback for the file */
829: if (cb != NULL) {
830: if (cb(cfp, arg) != CVS_EX_OK)
831: return (-1);
832: }
1.44 jfb 833: }
1.68 joris 834:
835: if (path != NULL)
836: break;
1.44 jfb 837: }
1.62 jfb 838: cvs_ent_close(entfile);
1.40 jfb 839: }
1.34 jfb 840:
1.10 jfb 841: if (flags & CF_SORT) {
1.68 joris 842: if (nfiles > 0)
843: cvs_file_sort(&(cf->cf_files), nfiles);
844: if (ndirs > 0)
845: cvs_file_sort(&dirs, ndirs);
1.10 jfb 846: }
1.26 jfb 847:
1.62 jfb 848: while (!SIMPLEQ_EMPTY(&dirs)) {
849: cfp = SIMPLEQ_FIRST(&dirs);
850: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 851:
852: if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
853: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
854: else
855: cfp->cf_flags &= ~CVS_GDIR_IGNORE;
856:
1.73 joris 857: if (cvs_file_getdir(cfp, flags, np, cb, arg) < 0) {
1.82 xsa 858: cvs_log(LP_ERR, "failed to get %s", CVS_FILE_NAME(cfp));
1.68 joris 859: continue;
860: }
1.26 jfb 861: }
1.3 jfb 862:
1.6 jfb 863: return (0);
1.3 jfb 864: }
865:
866:
867: /*
868: * cvs_file_free()
869: *
870: * Free a cvs_file structure and its contents.
871: */
872: void
1.14 jfb 873: cvs_file_free(CVSFILE *cf)
1.3 jfb 874: {
1.62 jfb 875: CVSFILE *child;
876:
1.47 jfb 877: if (cf->cf_name != NULL)
1.57 jfb 878: cvs_strfree(cf->cf_name);
1.62 jfb 879:
880: if (cf->cf_type == DT_DIR) {
881: if (cf->cf_root != NULL)
882: cvsroot_free(cf->cf_root);
883: if (cf->cf_repo != NULL)
884: free(cf->cf_repo);
885: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
886: child = SIMPLEQ_FIRST(&(cf->cf_files));
887: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
888: cvs_file_free(child);
889: }
1.64 joris 890: } else {
891: if (cf->cf_tag != NULL)
892: cvs_strfree(cf->cf_tag);
1.77 jfb 893: if (cf->cf_opts != NULL)
894: cvs_strfree(cf->cf_opts);
1.62 jfb 895: }
1.64 joris 896:
1.3 jfb 897: free(cf);
1.5 jfb 898: }
899:
900:
901: /*
902: * cvs_file_examine()
903: *
904: * Examine the contents of the CVS file structure <cf> with the function
905: * <exam>. The function is called for all subdirectories and files of the
906: * root file.
907: */
908: int
909: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
910: {
911: int ret;
1.14 jfb 912: CVSFILE *fp;
1.5 jfb 913:
914: if (cf->cf_type == DT_DIR) {
915: ret = (*exam)(cf, arg);
1.62 jfb 916: SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5 jfb 917: ret = cvs_file_examine(fp, exam, arg);
1.60 joris 918: if (ret != 0)
1.13 jfb 919: break;
1.5 jfb 920: }
1.38 deraadt 921: } else
1.13 jfb 922: ret = (*exam)(cf, arg);
923:
924: return (ret);
1.3 jfb 925: }
926:
927: /*
928: * cvs_file_sort()
929: *
1.16 jfb 930: * Sort a list of cvs file structures according to their filename. The list
931: * <flp> is modified according to the sorting algorithm. The number of files
932: * in the list must be given by <nfiles>.
933: * Returns 0 on success, or -1 on failure.
1.3 jfb 934: */
935: static int
1.16 jfb 936: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 937: {
938: int i;
939: size_t nb;
1.16 jfb 940: CVSFILE *cf, **cfvec;
941:
942: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
943: if (cfvec == NULL) {
944: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
945: return (-1);
946: }
1.3 jfb 947:
948: i = 0;
1.62 jfb 949: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 950: if (i == (int)nfiles) {
1.3 jfb 951: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 952: /* rebuild the list and abort sorting */
953: while (--i >= 0)
1.62 jfb 954: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16 jfb 955: free(cfvec);
1.3 jfb 956: return (-1);
957: }
1.16 jfb 958: cfvec[i++] = cf;
1.3 jfb 959:
960: /* now unlink it from the list,
961: * we'll put it back in order later
962: */
1.62 jfb 963: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 964: }
965:
966: /* clear the list just in case */
1.62 jfb 967: SIMPLEQ_INIT(flp);
1.3 jfb 968: nb = (size_t)i;
969:
970: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
971:
972: /* rebuild the list from the bottom up */
973: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 974: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 975:
1.16 jfb 976: free(cfvec);
1.3 jfb 977: return (0);
978: }
979:
980:
981: static int
982: cvs_file_cmp(const void *f1, const void *f2)
983: {
1.41 jfb 984: const CVSFILE *cf1, *cf2;
1.62 jfb 985: cf1 = *(CVSFILE * const *)f1;
986: cf2 = *(CVSFILE * const *)f2;
1.34 jfb 987: return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6 jfb 988: }
989:
990:
1.34 jfb 991: /*
992: * cvs_file_alloc()
993: *
994: * Allocate a CVSFILE structure and initialize its internals.
995: */
1.6 jfb 996: CVSFILE*
997: cvs_file_alloc(const char *path, u_int type)
998: {
999: CVSFILE *cfp;
1000:
1.14 jfb 1001: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 1002: if (cfp == NULL) {
1003: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
1004: return (NULL);
1005: }
1006: memset(cfp, 0, sizeof(*cfp));
1007:
1.62 jfb 1008: cfp->cf_type = type;
1009: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1010:
1011: if (type == DT_DIR) {
1012: SIMPLEQ_INIT(&(cfp->cf_files));
1013: }
1014:
1.57 jfb 1015: cfp->cf_name = cvs_strdup(basename(path));
1.34 jfb 1016: if (cfp->cf_name == NULL) {
1.57 jfb 1017: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 1018: cvs_file_free(cfp);
1.6 jfb 1019: return (NULL);
1020: }
1021:
1022: return (cfp);
1.1 jfb 1023: }
1.14 jfb 1024:
1025:
1026: /*
1027: * cvs_file_lget()
1028: *
1029: * Get the file and link it with the parent right away.
1.22 jfb 1030: * Returns a pointer to the created file structure on success, or NULL on
1031: * failure.
1.14 jfb 1032: */
1033: static CVSFILE*
1.62 jfb 1034: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14 jfb 1035: {
1.44 jfb 1036: int ret, cwd;
1037: u_int type;
1.14 jfb 1038: struct stat st;
1039: CVSFILE *cfp;
1040:
1.44 jfb 1041: type = DT_UNKNOWN;
1042: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62 jfb 1043:
1.44 jfb 1044: ret = stat(path, &st);
1045: if (ret == 0)
1046: type = IFTODT(st.st_mode);
1.14 jfb 1047:
1.44 jfb 1048: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 1049: return (NULL);
1050: cfp->cf_parent = parent;
1.54 joris 1051:
1052: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 1053: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 1054:
1.44 jfb 1055: if (ret == 0) {
1056: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 1057: if (cfp->cf_type == DT_REG)
1058: cfp->cf_mtime = st.st_mtime;
1.44 jfb 1059:
1060: if (ent == NULL)
1061: cfp->cf_cvstat = (cwd == 1) ?
1062: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14 jfb 1063: else {
1.44 jfb 1064: /* always show directories as up-to-date */
1065: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 1066: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 1067: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1068: cfp->cf_cvstat = CVS_FST_ADDED;
1069: else {
1070: /* check last modified time */
1.80 jfb 1071: if (ent->ce_mtime == (time_t)st.st_mtime)
1.44 jfb 1072: cfp->cf_cvstat = CVS_FST_UPTODATE;
1073: else
1074: cfp->cf_cvstat = CVS_FST_MODIFIED;
1075: }
1.14 jfb 1076: }
1.44 jfb 1077: } else {
1078: if (ent == NULL) {
1079: cvs_log(LP_ERR, "no Entry and no file for `%s'",
1080: CVS_FILE_NAME(cfp));
1081: cvs_file_free(cfp);
1082: return (NULL);
1.66 joris 1083: } else {
1084: if (ent->ce_type == CVS_ENT_FILE)
1085: cfp->cf_type = DT_REG;
1086: else if (ent->ce_type == CVS_ENT_DIR)
1087: cfp->cf_type = DT_DIR;
1088: else
1089: cvs_log(LP_WARN, "unknown ce_type %d",
1090: ent->ce_type);
1.67 joris 1091:
1092: if (ent->ce_status == CVS_ENT_REMOVED)
1093: cfp->cf_cvstat = CVS_FST_REMOVED;
1094: else
1095: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1096: }
1.14 jfb 1097: }
1.52 jfb 1098:
1.62 jfb 1099: if (ent != NULL) {
1100: /* steal the RCSNUM */
1101: cfp->cf_lrev = ent->ce_rev;
1.72 jfb 1102: ent->ce_rev = NULL;
1103:
1.77 jfb 1104: if (ent->ce_type == CVS_ENT_FILE) {
1105: if (ent->ce_tag[0] != '\0') {
1106: cfp->cf_tag = cvs_strdup(ent->ce_tag);
1107: if (cfp->cf_tag == NULL) {
1108: cvs_file_free(cfp);
1109: return (NULL);
1110: }
1111: }
1112:
1113: if (ent->ce_opts[0] != '\0') {
1114: cfp->cf_opts = cvs_strdup(ent->ce_opts);
1115: if (cfp->cf_opts == NULL) {
1116: cvs_file_free(cfp);
1117: return (NULL);
1118: }
1.65 joris 1119: }
1.62 jfb 1120: }
1121: }
1.14 jfb 1122:
1.69 joris 1123: if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26 jfb 1124: cvs_file_free(cfp);
1125: return (NULL);
1.14 jfb 1126: }
1.74 joris 1127:
1128: if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
1129: !strcmp(cfp->cf_repo, path))
1130: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14 jfb 1131:
1132: return (cfp);
1.23 jfb 1133: }
1134:
1135:
1136: static int
1137: cvs_file_cmpname(const char *name1, const char *name2)
1138: {
1139: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1140: (strcasecmp(name1, name2));
1.14 jfb 1141: }