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