Annotation of src/usr.bin/cvs/file.c, Revision 1.84
1.84 ! joris 1: /* $OpenBSD: file.c,v 1.83 2005/05/31 08:58:48 xsa Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26 jfb 4: * All rights reserved.
1.1 jfb 5: *
1.26 jfb 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.26 jfb 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.26 jfb 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26 jfb 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28: #include <sys/queue.h>
29: #include <sys/stat.h>
30:
1.83 xsa 31: #include <dirent.h>
1.1 jfb 32: #include <errno.h>
33: #include <fcntl.h>
1.83 xsa 34: #include <fnmatch.h>
1.51 jfb 35: #include <libgen.h>
1.83 xsa 36: #include <pwd.h>
37: #include <stdio.h>
1.1 jfb 38: #include <stdlib.h>
1.83 xsa 39: #include <string.h>
1.1 jfb 40: #include <unistd.h>
41:
42: #include "cvs.h"
1.83 xsa 43: #include "file.h"
1.1 jfb 44: #include "log.h"
1.57 jfb 45: #include "strtab.h"
1.1 jfb 46:
47:
48: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
49:
50: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
51:
52:
53: /* ignore pattern */
54: struct cvs_ignpat {
55: char ip_pat[MAXNAMLEN];
56: int ip_flags;
57: TAILQ_ENTRY (cvs_ignpat) ip_list;
58: };
59:
60:
61: /*
62: * Standard patterns to ignore.
63: */
64: static const char *cvs_ign_std[] = {
65: ".",
66: "..",
67: "*.o",
68: "*.so",
1.45 xsa 69: "*.a",
1.1 jfb 70: "*.bak",
71: "*.orig",
72: "*.rej",
1.45 xsa 73: "*.old",
1.1 jfb 74: "*.exe",
75: "*.depend",
1.45 xsa 76: "*.obj",
77: "*.elc",
78: "*.ln",
79: "*.olb",
1.1 jfb 80: "CVS",
81: "core",
1.49 jfb 82: "*.core",
1.8 jfb 83: ".#*",
1.45 xsa 84: "*~",
85: "_$*",
86: "*$",
1.1 jfb 87: #ifdef OLD_SMELLY_CRUFT
88: "RCSLOG",
89: "tags",
90: "TAGS",
91: "RCS",
92: "SCCS",
1.45 xsa 93: "cvslog.*", /* to ignore CVS_CLIENT_LOG output */
1.1 jfb 94: "#*",
95: ",*",
96: #endif
97: };
98:
99:
1.11 jfb 100: /*
101: * Entries in the CVS/Entries file with a revision of '0' have only been
102: * added. Compare against this revision to see if this is the case
103: */
1.4 jfb 104: static RCSNUM *cvs_addedrev;
105:
106:
1.1 jfb 107: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
108:
109:
1.73 joris 110: static int cvs_file_getdir(CVSFILE *, int, char *, int (*)(CVSFILE *, void *), void *);
1.69 joris 111: static int cvs_load_dirinfo (CVSFILE *, int);
1.62 jfb 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);
116: static CVSFILE* cvs_file_lget (const char *, int, CVSFILE *, struct cvs_ent *);
1.3 jfb 117:
118:
119:
1.1 jfb 120: /*
121: * cvs_file_init()
122: *
123: */
124: int
125: cvs_file_init(void)
126: {
1.61 xsa 127: int i, l;
1.1 jfb 128: size_t len;
129: char path[MAXPATHLEN], buf[MAXNAMLEN];
130: FILE *ifp;
131: struct passwd *pwd;
132:
133: TAILQ_INIT(&cvs_ign_pats);
134:
1.53 jfb 135: if ((cvs_addedrev = rcsnum_parse("0")) == NULL)
1.46 jfb 136: return (-1);
1.4 jfb 137:
1.1 jfb 138: /* standard patterns to ignore */
1.10 jfb 139: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26 jfb 140: cvs_file_ignore(cvs_ign_std[i]);
1.1 jfb 141:
142: /* read the cvsignore file in the user's home directory, if any */
143: pwd = getpwuid(getuid());
144: if (pwd != NULL) {
1.61 xsa 145: l = snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
146: if (l == -1 || l >= (int)sizeof(path)) {
147: errno = ENAMETOOLONG;
148: cvs_log(LP_ERRNO, "%s", path);
149: return (-1);
150: }
151:
1.1 jfb 152: ifp = fopen(path, "r");
153: if (ifp == NULL) {
154: if (errno != ENOENT)
1.34 jfb 155: cvs_log(LP_ERRNO,
156: "failed to open user's cvsignore", path);
1.38 deraadt 157: } else {
1.1 jfb 158: while (fgets(buf, sizeof(buf), ifp) != NULL) {
159: len = strlen(buf);
160: if (len == 0)
161: continue;
162: if (buf[len - 1] != '\n') {
163: cvs_log(LP_ERR, "line too long in `%s'",
164: path);
165: }
166: buf[--len] = '\0';
167: cvs_file_ignore(buf);
168: }
169: (void)fclose(ifp);
170: }
171: }
172:
173: return (0);
174: }
175:
176:
177: /*
178: * cvs_file_ignore()
179: *
180: * Add the pattern <pat> to the list of patterns for files to ignore.
181: * Returns 0 on success, or -1 on failure.
182: */
183: int
184: cvs_file_ignore(const char *pat)
185: {
186: char *cp;
187: struct cvs_ignpat *ip;
188:
189: ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
190: if (ip == NULL) {
191: cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
192: return (-1);
193: }
194:
195: strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
196:
197: /* check if we will need globbing for that pattern */
198: ip->ip_flags = CVS_IGN_STATIC;
199: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
200: if (CVS_CHAR_ISMETA(*cp)) {
201: ip->ip_flags &= ~CVS_IGN_STATIC;
202: break;
203: }
204: }
205:
206: TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
207:
208: return (0);
209: }
210:
211:
212: /*
1.5 jfb 213: * cvs_file_chkign()
1.1 jfb 214: *
215: * Returns 1 if the filename <file> is matched by one of the ignore
216: * patterns, or 0 otherwise.
217: */
218: int
1.5 jfb 219: cvs_file_chkign(const char *file)
1.1 jfb 220: {
1.23 jfb 221: int flags;
1.1 jfb 222: struct cvs_ignpat *ip;
223:
1.23 jfb 224: flags = FNM_PERIOD;
225: if (cvs_nocase)
226: flags |= FNM_CASEFOLD;
227:
1.1 jfb 228: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
229: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 230: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 231: return (1);
1.38 deraadt 232: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 233: return (1);
234: }
235:
236: return (0);
237: }
238:
239:
240: /*
1.6 jfb 241: * cvs_file_create()
1.1 jfb 242: *
1.6 jfb 243: * Create a new file whose path is specified in <path> and of type <type>.
1.26 jfb 244: * If the type is DT_DIR, the CVS administrative repository and files will be
245: * created.
1.25 jfb 246: * Returns the created file on success, or NULL on failure.
1.1 jfb 247: */
1.6 jfb 248: CVSFILE*
1.34 jfb 249: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1 jfb 250: {
1.6 jfb 251: int fd;
1.84 ! joris 252: int bail;
1.34 jfb 253: char fp[MAXPATHLEN];
1.6 jfb 254: CVSFILE *cfp;
1.62 jfb 255: CVSENTRIES *ent;
1.1 jfb 256:
1.6 jfb 257: cfp = cvs_file_alloc(path, type);
258: if (cfp == NULL)
259: return (NULL);
1.26 jfb 260:
1.84 ! joris 261: bail = 0;
1.22 jfb 262: cfp->cf_mode = mode;
1.34 jfb 263: cfp->cf_parent = parent;
1.1 jfb 264:
1.34 jfb 265: if (type == DT_DIR) {
1.62 jfb 266: cfp->cf_root = cvsroot_get(path);
1.84 ! joris 267:
! 268: /*
! 269: * If we do not have a valid root for this, try looking at
! 270: * the parent its root.
! 271: */
! 272: if (cfp->cf_root == NULL) {
! 273: if (parent != NULL && parent->cf_root != NULL) {
! 274: cfp->cf_root =
! 275: cvsroot_parse(parent->cf_root->cr_str);
! 276: if (cfp->cf_root == NULL)
! 277: bail = 1;
! 278: } else {
! 279: bail = 1;
! 280: }
! 281: }
! 282:
! 283: /* we tried, too bad */
! 284: if (bail) {
! 285: cvs_log(LP_ERR, "failed to obtain root info for `%s'",
! 286: path);
! 287: return (NULL);
! 288: }
! 289:
! 290: cfp->cf_repo = strdup(cvs_file_getpath(cfp, fp, sizeof(fp)));
1.62 jfb 291: if (cfp->cf_repo == NULL) {
1.34 jfb 292: cvs_file_free(cfp);
293: return (NULL);
294: }
1.33 joris 295:
1.79 joris 296: if (((mkdir(path, mode) == -1) && (errno != EEXIST)) ||
1.78 joris 297: (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo) < 0)) {
1.6 jfb 298: cvs_file_free(cfp);
299: return (NULL);
300: }
1.26 jfb 301:
1.62 jfb 302: ent = cvs_ent_open(path, O_RDWR);
303: if (ent != NULL) {
304: cvs_ent_close(ent);
1.50 jfb 305: }
1.38 deraadt 306: } else {
1.6 jfb 307: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
308: if (fd == -1) {
309: cvs_file_free(cfp);
1.1 jfb 310: return (NULL);
311: }
1.6 jfb 312: (void)close(fd);
1.1 jfb 313: }
314:
1.6 jfb 315: return (cfp);
1.3 jfb 316: }
317:
318:
319: /*
1.35 jfb 320: * cvs_file_copy()
321: *
322: * Allocate space to create a copy of the file <orig>. The copy inherits all
323: * of the original's attributes, but does not inherit its children if the
324: * original file is a directory. Note that files copied using this mechanism
325: * are linked to their parent, but the parent has no link to the file. This
326: * is so cvs_file_getpath() works.
327: * Returns the copied file on success, or NULL on failure. The returned
328: * structure should be freed using cvs_file_free().
329: */
330: CVSFILE*
331: cvs_file_copy(CVSFILE *orig)
332: {
333: char path[MAXPATHLEN];
334: CVSFILE *cfp;
335:
336: cvs_file_getpath(orig, path, sizeof(path));
337:
338: cfp = cvs_file_alloc(path, orig->cf_type);
339: if (cfp == NULL)
340: return (NULL);
341:
342: cfp->cf_parent = orig->cf_parent;
343: cfp->cf_mode = orig->cf_mode;
344: cfp->cf_cvstat = orig->cf_cvstat;
345:
1.62 jfb 346: if (orig->cf_type == DT_REG)
347: cfp->cf_mtime = orig->cf_mtime;
348: else if (orig->cf_type == DT_DIR) {
1.35 jfb 349: /* XXX copy CVS directory attributes */
350: }
351:
352: return (cfp);
353: }
354:
355:
356: /*
1.3 jfb 357: * cvs_file_get()
358: *
359: * Load a cvs_file structure with all the information pertaining to the file
360: * <path>.
1.4 jfb 361: * The <flags> parameter specifies various flags that alter the behaviour of
1.21 jfb 362: * the function. The CF_RECURSE flag causes the function to recursively load
363: * subdirectories when <path> is a directory.
364: * The CF_SORT flag causes the files to be sorted in alphabetical order upon
365: * loading. The special case of "." as a path specification generates
366: * recursion for a single level and is equivalent to calling cvs_file_get() on
367: * all files of that directory.
1.3 jfb 368: * Returns a pointer to the cvs file structure, which must later be freed
369: * with cvs_file_free().
370: */
371:
1.14 jfb 372: CVSFILE*
1.73 joris 373: cvs_file_get(const char *path, int flags, int (*cb)(CVSFILE *, void *),
374: void *arg)
1.3 jfb 375: {
1.68 joris 376: char *files[1];
377:
378: files[0] = path;
1.73 joris 379: return cvs_file_getspec(files, 1, flags, cb, arg);
1.9 jfb 380: }
381:
382:
383: /*
384: * cvs_file_getspec()
385: *
386: * Load a specific set of files whose paths are given in the vector <fspec>,
387: * whose size is given in <fsn>.
388: * Returns a pointer to the lowest common subdirectory to all specified
389: * files.
390: */
391: CVSFILE*
1.73 joris 392: cvs_file_getspec(char **fspec, int fsn, int flags, int (*cb)(CVSFILE *, void *),
393: void *arg)
1.9 jfb 394: {
1.26 jfb 395: int i;
1.68 joris 396: int pwd;
1.26 jfb 397: char *sp, *np, pcopy[MAXPATHLEN];
1.68 joris 398: CVSFILE *base, *nf;
399: CVSENTRIES *entfile;
400: struct cvs_ent *ent;
1.26 jfb 401:
1.68 joris 402: entfile = cvs_ent_open(".", O_RDONLY);
403: base = cvs_file_lget(".", 0, NULL, NULL);
1.26 jfb 404: if (base == NULL)
405: return (NULL);
406:
1.73 joris 407: /* XXX - needed for some commands */
408: if (cb != NULL) {
409: if (cb(base, arg) != CVS_EX_OK) {
410: cvs_file_free(base);
411: return (NULL);
412: }
413: }
414:
1.26 jfb 415: for (i = 0; i < fsn; i++) {
416: strlcpy(pcopy, fspec[i], sizeof(pcopy));
417: sp = pcopy;
1.68 joris 418: pwd = (!strcmp(pcopy, "."));
1.26 jfb 419:
1.68 joris 420: np = strchr(sp, '/');
421: if (np != NULL)
422: *np = '\0';
423:
424: if (pwd) {
425: nf = base;
426: } else {
427: nf = cvs_file_find(base, pcopy);
1.26 jfb 428: if (nf == NULL) {
1.68 joris 429: if (entfile != NULL)
430: ent = cvs_ent_get(entfile, pcopy);
431: else
432: ent = NULL;
433: nf = cvs_file_lget(pcopy, 0, base, ent);
1.26 jfb 434: if (nf == NULL) {
435: cvs_file_free(base);
436: return (NULL);
437: }
438:
1.68 joris 439: if (cvs_file_attach(base, nf) < 0) {
1.59 joris 440: cvs_file_free(base);
441: return (NULL);
442: }
1.26 jfb 443: }
1.68 joris 444: }
1.26 jfb 445:
1.68 joris 446: if (nf->cf_type == DT_DIR) {
447: if (np != NULL)
448: *np++;
449:
1.73 joris 450: if (cvs_file_getdir(nf, flags, np, cb, arg) < 0) {
1.68 joris 451: cvs_file_free(base);
452: return (NULL);
1.26 jfb 453: }
1.73 joris 454: } else {
455: if (cb != NULL) {
1.75 joris 456: if (cb(nf, arg) != CVS_EX_OK) {
457: cvs_file_free(base);
1.73 joris 458: return (NULL);
1.75 joris 459: }
1.73 joris 460: }
1.68 joris 461: }
1.26 jfb 462: }
463:
464: return (base);
1.3 jfb 465: }
466:
467:
468: /*
1.13 jfb 469: * cvs_file_find()
470: *
471: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
472: * The file's pathname <path> must be relative to the base of <hier>.
473: * Returns the entry on success, or NULL on failure.
474: */
475: CVSFILE*
476: cvs_file_find(CVSFILE *hier, const char *path)
477: {
478: char *pp, *sp, pbuf[MAXPATHLEN];
479: CVSFILE *sf, *cf;
480:
481: strlcpy(pbuf, path, sizeof(pbuf));
482:
483: cf = hier;
484: pp = pbuf;
485: do {
486: sp = strchr(pp, '/');
487: if (sp != NULL)
1.24 jfb 488: *(sp++) = '\0';
1.13 jfb 489:
490: /* special case */
491: if (*pp == '.') {
492: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
493: /* request to go back to parent */
494: if (cf->cf_parent == NULL) {
495: cvs_log(LP_NOTICE,
496: "path %s goes back too far", path);
497: return (NULL);
498: }
499: cf = cf->cf_parent;
500: continue;
1.38 deraadt 501: } else if (*(pp + 1) == '\0')
1.13 jfb 502: continue;
503: }
504:
1.62 jfb 505: SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.34 jfb 506: if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13 jfb 507: break;
508: if (sf == NULL)
509: return (NULL);
510:
511: cf = sf;
512: pp = sp;
513: } while (sp != NULL);
514:
1.24 jfb 515: return (cf);
1.13 jfb 516: }
517:
518:
519: /*
1.34 jfb 520: * cvs_file_getpath()
521: *
522: * Get the full path of the file <file> and store it in <buf>, which is of
523: * size <len>. For portability, it is recommended that <buf> always be
524: * at least MAXPATHLEN bytes long.
525: * Returns a pointer to the start of the path on success, or NULL on failure.
526: */
527: char*
528: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
529: {
530: u_int i;
1.77 jfb 531: const char *fp, *namevec[CVS_FILE_MAXDEPTH];
1.34 jfb 532: CVSFILE *top;
533:
534: buf[0] = '\0';
535: i = CVS_FILE_MAXDEPTH;
536: memset(namevec, 0, sizeof(namevec));
537:
538: /* find the top node */
539: for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
540: fp = CVS_FILE_NAME(top);
541:
542: /* skip self-references */
543: if ((fp[0] == '.') && (fp[1] == '\0'))
544: continue;
545: namevec[--i] = fp;
546: }
547:
548: if (i == 0)
549: return (NULL);
550: else if (i == CVS_FILE_MAXDEPTH) {
551: strlcpy(buf, ".", len);
552: return (buf);
553: }
554:
555: while (i < CVS_FILE_MAXDEPTH - 1) {
556: strlcat(buf, namevec[i++], len);
557: strlcat(buf, "/", len);
558: }
559: strlcat(buf, namevec[i], len);
560:
561: return (buf);
562: }
563:
564:
565: /*
1.22 jfb 566: * cvs_file_attach()
567: *
568: * Attach the file <file> as one of the children of parent <parent>, which
569: * has to be a file of type DT_DIR.
570: * Returns 0 on success, or -1 on failure.
571: */
572: int
573: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
574: {
575: if (parent->cf_type != DT_DIR)
576: return (-1);
577:
1.62 jfb 578: SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22 jfb 579: file->cf_parent = parent;
580:
581: return (0);
582: }
583:
584:
585: /*
1.68 joris 586: * Load directory information
1.3 jfb 587: */
1.6 jfb 588: static int
1.69 joris 589: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3 jfb 590: {
1.68 joris 591: char fpath[MAXPATHLEN];
592: char pbuf[MAXPATHLEN];
1.20 jfb 593: struct stat st;
1.68 joris 594: int l;
1.7 jfb 595:
1.34 jfb 596: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.62 jfb 597: cf->cf_root = cvsroot_get(fpath);
598: if (cf->cf_root == NULL)
1.48 jfb 599: return (-1);
600:
1.74 joris 601: if (flags & CF_MKADMIN)
1.78 joris 602: cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL);
1.69 joris 603:
1.74 joris 604: /* if the CVS administrative directory exists, load the info */
605: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
606: if (l == -1 || l >= (int)sizeof(pbuf)) {
607: errno = ENAMETOOLONG;
608: cvs_log(LP_ERRNO, "%s", pbuf);
609: return (-1);
610: }
1.61 xsa 611:
1.74 joris 612: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
613: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
614: cf->cf_repo = strdup(pbuf);
615: if (cf->cf_repo == NULL) {
616: cvs_log(LP_ERRNO,
617: "failed to dup repository string");
618: return (-1);
1.20 jfb 619: }
1.68 joris 620: }
621: }
1.26 jfb 622:
1.68 joris 623: return (0);
624: }
625:
626: /*
627: * cvs_file_getdir()
628: *
629: * Get a cvs directory structure for the directory whose path is <dir>.
630: * This function should not free the directory information on error, as this
631: * is performed by cvs_file_free().
632: */
633: static int
1.73 joris 634: cvs_file_getdir(CVSFILE *cf, int flags, char *path, int (*cb)(CVSFILE *, void *), void *arg)
1.68 joris 635: {
636: int l;
637: int check_entry;
638: u_int ndirs, nfiles;
639: char *cur, *np;
640: char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
641: struct dirent *ent;
1.76 joris 642: CVSFILE *cfp;
1.68 joris 643: struct cvs_ent *cvsent;
644: struct cvs_flist dirs;
645: DIR *dirp;
646: CVSENTRIES *entfile;
647:
1.81 joris 648: check_entry = 1;
649: ndirs = nfiles = 0;
1.68 joris 650: SIMPLEQ_INIT(&dirs);
651:
652: cvs_file_getpath(cf, fpath, sizeof(fpath));
653: entfile = cvs_ent_open(fpath, O_RDONLY);
654:
655: cf->cf_root = cvsroot_get(fpath);
656: if (cf->cf_root == NULL)
657: return (-1);
658:
659: cur = np = NULL;
660: if (path != NULL) {
661: cur = strchr(path, '/');
662: if (cur != NULL) {
663: *cur = '\0';
664: np = cur + 1;
665: if (np != NULL && *np == '\0')
666: np = NULL;
1.20 jfb 667: }
668: }
1.14 jfb 669:
1.56 jfb 670: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 671: return (0);
672:
1.73 joris 673: /* callback for the directory entry */
674: if (cb != NULL) {
675: if (cb(cf, arg) != CVS_EX_OK)
676: return (-1);
677: }
678:
1.68 joris 679: dirp = opendir(fpath);
680: if (dirp == NULL) {
681: cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6 jfb 682: return (-1);
1.3 jfb 683: }
684:
1.68 joris 685: while ((ent = readdir(dirp)) != NULL) {
686: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
687: continue;
1.11 jfb 688:
1.68 joris 689: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
690: continue;
1.24 jfb 691:
1.68 joris 692: if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
693: if (entfile != NULL)
694: (void)cvs_ent_remove(entfile,
695: ent->d_name);
696: continue;
697: }
1.70 joris 698:
699: if ((ent->d_type != DT_DIR) && (flags & CF_NOFILES))
700: continue;
1.56 jfb 701:
1.68 joris 702: if (path != NULL) {
703: if (strcmp(path, ent->d_name))
1.56 jfb 704: continue;
1.68 joris 705: }
1.11 jfb 706:
1.68 joris 707: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
708: ent->d_name);
709: if (l == -1 || l >= (int)sizeof(pbuf)) {
710: errno = ENAMETOOLONG;
711: cvs_log(LP_ERRNO, "%s", pbuf);
712: closedir(dirp);
713: return (-1);
714: }
1.61 xsa 715:
1.68 joris 716: cfp = cvs_file_find(cf, ent->d_name);
717: if (cfp == NULL) {
1.62 jfb 718: if (entfile != NULL)
719: cvsent = cvs_ent_get(entfile, ent->d_name);
1.71 joris 720: else
721: cvsent = NULL;
722:
1.62 jfb 723: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68 joris 724:
1.55 jfb 725: if (cfp == NULL) {
1.68 joris 726: closedir(dirp);
1.55 jfb 727: return (-1);
728: }
1.62 jfb 729: if (entfile != NULL)
730: cvs_ent_remove(entfile, cfp->cf_name);
1.55 jfb 731:
1.68 joris 732: if (cfp->cf_type != DT_DIR) {
1.62 jfb 733: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.68 joris 734: cf_list);
1.62 jfb 735: nfiles++;
1.11 jfb 736: }
1.68 joris 737: } else {
738: cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4 jfb 739: }
1.14 jfb 740:
1.68 joris 741: if (cfp->cf_type == DT_DIR) {
742: ndirs++;
743: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.73 joris 744: } else {
745: /* callback for the file */
746: if (cb != NULL) {
747: if (cb(cfp, arg) != CVS_EX_OK) {
748: closedir(dirp);
749: return (-1);
750: }
751: }
1.68 joris 752: }
753:
754: if (path != NULL) {
755: check_entry = 0;
756: break;
757: }
758: }
759:
760: closedir(dirp);
761:
762: if (entfile != NULL && check_entry) {
1.62 jfb 763: while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68 joris 764: if (path != NULL) {
765: if (strcmp(cvsent->ce_name, path))
766: continue;
767: }
768:
1.61 xsa 769: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44 jfb 770: cvsent->ce_name);
1.61 xsa 771: if (l == -1 || l >= (int)sizeof(pbuf)) {
772: errno = ENAMETOOLONG;
773: cvs_log(LP_ERRNO, "%s", pbuf);
774: return (-1);
775: }
776:
1.68 joris 777: cfp = cvs_file_find(cf, cvsent->ce_name);
778: if (cfp == NULL) {
779: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
780: if (cfp == NULL)
781: continue;
782:
783: if (cfp->cf_type != DT_DIR) {
784: SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
785: cfp, cf_list);
1.62 jfb 786: nfiles++;
1.44 jfb 787: }
1.68 joris 788: } else {
789: cfp->cf_flags |= CVS_GDIR_IGNORE;
790: }
791:
792: if (cfp->cf_type == DT_DIR) {
793: ndirs++;
794: SIMPLEQ_INSERT_TAIL(&dirs, cfp,
795: cf_list);
1.73 joris 796: } else {
797: /* callback for the file */
798: if (cb != NULL) {
799: if (cb(cfp, arg) != CVS_EX_OK)
800: return (-1);
801: }
1.44 jfb 802: }
1.68 joris 803:
804: if (path != NULL)
805: break;
1.44 jfb 806: }
1.62 jfb 807: cvs_ent_close(entfile);
1.40 jfb 808: }
1.34 jfb 809:
1.10 jfb 810: if (flags & CF_SORT) {
1.68 joris 811: if (nfiles > 0)
812: cvs_file_sort(&(cf->cf_files), nfiles);
813: if (ndirs > 0)
814: cvs_file_sort(&dirs, ndirs);
1.10 jfb 815: }
1.26 jfb 816:
1.62 jfb 817: while (!SIMPLEQ_EMPTY(&dirs)) {
818: cfp = SIMPLEQ_FIRST(&dirs);
819: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 820:
821: if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
822: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
823: else
824: cfp->cf_flags &= ~CVS_GDIR_IGNORE;
825:
1.73 joris 826: if (cvs_file_getdir(cfp, flags, np, cb, arg) < 0) {
1.82 xsa 827: cvs_log(LP_ERR, "failed to get %s", CVS_FILE_NAME(cfp));
1.68 joris 828: continue;
829: }
1.26 jfb 830: }
1.3 jfb 831:
1.6 jfb 832: return (0);
1.3 jfb 833: }
834:
835:
836: /*
837: * cvs_file_free()
838: *
839: * Free a cvs_file structure and its contents.
840: */
841: void
1.14 jfb 842: cvs_file_free(CVSFILE *cf)
1.3 jfb 843: {
1.62 jfb 844: CVSFILE *child;
845:
1.47 jfb 846: if (cf->cf_name != NULL)
1.57 jfb 847: cvs_strfree(cf->cf_name);
1.62 jfb 848:
849: if (cf->cf_type == DT_DIR) {
850: if (cf->cf_root != NULL)
851: cvsroot_free(cf->cf_root);
852: if (cf->cf_repo != NULL)
853: free(cf->cf_repo);
854: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
855: child = SIMPLEQ_FIRST(&(cf->cf_files));
856: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
857: cvs_file_free(child);
858: }
1.64 joris 859: } else {
860: if (cf->cf_tag != NULL)
861: cvs_strfree(cf->cf_tag);
1.77 jfb 862: if (cf->cf_opts != NULL)
863: cvs_strfree(cf->cf_opts);
1.62 jfb 864: }
1.64 joris 865:
1.3 jfb 866: free(cf);
1.5 jfb 867: }
868:
869:
870: /*
871: * cvs_file_examine()
872: *
873: * Examine the contents of the CVS file structure <cf> with the function
874: * <exam>. The function is called for all subdirectories and files of the
875: * root file.
876: */
877: int
878: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
879: {
880: int ret;
1.14 jfb 881: CVSFILE *fp;
1.5 jfb 882:
883: if (cf->cf_type == DT_DIR) {
884: ret = (*exam)(cf, arg);
1.62 jfb 885: SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5 jfb 886: ret = cvs_file_examine(fp, exam, arg);
1.60 joris 887: if (ret != 0)
1.13 jfb 888: break;
1.5 jfb 889: }
1.38 deraadt 890: } else
1.13 jfb 891: ret = (*exam)(cf, arg);
892:
893: return (ret);
1.3 jfb 894: }
895:
896: /*
897: * cvs_file_sort()
898: *
1.16 jfb 899: * Sort a list of cvs file structures according to their filename. The list
900: * <flp> is modified according to the sorting algorithm. The number of files
901: * in the list must be given by <nfiles>.
902: * Returns 0 on success, or -1 on failure.
1.3 jfb 903: */
904: static int
1.16 jfb 905: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 906: {
907: int i;
908: size_t nb;
1.16 jfb 909: CVSFILE *cf, **cfvec;
910:
911: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
912: if (cfvec == NULL) {
913: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
914: return (-1);
915: }
1.3 jfb 916:
917: i = 0;
1.62 jfb 918: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 919: if (i == (int)nfiles) {
1.3 jfb 920: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 921: /* rebuild the list and abort sorting */
922: while (--i >= 0)
1.62 jfb 923: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16 jfb 924: free(cfvec);
1.3 jfb 925: return (-1);
926: }
1.16 jfb 927: cfvec[i++] = cf;
1.3 jfb 928:
929: /* now unlink it from the list,
930: * we'll put it back in order later
931: */
1.62 jfb 932: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 933: }
934:
935: /* clear the list just in case */
1.62 jfb 936: SIMPLEQ_INIT(flp);
1.3 jfb 937: nb = (size_t)i;
938:
939: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
940:
941: /* rebuild the list from the bottom up */
942: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 943: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 944:
1.16 jfb 945: free(cfvec);
1.3 jfb 946: return (0);
947: }
948:
949:
950: static int
951: cvs_file_cmp(const void *f1, const void *f2)
952: {
1.41 jfb 953: const CVSFILE *cf1, *cf2;
1.62 jfb 954: cf1 = *(CVSFILE * const *)f1;
955: cf2 = *(CVSFILE * const *)f2;
1.34 jfb 956: return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6 jfb 957: }
958:
959:
1.34 jfb 960: /*
961: * cvs_file_alloc()
962: *
963: * Allocate a CVSFILE structure and initialize its internals.
964: */
1.6 jfb 965: CVSFILE*
966: cvs_file_alloc(const char *path, u_int type)
967: {
968: CVSFILE *cfp;
969:
1.14 jfb 970: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 971: if (cfp == NULL) {
972: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
973: return (NULL);
974: }
975: memset(cfp, 0, sizeof(*cfp));
976:
1.62 jfb 977: cfp->cf_type = type;
978: cfp->cf_cvstat = CVS_FST_UNKNOWN;
979:
980: if (type == DT_DIR) {
981: SIMPLEQ_INIT(&(cfp->cf_files));
982: }
983:
1.57 jfb 984: cfp->cf_name = cvs_strdup(basename(path));
1.34 jfb 985: if (cfp->cf_name == NULL) {
1.57 jfb 986: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 987: cvs_file_free(cfp);
1.6 jfb 988: return (NULL);
989: }
990:
991: return (cfp);
1.1 jfb 992: }
1.14 jfb 993:
994:
995: /*
996: * cvs_file_lget()
997: *
998: * Get the file and link it with the parent right away.
1.22 jfb 999: * Returns a pointer to the created file structure on success, or NULL on
1000: * failure.
1.14 jfb 1001: */
1002: static CVSFILE*
1.62 jfb 1003: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14 jfb 1004: {
1.44 jfb 1005: int ret, cwd;
1006: u_int type;
1.14 jfb 1007: struct stat st;
1008: CVSFILE *cfp;
1009:
1.44 jfb 1010: type = DT_UNKNOWN;
1011: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62 jfb 1012:
1.44 jfb 1013: ret = stat(path, &st);
1014: if (ret == 0)
1015: type = IFTODT(st.st_mode);
1.14 jfb 1016:
1.44 jfb 1017: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 1018: return (NULL);
1019: cfp->cf_parent = parent;
1.54 joris 1020:
1021: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 1022: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 1023:
1.44 jfb 1024: if (ret == 0) {
1025: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 1026: if (cfp->cf_type == DT_REG)
1027: cfp->cf_mtime = st.st_mtime;
1.44 jfb 1028:
1029: if (ent == NULL)
1030: cfp->cf_cvstat = (cwd == 1) ?
1031: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14 jfb 1032: else {
1.44 jfb 1033: /* always show directories as up-to-date */
1034: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 1035: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 1036: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1037: cfp->cf_cvstat = CVS_FST_ADDED;
1038: else {
1039: /* check last modified time */
1.80 jfb 1040: if (ent->ce_mtime == (time_t)st.st_mtime)
1.44 jfb 1041: cfp->cf_cvstat = CVS_FST_UPTODATE;
1042: else
1043: cfp->cf_cvstat = CVS_FST_MODIFIED;
1044: }
1.14 jfb 1045: }
1.44 jfb 1046: } else {
1047: if (ent == NULL) {
1048: cvs_log(LP_ERR, "no Entry and no file for `%s'",
1049: CVS_FILE_NAME(cfp));
1050: cvs_file_free(cfp);
1051: return (NULL);
1.66 joris 1052: } else {
1053: if (ent->ce_type == CVS_ENT_FILE)
1054: cfp->cf_type = DT_REG;
1055: else if (ent->ce_type == CVS_ENT_DIR)
1056: cfp->cf_type = DT_DIR;
1057: else
1058: cvs_log(LP_WARN, "unknown ce_type %d",
1059: ent->ce_type);
1.67 joris 1060:
1061: if (ent->ce_status == CVS_ENT_REMOVED)
1062: cfp->cf_cvstat = CVS_FST_REMOVED;
1063: else
1064: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1065: }
1.14 jfb 1066: }
1.52 jfb 1067:
1.62 jfb 1068: if (ent != NULL) {
1069: /* steal the RCSNUM */
1070: cfp->cf_lrev = ent->ce_rev;
1.72 jfb 1071: ent->ce_rev = NULL;
1072:
1.77 jfb 1073: if (ent->ce_type == CVS_ENT_FILE) {
1074: if (ent->ce_tag[0] != '\0') {
1075: cfp->cf_tag = cvs_strdup(ent->ce_tag);
1076: if (cfp->cf_tag == NULL) {
1077: cvs_file_free(cfp);
1078: return (NULL);
1079: }
1080: }
1081:
1082: if (ent->ce_opts[0] != '\0') {
1083: cfp->cf_opts = cvs_strdup(ent->ce_opts);
1084: if (cfp->cf_opts == NULL) {
1085: cvs_file_free(cfp);
1086: return (NULL);
1087: }
1.65 joris 1088: }
1.62 jfb 1089: }
1090: }
1.14 jfb 1091:
1.69 joris 1092: if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26 jfb 1093: cvs_file_free(cfp);
1094: return (NULL);
1.14 jfb 1095: }
1.74 joris 1096:
1097: if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
1098: !strcmp(cfp->cf_repo, path))
1099: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14 jfb 1100:
1101: return (cfp);
1.23 jfb 1102: }
1103:
1104:
1105: static int
1106: cvs_file_cmpname(const char *name1, const char *name2)
1107: {
1108: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1109: (strcasecmp(name1, name2));
1.14 jfb 1110: }