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