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