Annotation of src/usr.bin/cvs/util.c, Revision 1.45
1.45 ! xsa 1: /* $OpenBSD: util.c,v 1.44 2005/07/26 14:46:24 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.9 jfb 4: * All rights reserved.
1.1 jfb 5: *
1.9 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.9 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.9 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.9 jfb 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
27: #include <sys/types.h>
28: #include <sys/stat.h>
1.12 krapht 29: #include <sys/wait.h>
1.1 jfb 30:
31: #include <errno.h>
1.2 jfb 32: #include <fcntl.h>
1.31 xsa 33: #include <md5.h>
1.1 jfb 34: #include <stdio.h>
35: #include <stdlib.h>
1.31 xsa 36: #include <string.h>
1.1 jfb 37: #include <unistd.h>
38:
39: #include "cvs.h"
40: #include "log.h"
41:
42: /* letter -> mode type map */
43: static const int cvs_modetypes[26] = {
44: -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1,
45: -1, 2, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1,
46: };
47:
48: /* letter -> mode map */
49: static const mode_t cvs_modes[3][26] = {
50: {
51: 0, 0, 0, 0, 0, 0, 0, /* a - g */
52: 0, 0, 0, 0, 0, 0, 0, /* h - m */
53: 0, 0, 0, S_IRUSR, 0, 0, 0, /* n - u */
54: 0, S_IWUSR, S_IXUSR, 0, 0 /* v - z */
55: },
56: {
57: 0, 0, 0, 0, 0, 0, 0, /* a - g */
58: 0, 0, 0, 0, 0, 0, 0, /* h - m */
59: 0, 0, 0, S_IRGRP, 0, 0, 0, /* n - u */
60: 0, S_IWGRP, S_IXGRP, 0, 0 /* v - z */
61: },
62: {
63: 0, 0, 0, 0, 0, 0, 0, /* a - g */
64: 0, 0, 0, 0, 0, 0, 0, /* h - m */
65: 0, 0, 0, S_IROTH, 0, 0, 0, /* n - u */
66: 0, S_IWOTH, S_IXOTH, 0, 0 /* v - z */
67: }
68: };
69:
70:
71: /* octal -> string */
72: static const char *cvs_modestr[8] = {
73: "", "x", "w", "wx", "r", "rx", "rw", "rwx"
74: };
75:
76:
1.11 krapht 77: pid_t cvs_exec_pid;
78:
1.1 jfb 79:
80: /*
81: * cvs_readrepo()
82: *
83: * Read the path stored in the `Repository' CVS file for a given directory
84: * <dir>, and store that path into the buffer pointed to by <dst>, whose size
85: * is <len>.
86: */
87: int
88: cvs_readrepo(const char *dir, char *dst, size_t len)
89: {
1.37 xsa 90: size_t dlen, l;
1.1 jfb 91: FILE *fp;
92: char repo_path[MAXPATHLEN];
93:
1.37 xsa 94: l = cvs_path_cat(dir, "CVS/Repository", repo_path, sizeof(repo_path));
95: if (l >= sizeof(repo_path))
1.21 xsa 96: return (NULL);
97:
1.1 jfb 98: fp = fopen(repo_path, "r");
1.21 xsa 99: if (fp == NULL)
1.1 jfb 100: return (-1);
101:
102: if (fgets(dst, (int)len, fp) == NULL) {
103: if (ferror(fp)) {
104: cvs_log(LP_ERRNO, "failed to read from `%s'",
105: repo_path);
106: }
107: (void)fclose(fp);
108: return (-1);
109: }
110: dlen = strlen(dst);
111: if ((dlen > 0) && (dst[dlen - 1] == '\n'))
112: dst[--dlen] = '\0';
113:
114: (void)fclose(fp);
115: return (0);
1.9 jfb 116: }
117:
118:
119: /*
1.1 jfb 120: * cvs_strtomode()
121: *
122: * Read the contents of the string <str> and generate a permission mode from
123: * the contents of <str>, which is assumed to have the mode format of CVS.
124: * The CVS protocol specification states that any modes or mode types that are
125: * not recognized should be silently ignored. This function does not return
126: * an error in such cases, but will issue warnings.
127: * Returns 0 on success, or -1 on failure.
128: */
129: int
130: cvs_strtomode(const char *str, mode_t *mode)
131: {
1.2 jfb 132: char type;
1.1 jfb 133: mode_t m;
134: char buf[32], ms[4], *sp, *ep;
135:
136: m = 0;
1.30 jfb 137: if (strlcpy(buf, str, sizeof(buf)) >= sizeof(buf)) {
138: return (-1);
139: }
1.1 jfb 140: sp = buf;
141: ep = sp;
142:
143: for (sp = buf; ep != NULL; sp = ep + 1) {
144: ep = strchr(sp, ',');
145: if (ep != NULL)
1.2 jfb 146: *ep = '\0';
1.1 jfb 147:
1.14 weingart 148: memset(ms, 0, sizeof ms);
149: if (sscanf(sp, "%c=%3s", &type, ms) != 2 &&
150: sscanf(sp, "%c=", &type) != 1) {
1.1 jfb 151: cvs_log(LP_WARN, "failed to scan mode string `%s'", sp);
152: continue;
153: }
154:
155: if ((type <= 'a') || (type >= 'z') ||
156: (cvs_modetypes[type - 'a'] == -1)) {
157: cvs_log(LP_WARN,
158: "invalid mode type `%c'"
159: " (`u', `g' or `o' expected), ignoring", type);
160: continue;
161: }
162:
163: /* make type contain the actual mode index */
164: type = cvs_modetypes[type - 'a'];
165:
166: for (sp = ms; *sp != '\0'; sp++) {
167: if ((*sp <= 'a') || (*sp >= 'z') ||
1.5 jfb 168: (cvs_modes[(int)type][*sp - 'a'] == 0)) {
1.1 jfb 169: cvs_log(LP_WARN,
170: "invalid permission bit `%c'", *sp);
1.15 deraadt 171: } else
1.5 jfb 172: m |= cvs_modes[(int)type][*sp - 'a'];
1.1 jfb 173: }
174: }
175:
176: *mode = m;
177:
178: return (0);
179: }
180:
181:
182: /*
183: * cvs_modetostr()
184: *
1.30 jfb 185: * Generate a CVS-format string to represent the permissions mask on a file
186: * from the mode <mode> and store the result in <buf>, which can accept up to
187: * <len> bytes (including the terminating NUL byte). The result is guaranteed
188: * to be NUL-terminated.
1.1 jfb 189: * Returns 0 on success, or -1 on failure.
190: */
191: int
192: cvs_modetostr(mode_t mode, char *buf, size_t len)
193: {
194: size_t l;
195: char tmp[16], *bp;
196: mode_t um, gm, om;
197:
198: um = (mode & S_IRWXU) >> 6;
199: gm = (mode & S_IRWXG) >> 3;
200: om = mode & S_IRWXO;
201:
202: bp = buf;
203: *bp = '\0';
204: l = 0;
205:
206: if (um) {
207: snprintf(tmp, sizeof(tmp), "u=%s", cvs_modestr[um]);
1.9 jfb 208: l = strlcat(buf, tmp, len);
1.1 jfb 209: }
210: if (gm) {
211: if (um)
212: strlcat(buf, ",", len);
213: snprintf(tmp, sizeof(tmp), "g=%s", cvs_modestr[gm]);
1.9 jfb 214: strlcat(buf, tmp, len);
1.1 jfb 215: }
216: if (om) {
217: if (um || gm)
218: strlcat(buf, ",", len);
219: snprintf(tmp, sizeof(tmp), "o=%s", cvs_modestr[gm]);
1.9 jfb 220: strlcat(buf, tmp, len);
1.1 jfb 221: }
222:
223: return (0);
224: }
225:
226: /*
227: * cvs_cksum()
228: *
229: * Calculate the MD5 checksum of the file whose path is <file> and generate
230: * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
231: * given in <len> and must be at least 33.
232: * Returns 0 on success, or -1 on failure.
233: */
234: int
235: cvs_cksum(const char *file, char *dst, size_t len)
236: {
237: if (len < CVS_CKSUM_LEN) {
238: cvs_log(LP_WARN, "buffer too small for checksum");
239: return (-1);
240: }
241: if (MD5File(file, dst) == NULL) {
1.19 jfb 242: cvs_log(LP_ERRNO, "failed to generate checksum for %s", file);
1.1 jfb 243: return (-1);
244: }
245:
246: return (0);
247: }
248:
249: /*
250: * cvs_splitpath()
251: *
1.7 jfb 252: * Split a path <path> into the base portion and the filename portion.
253: * The path is copied in <base> and the last delimiter is replaced by a NUL
254: * byte. The <file> pointer is set to point to the first character after
255: * that delimiter.
1.1 jfb 256: * Returns 0 on success, or -1 on failure.
257: */
258: int
1.7 jfb 259: cvs_splitpath(const char *path, char *base, size_t blen, char **file)
1.1 jfb 260: {
261: size_t rlen;
1.7 jfb 262: char *sp;
1.1 jfb 263:
1.7 jfb 264: if ((rlen = strlcpy(base, path, blen)) >= blen)
265: return (-1);
1.6 jfb 266:
1.7 jfb 267: while ((rlen > 0) && (base[rlen - 1] == '/'))
268: base[--rlen] = '\0';
269:
270: sp = strrchr(base, '/');
1.1 jfb 271: if (sp == NULL) {
1.7 jfb 272: strlcpy(base, "./", blen);
273: strlcat(base, path, blen);
274: sp = base + 1;
1.1 jfb 275: }
276:
1.7 jfb 277: *sp = '\0';
278: if (file != NULL)
279: *file = sp + 1;
1.1 jfb 280:
281: return (0);
282: }
283:
284:
285: /*
286: * cvs_getargv()
287: *
288: * Parse a line contained in <line> and generate an argument vector by
289: * splitting the line on spaces and tabs. The resulting vector is stored in
290: * <argv>, which can accept up to <argvlen> entries.
1.20 david 291: * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1 jfb 292: */
293: int
294: cvs_getargv(const char *line, char **argv, int argvlen)
295: {
296: u_int i;
297: int argc, err;
298: char linebuf[256], qbuf[128], *lp, *cp, *arg;
299:
300: strlcpy(linebuf, line, sizeof(linebuf));
1.27 pat 301: memset(argv, 0, argvlen * sizeof(char *));
1.1 jfb 302: argc = 0;
303:
304: /* build the argument vector */
305: err = 0;
306: for (lp = linebuf; lp != NULL;) {
307: if (*lp == '"') {
308: /* double-quoted string */
309: lp++;
310: i = 0;
311: memset(qbuf, 0, sizeof(qbuf));
312: while (*lp != '"') {
1.16 jfb 313: if (*lp == '\\')
314: lp++;
1.1 jfb 315: if (*lp == '\0') {
316: cvs_log(LP_ERR, "no terminating quote");
317: err++;
318: break;
1.16 jfb 319: }
1.1 jfb 320:
1.9 jfb 321: qbuf[i++] = *lp++;
1.1 jfb 322: if (i == sizeof(qbuf)) {
323: err++;
324: break;
325: }
326: }
327:
328: arg = qbuf;
1.15 deraadt 329: } else {
1.1 jfb 330: cp = strsep(&lp, " \t");
331: if (cp == NULL)
332: break;
333: else if (*cp == '\0')
334: continue;
335:
336: arg = cp;
337: }
338:
1.16 jfb 339: if (argc == argvlen) {
340: err++;
341: break;
342: }
343:
1.1 jfb 344: argv[argc] = strdup(arg);
345: if (argv[argc] == NULL) {
346: cvs_log(LP_ERRNO, "failed to copy argument");
347: err++;
348: break;
349: }
350: argc++;
351: }
352:
1.43 xsa 353: if (err != 0) {
1.1 jfb 354: /* ditch the argument vector */
355: for (i = 0; i < (u_int)argc; i++)
356: free(argv[i]);
357: argc = -1;
358: }
359:
360: return (argc);
1.17 jfb 361: }
362:
363:
364: /*
365: * cvs_makeargv()
366: *
1.20 david 367: * Allocate an argument vector large enough to accommodate for all the
1.17 jfb 368: * arguments found in <line> and return it.
369: */
1.42 xsa 370: char **
1.17 jfb 371: cvs_makeargv(const char *line, int *argc)
372: {
373: int i, ret;
374: char *argv[1024], **copy;
1.27 pat 375: size_t size;
1.17 jfb 376:
377: ret = cvs_getargv(line, argv, 1024);
378: if (ret == -1)
379: return (NULL);
380:
1.27 pat 381: size = (ret + 1) * sizeof(char *);
382: copy = (char **)malloc(size);
1.17 jfb 383: if (copy == NULL) {
384: cvs_log(LP_ERRNO, "failed to allocate argument vector");
1.27 pat 385: cvs_freeargv(argv, ret);
1.17 jfb 386: return (NULL);
387: }
1.27 pat 388: memset(copy, 0, size);
1.17 jfb 389:
390: for (i = 0; i < ret; i++)
391: copy[i] = argv[i];
392: copy[ret] = NULL;
393:
394: *argc = ret;
395: return (copy);
1.1 jfb 396: }
397:
398:
399: /*
400: * cvs_freeargv()
401: *
402: * Free an argument vector previously generated by cvs_getargv().
403: */
404: void
405: cvs_freeargv(char **argv, int argc)
406: {
407: int i;
408:
409: for (i = 0; i < argc; i++)
1.16 jfb 410: if (argv[i] != NULL)
411: free(argv[i]);
1.2 jfb 412: }
413:
414:
415: /*
416: * cvs_mkadmin()
417: *
1.5 jfb 418: * Create the CVS administrative files within the directory <cdir>. If the
419: * files already exist, they are kept as is.
1.2 jfb 420: * Returns 0 on success, or -1 on failure.
421: */
422: int
1.28 joris 423: cvs_mkadmin(const char *dpath, const char *rootpath, const char *repopath)
1.2 jfb 424: {
1.37 xsa 425: size_t l;
1.28 joris 426: char path[MAXPATHLEN];
1.2 jfb 427: FILE *fp;
428: CVSENTRIES *ef;
1.5 jfb 429: struct stat st;
1.13 jfb 430:
1.37 xsa 431: l = cvs_path_cat(dpath, CVS_PATH_CVSDIR, path, sizeof(path));
432: if (l >= sizeof(path))
1.21 xsa 433: return (-1);
434:
1.28 joris 435: if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) {
1.2 jfb 436: cvs_log(LP_ERRNO, "failed to create directory %s", path);
437: return (-1);
438: }
439:
1.5 jfb 440: /* just create an empty Entries file */
1.13 jfb 441: ef = cvs_ent_open(dpath, O_WRONLY);
1.2 jfb 442: (void)cvs_ent_close(ef);
443:
1.37 xsa 444: l = cvs_path_cat(dpath, CVS_PATH_ROOTSPEC, path, sizeof(path));
445: if (l >= sizeof(path))
1.21 xsa 446: return (-1);
447:
1.28 joris 448: if ((stat(path, &st) != 0) && (errno == ENOENT)) {
1.5 jfb 449: fp = fopen(path, "w");
450: if (fp == NULL) {
451: cvs_log(LP_ERRNO, "failed to open %s", path);
452: return (-1);
453: }
1.28 joris 454: if (rootpath != NULL)
455: fprintf(fp, "%s\n", rootpath);
1.5 jfb 456: (void)fclose(fp);
1.2 jfb 457: }
458:
1.37 xsa 459: l = cvs_path_cat(dpath, CVS_PATH_REPOSITORY, path, sizeof(path));
460: if (l >= sizeof(path))
1.21 xsa 461: return (-1);
462:
1.28 joris 463: if ((stat(path, &st) != 0) && (errno == ENOENT)) {
1.2 jfb 464: fp = fopen(path, "w");
465: if (fp == NULL) {
466: cvs_log(LP_ERRNO, "failed to open %s", path);
467: return (-1);
468: }
1.28 joris 469: if (repopath != NULL)
470: fprintf(fp, "%s\n", repopath);
1.2 jfb 471: (void)fclose(fp);
472: }
473:
474: return (0);
1.11 krapht 475: }
476:
477:
478: /*
479: * cvs_exec()
480: */
481: int
482: cvs_exec(int argc, char **argv, int fds[3])
483: {
484: int ret;
485: pid_t pid;
486:
487: if ((pid = fork()) == -1) {
488: cvs_log(LP_ERRNO, "failed to fork");
489: return (-1);
490: } else if (pid == 0) {
491: execvp(argv[0], argv);
1.13 jfb 492: cvs_log(LP_ERRNO, "failed to exec %s", argv[0]);
493: exit(1);
1.11 krapht 494: }
495:
496: if (waitpid(pid, &ret, 0) == -1)
1.13 jfb 497: cvs_log(LP_ERRNO, "failed to waitpid");
1.11 krapht 498:
499: return (ret);
1.38 xsa 500: }
501:
502: /*
503: * cvs_chdir()
504: *
505: * Change to directory.
506: * chdir() wrapper with an error message.
507: * Returns 0 on success, or -1 on failure.
508: */
509: int
510: cvs_chdir(const char *path)
511: {
512: if (chdir(path) == -1) {
513: cvs_log(LP_ERRNO, "cannot change to dir `%s'", path);
1.40 xsa 514: return (-1);
515: }
516:
517: return (0);
518: }
519:
520: /*
521: * cvs_unlink()
522: *
523: * Removes the link named by <path>.
524: * unlink() wrapper with an error message.
525: * Returns 0 on success, or -1 on failure.
526: */
527: int
528: cvs_unlink(const char *path)
529: {
530: cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
531:
532: if (cvs_noexec == 1)
533: return (0);
534:
1.44 joris 535: if ((unlink(path) == -1) && (errno != ENOENT)) {
1.40 xsa 536: cvs_log(LP_ERRNO, "cannot remove `%s'", path);
1.38 xsa 537: return (-1);
538: }
539:
540: return (0);
1.1 jfb 541: }
1.24 joris 542:
543: /*
1.45 ! xsa 544: * cvs_rmdir()
1.30 jfb 545: *
546: * Remove a directory tree from disk.
547: * Returns 0 on success, or -1 on failure.
1.24 joris 548: */
549: int
1.45 ! xsa 550: cvs_rmdir(const char *path)
1.24 joris 551: {
1.33 pat 552: int ret = -1;
1.30 jfb 553: size_t len;
1.24 joris 554: DIR *dirp;
555: struct dirent *ent;
556: char fpath[MAXPATHLEN];
557:
558: if ((dirp = opendir(path)) == NULL) {
559: cvs_log(LP_ERRNO, "failed to open '%s'", path);
1.30 jfb 560: return (-1);
1.24 joris 561: }
562:
563: while ((ent = readdir(dirp)) != NULL) {
564: if (!strcmp(ent->d_name, ".") ||
565: !strcmp(ent->d_name, ".."))
566: continue;
567:
1.30 jfb 568: len = cvs_path_cat(path, ent->d_name, fpath, sizeof(fpath));
1.33 pat 569: if (len >= sizeof(fpath))
570: goto done;
1.24 joris 571:
572: if (ent->d_type == DT_DIR) {
1.45 ! xsa 573: if (cvs_rmdir(fpath) == -1)
1.33 pat 574: goto done;
1.41 xsa 575: } else if ((cvs_unlink(fpath) == -1) && (errno != ENOENT))
1.33 pat 576: goto done;
1.24 joris 577: }
578:
579:
1.30 jfb 580: if ((rmdir(path) == -1) && (errno != ENOENT)) {
1.24 joris 581: cvs_log(LP_ERRNO, "failed to remove '%s'", path);
1.33 pat 582: goto done;
1.30 jfb 583: }
1.24 joris 584:
1.33 pat 585: ret = 0;
586: done:
587: closedir(dirp);
1.34 joris 588: return (ret);
589: }
590:
591: /*
592: * Create a directory, and the parent directories if needed.
593: * based upon mkpath() from mkdir.c
594: */
595: int
596: cvs_create_dir(const char *path, int create_adm, char *root, char *repo)
597: {
598: size_t l;
599: int len, ret;
600: char *d, *s;
601: struct stat sb;
602: char rpath[MAXPATHLEN], entry[MAXPATHLEN];
603: CVSENTRIES *entf;
604: struct cvs_ent *ent;
605:
606: if (create_adm == 1 && (root == NULL || repo == NULL)) {
607: cvs_log(LP_ERR, "missing stuff in cvs_create_dir");
608: return (-1);
609: }
610:
611: if ((s = strdup(path)) == NULL)
612: return (-1);
613:
614: if (strlcpy(rpath, repo, sizeof(rpath)) >= sizeof(rpath) ||
615: strlcat(rpath, "/", sizeof(rpath)) >= sizeof(rpath)) {
616: errno = ENAMETOOLONG;
617: cvs_log(LP_ERRNO, "%s", rpath);
618: free(s);
619: return (-1);
620: }
621:
622: ret = -1;
623: entf = NULL;
624: d = strtok(s, "/");
625: while (d != NULL) {
626: if (stat(d, &sb)) {
627: /* try to create the directory */
628: if ((errno != ENOENT) || (mkdir(d, 0755) &&
629: errno != EEXIST)) {
630: cvs_log(LP_ERRNO, "failed to create `%s'", d);
631: goto done;
632: }
633: } else if (!S_ISDIR(sb.st_mode)) {
634: cvs_log(LP_ERR, "`%s' not a directory", d);
635: goto done;
636: }
637:
638: /*
639: * Create administrative files if requested.
640: */
1.43 xsa 641: if (create_adm == 1) {
1.34 joris 642: l = strlcat(rpath, d, sizeof(rpath));
643: if (l >= sizeof(rpath))
644: goto done;
645:
646: l = strlcat(rpath, "/", sizeof(rpath));
647: if (l >= sizeof(rpath))
648: goto done;
649:
650: if (cvs_mkadmin(d, root, rpath) < 0) {
651: cvs_log(LP_ERR, "failed to create adm files");
652: goto done;
653: }
654: }
655:
656: /*
657: * Add it to the parent directory entry file.
658: * (if any).
659: */
660: entf = cvs_ent_open(".", O_RDWR);
661: if (entf != NULL && strcmp(d, ".")) {
662: len = snprintf(entry, sizeof(entry), "D/%s////", d);
663: if (len == -1 || len >= (int)sizeof(entry)) {
664: errno = ENAMETOOLONG;
665: cvs_log(LP_ERRNO, "%s", entry);
666: goto done;
667: }
668:
669: if ((ent = cvs_ent_parse(entry)) == NULL) {
670: cvs_log(LP_ERR, "failed to parse entry");
671: goto done;
672: }
673:
674: cvs_ent_remove(entf, d);
675:
676: if (cvs_ent_add(entf, ent) < 0) {
677: cvs_log(LP_ERR, "failed to add entry");
678: goto done;
679: }
680: }
681:
682: if (entf != NULL) {
683: cvs_ent_close(entf);
684: entf = NULL;
685: }
686:
687: /*
688: * All went ok, switch to the newly created directory.
689: */
1.39 xsa 690: if (cvs_chdir(d) == -1)
1.34 joris 691: goto done;
692:
693: d = strtok(NULL, "/");
694: }
695:
696: ret = 0;
697: done:
698: if (entf != NULL)
699: cvs_ent_close(entf);
700: free(s);
1.33 pat 701: return (ret);
1.24 joris 702: }
703:
1.30 jfb 704: /*
705: * cvs_path_cat()
706: *
707: * Concatenate the two paths <base> and <end> and store the generated path
708: * into the buffer <dst>, which can accept up to <dlen> bytes, including the
709: * NUL byte. The result is guaranteed to be NUL-terminated.
710: * Returns the number of bytes necessary to store the full resulting path,
711: * not including the NUL byte (a value equal to or larger than <dlen>
712: * indicates truncation).
713: */
1.29 jfb 714: size_t
715: cvs_path_cat(const char *base, const char *end, char *dst, size_t dlen)
716: {
717: size_t len;
718:
719: len = strlcpy(dst, base, dlen);
720: if (len >= dlen - 1) {
721: errno = ENAMETOOLONG;
722: cvs_log(LP_ERRNO, "%s", dst);
723: } else {
724: dst[len] = '/';
725: dst[len + 1] = '\0';
726: len = strlcat(dst, end, dlen);
727: if (len >= dlen) {
728: errno = ENAMETOOLONG;
729: cvs_log(LP_ERRNO, "%s", dst);
730: }
731: }
732:
733: return (len);
1.35 xsa 734: }
735:
736: /*
737: * cvs_rcs_getpath()
738: *
739: * Get the RCS path of the file <file> and store it in <buf>, which is
740: * of size <len>. For portability, it is recommended that <buf> always be
741: * at least MAXPATHLEN bytes long.
742: * Returns a pointer to the start of the path on success, or NULL on failure.
743: */
1.42 xsa 744: char *
1.35 xsa 745: cvs_rcs_getpath(CVSFILE *file, char *buf, size_t len)
746: {
747: int l;
748: char *repo;
749: struct cvsroot *root;
750:
751: root = CVS_DIR_ROOT(file);
752: repo = CVS_DIR_REPO(file);
753:
1.36 deraadt 754: l = snprintf(buf, len, "%s/%s/%s%s",
1.35 xsa 755: root->cr_dir, repo, file->cf_name, RCS_FILE_EXT);
756: if (l == -1 || l >= (int)len) {
757: errno = ENAMETOOLONG;
758: cvs_log(LP_ERRNO, "%s", buf);
759: return (NULL);
760: }
761:
762: return (buf);
1.29 jfb 763: }