Annotation of src/usr.bin/cvs/file.c, Revision 1.79
1.79 ! joris 1: /* $OpenBSD: file.c,v 1.78 2005/05/24 20:04:43 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:
31: #include <pwd.h>
32: #include <errno.h>
33: #include <stdio.h>
34: #include <fcntl.h>
1.51 jfb 35: #include <libgen.h>
1.1 jfb 36: #include <dirent.h>
37: #include <stdlib.h>
38: #include <unistd.h>
39: #include <string.h>
40: #include <fnmatch.h>
41:
42: #include "cvs.h"
43: #include "log.h"
1.14 jfb 44: #include "file.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.34 jfb 252: char fp[MAXPATHLEN];
1.6 jfb 253: CVSFILE *cfp;
1.62 jfb 254: CVSENTRIES *ent;
1.1 jfb 255:
1.6 jfb 256: cfp = cvs_file_alloc(path, type);
257: if (cfp == NULL)
258: return (NULL);
1.26 jfb 259:
1.22 jfb 260: cfp->cf_mode = mode;
1.34 jfb 261: cfp->cf_parent = parent;
1.1 jfb 262:
1.34 jfb 263: if (type == DT_DIR) {
1.62 jfb 264: cfp->cf_root = cvsroot_get(path);
265: cfp->cf_repo = strdup(cvs_file_getpath(cfp,
1.34 jfb 266: fp, sizeof(fp)));
1.62 jfb 267: if (cfp->cf_repo == NULL) {
1.34 jfb 268: cvs_file_free(cfp);
269: return (NULL);
270: }
1.33 joris 271:
1.79 ! joris 272: if (((mkdir(path, mode) == -1) && (errno != EEXIST)) ||
1.78 joris 273: (cvs_mkadmin(path, cfp->cf_root->cr_str, cfp->cf_repo) < 0)) {
1.6 jfb 274: cvs_file_free(cfp);
275: return (NULL);
276: }
1.26 jfb 277:
1.62 jfb 278: ent = cvs_ent_open(path, O_RDWR);
279: if (ent != NULL) {
280: cvs_ent_close(ent);
1.50 jfb 281: }
1.38 deraadt 282: } else {
1.6 jfb 283: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
284: if (fd == -1) {
285: cvs_file_free(cfp);
1.1 jfb 286: return (NULL);
287: }
1.6 jfb 288: (void)close(fd);
1.1 jfb 289: }
290:
1.6 jfb 291: return (cfp);
1.3 jfb 292: }
293:
294:
295: /*
1.35 jfb 296: * cvs_file_copy()
297: *
298: * Allocate space to create a copy of the file <orig>. The copy inherits all
299: * of the original's attributes, but does not inherit its children if the
300: * original file is a directory. Note that files copied using this mechanism
301: * are linked to their parent, but the parent has no link to the file. This
302: * is so cvs_file_getpath() works.
303: * Returns the copied file on success, or NULL on failure. The returned
304: * structure should be freed using cvs_file_free().
305: */
306: CVSFILE*
307: cvs_file_copy(CVSFILE *orig)
308: {
309: char path[MAXPATHLEN];
310: CVSFILE *cfp;
311:
312: cvs_file_getpath(orig, path, sizeof(path));
313:
314: cfp = cvs_file_alloc(path, orig->cf_type);
315: if (cfp == NULL)
316: return (NULL);
317:
318: cfp->cf_parent = orig->cf_parent;
319: cfp->cf_mode = orig->cf_mode;
320: cfp->cf_cvstat = orig->cf_cvstat;
321:
1.62 jfb 322: if (orig->cf_type == DT_REG)
323: cfp->cf_mtime = orig->cf_mtime;
324: else if (orig->cf_type == DT_DIR) {
1.35 jfb 325: /* XXX copy CVS directory attributes */
326: }
327:
328: return (cfp);
329: }
330:
331:
332: /*
1.3 jfb 333: * cvs_file_get()
334: *
335: * Load a cvs_file structure with all the information pertaining to the file
336: * <path>.
1.4 jfb 337: * The <flags> parameter specifies various flags that alter the behaviour of
1.21 jfb 338: * the function. The CF_RECURSE flag causes the function to recursively load
339: * subdirectories when <path> is a directory.
340: * The CF_SORT flag causes the files to be sorted in alphabetical order upon
341: * loading. The special case of "." as a path specification generates
342: * recursion for a single level and is equivalent to calling cvs_file_get() on
343: * all files of that directory.
1.3 jfb 344: * Returns a pointer to the cvs file structure, which must later be freed
345: * with cvs_file_free().
346: */
347:
1.14 jfb 348: CVSFILE*
1.73 joris 349: cvs_file_get(const char *path, int flags, int (*cb)(CVSFILE *, void *),
350: void *arg)
1.3 jfb 351: {
1.68 joris 352: char *files[1];
353:
354: files[0] = path;
1.73 joris 355: return cvs_file_getspec(files, 1, flags, cb, arg);
1.9 jfb 356: }
357:
358:
359: /*
360: * cvs_file_getspec()
361: *
362: * Load a specific set of files whose paths are given in the vector <fspec>,
363: * whose size is given in <fsn>.
364: * Returns a pointer to the lowest common subdirectory to all specified
365: * files.
366: */
367: CVSFILE*
1.73 joris 368: cvs_file_getspec(char **fspec, int fsn, int flags, int (*cb)(CVSFILE *, void *),
369: void *arg)
1.9 jfb 370: {
1.26 jfb 371: int i;
1.68 joris 372: int pwd;
1.26 jfb 373: char *sp, *np, pcopy[MAXPATHLEN];
1.68 joris 374: CVSFILE *base, *nf;
375: CVSENTRIES *entfile;
376: struct cvs_ent *ent;
1.26 jfb 377:
1.68 joris 378: entfile = cvs_ent_open(".", O_RDONLY);
379: base = cvs_file_lget(".", 0, NULL, NULL);
1.26 jfb 380: if (base == NULL)
381: return (NULL);
382:
1.73 joris 383: /* XXX - needed for some commands */
384: if (cb != NULL) {
385: if (cb(base, arg) != CVS_EX_OK) {
386: cvs_file_free(base);
387: return (NULL);
388: }
389: }
390:
1.26 jfb 391: for (i = 0; i < fsn; i++) {
392: strlcpy(pcopy, fspec[i], sizeof(pcopy));
393: sp = pcopy;
1.68 joris 394: pwd = (!strcmp(pcopy, "."));
1.26 jfb 395:
1.68 joris 396: np = strchr(sp, '/');
397: if (np != NULL)
398: *np = '\0';
399:
400: if (pwd) {
401: nf = base;
402: } else {
403: nf = cvs_file_find(base, pcopy);
1.26 jfb 404: if (nf == NULL) {
1.68 joris 405: if (entfile != NULL)
406: ent = cvs_ent_get(entfile, pcopy);
407: else
408: ent = NULL;
409: nf = cvs_file_lget(pcopy, 0, base, ent);
1.26 jfb 410: if (nf == NULL) {
411: cvs_file_free(base);
412: return (NULL);
413: }
414:
1.68 joris 415: if (cvs_file_attach(base, nf) < 0) {
1.59 joris 416: cvs_file_free(base);
417: return (NULL);
418: }
1.26 jfb 419: }
1.68 joris 420: }
1.26 jfb 421:
1.68 joris 422: if (nf->cf_type == DT_DIR) {
423: if (np != NULL)
424: *np++;
425:
1.73 joris 426: if (cvs_file_getdir(nf, flags, np, cb, arg) < 0) {
1.68 joris 427: cvs_file_free(base);
428: return (NULL);
1.26 jfb 429: }
1.73 joris 430: } else {
431: if (cb != NULL) {
1.75 joris 432: if (cb(nf, arg) != CVS_EX_OK) {
433: cvs_file_free(base);
1.73 joris 434: return (NULL);
1.75 joris 435: }
1.73 joris 436: }
1.68 joris 437: }
1.26 jfb 438: }
439:
440: return (base);
1.3 jfb 441: }
442:
443:
444: /*
1.13 jfb 445: * cvs_file_find()
446: *
447: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
448: * The file's pathname <path> must be relative to the base of <hier>.
449: * Returns the entry on success, or NULL on failure.
450: */
451: CVSFILE*
452: cvs_file_find(CVSFILE *hier, const char *path)
453: {
454: char *pp, *sp, pbuf[MAXPATHLEN];
455: CVSFILE *sf, *cf;
456:
457: strlcpy(pbuf, path, sizeof(pbuf));
458:
459: cf = hier;
460: pp = pbuf;
461: do {
462: sp = strchr(pp, '/');
463: if (sp != NULL)
1.24 jfb 464: *(sp++) = '\0';
1.13 jfb 465:
466: /* special case */
467: if (*pp == '.') {
468: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
469: /* request to go back to parent */
470: if (cf->cf_parent == NULL) {
471: cvs_log(LP_NOTICE,
472: "path %s goes back too far", path);
473: return (NULL);
474: }
475: cf = cf->cf_parent;
476: continue;
1.38 deraadt 477: } else if (*(pp + 1) == '\0')
1.13 jfb 478: continue;
479: }
480:
1.62 jfb 481: SIMPLEQ_FOREACH(sf, &(cf->cf_files), cf_list)
1.34 jfb 482: if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13 jfb 483: break;
484: if (sf == NULL)
485: return (NULL);
486:
487: cf = sf;
488: pp = sp;
489: } while (sp != NULL);
490:
1.24 jfb 491: return (cf);
1.13 jfb 492: }
493:
494:
495: /*
1.34 jfb 496: * cvs_file_getpath()
497: *
498: * Get the full path of the file <file> and store it in <buf>, which is of
499: * size <len>. For portability, it is recommended that <buf> always be
500: * at least MAXPATHLEN bytes long.
501: * Returns a pointer to the start of the path on success, or NULL on failure.
502: */
503: char*
504: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
505: {
506: u_int i;
1.77 jfb 507: const char *fp, *namevec[CVS_FILE_MAXDEPTH];
1.34 jfb 508: CVSFILE *top;
509:
510: buf[0] = '\0';
511: i = CVS_FILE_MAXDEPTH;
512: memset(namevec, 0, sizeof(namevec));
513:
514: /* find the top node */
515: for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
516: fp = CVS_FILE_NAME(top);
517:
518: /* skip self-references */
519: if ((fp[0] == '.') && (fp[1] == '\0'))
520: continue;
521: namevec[--i] = fp;
522: }
523:
524: if (i == 0)
525: return (NULL);
526: else if (i == CVS_FILE_MAXDEPTH) {
527: strlcpy(buf, ".", len);
528: return (buf);
529: }
530:
531: while (i < CVS_FILE_MAXDEPTH - 1) {
532: strlcat(buf, namevec[i++], len);
533: strlcat(buf, "/", len);
534: }
535: strlcat(buf, namevec[i], len);
536:
537: return (buf);
538: }
539:
540:
541: /*
1.22 jfb 542: * cvs_file_attach()
543: *
544: * Attach the file <file> as one of the children of parent <parent>, which
545: * has to be a file of type DT_DIR.
546: * Returns 0 on success, or -1 on failure.
547: */
548: int
549: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
550: {
551: if (parent->cf_type != DT_DIR)
552: return (-1);
553:
1.62 jfb 554: SIMPLEQ_INSERT_TAIL(&(parent->cf_files), file, cf_list);
1.22 jfb 555: file->cf_parent = parent;
556:
557: return (0);
558: }
559:
560:
561: /*
1.68 joris 562: * Load directory information
1.3 jfb 563: */
1.6 jfb 564: static int
1.69 joris 565: cvs_load_dirinfo(CVSFILE *cf, int flags)
1.3 jfb 566: {
1.68 joris 567: char fpath[MAXPATHLEN];
568: char pbuf[MAXPATHLEN];
1.20 jfb 569: struct stat st;
1.68 joris 570: int l;
1.7 jfb 571:
1.34 jfb 572: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.62 jfb 573: cf->cf_root = cvsroot_get(fpath);
574: if (cf->cf_root == NULL)
1.48 jfb 575: return (-1);
576:
1.74 joris 577: if (flags & CF_MKADMIN)
1.78 joris 578: cvs_mkadmin(fpath, cf->cf_root->cr_str, NULL);
1.69 joris 579:
1.74 joris 580: /* if the CVS administrative directory exists, load the info */
581: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
582: if (l == -1 || l >= (int)sizeof(pbuf)) {
583: errno = ENAMETOOLONG;
584: cvs_log(LP_ERRNO, "%s", pbuf);
585: return (-1);
586: }
1.61 xsa 587:
1.74 joris 588: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
589: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
590: cf->cf_repo = strdup(pbuf);
591: if (cf->cf_repo == NULL) {
592: cvs_log(LP_ERRNO,
593: "failed to dup repository string");
594: return (-1);
1.20 jfb 595: }
1.68 joris 596: }
597: }
1.26 jfb 598:
1.68 joris 599: return (0);
600: }
601:
602: /*
603: * cvs_file_getdir()
604: *
605: * Get a cvs directory structure for the directory whose path is <dir>.
606: * This function should not free the directory information on error, as this
607: * is performed by cvs_file_free().
608: */
609: static int
1.73 joris 610: cvs_file_getdir(CVSFILE *cf, int flags, char *path, int (*cb)(CVSFILE *, void *), void *arg)
1.68 joris 611: {
612: int l;
613: int check_entry;
614: u_int ndirs, nfiles;
615: char *cur, *np;
616: char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
617: struct dirent *ent;
1.76 joris 618: CVSFILE *cfp;
1.68 joris 619: struct cvs_ent *cvsent;
620: struct cvs_flist dirs;
621: DIR *dirp;
622: CVSENTRIES *entfile;
623:
624: check_entry = ndirs = nfiles = 0;
625: SIMPLEQ_INIT(&dirs);
626:
627: cvs_file_getpath(cf, fpath, sizeof(fpath));
628: entfile = cvs_ent_open(fpath, O_RDONLY);
629:
630: cf->cf_root = cvsroot_get(fpath);
631: if (cf->cf_root == NULL)
632: return (-1);
633:
634: cur = np = NULL;
635: if (path != NULL) {
636: cur = strchr(path, '/');
637: if (cur != NULL) {
638: *cur = '\0';
639: np = cur + 1;
640: if (np != NULL && *np == '\0')
641: np = NULL;
1.20 jfb 642: }
643: }
1.14 jfb 644:
1.56 jfb 645: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 646: return (0);
647:
1.73 joris 648: /* callback for the directory entry */
649: if (cb != NULL) {
650: if (cb(cf, arg) != CVS_EX_OK)
651: return (-1);
652: }
653:
1.68 joris 654: dirp = opendir(fpath);
655: if (dirp == NULL) {
656: cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6 jfb 657: return (-1);
1.3 jfb 658: }
659:
1.68 joris 660: while ((ent = readdir(dirp)) != NULL) {
661: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
662: continue;
1.11 jfb 663:
1.68 joris 664: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
665: continue;
1.24 jfb 666:
1.68 joris 667: if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
668: if (entfile != NULL)
669: (void)cvs_ent_remove(entfile,
670: ent->d_name);
671: continue;
672: }
1.70 joris 673:
674: if ((ent->d_type != DT_DIR) && (flags & CF_NOFILES))
675: continue;
1.56 jfb 676:
1.68 joris 677: if (path != NULL) {
678: if (strcmp(path, ent->d_name))
1.56 jfb 679: continue;
1.68 joris 680: }
1.11 jfb 681:
1.68 joris 682: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
683: ent->d_name);
684: if (l == -1 || l >= (int)sizeof(pbuf)) {
685: errno = ENAMETOOLONG;
686: cvs_log(LP_ERRNO, "%s", pbuf);
687: closedir(dirp);
688: return (-1);
689: }
1.61 xsa 690:
1.68 joris 691: cfp = cvs_file_find(cf, ent->d_name);
692: if (cfp == NULL) {
1.62 jfb 693: if (entfile != NULL)
694: cvsent = cvs_ent_get(entfile, ent->d_name);
1.71 joris 695: else
696: cvsent = NULL;
697:
1.62 jfb 698: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68 joris 699:
1.55 jfb 700: if (cfp == NULL) {
1.68 joris 701: closedir(dirp);
1.55 jfb 702: return (-1);
703: }
1.62 jfb 704: if (entfile != NULL)
705: cvs_ent_remove(entfile, cfp->cf_name);
1.55 jfb 706:
1.68 joris 707: if (cfp->cf_type != DT_DIR) {
1.62 jfb 708: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.68 joris 709: cf_list);
1.62 jfb 710: nfiles++;
1.11 jfb 711: }
1.68 joris 712: } else {
713: cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4 jfb 714: }
1.14 jfb 715:
1.68 joris 716: if (cfp->cf_type == DT_DIR) {
717: ndirs++;
718: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.73 joris 719: } else {
720: /* callback for the file */
721: if (cb != NULL) {
722: if (cb(cfp, arg) != CVS_EX_OK) {
723: closedir(dirp);
724: return (-1);
725: }
726: }
1.68 joris 727: }
728:
729: if (path != NULL) {
730: check_entry = 0;
731: break;
732: }
733: }
734:
735: closedir(dirp);
736:
737: if (entfile != NULL && check_entry) {
1.62 jfb 738: while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68 joris 739: if (path != NULL) {
740: if (strcmp(cvsent->ce_name, path))
741: continue;
742: }
743:
1.61 xsa 744: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44 jfb 745: cvsent->ce_name);
1.61 xsa 746: if (l == -1 || l >= (int)sizeof(pbuf)) {
747: errno = ENAMETOOLONG;
748: cvs_log(LP_ERRNO, "%s", pbuf);
749: return (-1);
750: }
751:
1.68 joris 752: cfp = cvs_file_find(cf, cvsent->ce_name);
753: if (cfp == NULL) {
754: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
755: if (cfp == NULL)
756: continue;
757:
758: if (cfp->cf_type != DT_DIR) {
759: SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
760: cfp, cf_list);
1.62 jfb 761: nfiles++;
1.44 jfb 762: }
1.68 joris 763: } else {
764: cfp->cf_flags |= CVS_GDIR_IGNORE;
765: }
766:
767: if (cfp->cf_type == DT_DIR) {
768: ndirs++;
769: SIMPLEQ_INSERT_TAIL(&dirs, cfp,
770: cf_list);
1.73 joris 771: } else {
772: /* callback for the file */
773: if (cb != NULL) {
774: if (cb(cfp, arg) != CVS_EX_OK)
775: return (-1);
776: }
1.44 jfb 777: }
1.68 joris 778:
779: if (path != NULL)
780: break;
1.44 jfb 781: }
1.62 jfb 782: cvs_ent_close(entfile);
1.40 jfb 783: }
1.34 jfb 784:
1.10 jfb 785: if (flags & CF_SORT) {
1.68 joris 786: if (nfiles > 0)
787: cvs_file_sort(&(cf->cf_files), nfiles);
788: if (ndirs > 0)
789: cvs_file_sort(&dirs, ndirs);
1.10 jfb 790: }
1.26 jfb 791:
1.62 jfb 792: while (!SIMPLEQ_EMPTY(&dirs)) {
793: cfp = SIMPLEQ_FIRST(&dirs);
794: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 795:
796: if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
797: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
798: else
799: cfp->cf_flags &= ~CVS_GDIR_IGNORE;
800:
1.73 joris 801: if (cvs_file_getdir(cfp, flags, np, cb, arg) < 0) {
1.68 joris 802: cvs_log(LP_ERROR, "failed to get %s",
803: CVS_FILE_NAME(cfp));
804: continue;
805: }
1.26 jfb 806: }
1.3 jfb 807:
1.6 jfb 808: return (0);
1.3 jfb 809: }
810:
811:
812: /*
813: * cvs_file_free()
814: *
815: * Free a cvs_file structure and its contents.
816: */
817: void
1.14 jfb 818: cvs_file_free(CVSFILE *cf)
1.3 jfb 819: {
1.62 jfb 820: CVSFILE *child;
821:
1.47 jfb 822: if (cf->cf_name != NULL)
1.57 jfb 823: cvs_strfree(cf->cf_name);
1.62 jfb 824:
825: if (cf->cf_type == DT_DIR) {
826: if (cf->cf_root != NULL)
827: cvsroot_free(cf->cf_root);
828: if (cf->cf_repo != NULL)
829: free(cf->cf_repo);
830: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
831: child = SIMPLEQ_FIRST(&(cf->cf_files));
832: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
833: cvs_file_free(child);
834: }
1.64 joris 835: } else {
836: if (cf->cf_tag != NULL)
837: cvs_strfree(cf->cf_tag);
1.77 jfb 838: if (cf->cf_opts != NULL)
839: cvs_strfree(cf->cf_opts);
1.62 jfb 840: }
1.64 joris 841:
1.3 jfb 842: free(cf);
1.5 jfb 843: }
844:
845:
846: /*
847: * cvs_file_examine()
848: *
849: * Examine the contents of the CVS file structure <cf> with the function
850: * <exam>. The function is called for all subdirectories and files of the
851: * root file.
852: */
853: int
854: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
855: {
856: int ret;
1.14 jfb 857: CVSFILE *fp;
1.5 jfb 858:
859: if (cf->cf_type == DT_DIR) {
860: ret = (*exam)(cf, arg);
1.62 jfb 861: SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5 jfb 862: ret = cvs_file_examine(fp, exam, arg);
1.60 joris 863: if (ret != 0)
1.13 jfb 864: break;
1.5 jfb 865: }
1.38 deraadt 866: } else
1.13 jfb 867: ret = (*exam)(cf, arg);
868:
869: return (ret);
1.3 jfb 870: }
871:
872: /*
873: * cvs_file_sort()
874: *
1.16 jfb 875: * Sort a list of cvs file structures according to their filename. The list
876: * <flp> is modified according to the sorting algorithm. The number of files
877: * in the list must be given by <nfiles>.
878: * Returns 0 on success, or -1 on failure.
1.3 jfb 879: */
880: static int
1.16 jfb 881: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 882: {
883: int i;
884: size_t nb;
1.16 jfb 885: CVSFILE *cf, **cfvec;
886:
887: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
888: if (cfvec == NULL) {
889: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
890: return (-1);
891: }
1.3 jfb 892:
893: i = 0;
1.62 jfb 894: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 895: if (i == (int)nfiles) {
1.3 jfb 896: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 897: /* rebuild the list and abort sorting */
898: while (--i >= 0)
1.62 jfb 899: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16 jfb 900: free(cfvec);
1.3 jfb 901: return (-1);
902: }
1.16 jfb 903: cfvec[i++] = cf;
1.3 jfb 904:
905: /* now unlink it from the list,
906: * we'll put it back in order later
907: */
1.62 jfb 908: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 909: }
910:
911: /* clear the list just in case */
1.62 jfb 912: SIMPLEQ_INIT(flp);
1.3 jfb 913: nb = (size_t)i;
914:
915: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
916:
917: /* rebuild the list from the bottom up */
918: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 919: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 920:
1.16 jfb 921: free(cfvec);
1.3 jfb 922: return (0);
923: }
924:
925:
926: static int
927: cvs_file_cmp(const void *f1, const void *f2)
928: {
1.41 jfb 929: const CVSFILE *cf1, *cf2;
1.62 jfb 930: cf1 = *(CVSFILE * const *)f1;
931: cf2 = *(CVSFILE * const *)f2;
1.34 jfb 932: return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6 jfb 933: }
934:
935:
1.34 jfb 936: /*
937: * cvs_file_alloc()
938: *
939: * Allocate a CVSFILE structure and initialize its internals.
940: */
1.6 jfb 941: CVSFILE*
942: cvs_file_alloc(const char *path, u_int type)
943: {
944: CVSFILE *cfp;
945:
1.14 jfb 946: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 947: if (cfp == NULL) {
948: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
949: return (NULL);
950: }
951: memset(cfp, 0, sizeof(*cfp));
952:
1.62 jfb 953: cfp->cf_type = type;
954: cfp->cf_cvstat = CVS_FST_UNKNOWN;
955:
956: if (type == DT_DIR) {
957: SIMPLEQ_INIT(&(cfp->cf_files));
958: }
959:
1.57 jfb 960: cfp->cf_name = cvs_strdup(basename(path));
1.34 jfb 961: if (cfp->cf_name == NULL) {
1.57 jfb 962: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 963: cvs_file_free(cfp);
1.6 jfb 964: return (NULL);
965: }
966:
967: return (cfp);
1.1 jfb 968: }
1.14 jfb 969:
970:
971: /*
972: * cvs_file_lget()
973: *
974: * Get the file and link it with the parent right away.
1.22 jfb 975: * Returns a pointer to the created file structure on success, or NULL on
976: * failure.
1.14 jfb 977: */
978: static CVSFILE*
1.62 jfb 979: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14 jfb 980: {
1.44 jfb 981: int ret, cwd;
982: u_int type;
1.14 jfb 983: struct stat st;
984: CVSFILE *cfp;
985:
1.44 jfb 986: type = DT_UNKNOWN;
987: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62 jfb 988:
1.44 jfb 989: ret = stat(path, &st);
990: if (ret == 0)
991: type = IFTODT(st.st_mode);
1.14 jfb 992:
1.44 jfb 993: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 994: return (NULL);
995: cfp->cf_parent = parent;
1.54 joris 996:
997: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 998: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 999:
1.44 jfb 1000: if (ret == 0) {
1001: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 1002: if (cfp->cf_type == DT_REG)
1003: cfp->cf_mtime = st.st_mtime;
1.44 jfb 1004:
1005: if (ent == NULL)
1006: cfp->cf_cvstat = (cwd == 1) ?
1007: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14 jfb 1008: else {
1.44 jfb 1009: /* always show directories as up-to-date */
1010: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 1011: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 1012: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
1013: cfp->cf_cvstat = CVS_FST_ADDED;
1014: else {
1015: /* check last modified time */
1016: if (ent->ce_mtime >= (time_t)st.st_mtime)
1017: cfp->cf_cvstat = CVS_FST_UPTODATE;
1018: else
1019: cfp->cf_cvstat = CVS_FST_MODIFIED;
1020: }
1.14 jfb 1021: }
1.44 jfb 1022: } else {
1023: if (ent == NULL) {
1024: cvs_log(LP_ERR, "no Entry and no file for `%s'",
1025: CVS_FILE_NAME(cfp));
1026: cvs_file_free(cfp);
1027: return (NULL);
1.66 joris 1028: } else {
1029: if (ent->ce_type == CVS_ENT_FILE)
1030: cfp->cf_type = DT_REG;
1031: else if (ent->ce_type == CVS_ENT_DIR)
1032: cfp->cf_type = DT_DIR;
1033: else
1034: cvs_log(LP_WARN, "unknown ce_type %d",
1035: ent->ce_type);
1.67 joris 1036:
1037: if (ent->ce_status == CVS_ENT_REMOVED)
1038: cfp->cf_cvstat = CVS_FST_REMOVED;
1039: else
1040: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1041: }
1.14 jfb 1042: }
1.52 jfb 1043:
1.62 jfb 1044: if (ent != NULL) {
1045: /* steal the RCSNUM */
1046: cfp->cf_lrev = ent->ce_rev;
1.72 jfb 1047: ent->ce_rev = NULL;
1048:
1.77 jfb 1049: if (ent->ce_type == CVS_ENT_FILE) {
1050: if (ent->ce_tag[0] != '\0') {
1051: cfp->cf_tag = cvs_strdup(ent->ce_tag);
1052: if (cfp->cf_tag == NULL) {
1053: cvs_file_free(cfp);
1054: return (NULL);
1055: }
1056: }
1057:
1058: if (ent->ce_opts[0] != '\0') {
1059: cfp->cf_opts = cvs_strdup(ent->ce_opts);
1060: if (cfp->cf_opts == NULL) {
1061: cvs_file_free(cfp);
1062: return (NULL);
1063: }
1.65 joris 1064: }
1.62 jfb 1065: }
1066: }
1.14 jfb 1067:
1.69 joris 1068: if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26 jfb 1069: cvs_file_free(cfp);
1070: return (NULL);
1.14 jfb 1071: }
1.74 joris 1072:
1073: if ((cfp->cf_repo != NULL) && (cfp->cf_type == DT_DIR) &&
1074: !strcmp(cfp->cf_repo, path))
1075: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.14 jfb 1076:
1077: return (cfp);
1.23 jfb 1078: }
1079:
1080:
1081: static int
1082: cvs_file_cmpname(const char *name1, const char *name2)
1083: {
1084: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1085: (strcasecmp(name1, name2));
1.14 jfb 1086: }