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