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