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