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