Annotation of src/usr.bin/cvs/file.c, Revision 1.130
1.130 ! joris 1: /* $OpenBSD: file.c,v 1.129 2005/12/03 02:19:17 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"
45:
1.104 xsa 46: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
1.1 jfb 47:
1.104 xsa 48: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
1.1 jfb 49:
50:
51: /* ignore pattern */
52: struct cvs_ignpat {
1.104 xsa 53: char ip_pat[MAXNAMLEN];
54: int ip_flags;
1.124 reyk 55: TAILQ_ENTRY(cvs_ignpat) ip_list;
1.1 jfb 56: };
57:
58:
59: /*
60: * Standard patterns to ignore.
61: */
62: static const char *cvs_ign_std[] = {
63: ".",
64: "..",
65: "*.o",
66: "*.so",
1.45 xsa 67: "*.a",
1.1 jfb 68: "*.bak",
69: "*.orig",
70: "*.rej",
1.45 xsa 71: "*.old",
1.1 jfb 72: "*.exe",
73: "*.depend",
1.45 xsa 74: "*.obj",
75: "*.elc",
76: "*.ln",
77: "*.olb",
1.1 jfb 78: "CVS",
79: "core",
1.102 joris 80: "cvslog*",
1.49 jfb 81: "*.core",
1.8 jfb 82: ".#*",
1.45 xsa 83: "*~",
84: "_$*",
85: "*$",
1.1 jfb 86: #ifdef OLD_SMELLY_CRUFT
87: "RCSLOG",
88: "tags",
89: "TAGS",
90: "RCS",
91: "SCCS",
1.45 xsa 92: "cvslog.*", /* to ignore CVS_CLIENT_LOG output */
1.1 jfb 93: "#*",
94: ",*",
95: #endif
96: };
97:
98:
1.11 jfb 99: /*
100: * Entries in the CVS/Entries file with a revision of '0' have only been
101: * added. Compare against this revision to see if this is the case
102: */
1.4 jfb 103: static RCSNUM *cvs_addedrev;
104:
105:
1.104 xsa 106: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
1.1 jfb 107:
1.100 joris 108: static int cvs_file_getdir(CVSFILE *, int, int (*)(CVSFILE *, void *),
109: void *, int);
1.1 jfb 110:
1.104 xsa 111: static int cvs_load_dirinfo(CVSFILE *, int);
112: static int cvs_file_sort(struct cvs_flist *, u_int);
113: static int cvs_file_cmp(const void *, const void *);
114: static int cvs_file_cmpname(const char *, const char *);
115: static CVSFILE *cvs_file_alloc(const char *, u_int);
1.115 joris 116: static CVSFILE *cvs_file_lget(const char *, int, CVSFILE *, CVSENTRIES *,
117: 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.122 xsa 288: (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo,
1.124 reyk 289: NULL, NULL, 0) < 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) {
1.125 joris 398: cvs_log(LP_ERR, "failed to obtain '.' information");
1.100 joris 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:
431: /*
432: * Since some commands don't require any files to operate
433: * we can stop right here for those.
434: */
1.128 joris 435: if (cf->cf_root != NULL) {
436: if (cf->cf_root->cr_method != CVS_METHOD_LOCAL &&
437: cvs_cmdop == CVS_OP_CHECKOUT) {
438: cvs_file_free(cf);
439: return (0);
440: }
441: }
442:
443: cvs_file_free(cf);
444:
445: if (cvs_cmdop == CVS_OP_VERSION)
1.100 joris 446: return (0);
447:
1.26 jfb 448: for (i = 0; i < fsn; i++) {
449: strlcpy(pcopy, fspec[i], sizeof(pcopy));
1.123 joris 450:
451: /*
452: * get rid of any trailing slashes.
453: */
454: STRIP_SLASH(pcopy);
1.26 jfb 455:
1.100 joris 456: /*
457: * Load the information.
458: */
459: cf = cvs_file_loadinfo(pcopy, flags, cb, arg, freecf);
1.108 joris 460: if (cf == NULL) {
461: if (cvs_error != CVS_EX_OK)
1.126 joris 462: return (-1);
1.100 joris 463: continue;
1.108 joris 464: }
1.68 joris 465:
1.100 joris 466: /*
467: * If extra actions are needed, do them now.
468: */
469: if (cf->cf_type == DT_DIR) {
470: /* do possible extra actions .. */
1.68 joris 471: } else {
1.100 joris 472: /* do possible extra actions .. */
473: }
474:
475: /*
476: * Attach it to a list if requested, otherwise
477: * just free it again.
478: */
479: if (list != NULL)
480: SIMPLEQ_INSERT_TAIL(list, cf, cf_list);
481: else
482: cvs_file_free(cf);
483: }
484:
485: return (0);
486: }
487:
488: /*
489: * Load the neccesary information about file or directory <path>.
490: * Returns a pointer to the loaded information on success, or NULL
491: * on failure.
492: *
493: * If cb is not NULL, the requested path will be passed to that callback
494: * with <arg> as an argument.
495: *
496: * the <freecf> argument is passed to cvs_file_getdir, if this is 1
497: * CVSFILE * structs will be free'd once we are done with them.
498: */
1.114 xsa 499: CVSFILE *
1.100 joris 500: cvs_file_loadinfo(char *path, int flags, int (*cb)(CVSFILE *, void *),
501: void *arg, int freecf)
502: {
503: CVSFILE *cf, *base;
504: CVSENTRIES *entf;
505: struct cvs_ent *ent;
506: char *p;
507: char parent[MAXPATHLEN], item[MAXPATHLEN];
1.120 joris 508: int type, callit;
1.100 joris 509: struct stat st;
510: struct cvsroot *root;
511:
512: type = 0;
513: base = cf = NULL;
514: entf = NULL;
515: ent = NULL;
516:
517: /*
518: * We first have to find out what type of item we are
519: * dealing with. A file or a directory.
520: *
521: * We can do this by stat(2)'ing the item, but since it
522: * might be gone we also check the Entries file in the
523: * parent directory.
524: */
525:
526: /* get parent directory */
527: if ((p = strrchr(path, '/')) != NULL) {
528: *p++ = '\0';
529: strlcpy(parent, path, sizeof(parent));
530: strlcpy(item, p, sizeof(item));
531: *--p = '/';
532: } else {
533: strlcpy(parent, ".", sizeof(parent));
534: strlcpy(item, path, sizeof(item));
535: }
536:
537: /*
538: * There might not be an Entries file, so do not fail if there
539: * is none available to get the info from.
540: */
1.115 joris 541: entf = cvs_ent_open(parent, O_RDWR);
1.100 joris 542:
543: /*
544: * Load the Entry if we successfully opened the Entries file.
545: */
546: if (entf != NULL)
547: ent = cvs_ent_get(entf, item);
548:
549: /*
550: * No Entry available? fall back to stat(2)'ing the item, if
1.114 xsa 551: * that fails, assume a normal file.
1.100 joris 552: */
553: if (ent == NULL) {
1.103 joris 554: if (stat(path, &st) == -1)
1.100 joris 555: type = DT_REG;
1.103 joris 556: else
1.100 joris 557: type = IFTODT(st.st_mode);
558: } else {
559: if (ent->ce_type == CVS_ENT_DIR)
560: type = DT_DIR;
561: else
562: type = DT_REG;
563: }
564:
565: /*
566: * Get the base, which is <parent> for a normal file or
567: * <path> for a directory.
568: */
569: if (type == DT_DIR)
1.115 joris 570: base = cvs_file_lget(path, flags, NULL, entf, ent);
1.100 joris 571: else
1.115 joris 572: base = cvs_file_lget(parent, flags, NULL, entf, NULL);
1.100 joris 573:
574: if (base == NULL) {
575: cvs_log(LP_ERR, "failed to obtain directory info for '%s'",
576: parent);
1.108 joris 577: cvs_error = CVS_EX_FILE;
1.100 joris 578: goto fail;
579: }
580:
581: /*
582: * Sanity.
583: */
584: if (base->cf_type != DT_DIR) {
585: cvs_log(LP_ERR, "base directory isn't a directory at all");
586: goto fail;
587: }
588:
589: root = CVS_DIR_ROOT(base);
590: if (root == NULL) {
1.108 joris 591: cvs_error = CVS_EX_BADROOT;
1.100 joris 592: goto fail;
593: }
1.26 jfb 594:
1.100 joris 595: /*
596: * If we have a normal file, get the info and link it
597: * to the base.
598: */
599: if (type != DT_DIR) {
1.115 joris 600: cf = cvs_file_lget(path, flags, base, entf, ent);
1.100 joris 601: if (cf == NULL) {
1.108 joris 602: cvs_error = CVS_EX_DATA;
1.100 joris 603: goto fail;
1.68 joris 604: }
1.26 jfb 605:
1.100 joris 606: cvs_file_attach(base, cf);
607: }
608:
609: /*
610: * Always pass the base directory, unless:
611: * - we are running in server or local mode and the path is not "."
612: * - the directory does not exist on disk.
613: * - the callback is NULL.
1.122 xsa 614: */
1.120 joris 615: callit = 1;
616: if (cb == NULL)
617: callit = 0;
618:
1.121 joris 619: if ((cvs_cmdop == CVS_OP_SERVER) && (type != DT_DIR))
1.120 joris 620: callit = 0;
621:
1.121 joris 622: if ((root->cr_method == CVS_METHOD_LOCAL) && (type != DT_DIR))
1.120 joris 623: callit = 0;
624:
625: if (!(base->cf_flags & CVS_FILE_ONDISK))
626: callit = 0;
627:
628: if (callit != 0) {
629: if ((cvs_error = cb(base,arg)) != CVS_EX_OK)
630: goto fail;
631: }
1.100 joris 632:
633: /*
634: * If we have a normal file, pass it as well.
635: */
636: if (type != DT_DIR) {
637: if ((cb != NULL) && ((cvs_error = cb(cf, arg)) != CVS_EX_OK))
638: goto fail;
639: } else {
640: /*
641: * If the directory exists, recurse through it.
642: */
643: if ((base->cf_flags & CVS_FILE_ONDISK) &&
644: cvs_file_getdir(base, flags, cb, arg, freecf) < 0) {
1.108 joris 645: cvs_error = CVS_EX_FILE;
1.100 joris 646: goto fail;
1.68 joris 647: }
1.116 joris 648: }
649:
650: if (entf != NULL) {
651: cvs_ent_close(entf);
652: entf = NULL;
1.26 jfb 653: }
1.87 joris 654:
1.100 joris 655: return (base);
1.26 jfb 656:
1.100 joris 657: fail:
658: if (entf != NULL)
659: cvs_ent_close(entf);
660: if (base != NULL)
661: cvs_file_free(base);
662: return (NULL);
1.3 jfb 663: }
664:
665: /*
1.13 jfb 666: * cvs_file_find()
667: *
668: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
669: * The file's pathname <path> must be relative to the base of <hier>.
670: * Returns the entry on success, or NULL on failure.
671: */
1.109 xsa 672: CVSFILE *
1.13 jfb 673: cvs_file_find(CVSFILE *hier, const char *path)
674: {
675: char *pp, *sp, pbuf[MAXPATHLEN];
676: CVSFILE *sf, *cf;
677:
678: strlcpy(pbuf, path, sizeof(pbuf));
679:
680: cf = hier;
681: pp = pbuf;
682: do {
683: sp = strchr(pp, '/');
684: if (sp != NULL)
1.24 jfb 685: *(sp++) = '\0';
1.13 jfb 686:
687: /* special case */
688: if (*pp == '.') {
689: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
690: /* request to go back to parent */
691: if (cf->cf_parent == NULL) {
692: cvs_log(LP_NOTICE,
693: "path %s goes back too far", path);
694: return (NULL);
695: }
696: cf = cf->cf_parent;
697: continue;
1.38 deraadt 698: } else if (*(pp + 1) == '\0')
1.13 jfb 699: continue;
700: }
701:
1.62 jfb 702: SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.99 joris 703: if (cvs_file_cmpname(pp, sf->cf_name) == 0)
1.13 jfb 704: break;
705: if (sf == NULL)
706: return (NULL);
707:
708: cf = sf;
709: pp = sp;
710: } while (sp != NULL);
711:
1.24 jfb 712: return (cf);
1.13 jfb 713: }
714:
715:
716: /*
1.34 jfb 717: * cvs_file_getpath()
718: *
719: * Get the full path of the file <file> and store it in <buf>, which is of
720: * size <len>. For portability, it is recommended that <buf> always be
721: * at least MAXPATHLEN bytes long.
1.100 joris 722: * Returns a pointer to the start of the path.
1.34 jfb 723: */
1.104 xsa 724: char *
1.34 jfb 725: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
726: {
1.100 joris 727: memset(buf, '\0', len);
728: if (file->cf_dir != NULL) {
729: strlcat(buf, file->cf_dir, len);
1.34 jfb 730: strlcat(buf, "/", len);
731: }
732:
1.100 joris 733: strlcat(buf, file->cf_name, len);
1.34 jfb 734: return (buf);
735: }
736:
737: /*
1.22 jfb 738: * cvs_file_attach()
739: *
740: * Attach the file <file> as one of the children of parent <parent>, which
741: * has to be a file of type DT_DIR.
742: * Returns 0 on success, or -1 on failure.
743: */
744: int
745: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
746: {
747: if (parent->cf_type != DT_DIR)
748: return (-1);
749:
1.62 jfb 750: SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22 jfb 751: file->cf_parent = parent;
752:
753: return (0);
754: }
755:
756:
757: /*
1.68 joris 758: * Load directory information
1.3 jfb 759: */
1.6 jfb 760: static int
1.69 joris 761: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3 jfb 762: {
1.68 joris 763: char fpath[MAXPATHLEN];
764: char pbuf[MAXPATHLEN];
1.20 jfb 765: struct stat st;
1.68 joris 766: int l;
1.7 jfb 767:
1.34 jfb 768: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.100 joris 769:
770: /*
771: * Try to obtain the Root for this given directory, if we cannot
772: * get it, fail, unless we are dealing with a directory that is
773: * unknown or not on disk.
774: */
1.62 jfb 775: cf->cf_root = cvsroot_get(fpath);
1.91 joris 776: if (cf->cf_root == NULL) {
1.100 joris 777: if (cf->cf_cvstat == CVS_FST_UNKNOWN ||
778: !(cf->cf_flags & CVS_FILE_ONDISK))
1.91 joris 779: return (0);
1.48 jfb 780: return (-1);
1.91 joris 781: }
1.114 xsa 782:
1.74 joris 783: /* if the CVS administrative directory exists, load the info */
784: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
785: if (l == -1 || l >= (int)sizeof(pbuf)) {
786: errno = ENAMETOOLONG;
787: cvs_log(LP_ERRNO, "%s", pbuf);
788: return (-1);
789: }
1.61 xsa 790:
1.74 joris 791: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
792: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
793: cf->cf_repo = strdup(pbuf);
794: if (cf->cf_repo == NULL) {
795: cvs_log(LP_ERRNO,
796: "failed to dup repository string");
797: return (-1);
1.20 jfb 798: }
1.68 joris 799: }
1.100 joris 800: } else {
801: /*
802: * Fill in the repo path ourselfs.
803: */
1.107 joris 804: if (cvs_repo_base != NULL) {
805: l = snprintf(pbuf, sizeof(pbuf), "%s/%s",
806: cvs_repo_base, fpath);
807: if (l == -1 || l >= (int)sizeof(pbuf))
808: return (-1);
1.100 joris 809:
1.107 joris 810: cf->cf_repo = strdup(pbuf);
811: if (cf->cf_repo == NULL) {
812: cvs_log(LP_ERRNO, "failed to dup repo string");
813: return (-1);
814: }
815: } else
816: cf->cf_repo = NULL;
1.68 joris 817: }
1.26 jfb 818:
1.100 joris 819: if (flags & CF_MKADMIN)
1.122 xsa 820: cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL, NULL, NULL, 0);
1.100 joris 821:
1.68 joris 822: return (0);
823: }
824:
825: /*
826: * cvs_file_getdir()
827: *
828: * Get a cvs directory structure for the directory whose path is <dir>.
829: * This function should not free the directory information on error, as this
830: * is performed by cvs_file_free().
831: */
832: static int
1.100 joris 833: cvs_file_getdir(CVSFILE *cf, int flags, int (*cb)(CVSFILE *, void *),
834: void *arg, int freecf)
1.68 joris 835: {
1.100 joris 836: int ret;
837: size_t len;
838: DIR *dp;
839: struct dirent *de;
840: char fpath[MAXPATHLEN], pbuf[MAXPATHLEN];
841: CVSENTRIES *entf;
1.76 joris 842: CVSFILE *cfp;
1.100 joris 843: struct cvs_ent *ent;
1.68 joris 844: struct cvs_flist dirs;
1.100 joris 845: int nfiles, ndirs;
1.14 jfb 846:
1.56 jfb 847: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 848: return (0);
849:
1.128 joris 850: /*
851: * if we are working with a repository, fiddle with
852: * the pathname again.
853: */
854: if (flags & CF_REPO) {
855: ret = snprintf(fpath, sizeof(fpath), "%s%s%s",
856: cf->cf_root->cr_dir,
857: (cf->cf_dir != NULL) ? "/" : "",
858: (cf->cf_dir != NULL) ? cf->cf_dir : "");
859: if (ret == -1 || ret >= (int)sizeof(fpath))
860: return (-1);
861: free(cf->cf_dir);
862: if ((cf->cf_dir = strdup(fpath)) == NULL)
863: return (-1);
864: }
865:
1.100 joris 866: nfiles = ndirs = 0;
867: SIMPLEQ_INIT(&dirs);
868: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.73 joris 869:
1.100 joris 870: if ((dp = opendir(fpath)) == NULL) {
871: cvs_log(LP_ERRNO, "failed to open directory '%s'", fpath);
1.91 joris 872: return (-1);
873: }
874:
1.100 joris 875: ret = -1;
1.115 joris 876: entf = cvs_ent_open(fpath, O_RDWR);
1.100 joris 877: while ((de = readdir(dp)) != NULL) {
878: if (!strcmp(de->d_name, ".") ||
879: !strcmp(de->d_name, ".."))
1.68 joris 880: continue;
1.11 jfb 881:
1.117 joris 882: len = cvs_path_cat(fpath, de->d_name, pbuf, sizeof(pbuf));
883: if (len >= sizeof(pbuf))
884: goto done;
885:
886: if (entf != NULL)
887: ent = cvs_ent_get(entf, de->d_name);
888: else
889: ent = NULL;
890:
1.100 joris 891: /*
892: * Do some filtering on the current directory item.
893: */
894: if ((flags & CF_IGNORE) && cvs_file_chkign(de->d_name))
1.68 joris 895: continue;
1.24 jfb 896:
1.100 joris 897: if (!(flags & CF_RECURSE) && (de->d_type == DT_DIR)) {
1.117 joris 898: if (ent != NULL)
899: ent->processed = 1;
1.68 joris 900: continue;
901: }
1.70 joris 902:
1.100 joris 903: if ((de->d_type != DT_DIR) && (flags & CF_NOFILES))
1.70 joris 904: continue;
1.56 jfb 905:
1.115 joris 906: cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100 joris 907: if (cfp == NULL) {
908: cvs_log(LP_ERR, "failed to get '%s'", pbuf);
1.91 joris 909: goto done;
1.68 joris 910: }
1.61 xsa 911:
1.100 joris 912: /*
913: * A file is linked to the parent <cf>, a directory
914: * is added to the dirs SIMPLEQ list for later use.
915: */
916: if ((cfp->cf_type != DT_DIR) && !freecf) {
917: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
918: nfiles++;
919: } else if (cfp->cf_type == DT_DIR) {
920: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
921: ndirs++;
922: }
1.71 joris 923:
1.100 joris 924: /*
925: * Now, for a file, pass it to the callback if it was
926: * supplied to us.
927: */
928: if (cfp->cf_type != DT_DIR && cb != NULL) {
929: if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
1.91 joris 930: goto done;
1.4 jfb 931: }
1.14 jfb 932:
1.100 joris 933: /*
1.117 joris 934: * Mark the entry as processed.
1.100 joris 935: */
1.117 joris 936: if (ent != NULL)
937: ent->processed = 1;
1.68 joris 938:
1.100 joris 939: /*
940: * If we don't want to keep it, free it
941: */
942: if ((cfp->cf_type != DT_DIR) && freecf)
943: cvs_file_free(cfp);
1.68 joris 944: }
945:
1.100 joris 946: closedir(dp);
947: dp = NULL;
1.68 joris 948:
1.100 joris 949: /*
950: * Pass over all of the entries now, so we pickup any files
951: * that might have been lost, or are for some reason not on disk.
952: *
953: * (Follows the same procedure as above ... can we merge them?)
954: */
955: while ((entf != NULL) && ((ent = cvs_ent_next(entf)) != NULL)) {
1.117 joris 956: if (ent->processed == 1)
957: continue;
1.100 joris 958: if (!(flags & CF_RECURSE) && (ent->ce_type == CVS_ENT_DIR))
959: continue;
960: if ((flags & CF_NOFILES) && (ent->ce_type != CVS_ENT_DIR))
961: continue;
1.68 joris 962:
1.100 joris 963: len = cvs_path_cat(fpath, ent->ce_name, pbuf, sizeof(pbuf));
964: if (len >= sizeof(pbuf))
965: goto done;
1.61 xsa 966:
1.115 joris 967: cfp = cvs_file_lget(pbuf, flags, cf, entf, ent);
1.100 joris 968: if (cfp == NULL) {
969: cvs_log(LP_ERR, "failed to fetch '%s'", pbuf);
970: goto done;
971: }
1.68 joris 972:
1.100 joris 973: if ((cfp->cf_type != DT_DIR) && !freecf) {
974: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
975: nfiles++;
976: } else if (cfp->cf_type == DT_DIR) {
977: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
978: ndirs++;
979: }
1.68 joris 980:
1.100 joris 981: if (cfp->cf_type != DT_DIR && cb != NULL) {
982: if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
983: goto done;
984: }
1.68 joris 985:
1.100 joris 986: if ((cfp->cf_type != DT_DIR) && freecf)
987: cvs_file_free(cfp);
1.40 jfb 988: }
1.34 jfb 989:
1.100 joris 990: /*
991: * Sort files and dirs if requested.
992: */
1.10 jfb 993: if (flags & CF_SORT) {
1.68 joris 994: if (nfiles > 0)
995: cvs_file_sort(&(cf->cf_files), nfiles);
996: if (ndirs > 0)
997: cvs_file_sort(&dirs, ndirs);
1.10 jfb 998: }
1.26 jfb 999:
1.100 joris 1000: /*
1001: * Finally, run over the directories we have encountered.
1002: * Before calling cvs_file_getdir() on them, we pass them
1003: * to the callback first.
1004: */
1.62 jfb 1005: while (!SIMPLEQ_EMPTY(&dirs)) {
1006: cfp = SIMPLEQ_FIRST(&dirs);
1007: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 1008:
1.100 joris 1009: if (!freecf)
1.68 joris 1010: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
1011:
1.100 joris 1012: if (cb != NULL) {
1013: if ((cvs_error = cb(cfp, arg)) != CVS_EX_OK)
1014: goto done;
1.68 joris 1015: }
1.100 joris 1016:
1017: if ((cfp->cf_flags & CVS_FILE_ONDISK) &&
1018: (cvs_file_getdir(cfp, flags, cb, arg, freecf) < 0))
1019: goto done;
1020:
1021: if (freecf)
1022: cvs_file_free(cfp);
1.26 jfb 1023: }
1.3 jfb 1024:
1.91 joris 1025: ret = 0;
1.100 joris 1026: cfp = NULL;
1.91 joris 1027: done:
1.100 joris 1028: if ((cfp != NULL) && freecf)
1029: cvs_file_free(cfp);
1030:
1031: while (!SIMPLEQ_EMPTY(&dirs)) {
1032: cfp = SIMPLEQ_FIRST(&dirs);
1033: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1034:
1035: cvs_file_free(cfp);
1036: }
1037:
1038: if (entf != NULL)
1039: cvs_ent_close(entf);
1040: if (dp != NULL)
1041: closedir(dp);
1.91 joris 1042:
1043: return (ret);
1.3 jfb 1044: }
1045:
1046:
1047: /*
1048: * cvs_file_free()
1049: *
1050: * Free a cvs_file structure and its contents.
1051: */
1052: void
1.14 jfb 1053: cvs_file_free(CVSFILE *cf)
1.3 jfb 1054: {
1.62 jfb 1055: CVSFILE *child;
1056:
1.47 jfb 1057: if (cf->cf_name != NULL)
1.127 niallo 1058: free(cf->cf_name);
1.62 jfb 1059:
1.100 joris 1060: if (cf->cf_dir != NULL)
1.127 niallo 1061: free(cf->cf_dir);
1.100 joris 1062:
1.62 jfb 1063: if (cf->cf_type == DT_DIR) {
1064: if (cf->cf_root != NULL)
1.119 joris 1065: cvsroot_remove(cf->cf_root);
1.62 jfb 1066: if (cf->cf_repo != NULL)
1067: free(cf->cf_repo);
1068: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
1069: child = SIMPLEQ_FIRST(&(cf->cf_files));
1070: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
1071: cvs_file_free(child);
1072: }
1.64 joris 1073: } else {
1074: if (cf->cf_tag != NULL)
1.127 niallo 1075: free(cf->cf_tag);
1.77 jfb 1076: if (cf->cf_opts != NULL)
1.127 niallo 1077: free(cf->cf_opts);
1.62 jfb 1078: }
1.64 joris 1079:
1.3 jfb 1080: free(cf);
1.5 jfb 1081: }
1082:
1.3 jfb 1083:
1084: /*
1085: * cvs_file_sort()
1086: *
1.16 jfb 1087: * Sort a list of cvs file structures according to their filename. The list
1088: * <flp> is modified according to the sorting algorithm. The number of files
1089: * in the list must be given by <nfiles>.
1090: * Returns 0 on success, or -1 on failure.
1.3 jfb 1091: */
1092: static int
1.16 jfb 1093: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 1094: {
1095: int i;
1096: size_t nb;
1.16 jfb 1097: CVSFILE *cf, **cfvec;
1098:
1.113 xsa 1099: cfvec = (CVSFILE **)calloc((size_t)nfiles, sizeof(CVSFILE *));
1.16 jfb 1100: if (cfvec == NULL) {
1101: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
1102: return (-1);
1103: }
1.3 jfb 1104:
1105: i = 0;
1.62 jfb 1106: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 1107: if (i == (int)nfiles) {
1.3 jfb 1108: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 1109: /* rebuild the list and abort sorting */
1110: while (--i >= 0)
1.62 jfb 1111: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16 jfb 1112: free(cfvec);
1.3 jfb 1113: return (-1);
1114: }
1.16 jfb 1115: cfvec[i++] = cf;
1.3 jfb 1116:
1117: /* now unlink it from the list,
1118: * we'll put it back in order later
1119: */
1.62 jfb 1120: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 1121: }
1122:
1123: /* clear the list just in case */
1.62 jfb 1124: SIMPLEQ_INIT(flp);
1.3 jfb 1125: nb = (size_t)i;
1126:
1127: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
1128:
1129: /* rebuild the list from the bottom up */
1130: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 1131: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 1132:
1.16 jfb 1133: free(cfvec);
1.3 jfb 1134: return (0);
1135: }
1136:
1137:
1138: static int
1139: cvs_file_cmp(const void *f1, const void *f2)
1140: {
1.41 jfb 1141: const CVSFILE *cf1, *cf2;
1.62 jfb 1142: cf1 = *(CVSFILE * const *)f1;
1143: cf2 = *(CVSFILE * const *)f2;
1.99 joris 1144: return cvs_file_cmpname(cf1->cf_name, cf2->cf_name);
1.6 jfb 1145: }
1146:
1147:
1.34 jfb 1148: /*
1149: * cvs_file_alloc()
1150: *
1151: * Allocate a CVSFILE structure and initialize its internals.
1152: */
1.109 xsa 1153: CVSFILE *
1.6 jfb 1154: cvs_file_alloc(const char *path, u_int type)
1155: {
1156: CVSFILE *cfp;
1.100 joris 1157: char *p;
1.6 jfb 1158:
1.14 jfb 1159: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 1160: if (cfp == NULL) {
1161: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
1162: return (NULL);
1163: }
1164: memset(cfp, 0, sizeof(*cfp));
1165:
1.62 jfb 1166: cfp->cf_type = type;
1167: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1168:
1169: if (type == DT_DIR) {
1170: SIMPLEQ_INIT(&(cfp->cf_files));
1171: }
1172:
1.127 niallo 1173: cfp->cf_name = strdup(basename(path));
1.34 jfb 1174: if (cfp->cf_name == NULL) {
1.57 jfb 1175: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 1176: cvs_file_free(cfp);
1.6 jfb 1177: return (NULL);
1178: }
1179:
1.100 joris 1180: if ((p = strrchr(path, '/')) != NULL) {
1181: *p = '\0';
1182: if (strcmp(path, ".")) {
1.127 niallo 1183: cfp->cf_dir = strdup(path);
1.100 joris 1184: if (cfp->cf_dir == NULL) {
1185: cvs_log(LP_ERR,
1186: "failed to copy directory");
1187: cvs_file_free(cfp);
1188: return (NULL);
1189: }
1190: } else
1191: cfp->cf_dir = NULL;
1192: *p = '/';
1193: } else
1194: cfp->cf_dir = NULL;
1195:
1.6 jfb 1196: return (cfp);
1.1 jfb 1197: }
1.14 jfb 1198:
1199:
1200: /*
1201: * cvs_file_lget()
1202: *
1203: * Get the file and link it with the parent right away.
1.22 jfb 1204: * Returns a pointer to the created file structure on success, or NULL on
1205: * failure.
1.14 jfb 1206: */
1.109 xsa 1207: static CVSFILE *
1.115 joris 1208: cvs_file_lget(const char *path, int flags, CVSFILE *parent, CVSENTRIES *pent,
1209: struct cvs_ent *ent)
1.14 jfb 1210: {
1.128 joris 1211: char *c;
1.100 joris 1212: int ret;
1.44 jfb 1213: u_int type;
1.14 jfb 1214: struct stat st;
1215: CVSFILE *cfp;
1.128 joris 1216: struct cvsroot *root;
1.14 jfb 1217:
1.44 jfb 1218: type = DT_UNKNOWN;
1219: ret = stat(path, &st);
1220: if (ret == 0)
1221: type = IFTODT(st.st_mode);
1.14 jfb 1222:
1.128 joris 1223: if ((flags & CF_REPO) && (type != DT_DIR)) {
1224: if ((c = strrchr(path, ',')) == NULL)
1225: return (NULL);
1226: *c = '\0';
1227: }
1228:
1.44 jfb 1229: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 1230: return (NULL);
1231: cfp->cf_parent = parent;
1.115 joris 1232: cfp->cf_entry = pent;
1.54 joris 1233:
1234: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 1235: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 1236:
1.44 jfb 1237: if (ret == 0) {
1238: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 1239: if (cfp->cf_type == DT_REG)
1240: cfp->cf_mtime = st.st_mtime;
1.100 joris 1241: cfp->cf_flags |= CVS_FILE_ONDISK;
1.44 jfb 1242:
1243: if (ent == NULL)
1.100 joris 1244: if (cfp->cf_flags & CVS_DIRF_BASE)
1245: cfp->cf_cvstat = CVS_FST_UPTODATE;
1246: else
1247: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1.14 jfb 1248: else {
1.44 jfb 1249: /* always show directories as up-to-date */
1250: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 1251: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 1252: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1253: cfp->cf_cvstat = CVS_FST_ADDED;
1254: else {
1.130 ! joris 1255: /*
! 1256: * correct st.st_mtime first
! 1257: */
! 1258: if ((st.st_mtime =
! 1259: cvs_hack_time(st.st_mtime, 1)) == 0) {
! 1260: cvs_file_free(cfp);
! 1261: return (NULL);
! 1262: }
! 1263:
1.44 jfb 1264: /* check last modified time */
1.90 joris 1265: if (ent->ce_mtime == (time_t)st.st_mtime) {
1.44 jfb 1266: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.90 joris 1267: } else {
1.44 jfb 1268: cfp->cf_cvstat = CVS_FST_MODIFIED;
1.90 joris 1269: }
1.44 jfb 1270: }
1.94 joris 1271:
1272: cfp->cf_etime = ent->ce_mtime;
1.14 jfb 1273: }
1.44 jfb 1274: } else {
1275: if (ent == NULL) {
1.92 joris 1276: /* assume it is a file and unknown */
1277: cfp->cf_cvstat = CVS_FST_UNKNOWN;
1278: cfp->cf_type = DT_REG;
1.66 joris 1279: } else {
1280: if (ent->ce_type == CVS_ENT_FILE)
1281: cfp->cf_type = DT_REG;
1282: else if (ent->ce_type == CVS_ENT_DIR)
1283: cfp->cf_type = DT_DIR;
1284: else
1.93 joris 1285: cvs_log(LP_WARN, "unknown ce_type %d",
1.66 joris 1286: ent->ce_type);
1.67 joris 1287:
1288: if (ent->ce_status == CVS_ENT_REMOVED)
1289: cfp->cf_cvstat = CVS_FST_REMOVED;
1.90 joris 1290: else if (ent->ce_status == CVS_ENT_UPTODATE)
1291: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.96 joris 1292: else if (ent->ce_status == CVS_ENT_ADDED)
1293: cfp->cf_cvstat = CVS_FST_ADDED;
1.67 joris 1294: else
1295: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1296: }
1.128 joris 1297:
1298: /* XXX assume 0644 ? */
1299: cfp->cf_mode = 0644;
1.14 jfb 1300: }
1.52 jfb 1301:
1.62 jfb 1302: if (ent != NULL) {
1303: /* steal the RCSNUM */
1304: cfp->cf_lrev = ent->ce_rev;
1.72 jfb 1305:
1.77 jfb 1306: if (ent->ce_type == CVS_ENT_FILE) {
1307: if (ent->ce_tag[0] != '\0') {
1.127 niallo 1308: cfp->cf_tag = strdup(ent->ce_tag);
1.77 jfb 1309: if (cfp->cf_tag == NULL) {
1310: cvs_file_free(cfp);
1311: return (NULL);
1312: }
1313: }
1314:
1315: if (ent->ce_opts[0] != '\0') {
1.127 niallo 1316: cfp->cf_opts = strdup(ent->ce_opts);
1.77 jfb 1317: if (cfp->cf_opts == NULL) {
1318: cvs_file_free(cfp);
1319: return (NULL);
1320: }
1.65 joris 1321: }
1.62 jfb 1322: }
1323: }
1.14 jfb 1324:
1.115 joris 1325: if (cfp->cf_type == DT_DIR) {
1326: if (cvs_load_dirinfo(cfp, flags) < 0) {
1327: cvs_file_free(cfp);
1328: return (NULL);
1329: }
1.14 jfb 1330: }
1.74 joris 1331:
1.128 joris 1332: if (flags & CF_REPO) {
1333: root = CVS_DIR_ROOT(cfp);
1334:
1335: cfp->cf_mode = 0644;
1336: cfp->cf_cvstat = CVS_FST_LOST;
1337:
1338: if ((c = strdup(cfp->cf_dir)) == NULL) {
1339: cvs_file_free(cfp);
1340: return (NULL);
1341: }
1342:
1343: free(cfp->cf_dir);
1344:
1345: if (strcmp(c, root->cr_dir)) {
1346: c += strlen(root->cr_dir) + 1;
1347: if ((cfp->cf_dir = strdup(c)) == NULL) {
1348: cvs_file_free(cfp);
1349: return (NULL);
1350: }
1351:
1352: c -= strlen(root->cr_dir) + 1;
1353: } else {
1354: cfp->cf_dir = NULL;
1355: }
1356:
1357: free(c);
1358: }
1359:
1.74 joris 1360: if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
1361: !strcmp(cfp->cf_repo, path))
1362: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14 jfb 1363:
1.100 joris 1364: /*
1365: * In server mode, we do a few extra checks.
1366: */
1367: if (cvs_cmdop == CVS_OP_SERVER) {
1368: /*
1369: * If for some reason a file was added,
1370: * but does not exist anymore, start complaining.
1371: */
1372: if (!(cfp->cf_flags & CVS_FILE_ONDISK) &&
1.124 reyk 1373: (cfp->cf_cvstat == CVS_FST_ADDED) &&
1.100 joris 1374: (cfp->cf_type != DT_DIR))
1375: cvs_log(LP_WARN, "new-born %s has disappeared", path);
1376:
1377: /*
1378: * Any other needed checks?
1379: */
1380: }
1381:
1.14 jfb 1382: return (cfp);
1.23 jfb 1383: }
1384:
1385:
1386: static int
1387: cvs_file_cmpname(const char *name1, const char *name2)
1388: {
1389: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1390: (strcasecmp(name1, name2));
1.88 joris 1391: }
1392:
1393: /*
1.100 joris 1394: * remove any empty directories.
1.88 joris 1395: */
1396: int
1397: cvs_file_prune(char *path)
1398: {
1399: DIR *dirp;
1400: int l, pwd, empty;
1401: struct dirent *dp;
1402: char fpath[MAXPATHLEN];
1403: CVSENTRIES *entf;
1.89 joris 1404: CVSFILE *cfp;
1.88 joris 1405:
1406: pwd = (!strcmp(path, "."));
1407:
1408: if ((dirp = opendir(path)) == NULL) {
1409: cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1410: return (-1);
1411: }
1412:
1413: empty = 0;
1414: entf = cvs_ent_open(path, O_RDWR);
1415:
1416: while ((dp = readdir(dirp)) != NULL) {
1417: if (!strcmp(dp->d_name, ".") ||
1418: !strcmp(dp->d_name, "..") ||
1419: !strcmp(dp->d_name, CVS_PATH_CVSDIR))
1420: continue;
1421:
1422: empty++;
1423: if (dp->d_type == DT_DIR) {
1424: l = snprintf(fpath, sizeof(fpath), "%s%s%s",
1425: (pwd) ? "" : path, (pwd) ? "" : "/", dp->d_name);
1426: if (l == -1 || l >= (int)sizeof(fpath)) {
1427: errno = ENAMETOOLONG;
1428: cvs_log(LP_ERRNO, "%s", fpath);
1429: continue;
1430: }
1.89 joris 1431:
1432: cfp = cvs_file_find(cvs_files, fpath);
1433: if (cfp == NULL)
1.91 joris 1434: continue;
1435:
1436: /* ignore unknown directories */
1437: if (cfp->cf_cvstat == CVS_FST_UNKNOWN)
1.89 joris 1438: continue;
1.88 joris 1439:
1440: if (cvs_file_prune(fpath)) {
1441: empty--;
1442: if (entf)
1.128 joris 1443: cvs_ent_remove(entf, fpath, 0);
1.88 joris 1444: } else {
1445: empty++;
1446: }
1447: }
1448: }
1449:
1450: closedir(dirp);
1451: if (entf)
1452: cvs_ent_close(entf);
1453:
1454: empty = (empty == 0);
1455: if (empty) {
1.106 xsa 1456: if (cvs_rmdir(path) < 0) {
1.88 joris 1457: cvs_log(LP_ERR, "failed to prune `%s'", path);
1458: empty = 0;
1459: }
1.93 joris 1460: }
1.88 joris 1461:
1462: return (empty);
1.14 jfb 1463: }