Annotation of src/usr.bin/cvs/file.c, Revision 1.69
1.69 ! joris 1: /* $OpenBSD: file.c,v 1.68 2005/04/25 21:58:32 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.69 ! joris 110: static int cvs_load_dirinfo (CVSFILE *, int);
1.68 joris 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.69 ! joris 547: cvs_load_dirinfo(CVSFILE *cf, int flags)
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) {
1.69 ! joris 560: if (flags & CF_MKADMIN)
! 561: cvs_mkadmin(cf, 0755);
! 562:
1.26 jfb 563: /* if the CVS administrative directory exists, load the info */
1.61 xsa 564: l = snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
565: if (l == -1 || l >= (int)sizeof(pbuf)) {
566: errno = ENAMETOOLONG;
567: cvs_log(LP_ERRNO, "%s", pbuf);
568: return (-1);
569: }
570:
1.26 jfb 571: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
1.34 jfb 572: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
1.62 jfb 573: cf->cf_repo = strdup(pbuf);
574: if (cf->cf_repo == NULL) {
1.28 jfb 575: cvs_log(LP_ERRNO,
576: "failed to dup repository string");
1.26 jfb 577: return (-1);
578: }
1.20 jfb 579: }
1.68 joris 580: }
581: }
1.26 jfb 582:
1.68 joris 583: return (0);
584: }
585:
586: /*
587: * cvs_file_getdir()
588: *
589: * Get a cvs directory structure for the directory whose path is <dir>.
590: * This function should not free the directory information on error, as this
591: * is performed by cvs_file_free().
592: */
593: static int
594: cvs_file_getdir(CVSFILE *cf, int flags, char *path)
595: {
596: int l;
597: int check_entry;
598: u_int ndirs, nfiles;
599: char *cur, *np;
600: char pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
601: struct dirent *ent;
602: CVSFILE *cfp, *moo;
603: struct cvs_ent *cvsent;
604: struct cvs_flist dirs;
605: DIR *dirp;
606: CVSENTRIES *entfile;
607:
608: moo = NULL;
609: check_entry = ndirs = nfiles = 0;
610: SIMPLEQ_INIT(&dirs);
611:
612: cvs_file_getpath(cf, fpath, sizeof(fpath));
613: entfile = cvs_ent_open(fpath, O_RDONLY);
614:
615: cf->cf_root = cvsroot_get(fpath);
616: if (cf->cf_root == NULL)
617: return (-1);
618:
619: cur = np = NULL;
620: if (path != NULL) {
621: cur = strchr(path, '/');
622: if (cur != NULL) {
623: *cur = '\0';
624: np = cur + 1;
625: if (np != NULL && *np == '\0')
626: np = NULL;
1.20 jfb 627: }
628: }
1.14 jfb 629:
1.56 jfb 630: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 631: return (0);
632:
1.68 joris 633: dirp = opendir(fpath);
634: if (dirp == NULL) {
635: cvs_log(LP_ERRNO, "failed to open directory %s", fpath);
1.6 jfb 636: return (-1);
1.3 jfb 637: }
638:
1.68 joris 639: while ((ent = readdir(dirp)) != NULL) {
640: if (!strcmp(ent->d_name, ".") ||
641: !strcmp(ent->d_name, ".."))
642: continue;
1.10 jfb 643:
1.68 joris 644: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
645: continue;
1.11 jfb 646:
1.68 joris 647: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
648: continue;
1.24 jfb 649:
1.68 joris 650: if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
651: if (entfile != NULL)
652: (void)cvs_ent_remove(entfile,
653: ent->d_name);
654: continue;
655: }
1.56 jfb 656:
1.68 joris 657: if (path != NULL) {
658: if (strcmp(path, ent->d_name))
1.56 jfb 659: continue;
1.68 joris 660: }
1.11 jfb 661:
1.68 joris 662: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
663: ent->d_name);
664: if (l == -1 || l >= (int)sizeof(pbuf)) {
665: errno = ENAMETOOLONG;
666: cvs_log(LP_ERRNO, "%s", pbuf);
667: closedir(dirp);
668: return (-1);
669: }
1.61 xsa 670:
1.68 joris 671: cfp = cvs_file_find(cf, ent->d_name);
672: if (cfp == NULL) {
1.62 jfb 673: if (entfile != NULL)
674: cvsent = cvs_ent_get(entfile, ent->d_name);
675: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
1.68 joris 676:
1.55 jfb 677: if (cfp == NULL) {
1.68 joris 678: closedir(dirp);
1.55 jfb 679: return (-1);
680: }
1.62 jfb 681: if (entfile != NULL)
682: cvs_ent_remove(entfile, cfp->cf_name);
1.55 jfb 683:
1.68 joris 684: if (cfp->cf_type != DT_DIR) {
1.62 jfb 685: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp,
1.68 joris 686: cf_list);
1.62 jfb 687: nfiles++;
1.11 jfb 688: }
1.68 joris 689: } else {
690: cfp->cf_flags |= CVS_GDIR_IGNORE;
1.4 jfb 691: }
1.14 jfb 692:
1.68 joris 693: if (cfp->cf_type == DT_DIR) {
694: ndirs++;
695: SIMPLEQ_INSERT_TAIL(&dirs, cfp, cf_list);
696: }
697:
698: if (path != NULL) {
699: check_entry = 0;
700: break;
701: }
702: }
703:
704: closedir(dirp);
705:
706: if (entfile != NULL && check_entry) {
1.62 jfb 707: while ((cvsent = cvs_ent_next(entfile)) != NULL) {
1.68 joris 708: if (path != NULL) {
709: if (strcmp(cvsent->ce_name, path))
710: continue;
711: }
712:
1.61 xsa 713: l = snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
1.44 jfb 714: cvsent->ce_name);
1.61 xsa 715: if (l == -1 || l >= (int)sizeof(pbuf)) {
716: errno = ENAMETOOLONG;
717: cvs_log(LP_ERRNO, "%s", pbuf);
718: return (-1);
719: }
720:
1.68 joris 721: cfp = cvs_file_find(cf, cvsent->ce_name);
722: if (cfp == NULL) {
723: cfp = cvs_file_lget(pbuf, flags, cf, cvsent);
724: if (cfp == NULL)
725: continue;
726:
727: if (cfp->cf_type != DT_DIR) {
728: SIMPLEQ_INSERT_TAIL(&(cf->cf_files),
729: cfp, cf_list);
1.62 jfb 730: nfiles++;
1.44 jfb 731: }
1.68 joris 732: } else {
733: cfp->cf_flags |= CVS_GDIR_IGNORE;
734: }
735:
736: if (cfp->cf_type == DT_DIR) {
737: ndirs++;
738: SIMPLEQ_INSERT_TAIL(&dirs, cfp,
739: cf_list);
1.44 jfb 740: }
1.68 joris 741:
742: if (path != NULL)
743: break;
1.44 jfb 744: }
1.62 jfb 745: cvs_ent_close(entfile);
1.40 jfb 746: }
1.34 jfb 747:
1.10 jfb 748: if (flags & CF_SORT) {
1.68 joris 749: if (nfiles > 0)
750: cvs_file_sort(&(cf->cf_files), nfiles);
751: if (ndirs > 0)
752: cvs_file_sort(&dirs, ndirs);
1.10 jfb 753: }
1.26 jfb 754:
1.62 jfb 755: while (!SIMPLEQ_EMPTY(&dirs)) {
756: cfp = SIMPLEQ_FIRST(&dirs);
757: SIMPLEQ_REMOVE_HEAD(&dirs, cf_list);
1.68 joris 758:
759: if (!(cfp->cf_flags & CVS_GDIR_IGNORE))
760: SIMPLEQ_INSERT_TAIL(&(cf->cf_files), cfp, cf_list);
761: else
762: cfp->cf_flags &= ~CVS_GDIR_IGNORE;
763:
764: if (cvs_file_getdir(cfp, flags, np) < 0) {
765: cvs_log(LP_ERROR, "failed to get %s",
766: CVS_FILE_NAME(cfp));
767: continue;
768: }
1.26 jfb 769: }
1.3 jfb 770:
1.6 jfb 771: return (0);
1.3 jfb 772: }
773:
774:
775: /*
776: * cvs_file_free()
777: *
778: * Free a cvs_file structure and its contents.
779: */
780: void
1.14 jfb 781: cvs_file_free(CVSFILE *cf)
1.3 jfb 782: {
1.62 jfb 783: CVSFILE *child;
784:
1.47 jfb 785: if (cf->cf_name != NULL)
1.57 jfb 786: cvs_strfree(cf->cf_name);
1.62 jfb 787:
788: if (cf->cf_type == DT_DIR) {
789: if (cf->cf_root != NULL)
790: cvsroot_free(cf->cf_root);
791: if (cf->cf_repo != NULL)
792: free(cf->cf_repo);
793: while (!SIMPLEQ_EMPTY(&(cf->cf_files))) {
794: child = SIMPLEQ_FIRST(&(cf->cf_files));
795: SIMPLEQ_REMOVE_HEAD(&(cf->cf_files), cf_list);
796: cvs_file_free(child);
797: }
1.64 joris 798: } else {
799: if (cf->cf_tag != NULL)
800: cvs_strfree(cf->cf_tag);
1.62 jfb 801: }
1.64 joris 802:
1.3 jfb 803: free(cf);
1.5 jfb 804: }
805:
806:
807: /*
808: * cvs_file_examine()
809: *
810: * Examine the contents of the CVS file structure <cf> with the function
811: * <exam>. The function is called for all subdirectories and files of the
812: * root file.
813: */
814: int
815: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
816: {
817: int ret;
1.14 jfb 818: CVSFILE *fp;
1.5 jfb 819:
820: if (cf->cf_type == DT_DIR) {
821: ret = (*exam)(cf, arg);
1.62 jfb 822: SIMPLEQ_FOREACH(fp, &(cf->cf_files), cf_list) {
1.5 jfb 823: ret = cvs_file_examine(fp, exam, arg);
1.60 joris 824: if (ret != 0)
1.13 jfb 825: break;
1.5 jfb 826: }
1.38 deraadt 827: } else
1.13 jfb 828: ret = (*exam)(cf, arg);
829:
830: return (ret);
1.3 jfb 831: }
832:
833: /*
834: * cvs_file_sort()
835: *
1.16 jfb 836: * Sort a list of cvs file structures according to their filename. The list
837: * <flp> is modified according to the sorting algorithm. The number of files
838: * in the list must be given by <nfiles>.
839: * Returns 0 on success, or -1 on failure.
1.3 jfb 840: */
841: static int
1.16 jfb 842: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 843: {
844: int i;
845: size_t nb;
1.16 jfb 846: CVSFILE *cf, **cfvec;
847:
848: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
849: if (cfvec == NULL) {
850: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
851: return (-1);
852: }
1.3 jfb 853:
854: i = 0;
1.62 jfb 855: SIMPLEQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 856: if (i == (int)nfiles) {
1.3 jfb 857: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 858: /* rebuild the list and abort sorting */
859: while (--i >= 0)
1.62 jfb 860: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.16 jfb 861: free(cfvec);
1.3 jfb 862: return (-1);
863: }
1.16 jfb 864: cfvec[i++] = cf;
1.3 jfb 865:
866: /* now unlink it from the list,
867: * we'll put it back in order later
868: */
1.62 jfb 869: SIMPLEQ_REMOVE_HEAD(flp, cf_list);
1.3 jfb 870: }
871:
872: /* clear the list just in case */
1.62 jfb 873: SIMPLEQ_INIT(flp);
1.3 jfb 874: nb = (size_t)i;
875:
876: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
877:
878: /* rebuild the list from the bottom up */
879: for (i = (int)nb - 1; i >= 0; i--)
1.62 jfb 880: SIMPLEQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 881:
1.16 jfb 882: free(cfvec);
1.3 jfb 883: return (0);
884: }
885:
886:
887: static int
888: cvs_file_cmp(const void *f1, const void *f2)
889: {
1.41 jfb 890: const CVSFILE *cf1, *cf2;
1.62 jfb 891: cf1 = *(CVSFILE * const *)f1;
892: cf2 = *(CVSFILE * const *)f2;
1.34 jfb 893: return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6 jfb 894: }
895:
896:
1.34 jfb 897: /*
898: * cvs_file_alloc()
899: *
900: * Allocate a CVSFILE structure and initialize its internals.
901: */
1.6 jfb 902: CVSFILE*
903: cvs_file_alloc(const char *path, u_int type)
904: {
905: CVSFILE *cfp;
906:
1.14 jfb 907: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 908: if (cfp == NULL) {
909: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
910: return (NULL);
911: }
912: memset(cfp, 0, sizeof(*cfp));
913:
1.62 jfb 914: cfp->cf_type = type;
915: cfp->cf_cvstat = CVS_FST_UNKNOWN;
916:
917: if (type == DT_DIR) {
918: SIMPLEQ_INIT(&(cfp->cf_files));
919: }
920:
1.57 jfb 921: cfp->cf_name = cvs_strdup(basename(path));
1.34 jfb 922: if (cfp->cf_name == NULL) {
1.57 jfb 923: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 924: cvs_file_free(cfp);
1.6 jfb 925: return (NULL);
926: }
927:
928: return (cfp);
1.1 jfb 929: }
1.14 jfb 930:
931:
932: /*
933: * cvs_file_lget()
934: *
935: * Get the file and link it with the parent right away.
1.22 jfb 936: * Returns a pointer to the created file structure on success, or NULL on
937: * failure.
1.14 jfb 938: */
939: static CVSFILE*
1.62 jfb 940: cvs_file_lget(const char *path, int flags, CVSFILE *parent, struct cvs_ent *ent)
1.14 jfb 941: {
1.44 jfb 942: int ret, cwd;
943: u_int type;
1.14 jfb 944: struct stat st;
945: CVSFILE *cfp;
946:
1.44 jfb 947: type = DT_UNKNOWN;
948: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.62 jfb 949:
1.44 jfb 950: ret = stat(path, &st);
951: if (ret == 0)
952: type = IFTODT(st.st_mode);
1.14 jfb 953:
1.44 jfb 954: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 955: return (NULL);
956: cfp->cf_parent = parent;
1.54 joris 957:
958: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
1.62 jfb 959: cfp->cf_flags |= CVS_DIRF_BASE;
1.14 jfb 960:
1.44 jfb 961: if (ret == 0) {
962: cfp->cf_mode = st.st_mode & ACCESSPERMS;
1.62 jfb 963: if (cfp->cf_type == DT_REG)
964: cfp->cf_mtime = st.st_mtime;
1.44 jfb 965:
966: if (ent == NULL)
967: cfp->cf_cvstat = (cwd == 1) ?
968: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14 jfb 969: else {
1.44 jfb 970: /* always show directories as up-to-date */
971: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 972: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 973: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
974: cfp->cf_cvstat = CVS_FST_ADDED;
975: else {
976: /* check last modified time */
977: if (ent->ce_mtime >= (time_t)st.st_mtime)
978: cfp->cf_cvstat = CVS_FST_UPTODATE;
979: else
980: cfp->cf_cvstat = CVS_FST_MODIFIED;
981: }
1.14 jfb 982: }
1.44 jfb 983: } else {
984: if (ent == NULL) {
985: cvs_log(LP_ERR, "no Entry and no file for `%s'",
986: CVS_FILE_NAME(cfp));
987: cvs_file_free(cfp);
988: return (NULL);
1.66 joris 989: } else {
990: if (ent->ce_type == CVS_ENT_FILE)
991: cfp->cf_type = DT_REG;
992: else if (ent->ce_type == CVS_ENT_DIR)
993: cfp->cf_type = DT_DIR;
994: else
995: cvs_log(LP_WARN, "unknown ce_type %d",
996: ent->ce_type);
1.67 joris 997:
998: if (ent->ce_status == CVS_ENT_REMOVED)
999: cfp->cf_cvstat = CVS_FST_REMOVED;
1000: else
1001: cfp->cf_cvstat = CVS_FST_LOST;
1.66 joris 1002: }
1.14 jfb 1003: }
1.52 jfb 1004:
1.62 jfb 1005: if (ent != NULL) {
1006: /* steal the RCSNUM */
1007: cfp->cf_lrev = ent->ce_rev;
1.65 joris 1008: if (ent->ce_tag != NULL) {
1009: if ((cfp->cf_tag = cvs_strdup(ent->ce_tag)) == NULL) {
1010: cvs_file_free(cfp);
1011: return (NULL);
1012: }
1.62 jfb 1013: }
1014: ent->ce_rev = NULL;
1015: }
1.14 jfb 1016:
1.69 ! joris 1017: if ((cfp->cf_type == DT_DIR) && (cvs_load_dirinfo(cfp, flags) < 0)) {
1.26 jfb 1018: cvs_file_free(cfp);
1019: return (NULL);
1.14 jfb 1020: }
1021:
1022: return (cfp);
1.23 jfb 1023: }
1024:
1025:
1026: static int
1027: cvs_file_cmpname(const char *name1, const char *name2)
1028: {
1029: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
1030: (strcasecmp(name1, name2));
1.14 jfb 1031: }