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