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