Annotation of src/usr.bin/cvs/file.c, Revision 1.49
1.49 ! jfb 1: /* $OpenBSD: file.c,v 1.48 2005/01/06 20:15:16 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>
35: #include <dirent.h>
36: #include <stdlib.h>
37: #include <unistd.h>
38: #include <string.h>
39: #include <fnmatch.h>
40:
41: #include "cvs.h"
42: #include "log.h"
1.14 jfb 43: #include "file.h"
1.1 jfb 44:
45:
46: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
47:
48: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
49:
50:
51: /* ignore pattern */
52: struct cvs_ignpat {
53: char ip_pat[MAXNAMLEN];
54: int ip_flags;
55: TAILQ_ENTRY (cvs_ignpat) ip_list;
56: };
57:
58:
59: /*
60: * Standard patterns to ignore.
61: */
62: static const char *cvs_ign_std[] = {
63: ".",
64: "..",
65: "*.o",
66: "*.so",
1.45 xsa 67: "*.a",
1.1 jfb 68: "*.bak",
69: "*.orig",
70: "*.rej",
1.45 xsa 71: "*.old",
1.1 jfb 72: "*.exe",
73: "*.depend",
1.45 xsa 74: "*.obj",
75: "*.elc",
76: "*.ln",
77: "*.olb",
1.1 jfb 78: "CVS",
79: "core",
1.49 ! jfb 80: "*.core",
1.8 jfb 81: ".#*",
1.45 xsa 82: "*~",
83: "_$*",
84: "*$",
1.1 jfb 85: #ifdef OLD_SMELLY_CRUFT
86: "RCSLOG",
87: "tags",
88: "TAGS",
89: "RCS",
90: "SCCS",
1.45 xsa 91: "cvslog.*", /* to ignore CVS_CLIENT_LOG output */
1.1 jfb 92: "#*",
93: ",*",
94: #endif
95: };
96:
97:
1.11 jfb 98: /*
1.34 jfb 99: * Filename hash table used to avoid duplication of name strings when working
100: * on large source trees with common parts.
101: */
102: SLIST_HEAD(cvs_fhb, cvs_fname);
103:
104: static struct cvs_fhb cvs_fnht[CVS_FILE_NBUCKETS];
105:
106:
107:
108: /*
1.11 jfb 109: * Entries in the CVS/Entries file with a revision of '0' have only been
110: * added. Compare against this revision to see if this is the case
111: */
1.4 jfb 112: static RCSNUM *cvs_addedrev;
113:
114:
1.1 jfb 115: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
116:
117:
1.34 jfb 118: static int cvs_file_getdir (CVSFILE *, int);
119: static void cvs_file_freedir (struct cvs_dir *);
120: static int cvs_file_sort (struct cvs_flist *, u_int);
121: static int cvs_file_cmp (const void *, const void *);
122: static int cvs_file_cmpname (const char *, const char *);
123: static u_int8_t cvs_file_hashname (const char *);
124: static struct cvs_fname* cvs_file_getname (const char *);
1.47 jfb 125: static void cvs_file_freename (struct cvs_fname *);
1.34 jfb 126: static CVSFILE* cvs_file_alloc (const char *, u_int);
127: static CVSFILE* cvs_file_lget (const char *, int, CVSFILE *);
1.3 jfb 128:
129:
130:
1.1 jfb 131: /*
132: * cvs_file_init()
133: *
134: */
135: int
136: cvs_file_init(void)
137: {
138: int i;
139: size_t len;
140: char path[MAXPATHLEN], buf[MAXNAMLEN];
141: FILE *ifp;
142: struct passwd *pwd;
143:
1.34 jfb 144: /* initialize the filename hash table */
145: for (i = 0; i < CVS_FILE_NBUCKETS; i++)
146: SLIST_INIT(&(cvs_fnht[i]));
147:
1.1 jfb 148: TAILQ_INIT(&cvs_ign_pats);
149:
1.46 jfb 150: if (((cvs_addedrev = rcsnum_alloc()) == NULL) ||
151: (rcsnum_aton("0", NULL, cvs_addedrev) < 0))
152: return (-1);
1.4 jfb 153:
1.1 jfb 154: /* standard patterns to ignore */
1.10 jfb 155: for (i = 0; i < (int)(sizeof(cvs_ign_std)/sizeof(char *)); i++)
1.26 jfb 156: cvs_file_ignore(cvs_ign_std[i]);
1.1 jfb 157:
158: /* read the cvsignore file in the user's home directory, if any */
159: pwd = getpwuid(getuid());
160: if (pwd != NULL) {
161: snprintf(path, sizeof(path), "%s/.cvsignore", pwd->pw_dir);
162: ifp = fopen(path, "r");
163: if (ifp == NULL) {
164: if (errno != ENOENT)
1.34 jfb 165: cvs_log(LP_ERRNO,
166: "failed to open user's cvsignore", path);
1.38 deraadt 167: } else {
1.1 jfb 168: while (fgets(buf, sizeof(buf), ifp) != NULL) {
169: len = strlen(buf);
170: if (len == 0)
171: continue;
172: if (buf[len - 1] != '\n') {
173: cvs_log(LP_ERR, "line too long in `%s'",
174: path);
175: }
176: buf[--len] = '\0';
177: cvs_file_ignore(buf);
178: }
179: (void)fclose(ifp);
180: }
181: }
182:
183: return (0);
184: }
185:
186:
187: /*
188: * cvs_file_ignore()
189: *
190: * Add the pattern <pat> to the list of patterns for files to ignore.
191: * Returns 0 on success, or -1 on failure.
192: */
193: int
194: cvs_file_ignore(const char *pat)
195: {
196: char *cp;
197: struct cvs_ignpat *ip;
198:
199: ip = (struct cvs_ignpat *)malloc(sizeof(*ip));
200: if (ip == NULL) {
201: cvs_log(LP_ERR, "failed to allocate space for ignore pattern");
202: return (-1);
203: }
204:
205: strlcpy(ip->ip_pat, pat, sizeof(ip->ip_pat));
206:
207: /* check if we will need globbing for that pattern */
208: ip->ip_flags = CVS_IGN_STATIC;
209: for (cp = ip->ip_pat; *cp != '\0'; cp++) {
210: if (CVS_CHAR_ISMETA(*cp)) {
211: ip->ip_flags &= ~CVS_IGN_STATIC;
212: break;
213: }
214: }
215:
216: TAILQ_INSERT_TAIL(&cvs_ign_pats, ip, ip_list);
217:
218: return (0);
219: }
220:
221:
222: /*
1.5 jfb 223: * cvs_file_chkign()
1.1 jfb 224: *
225: * Returns 1 if the filename <file> is matched by one of the ignore
226: * patterns, or 0 otherwise.
227: */
228: int
1.5 jfb 229: cvs_file_chkign(const char *file)
1.1 jfb 230: {
1.23 jfb 231: int flags;
1.1 jfb 232: struct cvs_ignpat *ip;
233:
1.23 jfb 234: flags = FNM_PERIOD;
235: if (cvs_nocase)
236: flags |= FNM_CASEFOLD;
237:
1.1 jfb 238: TAILQ_FOREACH(ip, &cvs_ign_pats, ip_list) {
239: if (ip->ip_flags & CVS_IGN_STATIC) {
1.23 jfb 240: if (cvs_file_cmpname(file, ip->ip_pat) == 0)
1.1 jfb 241: return (1);
1.38 deraadt 242: } else if (fnmatch(ip->ip_pat, file, flags) == 0)
1.1 jfb 243: return (1);
244: }
245:
246: return (0);
247: }
248:
249:
250: /*
1.6 jfb 251: * cvs_file_create()
1.1 jfb 252: *
1.6 jfb 253: * Create a new file whose path is specified in <path> and of type <type>.
1.26 jfb 254: * If the type is DT_DIR, the CVS administrative repository and files will be
255: * created.
1.25 jfb 256: * Returns the created file on success, or NULL on failure.
1.1 jfb 257: */
1.6 jfb 258: CVSFILE*
1.34 jfb 259: cvs_file_create(CVSFILE *parent, const char *path, u_int type, mode_t mode)
1.1 jfb 260: {
1.6 jfb 261: int fd;
1.34 jfb 262: char fp[MAXPATHLEN];
1.6 jfb 263: CVSFILE *cfp;
1.1 jfb 264:
1.6 jfb 265: cfp = cvs_file_alloc(path, type);
266: if (cfp == NULL)
267: return (NULL);
1.26 jfb 268:
1.22 jfb 269: cfp->cf_mode = mode;
1.34 jfb 270: cfp->cf_parent = parent;
1.1 jfb 271:
1.34 jfb 272: if (type == DT_DIR) {
273: cfp->cf_ddat->cd_root = cvsroot_get(path);
274: cfp->cf_ddat->cd_repo = strdup(cvs_file_getpath(cfp,
275: fp, sizeof(fp)));
276: if (cfp->cf_ddat->cd_repo == NULL) {
277: cvs_file_free(cfp);
278: return (NULL);
279: }
1.33 joris 280:
1.26 jfb 281: if ((mkdir(path, mode) == -1) || (cvs_mkadmin(cfp, mode) < 0)) {
1.6 jfb 282: cvs_file_free(cfp);
283: return (NULL);
284: }
1.26 jfb 285:
1.27 jfb 286: cfp->cf_ddat->cd_ent = cvs_ent_open(path, O_RDWR);
1.38 deraadt 287: } else {
1.6 jfb 288: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, mode);
289: if (fd == -1) {
290: cvs_file_free(cfp);
1.1 jfb 291: return (NULL);
292: }
1.6 jfb 293: (void)close(fd);
1.1 jfb 294: }
295:
1.6 jfb 296: return (cfp);
1.3 jfb 297: }
298:
299:
300: /*
1.35 jfb 301: * cvs_file_copy()
302: *
303: * Allocate space to create a copy of the file <orig>. The copy inherits all
304: * of the original's attributes, but does not inherit its children if the
305: * original file is a directory. Note that files copied using this mechanism
306: * are linked to their parent, but the parent has no link to the file. This
307: * is so cvs_file_getpath() works.
308: * Returns the copied file on success, or NULL on failure. The returned
309: * structure should be freed using cvs_file_free().
310: */
311: CVSFILE*
312: cvs_file_copy(CVSFILE *orig)
313: {
314: char path[MAXPATHLEN];
315: CVSFILE *cfp;
316:
317: cvs_file_getpath(orig, path, sizeof(path));
318:
319: cfp = cvs_file_alloc(path, orig->cf_type);
320: if (cfp == NULL)
321: return (NULL);
322:
323: cfp->cf_parent = orig->cf_parent;
324: cfp->cf_mode = orig->cf_mode;
325: cfp->cf_mtime = orig->cf_mtime;
326: cfp->cf_cvstat = orig->cf_cvstat;
327:
328: if (orig->cf_type == DT_DIR) {
329: /* XXX copy CVS directory attributes */
330: }
331:
332: return (cfp);
333: }
334:
335:
336: /*
1.3 jfb 337: * cvs_file_get()
338: *
339: * Load a cvs_file structure with all the information pertaining to the file
340: * <path>.
1.4 jfb 341: * The <flags> parameter specifies various flags that alter the behaviour of
1.21 jfb 342: * the function. The CF_RECURSE flag causes the function to recursively load
343: * subdirectories when <path> is a directory.
344: * The CF_SORT flag causes the files to be sorted in alphabetical order upon
345: * loading. The special case of "." as a path specification generates
346: * recursion for a single level and is equivalent to calling cvs_file_get() on
347: * all files of that directory.
1.3 jfb 348: * Returns a pointer to the cvs file structure, which must later be freed
349: * with cvs_file_free().
350: */
351:
1.14 jfb 352: CVSFILE*
1.3 jfb 353: cvs_file_get(const char *path, int flags)
354: {
1.14 jfb 355: return cvs_file_lget(path, flags, NULL);
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*
368: cvs_file_getspec(char **fspec, int fsn, int flags)
369: {
1.26 jfb 370: int i;
371: char *sp, *np, pcopy[MAXPATHLEN];
372: CVSFILE *base, *cf, *nf;
373:
374: base = cvs_file_get(".", 0);
375: if (base == NULL)
376: return (NULL);
377:
378: for (i = 0; i < fsn; i++) {
379: strlcpy(pcopy, fspec[i], sizeof(pcopy));
380: cf = base;
381: sp = pcopy;
382:
383: do {
384: np = strchr(sp, '/');
385: if (np != NULL)
386: *np = '\0';
387: nf = cvs_file_find(cf, sp);
388: if (nf == NULL) {
389: nf = cvs_file_lget(pcopy, 0, cf);
390: if (nf == NULL) {
391: cvs_file_free(base);
392: return (NULL);
393: }
394:
395: cvs_file_attach(cf, nf);
396: }
397:
398: if (np != NULL) {
399: *np = '/';
400: sp = np + 1;
401: }
402:
403: cf = nf;
404: } while (np != NULL);
405: }
406:
407: return (base);
1.3 jfb 408: }
409:
410:
411: /*
1.13 jfb 412: * cvs_file_find()
413: *
414: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
415: * The file's pathname <path> must be relative to the base of <hier>.
416: * Returns the entry on success, or NULL on failure.
417: */
418: CVSFILE*
419: cvs_file_find(CVSFILE *hier, const char *path)
420: {
421: char *pp, *sp, pbuf[MAXPATHLEN];
422: CVSFILE *sf, *cf;
423:
424: strlcpy(pbuf, path, sizeof(pbuf));
425:
426: cf = hier;
427: pp = pbuf;
428: do {
429: sp = strchr(pp, '/');
430: if (sp != NULL)
1.24 jfb 431: *(sp++) = '\0';
1.13 jfb 432:
433: /* special case */
434: if (*pp == '.') {
435: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
436: /* request to go back to parent */
437: if (cf->cf_parent == NULL) {
438: cvs_log(LP_NOTICE,
439: "path %s goes back too far", path);
440: return (NULL);
441: }
442: cf = cf->cf_parent;
443: continue;
1.38 deraadt 444: } else if (*(pp + 1) == '\0')
1.13 jfb 445: continue;
446: }
447:
448: TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
1.34 jfb 449: if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13 jfb 450: break;
451: if (sf == NULL)
452: return (NULL);
453:
454: cf = sf;
455: pp = sp;
456: } while (sp != NULL);
457:
1.24 jfb 458: return (cf);
1.13 jfb 459: }
460:
461:
462: /*
1.34 jfb 463: * cvs_file_getpath()
464: *
465: * Get the full path of the file <file> and store it in <buf>, which is of
466: * size <len>. For portability, it is recommended that <buf> always be
467: * at least MAXPATHLEN bytes long.
468: * Returns a pointer to the start of the path on success, or NULL on failure.
469: */
470: char*
471: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
472: {
473: u_int i;
474: char *fp, *namevec[CVS_FILE_MAXDEPTH];
475: CVSFILE *top;
476:
477: buf[0] = '\0';
478: i = CVS_FILE_MAXDEPTH;
479: memset(namevec, 0, sizeof(namevec));
480:
481: /* find the top node */
482: for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
483: fp = CVS_FILE_NAME(top);
484:
485: /* skip self-references */
486: if ((fp[0] == '.') && (fp[1] == '\0'))
487: continue;
488: namevec[--i] = fp;
489: }
490:
491: if (i == 0)
492: return (NULL);
493: else if (i == CVS_FILE_MAXDEPTH) {
494: strlcpy(buf, ".", len);
495: return (buf);
496: }
497:
498: while (i < CVS_FILE_MAXDEPTH - 1) {
499: strlcat(buf, namevec[i++], len);
500: strlcat(buf, "/", len);
501: }
502: strlcat(buf, namevec[i], len);
503:
504: return (buf);
505: }
506:
507:
508: /*
1.22 jfb 509: * cvs_file_attach()
510: *
511: * Attach the file <file> as one of the children of parent <parent>, which
512: * has to be a file of type DT_DIR.
513: * Returns 0 on success, or -1 on failure.
514: */
515: int
516: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
517: {
1.23 jfb 518: struct cvs_dir *dp;
1.22 jfb 519:
520: if (parent->cf_type != DT_DIR)
521: return (-1);
522:
1.23 jfb 523: dp = parent->cf_ddat;
524:
525: TAILQ_INSERT_TAIL(&(dp->cd_files), file, cf_list);
526: dp->cd_nfiles++;
1.22 jfb 527: file->cf_parent = parent;
528:
529: return (0);
530: }
531:
532:
533: /*
1.3 jfb 534: * cvs_file_getdir()
535: *
536: * Get a cvs directory structure for the directory whose path is <dir>.
1.30 jfb 537: * This function should not free the directory information on error, as this
538: * is performed by cvs_file_free().
1.3 jfb 539: */
1.6 jfb 540: static int
1.14 jfb 541: cvs_file_getdir(CVSFILE *cf, int flags)
1.3 jfb 542: {
1.13 jfb 543: int ret, fd;
1.16 jfb 544: u_int ndirs;
1.3 jfb 545: long base;
1.43 jfb 546: u_char *dp, *ep;
1.34 jfb 547: char fbuf[2048], pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
1.3 jfb 548: struct dirent *ent;
1.14 jfb 549: CVSFILE *cfp;
1.20 jfb 550: struct stat st;
1.3 jfb 551: struct cvs_dir *cdp;
1.44 jfb 552: struct cvs_ent *cvsent;
1.10 jfb 553: struct cvs_flist dirs;
1.3 jfb 554:
1.18 jfb 555: ndirs = 0;
1.10 jfb 556: TAILQ_INIT(&dirs);
1.7 jfb 557: cdp = cf->cf_ddat;
558:
1.34 jfb 559: cvs_file_getpath(cf, fpath, sizeof(fpath));
560:
1.48 jfb 561: cdp->cd_root = cvsroot_get(fpath);
562: if (cdp->cd_root == NULL)
563: return (-1);
564:
1.26 jfb 565: if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
566: if (flags & CF_MKADMIN)
567: cvs_mkadmin(cf, 0755);
1.14 jfb 568:
1.26 jfb 569: /* if the CVS administrative directory exists, load the info */
1.34 jfb 570: snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
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.26 jfb 573: cdp->cd_repo = strdup(pbuf);
574: if (cdp->cd_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.26 jfb 580:
1.37 jfb 581: cdp->cd_ent = cvs_ent_open(fpath, O_RDONLY);
1.20 jfb 582: }
583: }
1.14 jfb 584:
1.48 jfb 585: if (!(flags & CF_RECURSE) ||
586: ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN)))
1.26 jfb 587: return (0);
588:
1.34 jfb 589: fd = open(fpath, O_RDONLY);
1.3 jfb 590: if (fd == -1) {
1.34 jfb 591: cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1.6 jfb 592: return (-1);
1.3 jfb 593: }
594:
1.44 jfb 595: /* To load all files, we first get the entries for the directory and
596: * load the information for each of those entries. The handle to
597: * the Entries file kept in the directory data is only temporary and
598: * the files should remove their entry when they use it. After all
599: * files in the directory have been processed, the Entries handle
600: * should only be left with those entries for which no real file
601: * exists. We then build file structures for those files too, as
602: * we will likely receive fresh copies from the server as part of the
603: * response.
604: */
1.11 jfb 605: do {
606: ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
607: if (ret == -1) {
608: cvs_log(LP_ERRNO, "failed to get directory entries");
609: (void)close(fd);
610: return (-1);
611: }
1.10 jfb 612:
1.11 jfb 613: dp = fbuf;
614: ep = fbuf + (size_t)ret;
615: while (dp < ep) {
616: ent = (struct dirent *)dp;
1.32 jfb 617: dp += ent->d_reclen;
1.31 jfb 618: if (ent->d_fileno == 0)
619: continue;
1.11 jfb 620:
621: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.24 jfb 622: continue;
623:
624: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
1.11 jfb 625: continue;
626:
1.34 jfb 627: snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
628: ent->d_name);
1.14 jfb 629: cfp = cvs_file_lget(pbuf, flags, cf);
1.11 jfb 630: if (cfp != NULL) {
1.26 jfb 631: if (cfp->cf_type == DT_DIR) {
632: TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
1.16 jfb 633: ndirs++;
1.38 deraadt 634: } else {
1.26 jfb 635: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
1.11 jfb 636: cf_list);
1.16 jfb 637: cdp->cd_nfiles++;
638: }
1.11 jfb 639: }
1.4 jfb 640: }
1.11 jfb 641: } while (ret > 0);
1.14 jfb 642:
1.40 jfb 643: if (cdp->cd_ent != NULL) {
1.44 jfb 644: /* now create file structure for files which have an
645: * entry in the Entries file but no file on disk
646: */
647: while ((cvsent = cvs_ent_next(cdp->cd_ent)) != NULL) {
648: snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
649: cvsent->ce_name);
650: cfp = cvs_file_lget(pbuf, flags, cf);
651: if (cfp != NULL) {
652: if (cfp->cf_type == DT_DIR) {
653: TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
654: ndirs++;
655: } else {
656: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
657: cf_list);
658: cdp->cd_nfiles++;
659: }
660: }
661: }
1.40 jfb 662: cvs_ent_close(cdp->cd_ent);
663: cdp->cd_ent = NULL;
664: }
1.34 jfb 665:
1.10 jfb 666: if (flags & CF_SORT) {
1.16 jfb 667: cvs_file_sort(&(cdp->cd_files), cdp->cd_nfiles);
668: cvs_file_sort(&dirs, ndirs);
1.10 jfb 669: }
1.26 jfb 670:
671: while (!TAILQ_EMPTY(&dirs)) {
672: cfp = TAILQ_FIRST(&dirs);
673: TAILQ_REMOVE(&dirs, cfp, cf_list);
1.10 jfb 674: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp, cf_list);
1.26 jfb 675: }
1.16 jfb 676: cdp->cd_nfiles += ndirs;
1.3 jfb 677:
678: (void)close(fd);
679:
1.6 jfb 680: return (0);
1.3 jfb 681: }
682:
683:
684: /*
685: * cvs_file_free()
686: *
687: * Free a cvs_file structure and its contents.
688: */
689: void
1.14 jfb 690: cvs_file_free(CVSFILE *cf)
1.3 jfb 691: {
692: if (cf->cf_ddat != NULL)
693: cvs_file_freedir(cf->cf_ddat);
1.47 jfb 694: if (cf->cf_name != NULL)
695: cvs_file_freename(cf->cf_name);
1.3 jfb 696: free(cf);
1.5 jfb 697: }
698:
699:
700: /*
701: * cvs_file_examine()
702: *
703: * Examine the contents of the CVS file structure <cf> with the function
704: * <exam>. The function is called for all subdirectories and files of the
705: * root file.
706: */
707: int
708: cvs_file_examine(CVSFILE *cf, int (*exam)(CVSFILE *, void *), void *arg)
709: {
710: int ret;
1.14 jfb 711: CVSFILE *fp;
1.5 jfb 712:
713: if (cf->cf_type == DT_DIR) {
714: ret = (*exam)(cf, arg);
1.10 jfb 715: TAILQ_FOREACH(fp, &(cf->cf_ddat->cd_files), cf_list) {
1.5 jfb 716: ret = cvs_file_examine(fp, exam, arg);
717: if (ret == -1)
1.13 jfb 718: break;
1.5 jfb 719: }
1.38 deraadt 720: } else
1.13 jfb 721: ret = (*exam)(cf, arg);
722:
723: return (ret);
1.3 jfb 724: }
725:
726:
727: /*
728: * cvs_file_freedir()
729: *
730: * Free a cvs_dir structure and its contents.
731: */
732: static void
733: cvs_file_freedir(struct cvs_dir *cd)
734: {
1.14 jfb 735: CVSFILE *cfp;
1.3 jfb 736:
737: if (cd->cd_root != NULL)
738: cvsroot_free(cd->cd_root);
739: if (cd->cd_repo != NULL)
740: free(cd->cd_repo);
741:
1.14 jfb 742: if (cd->cd_ent != NULL)
743: cvs_ent_close(cd->cd_ent);
744:
1.10 jfb 745: while (!TAILQ_EMPTY(&(cd->cd_files))) {
746: cfp = TAILQ_FIRST(&(cd->cd_files));
747: TAILQ_REMOVE(&(cd->cd_files), cfp, cf_list);
1.3 jfb 748: cvs_file_free(cfp);
749: }
750: }
751:
752:
753: /*
754: * cvs_file_sort()
755: *
1.16 jfb 756: * Sort a list of cvs file structures according to their filename. The list
757: * <flp> is modified according to the sorting algorithm. The number of files
758: * in the list must be given by <nfiles>.
759: * Returns 0 on success, or -1 on failure.
1.3 jfb 760: */
761: static int
1.16 jfb 762: cvs_file_sort(struct cvs_flist *flp, u_int nfiles)
1.3 jfb 763: {
764: int i;
765: size_t nb;
1.16 jfb 766: CVSFILE *cf, **cfvec;
767:
768: cfvec = (CVSFILE **)calloc(nfiles, sizeof(CVSFILE *));
769: if (cfvec == NULL) {
770: cvs_log(LP_ERRNO, "failed to allocate sorting vector");
771: return (-1);
772: }
1.3 jfb 773:
774: i = 0;
1.10 jfb 775: TAILQ_FOREACH(cf, flp, cf_list) {
1.16 jfb 776: if (i == (int)nfiles) {
1.3 jfb 777: cvs_log(LP_WARN, "too many files to sort");
1.16 jfb 778: /* rebuild the list and abort sorting */
779: while (--i >= 0)
780: TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
781: free(cfvec);
1.3 jfb 782: return (-1);
783: }
1.16 jfb 784: cfvec[i++] = cf;
1.3 jfb 785:
786: /* now unlink it from the list,
787: * we'll put it back in order later
788: */
1.10 jfb 789: TAILQ_REMOVE(flp, cf, cf_list);
1.3 jfb 790: }
791:
792: /* clear the list just in case */
1.10 jfb 793: TAILQ_INIT(flp);
1.3 jfb 794: nb = (size_t)i;
795:
796: heapsort(cfvec, nb, sizeof(cf), cvs_file_cmp);
797:
798: /* rebuild the list from the bottom up */
799: for (i = (int)nb - 1; i >= 0; i--)
1.10 jfb 800: TAILQ_INSERT_HEAD(flp, cfvec[i], cf_list);
1.3 jfb 801:
1.16 jfb 802: free(cfvec);
1.3 jfb 803: return (0);
804: }
805:
806:
807: static int
808: cvs_file_cmp(const void *f1, const void *f2)
809: {
1.41 jfb 810: const CVSFILE *cf1, *cf2;
811: cf1 = *(const CVSFILE **)f1;
812: cf2 = *(const CVSFILE **)f2;
1.34 jfb 813: return cvs_file_cmpname(CVS_FILE_NAME(cf1), CVS_FILE_NAME(cf2));
1.6 jfb 814: }
815:
816:
1.34 jfb 817: /*
818: * cvs_file_alloc()
819: *
820: * Allocate a CVSFILE structure and initialize its internals.
821: */
1.6 jfb 822: CVSFILE*
823: cvs_file_alloc(const char *path, u_int type)
824: {
825: size_t len;
826: char pbuf[MAXPATHLEN];
1.34 jfb 827: const char *fnp;
1.6 jfb 828: CVSFILE *cfp;
829: struct cvs_dir *ddat;
830:
1.14 jfb 831: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 832: if (cfp == NULL) {
833: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
834: return (NULL);
835: }
836: memset(cfp, 0, sizeof(*cfp));
837:
838: /* ditch trailing slashes */
839: strlcpy(pbuf, path, sizeof(pbuf));
840: len = strlen(pbuf);
841: while (pbuf[len - 1] == '/')
842: pbuf[--len] = '\0';
843:
1.34 jfb 844: fnp = strrchr(path, '/');
845: if (fnp == NULL)
846: fnp = path;
847: else
848: fnp++;
849:
850: cfp->cf_name = cvs_file_getname(fnp);
851: if (cfp->cf_name == NULL) {
1.35 jfb 852: cvs_log(LP_ERR, "failed to get file name from table");
1.6 jfb 853: return (NULL);
854: }
855: cfp->cf_type = type;
856: cfp->cf_cvstat = CVS_FST_UNKNOWN;
857:
858: if (type == DT_DIR) {
859: ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
860: if (ddat == NULL) {
1.41 jfb 861: cvs_log(LP_ERRNO, "failed to allocate directory data");
1.6 jfb 862: cvs_file_free(cfp);
863: return (NULL);
864: }
865: memset(ddat, 0, sizeof(*ddat));
1.10 jfb 866: TAILQ_INIT(&(ddat->cd_files));
1.6 jfb 867: cfp->cf_ddat = ddat;
868: }
869: return (cfp);
1.1 jfb 870: }
1.14 jfb 871:
872:
873: /*
874: * cvs_file_lget()
875: *
876: * Get the file and link it with the parent right away.
1.22 jfb 877: * Returns a pointer to the created file structure on success, or NULL on
878: * failure.
1.14 jfb 879: */
880: static CVSFILE*
881: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
882: {
1.44 jfb 883: int ret, cwd;
884: u_int type;
1.14 jfb 885: struct stat st;
886: CVSFILE *cfp;
1.34 jfb 887: struct cvs_ent *ent = NULL;
1.14 jfb 888:
1.44 jfb 889: type = DT_UNKNOWN;
890: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.14 jfb 891:
1.44 jfb 892: ret = stat(path, &st);
893: if (ret == 0)
894: type = IFTODT(st.st_mode);
1.14 jfb 895:
1.44 jfb 896: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 897: return (NULL);
898: cfp->cf_parent = parent;
899:
1.44 jfb 900: if ((parent != NULL) && (CVS_DIR_ENTRIES(parent) != NULL))
1.34 jfb 901: ent = cvs_ent_get(CVS_DIR_ENTRIES(parent), CVS_FILE_NAME(cfp));
1.14 jfb 902:
1.44 jfb 903: if (ret == 0) {
904: cfp->cf_mode = st.st_mode & ACCESSPERMS;
905: cfp->cf_mtime = st.st_mtime;
906:
907: if (ent == NULL)
908: cfp->cf_cvstat = (cwd == 1) ?
909: CVS_FST_UPTODATE : CVS_FST_UNKNOWN;
1.14 jfb 910: else {
1.44 jfb 911: /* always show directories as up-to-date */
912: if (ent->ce_type == CVS_ENT_DIR)
1.14 jfb 913: cfp->cf_cvstat = CVS_FST_UPTODATE;
1.44 jfb 914: else if (rcsnum_cmp(ent->ce_rev, cvs_addedrev, 2) == 0)
915: cfp->cf_cvstat = CVS_FST_ADDED;
916: else {
917: /* check last modified time */
918: if (ent->ce_mtime >= (time_t)st.st_mtime)
919: cfp->cf_cvstat = CVS_FST_UPTODATE;
920: else
921: cfp->cf_cvstat = CVS_FST_MODIFIED;
922: }
923:
924: cvs_ent_remove(CVS_DIR_ENTRIES(parent),
925: CVS_FILE_NAME(cfp));
1.14 jfb 926: }
1.44 jfb 927: } else {
928: if (ent == NULL) {
929: cvs_log(LP_ERR, "no Entry and no file for `%s'",
930: CVS_FILE_NAME(cfp));
931: cvs_file_free(cfp);
932: return (NULL);
933: } else
934: cfp->cf_cvstat = CVS_FST_LOST;
1.14 jfb 935: }
936:
1.26 jfb 937: if ((cfp->cf_type == DT_DIR) && (cvs_file_getdir(cfp, flags) < 0)) {
938: cvs_file_free(cfp);
939: return (NULL);
1.14 jfb 940: }
941:
942: return (cfp);
1.23 jfb 943: }
944:
945:
946: static int
947: cvs_file_cmpname(const char *name1, const char *name2)
948: {
949: return (cvs_nocase == 0) ? (strcmp(name1, name2)) :
950: (strcasecmp(name1, name2));
1.34 jfb 951: }
952:
953:
954: /*
955: * cvs_file_hashname()
956: *
957: * Generate an 8 bit hash value from the name of a file.
958: * XXX Improve my distribution!
959: */
960: static u_int8_t
961: cvs_file_hashname(const char *name)
962: {
963: const char *np;
964: u_int8_t h;
965:
966: h = 0xb5;
967: for (np = name; *np != '\0'; np++)
968: h ^= (*np << 3 ^ *np >> 1);
969:
970: return (h);
971: }
972:
973:
974: /*
975: * cvs_file_getname()
976: *
977: * Look for the file name <name> in the filename hash table.
978: * If no entry is found for that name, a new one is created and inserted into
979: * the table. The name's reference count is increased.
980: */
981: static struct cvs_fname*
982: cvs_file_getname(const char *name)
983: {
984: u_int8_t h;
985: struct cvs_fname *fnp;
986:
987: h = cvs_file_hashname(name);
988:
989: SLIST_FOREACH(fnp, &(cvs_fnht[h]), cf_list)
990: if (strcmp(name, fnp->cf_name) == 0) {
991: fnp->cf_ref++;
992: break;
993: }
994:
995: if (fnp == NULL) {
996: fnp = (struct cvs_fname *)malloc(sizeof(*fnp));
997: if (fnp == NULL) {
998: cvs_log(LP_ERRNO,
999: "failed to allocate new file name entry");
1000: return (NULL);
1001: }
1002:
1003: fnp->cf_name = strdup(name);
1.42 jfb 1004: if (fnp->cf_name == NULL) {
1005: cvs_log(LP_ERRNO, "failed to duplicate name");
1006: free(fnp);
1007: return (NULL);
1008: }
1009:
1.34 jfb 1010: fnp->cf_ref = 1;
1011: SLIST_INSERT_HEAD(&(cvs_fnht[h]), fnp, cf_list);
1012: }
1013:
1014: return (fnp);
1.47 jfb 1015: }
1016:
1017:
1018: /*
1019: * cvs_file_freename()
1020: *
1021: * Free the reference to a file name previously obtained with
1022: * cvs_file_getname().
1023: */
1024: static void
1025: cvs_file_freename(struct cvs_fname *fn)
1026: {
1027: u_int8_t h;
1028:
1029: if (fn->cf_ref == 0) {
1030: cvs_log(LP_WARN, "refcount for `%s' is already 0", fn->cf_name);
1031: return;
1032: }
1033:
1034: fn->cf_ref--;
1035: if (fn->cf_ref == 0) {
1036: /* no more references, free the file */
1037: h = cvs_file_hashname(fn->cf_name);
1038:
1039: SLIST_REMOVE(&(cvs_fnht[h]), fn, cvs_fname, cf_list);
1040: free(fn->cf_name);
1041: free(fn);
1042: }
1.14 jfb 1043: }