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