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