Annotation of src/usr.bin/cvs/file.c, Revision 1.132
1.132 ! joris 1: /* $OpenBSD: file.c,v 1.131 2005/12/04 17:39:02 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"
45:
1.104 xsa 46: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
1.1 jfb 47:
1.104 xsa 48: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
1.1 jfb 49:
50:
51: /* ignore pattern */
52: struct cvs_ignpat {
1.104 xsa 53: char ip_pat[MAXNAMLEN];
54: int ip_flags;
1.124 reyk 55: TAILQ_ENTRY(cvs_ignpat) ip_list;
1.1 jfb 56: };
57:
58:
59: /*
60: * Standard patterns to ignore.
61: */
62: static const char *cvs_ign_std[] = {
63: ".",
64: "..",
65: "*.o",
66: "*.so",
1.45 xsa 67: "*.a",
1.1 jfb 68: "*.bak",
69: "*.orig",
70: "*.rej",
1.45 xsa 71: "*.old",
1.1 jfb 72: "*.exe",
73: "*.depend",
1.45 xsa 74: "*.obj",
75: "*.elc",
76: "*.ln",
77: "*.olb",
1.1 jfb 78: "CVS",
79: "core",
1.102 joris 80: "cvslog*",
1.49 jfb 81: "*.core",
1.8 jfb 82: ".#*",
1.45 xsa 83: "*~",
84: "_$*",
85: "*$",
1.1 jfb 86: #ifdef OLD_SMELLY_CRUFT
87: "RCSLOG",
88: "tags",
89: "TAGS",
90: "RCS",
91: "SCCS",
1.45 xsa 92: "cvslog.*", /* to ignore CVS_CLIENT_LOG output */
1.1 jfb 93: "#*",
94: ",*",
95: #endif
96: };
97:
98:
1.11 jfb 99: /*
100: * Entries in the CVS/Entries file with a revision of '0' have only been
101: * added. Compare against this revision to see if this is the case
102: */
1.4 jfb 103: static RCSNUM *cvs_addedrev;
104:
105:
1.104 xsa 106: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
1.1 jfb 107:
1.100 joris 108: static int cvs_file_getdir(CVSFILE *, int, int (*)(CVSFILE *, void *),
109: void *, int);
1.1 jfb 110:
1.104 xsa 111: static int cvs_load_dirinfo(CVSFILE *, int);
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);
1.115 joris 116: static CVSFILE *cvs_file_lget(const char *, int, CVSFILE *, CVSENTRIES *,
117: struct cvs_ent *);
1.3 jfb 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:
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 */
1.111 xsa 142: l = snprintf(path, sizeof(path), "%s/.cvsignore", cvs_homedir);
143: if (l == -1 || l >= (int)sizeof(path)) {
144: errno = ENAMETOOLONG;
145: cvs_log(LP_ERRNO, "%s", path);
146: return (-1);
147: }
1.61 xsa 148:
1.111 xsa 149: ifp = fopen(path, "r");
150: if (ifp == NULL) {
151: if (errno != ENOENT)
152: cvs_log(LP_ERRNO,
153: "failed to open user's cvsignore file `%s'", path);
154: } else {
1.112 xsa 155: while (fgets(buf, (int)sizeof(buf), ifp) != NULL) {
1.111 xsa 156: len = strlen(buf);
157: if (len == 0)
158: continue;
159: if (buf[len - 1] != '\n') {
160: cvs_log(LP_ERR, "line too long in `%s'", path);
1.1 jfb 161: }
1.111 xsa 162: buf[--len] = '\0';
163: cvs_file_ignore(buf);
1.1 jfb 164: }
1.111 xsa 165: (void)fclose(ifp);
1.1 jfb 166: }
167:
168: return (0);
169: }
170:
171:
172: /*
173: * cvs_file_ignore()
174: *
175: * Add the pattern <pat> to the list of patterns for files to ignore.
176: * Returns 0 on success, or -1 on failure.
177: */
178: int
179: cvs_file_ignore(const char *pat)
180: {
181: char *cp;
182: struct cvs_ignpat *ip;
183:
1.132 ! joris 184: ip = (struct cvs_ignpat *)xmalloc(sizeof(*ip));
1.1 jfb 185: strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
186:
187: /* check if we will need globbing for that pattern */
188: ip->ip_flags = CVS_IGN_STATIC;
189: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
190: if (CVS_CHAR_ISMETA(*cp)) {
191: ip->ip_flags &= ~CVS_IGN_STATIC;
192: break;
193: }
194: }
195:
196: TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
197:
198: return (0);
199: }
200:
201:
202: /*
1.5 jfb 203: * cvs_file_chkign()
1.1 jfb 204: *
205: * Returns 1 if the filename <file> is matched by one of the ignore
206: * patterns, or 0 otherwise.
207: */
208: int
1.5 jfb 209: cvs_file_chkign(const char *file)
1.1 jfb 210: {
1.23 jfb 211: int flags;
1.1 jfb 212: struct cvs_ignpat *ip;
213:
1.23 jfb 214: flags = FNM_PERIOD;
215: if (cvs_nocase)
216: flags |= FNM_CASEFOLD;
217:
1.1 jfb 218: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
219: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 220: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 221: return (1);
1.38 deraadt 222: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 223: return (1);
224: }
225:
226: return (0);
227: }
228:
229:
230: /*
1.6 jfb 231: * cvs_file_create()
1.1 jfb 232: *
1.6 jfb 233: * Create a new file whose path is specified in <path> and of type <type>.
1.26 jfb 234: * If the type is DT_DIR, the CVS administrative repository and files will be
235: * created.
1.25 jfb 236: * Returns the created file on success, or NULL on failure.
1.1 jfb 237: */
1.109 xsa 238: CVSFILE *
1.34 jfb 239: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1 jfb 240: {
1.85 joris 241: int fd, l;
242: char fp[MAXPATHLEN], repo[MAXPATHLEN];
1.6 jfb 243: CVSFILE *cfp;
1.1 jfb 244:
1.6 jfb 245: cfp = cvs_file_alloc(path, type);
246: if (cfp == NULL)
247: return (NULL);
1.26 jfb 248:
1.100 joris 249: l = 0;
1.22 jfb 250: cfp->cf_mode = mode;
1.34 jfb 251: cfp->cf_parent = parent;
1.1 jfb 252:
1.34 jfb 253: if (type == DT_DIR) {
1.62 jfb 254: cfp->cf_root = cvsroot_get(path);
1.84 joris 255: if (cfp->cf_root == NULL) {
1.100 joris 256: cvs_file_free(cfp);
1.84 joris 257: return (NULL);
258: }
259:
1.85 joris 260: if (cvs_repo_base != NULL) {
261: cvs_file_getpath(cfp, fp, sizeof(fp));
262: l = snprintf(repo, sizeof(repo), "%s/%s", cvs_repo_base,
263: fp);
264: } else {
265: cvs_file_getpath(cfp, repo, sizeof(repo));
266: l = 0;
267: }
268:
269: if (l == -1 || l >= (int)sizeof(repo)) {
270: errno = ENAMETOOLONG;
271: cvs_log(LP_ERRNO, "%s", repo);
272: cvs_file_free(cfp);
273: return (NULL);
274: }
275:
1.132 ! joris 276: cfp->cf_repo = xstrdup(repo);
1.79 joris 277: if (((mkdir(path, mode) == -1) && (errno != EEXIST)) ||
1.122 xsa 278: (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo,
1.124 reyk 279: NULL, NULL, 0) < 0)) {
1.6 jfb 280: cvs_file_free(cfp);
281: return (NULL);
1.50 jfb 282: }
1.38 deraadt 283: } else {
1.6 jfb 284: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
285: if (fd == -1) {
286: cvs_file_free(cfp);
1.1 jfb 287: return (NULL);
288: }
1.6 jfb 289: (void)close(fd);
1.1 jfb 290: }
291:
1.6 jfb 292: return (cfp);
1.3 jfb 293: }
294:
295:
296: /*
1.35 jfb 297: * cvs_file_copy()
298: *
299: * Allocate space to create a copy of the file <orig>. The copy inherits all
300: * of the original's attributes, but does not inherit its children if the
301: * original file is a directory. Note that files copied using this mechanism
302: * are linked to their parent, but the parent has no link to the file. This
303: * is so cvs_file_getpath() works.
304: * Returns the copied file on success, or NULL on failure. The returned
305: * structure should be freed using cvs_file_free().
306: */
1.109 xsa 307: CVSFILE *
1.35 jfb 308: cvs_file_copy(CVSFILE *orig)
309: {
310: char path[MAXPATHLEN];
311: CVSFILE *cfp;
312:
313: cvs_file_getpath(orig, path, sizeof(path));
314:
315: cfp = cvs_file_alloc(path, orig->cf_type);
316: if (cfp == NULL)
317: return (NULL);
318:
319: cfp->cf_parent = orig->cf_parent;
320: cfp->cf_mode = orig->cf_mode;
321: cfp->cf_cvstat = orig->cf_cvstat;
322:
1.94 joris 323: if (orig->cf_type == DT_REG) {
324: cfp->cf_etime = orig->cf_etime;
1.62 jfb 325: cfp->cf_mtime = orig->cf_mtime;
1.94 joris 326: } else if (orig->cf_type == DT_DIR) {
1.35 jfb 327: /* XXX copy CVS directory attributes */
328: }
329:
330: return (cfp);
331: }
332:
333:
334: /*
1.3 jfb 335: * cvs_file_get()
336: *
337: * Load a cvs_file structure with all the information pertaining to the file
338: * <path>.
1.4 jfb 339: * The <flags> parameter specifies various flags that alter the behaviour of
1.21 jfb 340: * the function. The CF_RECURSE flag causes the function to recursively load
341: * subdirectories when <path> is a directory.
342: * The CF_SORT flag causes the files to be sorted in alphabetical order upon
343: * loading. The special case of "." as a path specification generates
344: * recursion for a single level and is equivalent to calling cvs_file_get() on
345: * all files of that directory.
1.3 jfb 346: * Returns a pointer to the cvs file structure, which must later be freed
347: * with cvs_file_free().
348: */
349:
1.100 joris 350: int
1.73 joris 351: cvs_file_get(const char *path, int flags, int (*cb)(CVSFILE *, void *),
1.100 joris 352: void *arg, struct cvs_flist *list)
1.3 jfb 353: {
1.68 joris 354: char *files[1];
355:
1.105 joris 356: *(const char **)files = path;
1.100 joris 357: return cvs_file_getspec(files, 1, flags, cb, arg, list);
1.9 jfb 358: }
359:
360:
361: /*
362: * cvs_file_getspec()
363: *
1.100 joris 364: * Obtain the info about the supplied files or directories.
1.9 jfb 365: */
1.100 joris 366: int
1.73 joris 367: cvs_file_getspec(char **fspec, int fsn, int flags, int (*cb)(CVSFILE *, void *),
1.100 joris 368: void *arg, struct cvs_flist *list)
1.9 jfb 369: {
1.100 joris 370: int i, freecf;
371: char pcopy[MAXPATHLEN];
372: CVSFILE *cf;
373: extern char *cvs_rootstr;
374:
375: freecf = (list == NULL);
376: cvs_error = CVS_EX_DATA;
377:
378: /* init the list */
379: if (list != NULL)
380: SIMPLEQ_INIT(list);
1.26 jfb 381:
1.100 joris 382: /*
383: * Fetch the needed information about ".", so we can setup a few
384: * things to get ourselfs going.
385: */
1.115 joris 386: cf = cvs_file_lget(".", 0, NULL, NULL, NULL);
1.100 joris 387: if (cf == NULL) {
1.125 joris 388: cvs_log(LP_ERR, "failed to obtain '.' information");
1.100 joris 389: return (-1);
390: }
1.87 joris 391:
1.85 joris 392: /*
1.100 joris 393: * save the base repository path so we can use it to create
394: * the correct full repopath later on.
1.85 joris 395: */
1.100 joris 396: if (cf->cf_repo != NULL) {
397: if (cvs_repo_base != NULL)
1.132 ! joris 398: xfree(cvs_repo_base);
! 399: cvs_repo_base = xstrdup(cf->cf_repo);
1.85 joris 400: }
1.26 jfb 401:
1.95 joris 402: /*
1.100 joris 403: * This will go away when we have support for multiple Roots.
1.95 joris 404: */
1.100 joris 405: if (cvs_rootstr == NULL && cf->cf_root != NULL) {
1.132 ! joris 406: cvs_rootstr = xstrdup(cf->cf_root->cr_str);
1.73 joris 407: }
408:
1.100 joris 409: cvs_error = CVS_EX_OK;
410:
411: /*
412: * Since some commands don't require any files to operate
413: * we can stop right here for those.
414: */
1.128 joris 415: if (cf->cf_root != NULL) {
416: if (cf->cf_root->cr_method != CVS_METHOD_LOCAL &&
417: cvs_cmdop == CVS_OP_CHECKOUT) {
418: cvs_file_free(cf);
419: return (0);
420: }
421: }
422:
423: cvs_file_free(cf);
424:
425: if (cvs_cmdop == CVS_OP_VERSION)
1.100 joris 426: return (0);
427:
1.26 jfb 428: for (i = 0; i < fsn; i++) {
429: strlcpy(pcopy, fspec[i], sizeof(pcopy));
1.123 joris 430:
431: /*
432: * get rid of any trailing slashes.
433: */
434: STRIP_SLASH(pcopy);
1.26 jfb 435:
1.100 joris 436: /*
437: * Load the information.
438: */
439: cf = cvs_file_loadinfo(pcopy, flags, cb, arg, freecf);
1.108 joris 440: if (cf == NULL) {
441: if (cvs_error != CVS_EX_OK)
1.126 joris 442: return (-1);
1.100 joris 443: continue;
1.108 joris 444: }
1.68 joris 445:
1.100 joris 446: /*
447: * If extra actions are needed, do them now.
448: */
449: if (cf->cf_type == DT_DIR) {
450: /* do possible extra actions .. */
1.68 joris 451: } else {
1.100 joris 452: /* do possible extra actions .. */
453: }
454:
455: /*
456: * Attach it to a list if requested, otherwise
457: * just free it again.
458: */
459: if (list != NULL)
460: SIMPLEQ_INSERT_TAIL(list, cf, cf_list);
461: else
462: cvs_file_free(cf);
463: }
464:
465: return (0);
466: }
467:
468: /*
469: * Load the neccesary information about file or directory <path>.
470: * Returns a pointer to the loaded information on success, or NULL
471: * on failure.
472: *
473: * If cb is not NULL, the requested path will be passed to that callback
474: * with <arg> as an argument.
475: *
476: * the <freecf> argument is passed to cvs_file_getdir, if this is 1
477: * CVSFILE * structs will be free'd once we are done with them.
478: */
1.114 xsa 479: CVSFILE *
1.100 joris 480: cvs_file_loadinfo(char *path, int flags, int (*cb)(CVSFILE *, void *),
481: void *arg, int freecf)
482: {
483: CVSFILE *cf, *base;
484: CVSENTRIES *entf;
485: struct cvs_ent *ent;
486: char *p;
487: char parent[MAXPATHLEN], item[MAXPATHLEN];
1.120 joris 488: int type, callit;
1.100 joris 489: struct stat st;
490: struct cvsroot *root;
491:
492: type = 0;
493: base = cf = NULL;
494: entf = NULL;
495: ent = NULL;
496:
497: /*
498: * We first have to find out what type of item we are
499: * dealing with. A file or a directory.
500: *
501: * We can do this by stat(2)'ing the item, but since it
502: * might be gone we also check the Entries file in the
503: * parent directory.
504: */
505:
506: /* get parent directory */
507: if ((p = strrchr(path, '/')) != NULL) {
508: *p++ = '\0';
509: strlcpy(parent, path, sizeof(parent));
510: strlcpy(item, p, sizeof(item));
511: *--p = '/';
512: } else {
513: strlcpy(parent, ".", sizeof(parent));
514: strlcpy(item, path, sizeof(item));
515: }
516:
517: /*
518: * There might not be an Entries file, so do not fail if there
519: * is none available to get the info from.
520: */
1.115 joris 521: entf = cvs_ent_open(parent, O_RDWR);
1.100 joris 522:
523: /*
524: * Load the Entry if we successfully opened the Entries file.
525: */
526: if (entf != NULL)
527: ent = cvs_ent_get(entf, item);
528:
529: /*
530: * No Entry available? fall back to stat(2)'ing the item, if
1.114 xsa 531: * that fails, assume a normal file.
1.100 joris 532: */
533: if (ent == NULL) {
1.103 joris 534: if (stat(path, &st) == -1)
1.100 joris 535: type = DT_REG;
1.103 joris 536: else
1.100 joris 537: type = IFTODT(st.st_mode);
538: } else {
539: if (ent->ce_type == CVS_ENT_DIR)
540: type = DT_DIR;
541: else
542: type = DT_REG;
543: }
544:
545: /*
546: * Get the base, which is <parent> for a normal file or
547: * <path> for a directory.
548: */
549: if (type == DT_DIR)
1.115 joris 550: base = cvs_file_lget(path, flags, NULL, entf, ent);
1.100 joris 551: else
1.115 joris 552: base = cvs_file_lget(parent, flags, NULL, entf, NULL);
1.100 joris 553:
554: if (base == NULL) {
555: cvs_log(LP_ERR, "failed to obtain directory info for '%s'",
556: parent);
1.108 joris 557: cvs_error = CVS_EX_FILE;
1.100 joris 558: goto fail;
559: }
560:
561: /*
562: * Sanity.
563: */
564: if (base->cf_type != DT_DIR) {
565: cvs_log(LP_ERR, "base directory isn't a directory at all");
566: goto fail;
567: }
568:
569: root = CVS_DIR_ROOT(base);
570: if (root == NULL) {
1.108 joris 571: cvs_error = CVS_EX_BADROOT;
1.100 joris 572: goto fail;
573: }
1.26 jfb 574:
1.100 joris 575: /*
576: * If we have a normal file, get the info and link it
577: * to the base.
578: */
579: if (type != DT_DIR) {
1.115 joris 580: cf = cvs_file_lget(path, flags, base, entf, ent);
1.100 joris 581: if (cf == NULL) {
1.108 joris 582: cvs_error = CVS_EX_DATA;
1.100 joris 583: goto fail;
1.68 joris 584: }
1.26 jfb 585:
1.100 joris 586: cvs_file_attach(base, cf);
587: }
588:
589: /*
590: * Always pass the base directory, unless:
591: * - we are running in server or local mode and the path is not "."
592: * - the directory does not exist on disk.
593: * - the callback is NULL.
1.122 xsa 594: */
1.120 joris 595: callit = 1;
596: if (cb == NULL)
597: callit = 0;
598:
1.121 joris 599: if ((cvs_cmdop == CVS_OP_SERVER) && (type != DT_DIR))
1.120 joris 600: callit = 0;
601:
1.121 joris 602: if ((root->cr_method == CVS_METHOD_LOCAL) && (type != DT_DIR))
1.120 joris 603: callit = 0;
604:
605: if (!(base->cf_flags & CVS_FILE_ONDISK))
606: callit = 0;
607:
608: if (callit != 0) {
609: if ((cvs_error = cb(base,arg)) != CVS_EX_OK)
610: goto fail;
611: }
1.100 joris 612:
613: /*
614: * If we have a normal file, pass it as well.
615: */
616: if (type != DT_DIR) {
617: if ((cb != NULL) && ((cvs_error = cb(cf, arg)) != CVS_EX_OK))
618: goto fail;
619: } else {
620: /*
621: * If the directory exists, recurse through it.
622: */
623: if ((base->cf_flags & CVS_FILE_ONDISK) &&
624: cvs_file_getdir(base, flags, cb, arg, freecf) < 0) {
1.108 joris 625: cvs_error = CVS_EX_FILE;
1.100 joris 626: goto fail;
1.68 joris 627: }
1.116 joris 628: }
629:
630: if (entf != NULL) {
631: cvs_ent_close(entf);
632: entf = NULL;
1.26 jfb 633: }
1.87 joris 634:
1.100 joris 635: return (base);
1.26 jfb 636:
1.100 joris 637: fail:
638: if (entf != NULL)
639: cvs_ent_close(entf);
640: if (base != NULL)
641: cvs_file_free(base);
642: return (NULL);
1.3 jfb 643: }
644:
645: /*
1.13 jfb 646: * cvs_file_find()
647: *
648: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
649: * The file's pathname <path> must be relative to the base of <hier>.
650: * Returns the entry on success, or NULL on failure.
651: */
1.109 xsa 652: CVSFILE *
1.13 jfb 653: cvs_file_find(CVSFILE *hier, const char *path)
654: {
655: char *pp, *sp, pbuf[MAXPATHLEN];
656: CVSFILE *sf, *cf;
657:
658: strlcpy(pbuf, path, sizeof(pbuf));
659:
660: cf = hier;
661: pp = pbuf;
662: do {
663: sp = strchr(pp, '/');
664: if (sp != NULL)
1.24 jfb 665: *(sp++) = '\0';
1.13 jfb 666:
667: /* special case */
668: if (*pp == '.') {
669: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
670: /* request to go back to parent */
671: if (cf->cf_parent == NULL) {
672: cvs_log(LP_NOTICE,
673: "path %s goes back too far", path);
674: return (NULL);
675: }
676: cf = cf->cf_parent;
677: continue;
1.38 deraadt 678: } else if (*(pp + 1) == '\0')
1.13 jfb 679: continue;
680: }
681:
1.62 jfb 682: SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.99 joris 683: if (cvs_file_cmpname(pp, sf->cf_name) == 0)
1.13 jfb 684: break;
685: if (sf == NULL)
686: return (NULL);
687:
688: cf = sf;
689: pp = sp;
690: } while (sp != NULL);
691:
1.24 jfb 692: return (cf);
1.13 jfb 693: }
694:
695:
696: /*
1.34 jfb 697: * cvs_file_getpath()
698: *
699: * Get the full path of the file <file> and store it in <buf>, which is of
700: * size <len>. For portability, it is recommended that <buf> always be
701: * at least MAXPATHLEN bytes long.
1.100 joris 702: * Returns a pointer to the start of the path.
1.34 jfb 703: */
1.104 xsa 704: char *
1.34 jfb 705: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
706: {
1.100 joris 707: memset(buf, '\0', len);
708: if (file->cf_dir != NULL) {
709: strlcat(buf, file->cf_dir, len);
1.34 jfb 710: strlcat(buf, "/", len);
711: }
712:
1.100 joris 713: strlcat(buf, file->cf_name, len);
1.34 jfb 714: return (buf);
715: }
716:
717: /*
1.22 jfb 718: * cvs_file_attach()
719: *
720: * Attach the file <file> as one of the children of parent <parent>, which
721: * has to be a file of type DT_DIR.
722: * Returns 0 on success, or -1 on failure.
723: */
724: int
725: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
726: {
727: if (parent->cf_type != DT_DIR)
728: return (-1);
729:
1.62 jfb 730: SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22 jfb 731: file->cf_parent = parent;
732:
733: return (0);
734: }
735:
736:
737: /*
1.68 joris 738: * Load directory information
1.3 jfb 739: */
1.6 jfb 740: static int
1.69 joris 741: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3 jfb 742: {
1.68 joris 743: char fpath[MAXPATHLEN];
744: char pbuf[MAXPATHLEN];
1.20 jfb 745: struct stat st;
1.68 joris 746: int l;
1.7 jfb 747:
1.34 jfb 748: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.100 joris 749:
750: /*
751: * Try to obtain the Root for this given directory, if we cannot
752: * get it, fail, unless we are dealing with a directory that is
753: * unknown or not on disk.
754: */
1.62 jfb 755: cf->cf_root = cvsroot_get(fpath);
1.91 joris 756: if (cf->cf_root == NULL) {
1.100 joris 757: if (cf->cf_cvstat == CVS_FST_UNKNOWN ||
758: !(cf->cf_flags & CVS_FILE_ONDISK))
1.91 joris 759: return (0);
1.48 jfb 760: return (-1);
1.91 joris 761: }
1.114 xsa 762:
1.74 joris 763: /* if the CVS administrative directory exists, load the info */
764: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
765: if (l == -1 || l >= (int)sizeof(pbuf)) {
766: errno = ENAMETOOLONG;
767: cvs_log(LP_ERRNO, "%s", pbuf);
768: return (-1);
769: }
1.61 xsa 770:
1.74 joris 771: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
1.132 ! joris 772: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0)
! 773: cf->cf_repo = xstrdup(pbuf);
1.100 joris 774: } else {
775: /*
776: * Fill in the repo path ourselfs.
777: */
1.107 joris 778: if (cvs_repo_base != NULL) {
779: l = snprintf(pbuf, sizeof(pbuf), "%s/%s",
780: cvs_repo_base, fpath);
781: if (l == -1 || l >= (int)sizeof(pbuf))
782: return (-1);
1.100 joris 783:
1.132 ! joris 784: cf->cf_repo = xstrdup(pbuf);
1.107 joris 785: } else
786: cf->cf_repo = NULL;
1.68 joris 787: }
1.100 joris 788:
1.68 joris 789: return (0);
790: }
791:
792: /*
793: * cvs_file_getdir()
794: *
795: * Get a cvs directory structure for the directory whose path is <dir>.
796: * This function should not free the directory information on error, as this
797: * is performed by cvs_file_free().
798: */
799: static int
1.100 joris 800: cvs_file_getdir(CVSFILE *cf, int flags, int (*cb)(CVSFILE *, void *),
801: void *arg, int freecf)
1.68 joris 802: {
1.100 joris 803: int ret;
804: size_t len;
805: DIR *dp;
806: struct dirent *de;
807: char fpath[MAXPATHLEN], pbuf[MAXPATHLEN];
808: CVSENTRIES *entf;
1.76 joris 809: CVSFILE *cfp;
1.100 joris 810: struct cvs_ent *ent;
1.68 joris 811: struct cvs_flist dirs;
1.100 joris 812: int nfiles, ndirs;
1.14 jfb 813:
1.56 jfb 814: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 815: return (0);
816:
1.128 joris 817: /*
818: * if we are working with a repository, fiddle with
819: * the pathname again.
820: */
821: if (flags & CF_REPO) {
822: ret = snprintf(fpath, sizeof(fpath), "%s%s%s",
823: cf->cf_root->cr_dir,
824: (cf->cf_dir != NULL) ? "/" : "",
825: (cf->cf_dir != NULL) ? cf->cf_dir : "");
826: if (ret == -1 || ret >= (int)sizeof(fpath))
827: return (-1);
1.132 ! joris 828: xfree(cf->cf_dir);
! 829: cf->cf_dir = xstrdup(fpath);
1.128 joris 830: }
831:
1.100 joris 832: nfiles = ndirs = 0;
833: SIMPLEQ_INIT(&dirs);
834: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.73 joris 835:
1.100 joris 836: if ((dp = opendir(fpath)) == NULL) {
837: cvs_log(LP_ERRNO, "failed to open directory '%s'", fpath);
1.91 joris 838: return (-1);
839: }
840:
1.100 joris 841: ret = -1;
1.115 joris 842: entf = cvs_ent_open(fpath, O_RDWR);
1.100 joris 843: while ((de = readdir(dp)) != NULL) {
844: if (!strcmp(de->d_name, ".") ||
845: !strcmp(de->d_name, ".."))
1.68 joris 846: continue;
1.11 jfb 847:
1.117 joris 848: len = cvs_path_cat(fpath, de->d_name, pbuf, sizeof(pbuf));
849: if (len >= sizeof(pbuf))
850: goto done;
851:
852: if (entf != NULL)
853: ent = cvs_ent_get(entf, de->d_name);
854: else
855: ent = NULL;
856:
1.100 joris 857: /*
858: * Do some filtering on the current directory item.
859: */
860: if ((flags & CF_IGNORE) && cvs_file_chkign(de->d_name))
1.68 joris 861: continue;
1.24 jfb 862:
1.100 joris 863: if (!(flags & CF_RECURSE) && (de->d_type == DT_DIR)) {
1.117 joris 864: if (ent != NULL)
865: ent->processed = 1;
1.68 joris 866: continue;
867: }
1.70 joris 868:
1.100 joris 869: if ((de->d_type != DT_DIR) && (flags & CF_NOFILES))
1.70 joris 870: continue;
1.56 jfb 871:
1.115 joris 872: cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100 joris 873: if (cfp == NULL) {
874: cvs_log(LP_ERR, "failed to get '%s'", pbuf);
1.91 joris 875: goto done;
1.68 joris 876: }
1.61 xsa 877:
1.100 joris 878: /*
879: * A file is linked to the parent <cf>, a directory
880: * is added to the dirs SIMPLEQ list for later use.
881: */
882: if ((cfp->cf_type != DT_DIR) && !freecf) {
883: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
884: nfiles++;
885: } else if (cfp->cf_type == DT_DIR) {
886: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
887: ndirs++;
888: }
1.71 joris 889:
1.100 joris 890: /*
891: * Now, for a file, pass it to the callback if it was
892: * supplied to us.
893: */
894: if (cfp->cf_type != DT_DIR && cb != NULL) {
895: if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
1.91 joris 896: goto done;
1.4 jfb 897: }
1.14 jfb 898:
1.100 joris 899: /*
1.117 joris 900: * Mark the entry as processed.
1.100 joris 901: */
1.117 joris 902: if (ent != NULL)
903: ent->processed = 1;
1.68 joris 904:
1.100 joris 905: /*
906: * If we don't want to keep it, free it
907: */
908: if ((cfp->cf_type != DT_DIR) && freecf)
909: cvs_file_free(cfp);
1.68 joris 910: }
911:
1.100 joris 912: closedir(dp);
913: dp = NULL;
1.68 joris 914:
1.100 joris 915: /*
916: * Pass over all of the entries now, so we pickup any files
917: * that might have been lost, or are for some reason not on disk.
918: *
919: * (Follows the same procedure as above ... can we merge them?)
920: */
921: while ((entf != NULL) && ((ent = cvs_ent_next(entf)) != NULL)) {
1.117 joris 922: if (ent->processed == 1)
923: continue;
1.100 joris 924: if (!(flags & CF_RECURSE) && (ent->ce_type == CVS_ENT_DIR))
925: continue;
926: if ((flags & CF_NOFILES) && (ent->ce_type != CVS_ENT_DIR))
927: continue;
1.68 joris 928:
1.100 joris 929: len = cvs_path_cat(fpath, ent->ce_name, pbuf, sizeof(pbuf));
930: if (len >= sizeof(pbuf))
931: goto done;
1.61 xsa 932:
1.115 joris 933: cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100 joris 934: if (cfp == NULL) {
935: cvs_log(LP_ERR, "failed to fetch '%s'", pbuf);
936: goto done;
937: }
1.68 joris 938:
1.100 joris 939: if ((cfp->cf_type != DT_DIR) && !freecf) {
940: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
941: nfiles++;
942: } else if (cfp->cf_type == DT_DIR) {
943: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
944: ndirs++;
945: }
1.68 joris 946:
1.100 joris 947: if (cfp->cf_type != DT_DIR && cb != NULL) {
948: if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
949: goto done;
950: }
1.68 joris 951:
1.100 joris 952: if ((cfp->cf_type != DT_DIR) && freecf)
953: cvs_file_free(cfp);
1.40 jfb 954: }
1.34 jfb 955:
1.100 joris 956: /*
957: * Sort files and dirs if requested.
958: */
1.10 jfb 959: if (flags & CF_SORT) {
1.68 joris 960: if (nfiles > 0)
961: cvs_file_sort(&(cf->cf_files), nfiles);
962: if (ndirs > 0)
963: cvs_file_sort(&dirs, ndirs);
1.10 jfb 964: }
1.26 jfb 965:
1.100 joris 966: /*
967: * Finally, run over the directories we have encountered.
968: * Before calling cvs_file_getdir() on them, we pass them
969: * to the callback first.
970: */
1.62 jfb 971: while (!SIMPLEQ_EMPTY(&dirs)) {
972: cfp = SIMPLEQ_FIRST(&dirs);
973: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 974:
1.100 joris 975: if (!freecf)
1.68 joris 976: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
977:
1.100 joris 978: if (cb != NULL) {
979: if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
980: goto done;
1.68 joris 981: }
1.100 joris 982:
983: if ((cfp->cf_flags & CVS_FILE_ONDISK) &&
984: (cvs_file_getdir(cfp, flags, cb, arg, freecf) < 0))
985: goto done;
986:
987: if (freecf)
988: cvs_file_free(cfp);
1.26 jfb 989: }
1.3 jfb 990:
1.91 joris 991: ret = 0;
1.100 joris 992: cfp = NULL;
1.91 joris 993: done:
1.100 joris 994: if ((cfp != NULL) && freecf)
995: cvs_file_free(cfp);
996:
997: while (!SIMPLEQ_EMPTY(&dirs)) {
998: cfp = SIMPLEQ_FIRST(&dirs);
999: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1000:
1001: cvs_file_free(cfp);
1002: }
1003:
1004: if (entf != NULL)
1005: cvs_ent_close(entf);
1006: if (dp != NULL)
1007: closedir(dp);
1.91 joris 1008:
1009: return (ret);
1.3 jfb 1010: }
1011:
1012:
1013: /*
1014: * cvs_file_free()
1015: *
1016: * Free a cvs_file structure and its contents.
1017: */
1018: void
1.14 jfb 1019: cvs_file_free(CVSFILE *cf)
1.3 jfb 1020: {
1.62 jfb 1021: CVSFILE *child;
1022:
1.47 jfb 1023: if (cf->cf_name != NULL)
1.132 ! joris 1024: xfree(cf->cf_name);
1.62 jfb 1025:
1.100 joris 1026: if (cf->cf_dir != NULL)
1.132 ! joris 1027: xfree(cf->cf_dir);
1.100 joris 1028:
1.62 jfb 1029: if (cf->cf_type == DT_DIR) {
1030: if (cf->cf_root != NULL)
1.119 joris 1031: cvsroot_remove(cf->cf_root);
1.62 jfb 1032: if (cf->cf_repo != NULL)
1.132 ! joris 1033: xfree(cf->cf_repo);
1.62 jfb 1034: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
1035: child = SIMPLEQ_FIRST(&(cf->cf_files));
1036: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
1037: cvs_file_free(child);
1038: }
1.64 joris 1039: } else {
1040: if (cf->cf_tag != NULL)
1.132 ! joris 1041: xfree(cf->cf_tag);
1.77 jfb 1042: if (cf->cf_opts != NULL)
1.132 ! joris 1043: xfree(cf->cf_opts);
1.62 jfb 1044: }
1.64 joris 1045:
1.132 ! joris 1046: xfree(cf);
1.5 jfb 1047: }
1048:
1.3 jfb 1049:
1050: /*
1051: * cvs_file_sort()
1052: *
1.16 jfb 1053: * Sort a list of cvs file structures according to their filename. The list
1054: * <flp> is modified according to the sorting algorithm. The number of files
1055: * in the list must be given by <nfiles>.
1056: * Returns 0 on success, or -1 on failure.
1.3 jfb 1057: */
1058: static int
1.16 jfb 1059: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 1060: {
1061: int i;
1062: size_t nb;
1.16 jfb 1063: CVSFILE *cf, **cfvec;
1064:
1.113 xsa 1065: cfvec = (CVSFILE **)calloc((size_t)nfiles, sizeof(CVSFILE *));
1.16 jfb 1066: if (cfvec == NULL) {
1067: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
1068: return (-1);
1069: }
1.3 jfb 1070:
1071: i = 0;
1.62 jfb 1072: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 1073: if (i == (int)nfiles) {
1.3 jfb 1074: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 1075: /* rebuild the list and abort sorting */
1076: while (--i >= 0)
1.62 jfb 1077: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.132 ! joris 1078: xfree(cfvec);
1.3 jfb 1079: return (-1);
1080: }
1.16 jfb 1081: cfvec[i++] = cf;
1.3 jfb 1082:
1083: /* now unlink it from the list,
1084: * we'll put it back in order later
1085: */
1.62 jfb 1086: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 1087: }
1088:
1089: /* clear the list just in case */
1.62 jfb 1090: SIMPLEQ_INIT(flp);
1.3 jfb 1091: nb = (size_t)i;
1092:
1093: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
1094:
1095: /* rebuild the list from the bottom up */
1096: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 1097: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 1098:
1.132 ! joris 1099: xfree(cfvec);
1.3 jfb 1100: return (0);
1101: }
1102:
1103:
1104: static int
1105: cvs_file_cmp(const void *f1, const void *f2)
1106: {
1.41 jfb 1107: const CVSFILE *cf1, *cf2;
1.62 jfb 1108: cf1 = *(CVSFILE * const *)f1;
1109: cf2 = *(CVSFILE * const *)f2;
1.99 joris 1110: return cvs_file_cmpname(cf1->cf_name, cf2->cf_name);
1.6 jfb 1111: }
1112:
1113:
1.34 jfb 1114: /*
1115: * cvs_file_alloc()
1116: *
1117: * Allocate a CVSFILE structure and initialize its internals.
1118: */
1.109 xsa 1119: CVSFILE *
1.6 jfb 1120: cvs_file_alloc(const char *path, u_int type)
1121: {
1122: CVSFILE *cfp;
1.100 joris 1123: char *p;
1.6 jfb 1124:
1.132 ! joris 1125: cfp = (CVSFILE *)xmalloc(sizeof(*cfp));
1.6 jfb 1126: memset(cfp, 0, sizeof(*cfp));
1127:
1.62 jfb 1128: cfp->cf_type = type;
1129: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1130:
1131: if (type == DT_DIR) {
1132: SIMPLEQ_INIT(&(cfp->cf_files));
1133: }
1134:
1.132 ! joris 1135: cfp->cf_name = xstrdup(basename(path));
1.100 joris 1136: if ((p = strrchr(path, '/')) != NULL) {
1137: *p = '\0';
1.132 ! joris 1138: if (strcmp(path, "."))
! 1139: cfp->cf_dir = xstrdup(path);
! 1140: else
1.100 joris 1141: cfp->cf_dir = NULL;
1142: *p = '/';
1143: } else
1144: cfp->cf_dir = NULL;
1145:
1.6 jfb 1146: return (cfp);
1.1 jfb 1147: }
1.14 jfb 1148:
1149:
1150: /*
1151: * cvs_file_lget()
1152: *
1153: * Get the file and link it with the parent right away.
1.22 jfb 1154: * Returns a pointer to the created file structure on success, or NULL on
1155: * failure.
1.14 jfb 1156: */
1.109 xsa 1157: static CVSFILE *
1.115 joris 1158: cvs_file_lget(const char *path, int flags, CVSFILE *parent, CVSENTRIES *pent,
1159: struct cvs_ent *ent)
1.14 jfb 1160: {
1.128 joris 1161: char *c;
1.100 joris 1162: int ret;
1.44 jfb 1163: u_int type;
1.14 jfb 1164: struct stat st;
1165: CVSFILE *cfp;
1.128 joris 1166: struct cvsroot *root;
1.14 jfb 1167:
1.44 jfb 1168: type = DT_UNKNOWN;
1169: ret = stat(path, &st);
1170: if (ret == 0)
1171: type = IFTODT(st.st_mode);
1.14 jfb 1172:
1.128 joris 1173: if ((flags & CF_REPO) && (type != DT_DIR)) {
1174: if ((c = strrchr(path, ',')) == NULL)
1175: return (NULL);
1176: *c = '\0';
1177: }
1178:
1.44 jfb 1179: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 1180: return (NULL);
1181: cfp->cf_parent = parent;
1.115 joris 1182: cfp->cf_entry = pent;
1.54 joris 1183:
1184: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 1185: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 1186:
1.44 jfb 1187: if (ret == 0) {
1188: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 1189: if (cfp->cf_type == DT_REG)
1190: cfp->cf_mtime = st.st_mtime;
1.100 joris 1191: cfp->cf_flags |= CVS_FILE_ONDISK;
1.44 jfb 1192:
1193: if (ent == NULL)
1.100 joris 1194: if (cfp->cf_flags & CVS_DIRF_BASE)
1195: cfp->cf_cvstat = CVS_FST_UPTODATE;
1196: else
1197: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1.14 jfb 1198: else {
1.44 jfb 1199: /* always show directories as up-to-date */
1200: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 1201: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 1202: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1203: cfp->cf_cvstat = CVS_FST_ADDED;
1204: else {
1.130 joris 1205: /*
1206: * correct st.st_mtime first
1207: */
1208: if ((st.st_mtime =
1209: cvs_hack_time(st.st_mtime, 1)) == 0) {
1210: cvs_file_free(cfp);
1211: return (NULL);
1212: }
1213:
1.44 jfb 1214: /* check last modified time */
1.90 joris 1215: if (ent->ce_mtime == (time_t)st.st_mtime) {
1.44 jfb 1216: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.90 joris 1217: } else {
1.44 jfb 1218: cfp->cf_cvstat = CVS_FST_MODIFIED;
1.90 joris 1219: }
1.44 jfb 1220: }
1.94 joris 1221:
1222: cfp->cf_etime = ent->ce_mtime;
1.14 jfb 1223: }
1.44 jfb 1224: } else {
1225: if (ent == NULL) {
1.92 joris 1226: /* assume it is a file and unknown */
1227: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1228: cfp->cf_type = DT_REG;
1.66 joris 1229: } else {
1230: if (ent->ce_type == CVS_ENT_FILE)
1231: cfp->cf_type = DT_REG;
1232: else if (ent->ce_type == CVS_ENT_DIR)
1233: cfp->cf_type = DT_DIR;
1234: else
1.93 joris 1235: cvs_log(LP_WARN, "unknown ce_type %d",
1.66 joris 1236: ent->ce_type);
1.67 joris 1237:
1238: if (ent->ce_status == CVS_ENT_REMOVED)
1239: cfp->cf_cvstat = CVS_FST_REMOVED;
1.90 joris 1240: else if (ent->ce_status == CVS_ENT_UPTODATE)
1241: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.96 joris 1242: else if (ent->ce_status == CVS_ENT_ADDED)
1243: cfp->cf_cvstat = CVS_FST_ADDED;
1.67 joris 1244: else
1245: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1246: }
1.128 joris 1247:
1248: /* XXX assume 0644 ? */
1249: cfp->cf_mode = 0644;
1.14 jfb 1250: }
1.52 jfb 1251:
1.62 jfb 1252: if (ent != NULL) {
1253: /* steal the RCSNUM */
1254: cfp->cf_lrev = ent->ce_rev;
1.72 jfb 1255:
1.77 jfb 1256: if (ent->ce_type == CVS_ENT_FILE) {
1.132 ! joris 1257: if (ent->ce_tag[0] != '\0')
! 1258: cfp->cf_tag = xstrdup(ent->ce_tag);
1.77 jfb 1259:
1.132 ! joris 1260: if (ent->ce_opts[0] != '\0')
! 1261: cfp->cf_opts = xstrdup(ent->ce_opts);
1.62 jfb 1262: }
1263: }
1.14 jfb 1264:
1.115 joris 1265: if (cfp->cf_type == DT_DIR) {
1266: if (cvs_load_dirinfo(cfp, flags) < 0) {
1267: cvs_file_free(cfp);
1268: return (NULL);
1269: }
1.14 jfb 1270: }
1.74 joris 1271:
1.128 joris 1272: if (flags & CF_REPO) {
1273: root = CVS_DIR_ROOT(cfp);
1274:
1275: cfp->cf_mode = 0644;
1276: cfp->cf_cvstat = CVS_FST_LOST;
1277:
1.132 ! joris 1278: c = xstrdup(cfp->cf_dir);
! 1279: xfree(cfp->cf_dir);
1.128 joris 1280:
1281: if (strcmp(c, root->cr_dir)) {
1282: c += strlen(root->cr_dir) + 1;
1.132 ! joris 1283: cfp->cf_dir = xstrdup(c);
1.128 joris 1284: c -= strlen(root->cr_dir) + 1;
1285: } else {
1286: cfp->cf_dir = NULL;
1287: }
1288:
1.132 ! joris 1289: xfree(c);
1.128 joris 1290: }
1291:
1.74 joris 1292: if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
1293: !strcmp(cfp->cf_repo, path))
1294: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14 jfb 1295:
1.100 joris 1296: /*
1297: * In server mode, we do a few extra checks.
1298: */
1299: if (cvs_cmdop == CVS_OP_SERVER) {
1300: /*
1301: * If for some reason a file was added,
1302: * but does not exist anymore, start complaining.
1303: */
1304: if (!(cfp->cf_flags & CVS_FILE_ONDISK) &&
1.124 reyk 1305: (cfp->cf_cvstat == CVS_FST_ADDED) &&
1.100 joris 1306: (cfp->cf_type != DT_DIR))
1307: cvs_log(LP_WARN, "new-born %s has disappeared", path);
1308:
1309: /*
1310: * Any other needed checks?
1311: */
1312: }
1313:
1.14 jfb 1314: return (cfp);
1.23 jfb 1315: }
1316:
1317:
1318: static int
1319: cvs_file_cmpname(const char *name1, const char *name2)
1320: {
1321: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1322: (strcasecmp(name1, name2));
1.88 joris 1323: }
1324:
1325: /*
1.100 joris 1326: * remove any empty directories.
1.88 joris 1327: */
1328: int
1329: cvs_file_prune(char *path)
1330: {
1331: DIR *dirp;
1332: int l, pwd, empty;
1333: struct dirent *dp;
1334: char fpath[MAXPATHLEN];
1335: CVSENTRIES *entf;
1.89 joris 1336: CVSFILE *cfp;
1.88 joris 1337:
1338: pwd = (!strcmp(path, "."));
1339:
1340: if ((dirp = opendir(path)) == NULL) {
1341: cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1342: return (-1);
1343: }
1344:
1345: empty = 0;
1346: entf = cvs_ent_open(path, O_RDWR);
1347:
1348: while ((dp = readdir(dirp)) != NULL) {
1349: if (!strcmp(dp->d_name, ".") ||
1350: !strcmp(dp->d_name, "..") ||
1351: !strcmp(dp->d_name, CVS_PATH_CVSDIR))
1352: continue;
1353:
1354: empty++;
1355: if (dp->d_type == DT_DIR) {
1356: l = snprintf(fpath, sizeof(fpath), "%s%s%s",
1357: (pwd) ? "" : path, (pwd) ? "" : "/", dp->d_name);
1358: if (l == -1 || l >= (int)sizeof(fpath)) {
1359: errno = ENAMETOOLONG;
1360: cvs_log(LP_ERRNO, "%s", fpath);
1361: continue;
1362: }
1.89 joris 1363:
1364: cfp = cvs_file_find(cvs_files, fpath);
1365: if (cfp == NULL)
1.91 joris 1366: continue;
1367:
1368: /* ignore unknown directories */
1369: if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
1.89 joris 1370: continue;
1.88 joris 1371:
1372: if (cvs_file_prune(fpath)) {
1373: empty--;
1374: if (entf)
1.128 joris 1375: cvs_ent_remove(entf, fpath, 0);
1.88 joris 1376: } else {
1377: empty++;
1378: }
1379: }
1380: }
1381:
1382: closedir(dirp);
1383: if (entf)
1384: cvs_ent_close(entf);
1385:
1386: empty = (empty == 0);
1387: if (empty) {
1.106 xsa 1388: if (cvs_rmdir(path) < 0) {
1.88 joris 1389: cvs_log(LP_ERR, "failed to prune `%s'", path);
1390: empty = 0;
1391: }
1.93 joris 1392: }
1.88 joris 1393:
1394: return (empty);
1.14 jfb 1395: }