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