Annotation of src/usr.bin/cvs/file.c, Revision 1.96
1.96 ! joris 1: /* $OpenBSD: file.c,v 1.95 2005/07/07 20:24:35 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:
1.1 jfb 119: /*
120: * cvs_file_init()
121: *
122: */
123: int
124: cvs_file_init(void)
125: {
1.61 xsa 126: int i, l;
1.1 jfb 127: size_t len;
128: char path[MAXPATHLEN], buf[MAXNAMLEN];
129: FILE *ifp;
130: struct passwd *pwd;
131:
132: TAILQ_INIT(&cvs_ign_pats);
133:
1.53 jfb 134: if ((cvs_addedrev = rcsnum_parse("0")) == NULL)
1.46 jfb 135: return (-1);
1.4 jfb 136:
1.1 jfb 137: /* standard patterns to ignore */
1.10 jfb 138: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26 jfb 139: cvs_file_ignore(cvs_ign_std[i]);
1.1 jfb 140:
141: /* read the cvsignore file in the user's home directory, if any */
142: pwd = getpwuid(getuid());
143: if (pwd != NULL) {
1.61 xsa 144: l = snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
145: if (l == -1 || l >= (int)sizeof(path)) {
146: errno = ENAMETOOLONG;
147: cvs_log(LP_ERRNO, "%s", path);
148: return (-1);
149: }
150:
1.1 jfb 151: ifp = fopen(path, "r");
152: if (ifp == NULL) {
153: if (errno != ENOENT)
1.34 jfb 154: cvs_log(LP_ERRNO,
155: "failed to open user's cvsignore", path);
1.38 deraadt 156: } else {
1.1 jfb 157: while (fgets(buf, sizeof(buf), ifp) != NULL) {
158: len = strlen(buf);
159: if (len == 0)
160: continue;
161: if (buf[len - 1] != '\n') {
162: cvs_log(LP_ERR, "line too long in `%s'",
163: path);
164: }
165: buf[--len] = '\0';
166: cvs_file_ignore(buf);
167: }
168: (void)fclose(ifp);
169: }
170: }
171:
172: return (0);
173: }
174:
175:
176: /*
177: * cvs_file_ignore()
178: *
179: * Add the pattern <pat> to the list of patterns for files to ignore.
180: * Returns 0 on success, or -1 on failure.
181: */
182: int
183: cvs_file_ignore(const char *pat)
184: {
185: char *cp;
186: struct cvs_ignpat *ip;
187:
188: ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
189: if (ip == NULL) {
190: cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
191: return (-1);
192: }
193:
194: strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
195:
196: /* check if we will need globbing for that pattern */
197: ip->ip_flags = CVS_IGN_STATIC;
198: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
199: if (CVS_CHAR_ISMETA(*cp)) {
200: ip->ip_flags &= ~CVS_IGN_STATIC;
201: break;
202: }
203: }
204:
205: TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
206:
207: return (0);
208: }
209:
210:
211: /*
1.5 jfb 212: * cvs_file_chkign()
1.1 jfb 213: *
214: * Returns 1 if the filename <file> is matched by one of the ignore
215: * patterns, or 0 otherwise.
216: */
217: int
1.5 jfb 218: cvs_file_chkign(const char *file)
1.1 jfb 219: {
1.23 jfb 220: int flags;
1.1 jfb 221: struct cvs_ignpat *ip;
222:
1.23 jfb 223: flags = FNM_PERIOD;
224: if (cvs_nocase)
225: flags |= FNM_CASEFOLD;
226:
1.1 jfb 227: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
228: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 229: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 230: return (1);
1.38 deraadt 231: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 232: return (1);
233: }
234:
235: return (0);
236: }
237:
238:
239: /*
1.6 jfb 240: * cvs_file_create()
1.1 jfb 241: *
1.6 jfb 242: * Create a new file whose path is specified in <path> and of type <type>.
1.26 jfb 243: * If the type is DT_DIR, the CVS administrative repository and files will be
244: * created.
1.25 jfb 245: * Returns the created file on success, or NULL on failure.
1.1 jfb 246: */
1.6 jfb 247: CVSFILE*
1.34 jfb 248: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1 jfb 249: {
1.85 joris 250: int fd, l;
1.84 joris 251: int bail;
1.85 joris 252: char fp[MAXPATHLEN], repo[MAXPATHLEN];
1.6 jfb 253: CVSFILE *cfp;
1.62 jfb 254: CVSENTRIES *ent;
1.1 jfb 255:
1.6 jfb 256: cfp = cvs_file_alloc(path, type);
257: if (cfp == NULL)
258: return (NULL);
1.26 jfb 259:
1.85 joris 260: bail = l = 0;
1.22 jfb 261: cfp->cf_mode = mode;
1.34 jfb 262: cfp->cf_parent = parent;
1.1 jfb 263:
1.34 jfb 264: if (type == DT_DIR) {
1.62 jfb 265: cfp->cf_root = cvsroot_get(path);
1.84 joris 266:
267: /*
268: * If we do not have a valid root for this, try looking at
269: * the parent its root.
270: */
271: if (cfp->cf_root == NULL) {
272: if (parent != NULL && parent->cf_root != NULL) {
273: cfp->cf_root =
274: cvsroot_parse(parent->cf_root->cr_str);
275: if (cfp->cf_root == NULL)
276: bail = 1;
277: } else {
278: bail = 1;
279: }
280: }
281:
282: /* we tried, too bad */
283: if (bail) {
284: cvs_log(LP_ERR, "failed to obtain root info for `%s'",
285: path);
286: return (NULL);
287: }
288:
1.85 joris 289: if (cvs_repo_base != NULL) {
290: cvs_file_getpath(cfp, fp, sizeof(fp));
291: l = snprintf(repo, sizeof(repo), "%s/%s", cvs_repo_base,
292: fp);
293: } else {
294: cvs_file_getpath(cfp, repo, sizeof(repo));
295: l = 0;
296: }
297:
298: if (l == -1 || l >= (int)sizeof(repo)) {
299: errno = ENAMETOOLONG;
300: cvs_log(LP_ERRNO, "%s", repo);
301: cvs_file_free(cfp);
302: return (NULL);
303: }
304:
305: cfp->cf_repo = strdup(repo);
1.62 jfb 306: if (cfp->cf_repo == NULL) {
1.34 jfb 307: cvs_file_free(cfp);
308: return (NULL);
309: }
1.33 joris 310:
1.79 joris 311: if (((mkdir(path, mode) == -1) && (errno != EEXIST)) ||
1.78 joris 312: (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo) < 0)) {
1.6 jfb 313: cvs_file_free(cfp);
314: return (NULL);
315: }
1.26 jfb 316:
1.62 jfb 317: ent = cvs_ent_open(path, O_RDWR);
318: if (ent != NULL) {
319: cvs_ent_close(ent);
1.50 jfb 320: }
1.38 deraadt 321: } else {
1.6 jfb 322: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
323: if (fd == -1) {
324: cvs_file_free(cfp);
1.1 jfb 325: return (NULL);
326: }
1.6 jfb 327: (void)close(fd);
1.1 jfb 328: }
329:
1.6 jfb 330: return (cfp);
1.3 jfb 331: }
332:
333:
334: /*
1.35 jfb 335: * cvs_file_copy()
336: *
337: * Allocate space to create a copy of the file <orig>. The copy inherits all
338: * of the original's attributes, but does not inherit its children if the
339: * original file is a directory. Note that files copied using this mechanism
340: * are linked to their parent, but the parent has no link to the file. This
341: * is so cvs_file_getpath() works.
342: * Returns the copied file on success, or NULL on failure. The returned
343: * structure should be freed using cvs_file_free().
344: */
345: CVSFILE*
346: cvs_file_copy(CVSFILE *orig)
347: {
348: char path[MAXPATHLEN];
349: CVSFILE *cfp;
350:
351: cvs_file_getpath(orig, path, sizeof(path));
352:
353: cfp = cvs_file_alloc(path, orig->cf_type);
354: if (cfp == NULL)
355: return (NULL);
356:
357: cfp->cf_parent = orig->cf_parent;
358: cfp->cf_mode = orig->cf_mode;
359: cfp->cf_cvstat = orig->cf_cvstat;
360:
1.94 joris 361: if (orig->cf_type == DT_REG) {
362: cfp->cf_etime = orig->cf_etime;
1.62 jfb 363: cfp->cf_mtime = orig->cf_mtime;
1.94 joris 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.95 joris 439: /*
440: * XXX - needed for some commands
441: */
1.73 joris 442: if (cb != NULL) {
443: if (cb(base, arg) != CVS_EX_OK) {
444: cvs_file_free(base);
1.87 joris 445: if (entfile)
446: cvs_ent_close(entfile);
1.73 joris 447: return (NULL);
448: }
449: }
450:
1.26 jfb 451: for (i = 0; i < fsn; i++) {
452: strlcpy(pcopy, fspec[i], sizeof(pcopy));
453: sp = pcopy;
1.68 joris 454: pwd = (!strcmp(pcopy, "."));
1.26 jfb 455:
1.68 joris 456: np = strchr(sp, '/');
457: if (np != NULL)
458: *np = '\0';
459:
460: if (pwd) {
461: nf = base;
462: } else {
463: nf = cvs_file_find(base, pcopy);
1.26 jfb 464: if (nf == NULL) {
1.68 joris 465: if (entfile != NULL)
466: ent = cvs_ent_get(entfile, pcopy);
467: else
468: ent = NULL;
469: nf = cvs_file_lget(pcopy, 0, base, ent);
1.26 jfb 470: if (nf == NULL) {
471: cvs_file_free(base);
1.87 joris 472: if (entfile)
473: cvs_ent_close(entfile);
1.26 jfb 474: return (NULL);
475: }
476:
1.68 joris 477: if (cvs_file_attach(base, nf) < 0) {
1.59 joris 478: cvs_file_free(base);
1.87 joris 479: if (entfile)
480: cvs_ent_close(entfile);
1.59 joris 481: return (NULL);
482: }
1.26 jfb 483: }
1.68 joris 484: }
1.26 jfb 485:
1.68 joris 486: if (nf->cf_type == DT_DIR) {
487: if (np != NULL)
488: *np++;
489:
1.73 joris 490: if (cvs_file_getdir(nf, flags, np, cb, arg) < 0) {
1.68 joris 491: cvs_file_free(base);
1.87 joris 492: if (entfile)
493: cvs_ent_close(entfile);
1.68 joris 494: return (NULL);
1.26 jfb 495: }
1.73 joris 496: } else {
497: if (cb != NULL) {
1.75 joris 498: if (cb(nf, arg) != CVS_EX_OK) {
499: cvs_file_free(base);
1.87 joris 500: if (entfile)
501: cvs_ent_close(entfile);
1.73 joris 502: return (NULL);
1.75 joris 503: }
1.73 joris 504: }
1.68 joris 505: }
1.26 jfb 506: }
1.87 joris 507:
508: if (entfile)
509: cvs_ent_close(entfile);
1.26 jfb 510:
511: return (base);
1.3 jfb 512: }
513:
514:
515: /*
1.13 jfb 516: * cvs_file_find()
517: *
518: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
519: * The file's pathname <path> must be relative to the base of <hier>.
520: * Returns the entry on success, or NULL on failure.
521: */
522: CVSFILE*
523: cvs_file_find(CVSFILE *hier, const char *path)
524: {
525: char *pp, *sp, pbuf[MAXPATHLEN];
526: CVSFILE *sf, *cf;
527:
528: strlcpy(pbuf, path, sizeof(pbuf));
529:
530: cf = hier;
531: pp = pbuf;
532: do {
533: sp = strchr(pp, '/');
534: if (sp != NULL)
1.24 jfb 535: *(sp++) = '\0';
1.13 jfb 536:
537: /* special case */
538: if (*pp == '.') {
539: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
540: /* request to go back to parent */
541: if (cf->cf_parent == NULL) {
542: cvs_log(LP_NOTICE,
543: "path %s goes back too far", path);
544: return (NULL);
545: }
546: cf = cf->cf_parent;
547: continue;
1.38 deraadt 548: } else if (*(pp + 1) == '\0')
1.13 jfb 549: continue;
550: }
551:
1.62 jfb 552: SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.34 jfb 553: if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13 jfb 554: break;
555: if (sf == NULL)
556: return (NULL);
557:
558: cf = sf;
559: pp = sp;
560: } while (sp != NULL);
561:
1.24 jfb 562: return (cf);
1.13 jfb 563: }
564:
565:
566: /*
1.34 jfb 567: * cvs_file_getpath()
568: *
569: * Get the full path of the file <file> and store it in <buf>, which is of
570: * size <len>. For portability, it is recommended that <buf> always be
571: * at least MAXPATHLEN bytes long.
572: * Returns a pointer to the start of the path on success, or NULL on failure.
573: */
574: char*
575: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
576: {
577: u_int i;
1.77 jfb 578: const char *fp, *namevec[CVS_FILE_MAXDEPTH];
1.34 jfb 579: CVSFILE *top;
580:
581: buf[0] = '\0';
582: i = CVS_FILE_MAXDEPTH;
583: memset(namevec, 0, sizeof(namevec));
584:
585: /* find the top node */
586: for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
587: fp = CVS_FILE_NAME(top);
588:
589: /* skip self-references */
590: if ((fp[0] == '.') && (fp[1] == '\0'))
591: continue;
592: namevec[--i] = fp;
593: }
594:
595: if (i == 0)
596: return (NULL);
597: else if (i == CVS_FILE_MAXDEPTH) {
598: strlcpy(buf, ".", len);
599: return (buf);
600: }
601:
602: while (i < CVS_FILE_MAXDEPTH - 1) {
603: strlcat(buf, namevec[i++], len);
604: strlcat(buf, "/", len);
605: }
606: strlcat(buf, namevec[i], len);
607:
608: return (buf);
609: }
610:
611:
612: /*
1.22 jfb 613: * cvs_file_attach()
614: *
615: * Attach the file <file> as one of the children of parent <parent>, which
616: * has to be a file of type DT_DIR.
617: * Returns 0 on success, or -1 on failure.
618: */
619: int
620: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
621: {
622: if (parent->cf_type != DT_DIR)
623: return (-1);
624:
1.62 jfb 625: SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22 jfb 626: file->cf_parent = parent;
627:
628: return (0);
629: }
630:
631:
632: /*
1.68 joris 633: * Load directory information
1.3 jfb 634: */
1.6 jfb 635: static int
1.69 joris 636: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3 jfb 637: {
1.68 joris 638: char fpath[MAXPATHLEN];
639: char pbuf[MAXPATHLEN];
1.20 jfb 640: struct stat st;
1.68 joris 641: int l;
1.7 jfb 642:
1.34 jfb 643: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.62 jfb 644: cf->cf_root = cvsroot_get(fpath);
1.91 joris 645: if (cf->cf_root == NULL) {
646: /*
647: * Do not fail here for an unknown directory.
648: */
649: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
650: return (0);
1.48 jfb 651: return (-1);
1.91 joris 652: }
1.48 jfb 653:
1.74 joris 654: if (flags & CF_MKADMIN)
1.78 joris 655: cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL);
1.69 joris 656:
1.74 joris 657: /* if the CVS administrative directory exists, load the info */
658: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
659: if (l == -1 || l >= (int)sizeof(pbuf)) {
660: errno = ENAMETOOLONG;
661: cvs_log(LP_ERRNO, "%s", pbuf);
662: return (-1);
663: }
1.61 xsa 664:
1.74 joris 665: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
666: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
667: cf->cf_repo = strdup(pbuf);
668: if (cf->cf_repo == NULL) {
669: cvs_log(LP_ERRNO,
670: "failed to dup repository string");
671: return (-1);
1.20 jfb 672: }
1.68 joris 673: }
674: }
1.26 jfb 675:
1.68 joris 676: return (0);
677: }
678:
679: /*
680: * cvs_file_getdir()
681: *
682: * Get a cvs directory structure for the directory whose path is <dir>.
683: * This function should not free the directory information on error, as this
684: * is performed by cvs_file_free().
685: */
686: static int
1.73 joris 687: cvs_file_getdir(CVSFILE *cf, int flags, char *path, int (*cb)(CVSFILE *, void *), void *arg)
1.68 joris 688: {
1.91 joris 689: int l, ret;
1.68 joris 690: int check_entry;
691: u_int ndirs, nfiles;
692: char *cur, *np;
693: char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
694: struct dirent *ent;
1.76 joris 695: CVSFILE *cfp;
1.68 joris 696: struct cvs_ent *cvsent;
697: struct cvs_flist dirs;
698: DIR *dirp;
699: CVSENTRIES *entfile;
700:
1.91 joris 701: ret = -1;
1.81 joris 702: check_entry = 1;
703: ndirs = nfiles = 0;
1.68 joris 704: SIMPLEQ_INIT(&dirs);
705:
706: cvs_file_getpath(cf, fpath, sizeof(fpath));
707:
708: cur = np = NULL;
709: if (path != NULL) {
710: cur = strchr(path, '/');
711: if (cur != NULL) {
712: *cur = '\0';
713: np = cur + 1;
714: if (np != NULL && *np == '\0')
715: np = NULL;
1.20 jfb 716: }
717: }
1.14 jfb 718:
1.56 jfb 719: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 720: return (0);
721:
1.95 joris 722: /*
723: * XXX - Do not call the callback for ".", this has
724: * already been done in cvs_file_getspec().
725: */
726: if (cb != NULL && strcmp(cf->cf_name, ".")) {
1.73 joris 727: if (cb(cf, arg) != CVS_EX_OK)
728: return (-1);
729: }
730:
1.91 joris 731: cf->cf_root = cvsroot_get(fpath);
732: if (cf->cf_root == NULL) {
733: /*
734: * Do not fail here for an unknown directory.
735: */
736: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
737: return (0);
738: return (-1);
739: }
740:
1.68 joris 741: dirp = opendir(fpath);
742: if (dirp == NULL) {
743: cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6 jfb 744: return (-1);
1.3 jfb 745: }
746:
1.91 joris 747: entfile = cvs_ent_open(fpath, O_RDONLY);
1.68 joris 748: while ((ent = readdir(dirp)) != NULL) {
749: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
750: continue;
1.11 jfb 751:
1.68 joris 752: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
753: continue;
1.24 jfb 754:
1.68 joris 755: if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
756: if (entfile != NULL)
757: (void)cvs_ent_remove(entfile,
758: ent->d_name);
759: continue;
760: }
1.70 joris 761:
762: if ((ent->d_type != DT_DIR) && (flags & CF_NOFILES))
763: continue;
1.56 jfb 764:
1.68 joris 765: if (path != NULL) {
766: if (strcmp(path, ent->d_name))
1.56 jfb 767: continue;
1.68 joris 768: }
1.11 jfb 769:
1.68 joris 770: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
771: ent->d_name);
772: if (l == -1 || l >= (int)sizeof(pbuf)) {
773: errno = ENAMETOOLONG;
774: cvs_log(LP_ERRNO, "%s", pbuf);
775: closedir(dirp);
1.91 joris 776: goto done;
1.68 joris 777: }
1.61 xsa 778:
1.68 joris 779: cfp = cvs_file_find(cf, ent->d_name);
780: if (cfp == NULL) {
1.62 jfb 781: if (entfile != NULL)
782: cvsent = cvs_ent_get(entfile, ent->d_name);
1.71 joris 783: else
784: cvsent = NULL;
785:
1.62 jfb 786: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68 joris 787:
1.55 jfb 788: if (cfp == NULL) {
1.68 joris 789: closedir(dirp);
1.91 joris 790: goto done;
1.55 jfb 791: }
1.62 jfb 792: if (entfile != NULL)
793: cvs_ent_remove(entfile, cfp->cf_name);
1.55 jfb 794:
1.68 joris 795: if (cfp->cf_type != DT_DIR) {
1.62 jfb 796: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.93 joris 797: cf_list);
1.62 jfb 798: nfiles++;
1.11 jfb 799: }
1.68 joris 800: } else {
801: cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4 jfb 802: }
1.14 jfb 803:
1.68 joris 804: if (cfp->cf_type == DT_DIR) {
805: ndirs++;
806: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.73 joris 807: } else {
808: /* callback for the file */
809: if (cb != NULL) {
810: if (cb(cfp, arg) != CVS_EX_OK) {
811: closedir(dirp);
1.91 joris 812: goto done;
1.73 joris 813: }
814: }
1.68 joris 815: }
816:
817: if (path != NULL) {
818: check_entry = 0;
819: break;
820: }
821: }
822:
823: closedir(dirp);
824:
825: if (entfile != NULL && check_entry) {
1.62 jfb 826: while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68 joris 827: if (path != NULL) {
828: if (strcmp(cvsent->ce_name, path))
829: continue;
830: }
831:
1.61 xsa 832: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44 jfb 833: cvsent->ce_name);
1.61 xsa 834: if (l == -1 || l >= (int)sizeof(pbuf)) {
835: errno = ENAMETOOLONG;
836: cvs_log(LP_ERRNO, "%s", pbuf);
1.91 joris 837: goto done;
1.61 xsa 838: }
839:
1.68 joris 840: cfp = cvs_file_find(cf, cvsent->ce_name);
841: if (cfp == NULL) {
842: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
843: if (cfp == NULL)
844: continue;
845:
846: if (cfp->cf_type != DT_DIR) {
847: SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
848: cfp, cf_list);
1.62 jfb 849: nfiles++;
1.44 jfb 850: }
1.68 joris 851: } else {
852: cfp->cf_flags |= CVS_GDIR_IGNORE;
853: }
854:
855: if (cfp->cf_type == DT_DIR) {
856: ndirs++;
1.93 joris 857: SIMPLEQ_INSERT_TAIL(&dirs, cfp,
1.68 joris 858: cf_list);
1.73 joris 859: } else {
860: /* callback for the file */
861: if (cb != NULL) {
862: if (cb(cfp, arg) != CVS_EX_OK)
1.91 joris 863: goto done;
1.73 joris 864: }
1.44 jfb 865: }
1.68 joris 866:
867: if (path != NULL)
868: break;
1.44 jfb 869: }
1.40 jfb 870: }
1.34 jfb 871:
1.10 jfb 872: if (flags & CF_SORT) {
1.68 joris 873: if (nfiles > 0)
874: cvs_file_sort(&(cf->cf_files), nfiles);
875: if (ndirs > 0)
876: cvs_file_sort(&dirs, ndirs);
1.10 jfb 877: }
1.26 jfb 878:
1.62 jfb 879: while (!SIMPLEQ_EMPTY(&dirs)) {
880: cfp = SIMPLEQ_FIRST(&dirs);
881: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 882:
883: if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
884: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
885: else
886: cfp->cf_flags &= ~CVS_GDIR_IGNORE;
887:
1.73 joris 888: if (cvs_file_getdir(cfp, flags, np, cb, arg) < 0) {
1.82 xsa 889: cvs_log(LP_ERR, "failed to get %s", CVS_FILE_NAME(cfp));
1.68 joris 890: continue;
891: }
1.26 jfb 892: }
1.3 jfb 893:
1.91 joris 894: ret = 0;
895: done:
896: if (entfile != NULL)
897: cvs_ent_close(entfile);
898:
899: return (ret);
1.3 jfb 900: }
901:
902:
903: /*
904: * cvs_file_free()
905: *
906: * Free a cvs_file structure and its contents.
907: */
908: void
1.14 jfb 909: cvs_file_free(CVSFILE *cf)
1.3 jfb 910: {
1.62 jfb 911: CVSFILE *child;
912:
1.47 jfb 913: if (cf->cf_name != NULL)
1.57 jfb 914: cvs_strfree(cf->cf_name);
1.62 jfb 915:
916: if (cf->cf_type == DT_DIR) {
917: if (cf->cf_root != NULL)
918: cvsroot_free(cf->cf_root);
919: if (cf->cf_repo != NULL)
920: free(cf->cf_repo);
921: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
922: child = SIMPLEQ_FIRST(&(cf->cf_files));
923: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
924: cvs_file_free(child);
925: }
1.64 joris 926: } else {
927: if (cf->cf_tag != NULL)
928: cvs_strfree(cf->cf_tag);
1.77 jfb 929: if (cf->cf_opts != NULL)
930: cvs_strfree(cf->cf_opts);
1.62 jfb 931: }
1.64 joris 932:
1.3 jfb 933: free(cf);
1.5 jfb 934: }
935:
936:
937: /*
938: * cvs_file_examine()
939: *
940: * Examine the contents of the CVS file structure <cf> with the function
941: * <exam>. The function is called for all subdirectories and files of the
942: * root file.
943: */
944: int
945: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
946: {
947: int ret;
1.14 jfb 948: CVSFILE *fp;
1.5 jfb 949:
950: if (cf->cf_type == DT_DIR) {
951: ret = (*exam)(cf, arg);
1.62 jfb 952: SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5 jfb 953: ret = cvs_file_examine(fp, exam, arg);
1.60 joris 954: if (ret != 0)
1.13 jfb 955: break;
1.5 jfb 956: }
1.38 deraadt 957: } else
1.13 jfb 958: ret = (*exam)(cf, arg);
959:
960: return (ret);
1.3 jfb 961: }
962:
963: /*
964: * cvs_file_sort()
965: *
1.16 jfb 966: * Sort a list of cvs file structures according to their filename. The list
967: * <flp> is modified according to the sorting algorithm. The number of files
968: * in the list must be given by <nfiles>.
969: * Returns 0 on success, or -1 on failure.
1.3 jfb 970: */
971: static int
1.16 jfb 972: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 973: {
974: int i;
975: size_t nb;
1.16 jfb 976: CVSFILE *cf, **cfvec;
977:
978: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
979: if (cfvec == NULL) {
980: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
981: return (-1);
982: }
1.3 jfb 983:
984: i = 0;
1.62 jfb 985: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 986: if (i == (int)nfiles) {
1.3 jfb 987: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 988: /* rebuild the list and abort sorting */
989: while (--i >= 0)
1.62 jfb 990: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16 jfb 991: free(cfvec);
1.3 jfb 992: return (-1);
993: }
1.16 jfb 994: cfvec[i++] = cf;
1.3 jfb 995:
996: /* now unlink it from the list,
997: * we'll put it back in order later
998: */
1.62 jfb 999: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 1000: }
1001:
1002: /* clear the list just in case */
1.62 jfb 1003: SIMPLEQ_INIT(flp);
1.3 jfb 1004: nb = (size_t)i;
1005:
1006: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
1007:
1008: /* rebuild the list from the bottom up */
1009: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 1010: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 1011:
1.16 jfb 1012: free(cfvec);
1.3 jfb 1013: return (0);
1014: }
1015:
1016:
1017: static int
1018: cvs_file_cmp(const void *f1, const void *f2)
1019: {
1.41 jfb 1020: const CVSFILE *cf1, *cf2;
1.62 jfb 1021: cf1 = *(CVSFILE * const *)f1;
1022: cf2 = *(CVSFILE * const *)f2;
1.34 jfb 1023: return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6 jfb 1024: }
1025:
1026:
1.34 jfb 1027: /*
1028: * cvs_file_alloc()
1029: *
1030: * Allocate a CVSFILE structure and initialize its internals.
1031: */
1.6 jfb 1032: CVSFILE*
1033: cvs_file_alloc(const char *path, u_int type)
1034: {
1035: CVSFILE *cfp;
1036:
1.14 jfb 1037: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 1038: if (cfp == NULL) {
1039: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
1040: return (NULL);
1041: }
1042: memset(cfp, 0, sizeof(*cfp));
1043:
1.62 jfb 1044: cfp->cf_type = type;
1045: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1046:
1047: if (type == DT_DIR) {
1048: SIMPLEQ_INIT(&(cfp->cf_files));
1049: }
1050:
1.57 jfb 1051: cfp->cf_name = cvs_strdup(basename(path));
1.34 jfb 1052: if (cfp->cf_name == NULL) {
1.57 jfb 1053: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 1054: cvs_file_free(cfp);
1.6 jfb 1055: return (NULL);
1056: }
1057:
1058: return (cfp);
1.1 jfb 1059: }
1.14 jfb 1060:
1061:
1062: /*
1063: * cvs_file_lget()
1064: *
1065: * Get the file and link it with the parent right away.
1.22 jfb 1066: * Returns a pointer to the created file structure on success, or NULL on
1067: * failure.
1.14 jfb 1068: */
1069: static CVSFILE*
1.62 jfb 1070: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14 jfb 1071: {
1.44 jfb 1072: int ret, cwd;
1073: u_int type;
1.14 jfb 1074: struct stat st;
1075: CVSFILE *cfp;
1076:
1.44 jfb 1077: type = DT_UNKNOWN;
1078: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62 jfb 1079:
1.44 jfb 1080: ret = stat(path, &st);
1081: if (ret == 0)
1082: type = IFTODT(st.st_mode);
1.14 jfb 1083:
1.44 jfb 1084: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 1085: return (NULL);
1086: cfp->cf_parent = parent;
1.54 joris 1087:
1088: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 1089: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 1090:
1.44 jfb 1091: if (ret == 0) {
1092: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 1093: if (cfp->cf_type == DT_REG)
1094: cfp->cf_mtime = st.st_mtime;
1.44 jfb 1095:
1096: if (ent == NULL)
1097: cfp->cf_cvstat = (cwd == 1) ?
1098: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14 jfb 1099: else {
1.44 jfb 1100: /* always show directories as up-to-date */
1101: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 1102: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 1103: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1104: cfp->cf_cvstat = CVS_FST_ADDED;
1105: else {
1106: /* check last modified time */
1.90 joris 1107: if (ent->ce_mtime == (time_t)st.st_mtime) {
1.44 jfb 1108: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.90 joris 1109: } else {
1.44 jfb 1110: cfp->cf_cvstat = CVS_FST_MODIFIED;
1.90 joris 1111: }
1.44 jfb 1112: }
1.94 joris 1113:
1114: cfp->cf_etime = ent->ce_mtime;
1.14 jfb 1115: }
1.44 jfb 1116: } else {
1117: if (ent == NULL) {
1.92 joris 1118: /* assume it is a file and unknown */
1119: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1120: cfp->cf_type = DT_REG;
1.66 joris 1121: } else {
1122: if (ent->ce_type == CVS_ENT_FILE)
1123: cfp->cf_type = DT_REG;
1124: else if (ent->ce_type == CVS_ENT_DIR)
1125: cfp->cf_type = DT_DIR;
1126: else
1.93 joris 1127: cvs_log(LP_WARN, "unknown ce_type %d",
1.66 joris 1128: ent->ce_type);
1.67 joris 1129:
1130: if (ent->ce_status == CVS_ENT_REMOVED)
1131: cfp->cf_cvstat = CVS_FST_REMOVED;
1.90 joris 1132: else if (ent->ce_status == CVS_ENT_UPTODATE)
1133: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.96 ! joris 1134: else if (ent->ce_status == CVS_ENT_ADDED)
! 1135: cfp->cf_cvstat = CVS_FST_ADDED;
1.67 joris 1136: else
1137: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1138: }
1.14 jfb 1139: }
1.52 jfb 1140:
1.62 jfb 1141: if (ent != NULL) {
1142: /* steal the RCSNUM */
1143: cfp->cf_lrev = ent->ce_rev;
1.72 jfb 1144: ent->ce_rev = NULL;
1145:
1.77 jfb 1146: if (ent->ce_type == CVS_ENT_FILE) {
1147: if (ent->ce_tag[0] != '\0') {
1148: cfp->cf_tag = cvs_strdup(ent->ce_tag);
1149: if (cfp->cf_tag == NULL) {
1150: cvs_file_free(cfp);
1151: return (NULL);
1152: }
1153: }
1154:
1155: if (ent->ce_opts[0] != '\0') {
1156: cfp->cf_opts = cvs_strdup(ent->ce_opts);
1157: if (cfp->cf_opts == NULL) {
1158: cvs_file_free(cfp);
1159: return (NULL);
1160: }
1.65 joris 1161: }
1.62 jfb 1162: }
1163: }
1.14 jfb 1164:
1.69 joris 1165: if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26 jfb 1166: cvs_file_free(cfp);
1167: return (NULL);
1.14 jfb 1168: }
1.74 joris 1169:
1170: if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
1171: !strcmp(cfp->cf_repo, path))
1172: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14 jfb 1173:
1174: return (cfp);
1.23 jfb 1175: }
1176:
1177:
1178: static int
1179: cvs_file_cmpname(const char *name1, const char *name2)
1180: {
1181: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1182: (strcasecmp(name1, name2));
1.88 joris 1183: }
1184:
1185: /*
1186: * remove a directory if it does not contain
1187: * any files other than the CVS/ administrative files.
1188: */
1189: int
1190: cvs_file_prune(char *path)
1191: {
1192: DIR *dirp;
1193: int l, pwd, empty;
1194: struct dirent *dp;
1195: char fpath[MAXPATHLEN];
1196: CVSENTRIES *entf;
1.89 joris 1197: CVSFILE *cfp;
1.88 joris 1198:
1199: pwd = (!strcmp(path, "."));
1200:
1201: if ((dirp = opendir(path)) == NULL) {
1202: cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1203: return (-1);
1204: }
1205:
1206: empty = 0;
1207: entf = cvs_ent_open(path, O_RDWR);
1208:
1209: while ((dp = readdir(dirp)) != NULL) {
1210: if (!strcmp(dp->d_name, ".") ||
1211: !strcmp(dp->d_name, "..") ||
1212: !strcmp(dp->d_name, CVS_PATH_CVSDIR))
1213: continue;
1214:
1215: empty++;
1216: if (dp->d_type == DT_DIR) {
1217: l = snprintf(fpath, sizeof(fpath), "%s%s%s",
1218: (pwd) ? "" : path, (pwd) ? "" : "/", dp->d_name);
1219: if (l == -1 || l >= (int)sizeof(fpath)) {
1220: errno = ENAMETOOLONG;
1221: cvs_log(LP_ERRNO, "%s", fpath);
1222: continue;
1223: }
1.89 joris 1224:
1225: cfp = cvs_file_find(cvs_files, fpath);
1226: if (cfp == NULL)
1.91 joris 1227: continue;
1228:
1229: /* ignore unknown directories */
1230: if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
1.89 joris 1231: continue;
1.88 joris 1232:
1233: if (cvs_file_prune(fpath)) {
1234: empty--;
1235: if (entf)
1236: cvs_ent_remove(entf, fpath);
1237: } else {
1238: empty++;
1239: }
1240: }
1241: }
1242:
1243: closedir(dirp);
1244: if (entf)
1245: cvs_ent_close(entf);
1246:
1247: empty = (empty == 0);
1248: if (empty) {
1249: if (cvs_remove_dir(path) < 0) {
1250: cvs_log(LP_ERR, "failed to prune `%s'", path);
1251: empty = 0;
1252: }
1.93 joris 1253: }
1.88 joris 1254:
1255: return (empty);
1.14 jfb 1256: }