Annotation of src/usr.bin/cvs/file.c, Revision 1.60
1.60 ! joris 1: /* $OpenBSD: file.c,v 1.59 2005/03/29 17:37:37 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.26 jfb 4: * All rights reserved.
1.1 jfb 5: *
1.26 jfb 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.26 jfb 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.26 jfb 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.26 jfb 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28: #include <sys/queue.h>
29: #include <sys/stat.h>
30:
31: #include <pwd.h>
32: #include <errno.h>
33: #include <stdio.h>
34: #include <fcntl.h>
1.51 jfb 35: #include <libgen.h>
1.1 jfb 36: #include <dirent.h>
37: #include <stdlib.h>
38: #include <unistd.h>
39: #include <string.h>
40: #include <fnmatch.h>
41:
42: #include "cvs.h"
43: #include "log.h"
1.14 jfb 44: #include "file.h"
1.57 jfb 45: #include "strtab.h"
1.1 jfb 46:
47:
48: #define CVS_IGN_STATIC 0x01 /* pattern is static, no need to glob */
49:
50: #define CVS_CHAR_ISMETA(c) ((c == '*') || (c == '?') || (c == '['))
51:
52:
53: /* ignore pattern */
54: struct cvs_ignpat {
55: char ip_pat[MAXNAMLEN];
56: int ip_flags;
57: TAILQ_ENTRY (cvs_ignpat) ip_list;
58: };
59:
60:
61: /*
62: * Standard patterns to ignore.
63: */
64: static const char *cvs_ign_std[] = {
65: ".",
66: "..",
67: "*.o",
68: "*.so",
1.45 xsa 69: "*.a",
1.1 jfb 70: "*.bak",
71: "*.orig",
72: "*.rej",
1.45 xsa 73: "*.old",
1.1 jfb 74: "*.exe",
75: "*.depend",
1.45 xsa 76: "*.obj",
77: "*.elc",
78: "*.ln",
79: "*.olb",
1.1 jfb 80: "CVS",
81: "core",
1.49 jfb 82: "*.core",
1.8 jfb 83: ".#*",
1.45 xsa 84: "*~",
85: "_$*",
86: "*$",
1.1 jfb 87: #ifdef OLD_SMELLY_CRUFT
88: "RCSLOG",
89: "tags",
90: "TAGS",
91: "RCS",
92: "SCCS",
1.45 xsa 93: "cvslog.*", /* to ignore CVS_CLIENT_LOG output */
1.1 jfb 94: "#*",
95: ",*",
96: #endif
97: };
98:
99:
1.11 jfb 100: /*
101: * Entries in the CVS/Entries file with a revision of '0' have only been
102: * added. Compare against this revision to see if this is the case
103: */
1.4 jfb 104: static RCSNUM *cvs_addedrev;
105:
106:
1.1 jfb 107: TAILQ_HEAD(, cvs_ignpat) cvs_ign_pats;
108:
109:
1.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:
1.59 joris 383: if (cvs_file_attach(cf, nf) < 0) {
384: cvs_file_free(base);
385: return (NULL);
386: }
1.26 jfb 387: }
388:
389: if (np != NULL) {
390: *np = '/';
391: sp = np + 1;
392: }
393:
394: cf = nf;
395: } while (np != NULL);
396: }
397:
398: return (base);
1.3 jfb 399: }
400:
401:
402: /*
1.13 jfb 403: * cvs_file_find()
404: *
405: * Find the pointer to a CVS file entry within the file hierarchy <hier>.
406: * The file's pathname <path> must be relative to the base of <hier>.
407: * Returns the entry on success, or NULL on failure.
408: */
409: CVSFILE*
410: cvs_file_find(CVSFILE *hier, const char *path)
411: {
412: char *pp, *sp, pbuf[MAXPATHLEN];
413: CVSFILE *sf, *cf;
414:
415: strlcpy(pbuf, path, sizeof(pbuf));
416:
417: cf = hier;
418: pp = pbuf;
419: do {
420: sp = strchr(pp, '/');
421: if (sp != NULL)
1.24 jfb 422: *(sp++) = '\0';
1.13 jfb 423:
424: /* special case */
425: if (*pp == '.') {
426: if ((*(pp + 1) == '.') && (*(pp + 2) == '\0')) {
427: /* request to go back to parent */
428: if (cf->cf_parent == NULL) {
429: cvs_log(LP_NOTICE,
430: "path %s goes back too far", path);
431: return (NULL);
432: }
433: cf = cf->cf_parent;
434: continue;
1.38 deraadt 435: } else if (*(pp + 1) == '\0')
1.13 jfb 436: continue;
437: }
438:
439: TAILQ_FOREACH(sf, &(cf->cf_ddat->cd_files), cf_list)
1.34 jfb 440: if (cvs_file_cmpname(pp, CVS_FILE_NAME(sf)) == 0)
1.13 jfb 441: break;
442: if (sf == NULL)
443: return (NULL);
444:
445: cf = sf;
446: pp = sp;
447: } while (sp != NULL);
448:
1.24 jfb 449: return (cf);
1.13 jfb 450: }
451:
452:
453: /*
1.34 jfb 454: * cvs_file_getpath()
455: *
456: * Get the full path of the file <file> and store it in <buf>, which is of
457: * size <len>. For portability, it is recommended that <buf> always be
458: * at least MAXPATHLEN bytes long.
459: * Returns a pointer to the start of the path on success, or NULL on failure.
460: */
461: char*
462: cvs_file_getpath(CVSFILE *file, char *buf, size_t len)
463: {
464: u_int i;
465: char *fp, *namevec[CVS_FILE_MAXDEPTH];
466: CVSFILE *top;
467:
468: buf[0] = '\0';
469: i = CVS_FILE_MAXDEPTH;
470: memset(namevec, 0, sizeof(namevec));
471:
472: /* find the top node */
473: for (top = file; (top != NULL) && (i > 0); top = top->cf_parent) {
474: fp = CVS_FILE_NAME(top);
475:
476: /* skip self-references */
477: if ((fp[0] == '.') && (fp[1] == '\0'))
478: continue;
479: namevec[--i] = fp;
480: }
481:
482: if (i == 0)
483: return (NULL);
484: else if (i == CVS_FILE_MAXDEPTH) {
485: strlcpy(buf, ".", len);
486: return (buf);
487: }
488:
489: while (i < CVS_FILE_MAXDEPTH - 1) {
490: strlcat(buf, namevec[i++], len);
491: strlcat(buf, "/", len);
492: }
493: strlcat(buf, namevec[i], len);
494:
495: return (buf);
496: }
497:
498:
499: /*
1.22 jfb 500: * cvs_file_attach()
501: *
502: * Attach the file <file> as one of the children of parent <parent>, which
503: * has to be a file of type DT_DIR.
504: * Returns 0 on success, or -1 on failure.
505: */
506: int
507: cvs_file_attach(CVSFILE *parent, CVSFILE *file)
508: {
1.23 jfb 509: struct cvs_dir *dp;
1.22 jfb 510:
511: if (parent->cf_type != DT_DIR)
512: return (-1);
513:
1.23 jfb 514: dp = parent->cf_ddat;
515:
516: TAILQ_INSERT_TAIL(&(dp->cd_files), file, cf_list);
517: dp->cd_nfiles++;
1.22 jfb 518: file->cf_parent = parent;
519:
520: return (0);
521: }
522:
523:
524: /*
1.3 jfb 525: * cvs_file_getdir()
526: *
527: * Get a cvs directory structure for the directory whose path is <dir>.
1.30 jfb 528: * This function should not free the directory information on error, as this
529: * is performed by cvs_file_free().
1.3 jfb 530: */
1.6 jfb 531: static int
1.14 jfb 532: cvs_file_getdir(CVSFILE *cf, int flags)
1.3 jfb 533: {
1.13 jfb 534: int ret, fd;
1.16 jfb 535: u_int ndirs;
1.3 jfb 536: long base;
1.43 jfb 537: u_char *dp, *ep;
1.34 jfb 538: char fbuf[2048], pbuf[MAXPATHLEN], fpath[MAXPATHLEN];
1.3 jfb 539: struct dirent *ent;
1.14 jfb 540: CVSFILE *cfp;
1.20 jfb 541: struct stat st;
1.3 jfb 542: struct cvs_dir *cdp;
1.44 jfb 543: struct cvs_ent *cvsent;
1.10 jfb 544: struct cvs_flist dirs;
1.3 jfb 545:
1.18 jfb 546: ndirs = 0;
1.10 jfb 547: TAILQ_INIT(&dirs);
1.7 jfb 548: cdp = cf->cf_ddat;
549:
1.34 jfb 550: cvs_file_getpath(cf, fpath, sizeof(fpath));
551:
1.48 jfb 552: cdp->cd_root = cvsroot_get(fpath);
553: if (cdp->cd_root == NULL)
554: return (-1);
555:
1.26 jfb 556: if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
557: if (flags & CF_MKADMIN)
558: cvs_mkadmin(cf, 0755);
1.14 jfb 559:
1.26 jfb 560: /* if the CVS administrative directory exists, load the info */
1.34 jfb 561: snprintf(pbuf, sizeof(pbuf), "%s/" CVS_PATH_CVSDIR, fpath);
1.26 jfb 562: if ((stat(pbuf, &st) == 0) && S_ISDIR(st.st_mode)) {
1.34 jfb 563: if (cvs_readrepo(fpath, pbuf, sizeof(pbuf)) == 0) {
1.26 jfb 564: cdp->cd_repo = strdup(pbuf);
565: if (cdp->cd_repo == NULL) {
1.28 jfb 566: cvs_log(LP_ERRNO,
567: "failed to dup repository string");
1.26 jfb 568: return (-1);
569: }
1.20 jfb 570: }
1.26 jfb 571:
1.37 jfb 572: cdp->cd_ent = cvs_ent_open(fpath, O_RDONLY);
1.20 jfb 573: }
574: }
1.14 jfb 575:
1.56 jfb 576: if ((flags & CF_KNOWN) && (cf->cf_cvstat == CVS_FST_UNKNOWN))
1.26 jfb 577: return (0);
578:
1.34 jfb 579: fd = open(fpath, O_RDONLY);
1.3 jfb 580: if (fd == -1) {
1.34 jfb 581: cvs_log(LP_ERRNO, "failed to open `%s'", fpath);
1.6 jfb 582: return (-1);
1.3 jfb 583: }
584:
1.44 jfb 585: /* To load all files, we first get the entries for the directory and
586: * load the information for each of those entries. The handle to
587: * the Entries file kept in the directory data is only temporary and
588: * the files should remove their entry when they use it. After all
589: * files in the directory have been processed, the Entries handle
590: * should only be left with those entries for which no real file
591: * exists. We then build file structures for those files too, as
592: * we will likely receive fresh copies from the server as part of the
593: * response.
594: */
1.11 jfb 595: do {
596: ret = getdirentries(fd, fbuf, sizeof(fbuf), &base);
597: if (ret == -1) {
598: cvs_log(LP_ERRNO, "failed to get directory entries");
599: (void)close(fd);
600: return (-1);
601: }
1.10 jfb 602:
1.11 jfb 603: dp = fbuf;
604: ep = fbuf + (size_t)ret;
605: while (dp < ep) {
606: ent = (struct dirent *)dp;
1.32 jfb 607: dp += ent->d_reclen;
1.31 jfb 608: if (ent->d_fileno == 0)
609: continue;
1.11 jfb 610:
611: if ((flags & CF_IGNORE) && cvs_file_chkign(ent->d_name))
1.24 jfb 612: continue;
613:
614: if ((flags & CF_NOSYMS) && (ent->d_type == DT_LNK))
1.11 jfb 615: continue;
1.56 jfb 616:
617: if (!(flags & CF_RECURSE) && (ent->d_type == DT_DIR)) {
618: if (cdp->cd_ent != NULL)
619: (void)cvs_ent_remove(cdp->cd_ent,
620: ent->d_name);
621: continue;
622: }
1.11 jfb 623:
1.34 jfb 624: snprintf(pbuf, sizeof(pbuf), "%s/%s", fpath,
625: ent->d_name);
1.14 jfb 626: cfp = cvs_file_lget(pbuf, flags, cf);
1.55 jfb 627: if (cfp == NULL) {
628: (void)close(fd);
629: return (-1);
630: }
631:
632: if (cfp->cf_type == DT_DIR) {
633: TAILQ_INSERT_TAIL(&dirs, cfp, cf_list);
634: ndirs++;
635: } else {
636: TAILQ_INSERT_TAIL(&(cdp->cd_files), cfp,
637: cf_list);
638: cdp->cd_nfiles++;
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)
1.57 jfb 695: cvs_strfree(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);
1.60 ! joris 717: if (ret != 0)
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: CVSFILE *cfp;
826: struct cvs_dir *ddat;
827:
1.14 jfb 828: cfp = (CVSFILE *)malloc(sizeof(*cfp));
1.6 jfb 829: if (cfp == NULL) {
830: cvs_log(LP_ERRNO, "failed to allocate CVS file data");
831: return (NULL);
832: }
833: memset(cfp, 0, sizeof(*cfp));
834:
1.57 jfb 835: cfp->cf_name = cvs_strdup(basename(path));
1.34 jfb 836: if (cfp->cf_name == NULL) {
1.57 jfb 837: cvs_log(LP_ERR, "failed to copy file name");
1.58 tedu 838: cvs_file_free(cfp);
1.6 jfb 839: return (NULL);
840: }
841: cfp->cf_type = type;
842: cfp->cf_cvstat = CVS_FST_UNKNOWN;
843:
844: if (type == DT_DIR) {
845: ddat = (struct cvs_dir *)malloc(sizeof(*ddat));
846: if (ddat == NULL) {
1.41 jfb 847: cvs_log(LP_ERRNO, "failed to allocate directory data");
1.6 jfb 848: cvs_file_free(cfp);
849: return (NULL);
850: }
851: memset(ddat, 0, sizeof(*ddat));
1.10 jfb 852: TAILQ_INIT(&(ddat->cd_files));
1.6 jfb 853: cfp->cf_ddat = ddat;
854: }
855: return (cfp);
1.1 jfb 856: }
1.14 jfb 857:
858:
859: /*
860: * cvs_file_lget()
861: *
862: * Get the file and link it with the parent right away.
1.22 jfb 863: * Returns a pointer to the created file structure on success, or NULL on
864: * failure.
1.14 jfb 865: */
866: static CVSFILE*
867: cvs_file_lget(const char *path, int flags, CVSFILE *parent)
868: {
1.44 jfb 869: int ret, cwd;
870: u_int type;
1.14 jfb 871: struct stat st;
872: CVSFILE *cfp;
1.34 jfb 873: struct cvs_ent *ent = NULL;
1.14 jfb 874:
1.44 jfb 875: type = DT_UNKNOWN;
876: cwd = (strcmp(path, ".") == 0) ? 1 : 0;
1.14 jfb 877:
1.44 jfb 878: ret = stat(path, &st);
879: if (ret == 0)
880: type = IFTODT(st.st_mode);
1.14 jfb 881:
1.44 jfb 882: if ((cfp = cvs_file_alloc(path, type)) == NULL)
1.14 jfb 883: return (NULL);
884: cfp->cf_parent = parent;
1.54 joris 885:
886: if ((cfp->cf_type == DT_DIR) && (cfp->cf_parent == NULL))
887: cfp->cf_ddat->cd_flags |= CVS_DIRF_BASE;
1.14 jfb 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.14 jfb 940: }