Annotation of src/usr.bin/cvs/util.c, Revision 1.53
1.53 ! reyk 1: /* $OpenBSD: util.c,v 1.52 2005/09/15 17:01:10 xsa 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: * cvs_getargv()
286: *
287: * Parse a line contained in <line> and generate an argument vector by
288: * splitting the line on spaces and tabs. The resulting vector is stored in
289: * <argv>, which can accept up to <argvlen> entries.
1.20 david 290: * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1 jfb 291: */
292: int
293: cvs_getargv(const char *line, char **argv, int argvlen)
294: {
295: u_int i;
296: int argc, err;
297: char linebuf[256], qbuf[128], *lp, *cp, *arg;
298:
299: strlcpy(linebuf, line, sizeof(linebuf));
1.27 pat 300: memset(argv, 0, argvlen * sizeof(char *));
1.1 jfb 301: argc = 0;
302:
303: /* build the argument vector */
304: err = 0;
305: for (lp = linebuf; lp != NULL;) {
306: if (*lp == '"') {
307: /* double-quoted string */
308: lp++;
309: i = 0;
310: memset(qbuf, 0, sizeof(qbuf));
311: while (*lp != '"') {
1.16 jfb 312: if (*lp == '\\')
313: lp++;
1.1 jfb 314: if (*lp == '\0') {
315: cvs_log(LP_ERR, "no terminating quote");
316: err++;
317: break;
1.16 jfb 318: }
1.1 jfb 319:
1.9 jfb 320: qbuf[i++] = *lp++;
1.1 jfb 321: if (i == sizeof(qbuf)) {
322: err++;
323: break;
324: }
325: }
326:
327: arg = qbuf;
1.15 deraadt 328: } else {
1.1 jfb 329: cp = strsep(&lp, " \t");
330: if (cp == NULL)
331: break;
332: else if (*cp == '\0')
333: continue;
334:
335: arg = cp;
336: }
337:
1.16 jfb 338: if (argc == argvlen) {
339: err++;
340: break;
341: }
342:
1.1 jfb 343: argv[argc] = strdup(arg);
344: if (argv[argc] == NULL) {
345: cvs_log(LP_ERRNO, "failed to copy argument");
346: err++;
347: break;
348: }
349: argc++;
350: }
351:
1.43 xsa 352: if (err != 0) {
1.1 jfb 353: /* ditch the argument vector */
354: for (i = 0; i < (u_int)argc; i++)
355: free(argv[i]);
356: argc = -1;
357: }
358:
359: return (argc);
1.17 jfb 360: }
361:
362:
363: /*
364: * cvs_makeargv()
365: *
1.20 david 366: * Allocate an argument vector large enough to accommodate for all the
1.17 jfb 367: * arguments found in <line> and return it.
368: */
1.42 xsa 369: char **
1.17 jfb 370: cvs_makeargv(const char *line, int *argc)
371: {
372: int i, ret;
373: char *argv[1024], **copy;
1.27 pat 374: size_t size;
1.17 jfb 375:
376: ret = cvs_getargv(line, argv, 1024);
377: if (ret == -1)
378: return (NULL);
379:
1.27 pat 380: size = (ret + 1) * sizeof(char *);
381: copy = (char **)malloc(size);
1.17 jfb 382: if (copy == NULL) {
383: cvs_log(LP_ERRNO, "failed to allocate argument vector");
1.27 pat 384: cvs_freeargv(argv, ret);
1.17 jfb 385: return (NULL);
386: }
1.27 pat 387: memset(copy, 0, size);
1.17 jfb 388:
389: for (i = 0; i < ret; i++)
390: copy[i] = argv[i];
391: copy[ret] = NULL;
392:
393: *argc = ret;
394: return (copy);
1.1 jfb 395: }
396:
397:
398: /*
399: * cvs_freeargv()
400: *
401: * Free an argument vector previously generated by cvs_getargv().
402: */
403: void
404: cvs_freeargv(char **argv, int argc)
405: {
406: int i;
407:
408: for (i = 0; i < argc; i++)
1.16 jfb 409: if (argv[i] != NULL)
410: free(argv[i]);
1.2 jfb 411: }
412:
413:
414: /*
415: * cvs_mkadmin()
416: *
1.5 jfb 417: * Create the CVS administrative files within the directory <cdir>. If the
418: * files already exist, they are kept as is.
1.2 jfb 419: * Returns 0 on success, or -1 on failure.
420: */
421: int
1.52 xsa 422: cvs_mkadmin(const char *dpath, const char *rootpath, const char *repopath,
423: char *tag, char *date, int nb)
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.52 xsa 431: cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s, %d)",
432: dpath, rootpath, repopath, tag ? tag : "", date ? date : "", nb);
433:
1.37 xsa 434: l = cvs_path_cat(dpath, CVS_PATH_CVSDIR, path, sizeof(path));
435: if (l >= sizeof(path))
1.21 xsa 436: return (-1);
437:
1.28 joris 438: if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) {
1.2 jfb 439: cvs_log(LP_ERRNO, "failed to create directory %s", path);
440: return (-1);
441: }
442:
1.5 jfb 443: /* just create an empty Entries file */
1.13 jfb 444: ef = cvs_ent_open(dpath, O_WRONLY);
1.48 moritz 445: if (ef != NULL)
446: cvs_ent_close(ef);
1.2 jfb 447:
1.37 xsa 448: l = cvs_path_cat(dpath, CVS_PATH_ROOTSPEC, path, sizeof(path));
449: if (l >= sizeof(path))
1.21 xsa 450: return (-1);
451:
1.47 xsa 452: if ((stat(path, &st) == -1) && (errno == ENOENT)) {
1.5 jfb 453: fp = fopen(path, "w");
454: if (fp == NULL) {
455: cvs_log(LP_ERRNO, "failed to open %s", path);
456: return (-1);
457: }
1.28 joris 458: if (rootpath != NULL)
459: fprintf(fp, "%s\n", rootpath);
1.5 jfb 460: (void)fclose(fp);
1.2 jfb 461: }
462:
1.37 xsa 463: l = cvs_path_cat(dpath, CVS_PATH_REPOSITORY, path, sizeof(path));
464: if (l >= sizeof(path))
1.21 xsa 465: return (-1);
466:
1.47 xsa 467: if ((stat(path, &st) == -1) && (errno == ENOENT)) {
1.2 jfb 468: fp = fopen(path, "w");
469: if (fp == NULL) {
470: cvs_log(LP_ERRNO, "failed to open %s", path);
471: return (-1);
472: }
1.28 joris 473: if (repopath != NULL)
474: fprintf(fp, "%s\n", repopath);
1.2 jfb 475: (void)fclose(fp);
476: }
477:
1.52 xsa 478: /* create CVS/Tag file (if needed) */
479: (void)cvs_write_tagfile(tag, date, nb);
480:
1.2 jfb 481: return (0);
1.11 krapht 482: }
483:
484:
485: /*
486: * cvs_exec()
487: */
488: int
489: cvs_exec(int argc, char **argv, int fds[3])
490: {
491: int ret;
492: pid_t pid;
493:
494: if ((pid = fork()) == -1) {
495: cvs_log(LP_ERRNO, "failed to fork");
496: return (-1);
497: } else if (pid == 0) {
498: execvp(argv[0], argv);
1.13 jfb 499: cvs_log(LP_ERRNO, "failed to exec %s", argv[0]);
500: exit(1);
1.11 krapht 501: }
502:
503: if (waitpid(pid, &ret, 0) == -1)
1.13 jfb 504: cvs_log(LP_ERRNO, "failed to waitpid");
1.11 krapht 505:
506: return (ret);
1.38 xsa 507: }
508:
509: /*
510: * cvs_chdir()
511: *
512: * Change to directory.
513: * chdir() wrapper with an error message.
514: * Returns 0 on success, or -1 on failure.
1.50 xsa 515: */
1.38 xsa 516: int
517: cvs_chdir(const char *path)
518: {
519: if (chdir(path) == -1) {
520: cvs_log(LP_ERRNO, "cannot change to dir `%s'", path);
1.49 xsa 521: return (-1);
522: }
523:
524: return (0);
525: }
526:
527: /*
528: * cvs_rename()
529: * Change the name of a file.
530: * rename() wrapper with an error message.
531: * Returns 0 on success, or -1 on failure.
532: */
533: int
534: cvs_rename(const char *from, const char *to)
535: {
536: cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
537:
538: if (cvs_noexec == 1)
539: return (0);
540:
541: if (rename(from, to) == -1) {
542: cvs_log(LP_ERRNO, "cannot rename file `%s' to `%s'", from, to);
1.40 xsa 543: return (-1);
544: }
545:
546: return (0);
547: }
548:
549: /*
550: * cvs_unlink()
551: *
552: * Removes the link named by <path>.
553: * unlink() wrapper with an error message.
554: * Returns 0 on success, or -1 on failure.
555: */
556: int
557: cvs_unlink(const char *path)
558: {
559: cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
560:
561: if (cvs_noexec == 1)
562: return (0);
563:
1.44 joris 564: if ((unlink(path) == -1) && (errno != ENOENT)) {
1.40 xsa 565: cvs_log(LP_ERRNO, "cannot remove `%s'", path);
1.38 xsa 566: return (-1);
567: }
568:
569: return (0);
1.1 jfb 570: }
1.24 joris 571:
572: /*
1.45 xsa 573: * cvs_rmdir()
1.30 jfb 574: *
575: * Remove a directory tree from disk.
576: * Returns 0 on success, or -1 on failure.
1.24 joris 577: */
578: int
1.45 xsa 579: cvs_rmdir(const char *path)
1.24 joris 580: {
1.33 pat 581: int ret = -1;
1.30 jfb 582: size_t len;
1.24 joris 583: DIR *dirp;
584: struct dirent *ent;
585: char fpath[MAXPATHLEN];
1.46 xsa 586:
587: cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
588:
589: if (cvs_noexec == 1)
590: return (0);
1.24 joris 591:
592: if ((dirp = opendir(path)) == NULL) {
593: cvs_log(LP_ERRNO, "failed to open '%s'", path);
1.30 jfb 594: return (-1);
1.24 joris 595: }
596:
597: while ((ent = readdir(dirp)) != NULL) {
598: if (!strcmp(ent->d_name, ".") ||
599: !strcmp(ent->d_name, ".."))
600: continue;
601:
1.30 jfb 602: len = cvs_path_cat(path, ent->d_name, fpath, sizeof(fpath));
1.33 pat 603: if (len >= sizeof(fpath))
604: goto done;
1.24 joris 605:
606: if (ent->d_type == DT_DIR) {
1.45 xsa 607: if (cvs_rmdir(fpath) == -1)
1.33 pat 608: goto done;
1.41 xsa 609: } else if ((cvs_unlink(fpath) == -1) && (errno != ENOENT))
1.33 pat 610: goto done;
1.24 joris 611: }
612:
613:
1.30 jfb 614: if ((rmdir(path) == -1) && (errno != ENOENT)) {
1.24 joris 615: cvs_log(LP_ERRNO, "failed to remove '%s'", path);
1.33 pat 616: goto done;
1.30 jfb 617: }
1.24 joris 618:
1.33 pat 619: ret = 0;
620: done:
621: closedir(dirp);
1.34 joris 622: return (ret);
623: }
624:
625: /*
626: * Create a directory, and the parent directories if needed.
627: * based upon mkpath() from mkdir.c
628: */
629: int
630: cvs_create_dir(const char *path, int create_adm, char *root, char *repo)
631: {
632: size_t l;
633: int len, ret;
634: char *d, *s;
635: struct stat sb;
636: char rpath[MAXPATHLEN], entry[MAXPATHLEN];
637: CVSENTRIES *entf;
638: struct cvs_ent *ent;
639:
640: if (create_adm == 1 && (root == NULL || repo == NULL)) {
641: cvs_log(LP_ERR, "missing stuff in cvs_create_dir");
642: return (-1);
643: }
644:
645: if ((s = strdup(path)) == NULL)
646: return (-1);
647:
648: if (strlcpy(rpath, repo, sizeof(rpath)) >= sizeof(rpath) ||
649: strlcat(rpath, "/", sizeof(rpath)) >= sizeof(rpath)) {
650: errno = ENAMETOOLONG;
651: cvs_log(LP_ERRNO, "%s", rpath);
652: free(s);
653: return (-1);
654: }
655:
656: ret = -1;
657: entf = NULL;
658: d = strtok(s, "/");
659: while (d != NULL) {
660: if (stat(d, &sb)) {
661: /* try to create the directory */
662: if ((errno != ENOENT) || (mkdir(d, 0755) &&
663: errno != EEXIST)) {
664: cvs_log(LP_ERRNO, "failed to create `%s'", d);
665: goto done;
666: }
667: } else if (!S_ISDIR(sb.st_mode)) {
668: cvs_log(LP_ERR, "`%s' not a directory", d);
669: goto done;
670: }
671:
672: /*
673: * Create administrative files if requested.
674: */
1.43 xsa 675: if (create_adm == 1) {
1.34 joris 676: l = strlcat(rpath, d, sizeof(rpath));
677: if (l >= sizeof(rpath))
678: goto done;
679:
680: l = strlcat(rpath, "/", sizeof(rpath));
681: if (l >= sizeof(rpath))
682: goto done;
683:
1.52 xsa 684: if (cvs_mkadmin(d, root, rpath, NULL, NULL, 0) < 0) {
1.34 joris 685: cvs_log(LP_ERR, "failed to create adm files");
686: goto done;
687: }
688: }
689:
690: /*
691: * Add it to the parent directory entry file.
692: * (if any).
693: */
694: entf = cvs_ent_open(".", O_RDWR);
695: if (entf != NULL && strcmp(d, ".")) {
696: len = snprintf(entry, sizeof(entry), "D/%s////", d);
697: if (len == -1 || len >= (int)sizeof(entry)) {
698: errno = ENAMETOOLONG;
699: cvs_log(LP_ERRNO, "%s", entry);
700: goto done;
701: }
702:
703: if ((ent = cvs_ent_parse(entry)) == NULL) {
704: cvs_log(LP_ERR, "failed to parse entry");
705: goto done;
706: }
707:
708: cvs_ent_remove(entf, d);
709:
710: if (cvs_ent_add(entf, ent) < 0) {
711: cvs_log(LP_ERR, "failed to add entry");
712: goto done;
713: }
714: }
715:
716: if (entf != NULL) {
717: cvs_ent_close(entf);
718: entf = NULL;
719: }
720:
721: /*
722: * All went ok, switch to the newly created directory.
723: */
1.39 xsa 724: if (cvs_chdir(d) == -1)
1.34 joris 725: goto done;
726:
727: d = strtok(NULL, "/");
728: }
729:
730: ret = 0;
731: done:
732: if (entf != NULL)
733: cvs_ent_close(entf);
734: free(s);
1.33 pat 735: return (ret);
1.24 joris 736: }
737:
1.30 jfb 738: /*
739: * cvs_path_cat()
740: *
741: * Concatenate the two paths <base> and <end> and store the generated path
742: * into the buffer <dst>, which can accept up to <dlen> bytes, including the
743: * NUL byte. The result is guaranteed to be NUL-terminated.
744: * Returns the number of bytes necessary to store the full resulting path,
745: * not including the NUL byte (a value equal to or larger than <dlen>
746: * indicates truncation).
747: */
1.29 jfb 748: size_t
749: cvs_path_cat(const char *base, const char *end, char *dst, size_t dlen)
750: {
751: size_t len;
752:
753: len = strlcpy(dst, base, dlen);
754: if (len >= dlen - 1) {
755: errno = ENAMETOOLONG;
756: cvs_log(LP_ERRNO, "%s", dst);
757: } else {
758: dst[len] = '/';
759: dst[len + 1] = '\0';
760: len = strlcat(dst, end, dlen);
761: if (len >= dlen) {
762: errno = ENAMETOOLONG;
763: cvs_log(LP_ERRNO, "%s", dst);
764: }
765: }
766:
767: return (len);
1.35 xsa 768: }
769:
770: /*
771: * cvs_rcs_getpath()
772: *
773: * Get the RCS path of the file <file> and store it in <buf>, which is
774: * of size <len>. For portability, it is recommended that <buf> always be
775: * at least MAXPATHLEN bytes long.
776: * Returns a pointer to the start of the path on success, or NULL on failure.
777: */
1.42 xsa 778: char *
1.35 xsa 779: cvs_rcs_getpath(CVSFILE *file, char *buf, size_t len)
780: {
781: int l;
782: char *repo;
783: struct cvsroot *root;
784:
785: root = CVS_DIR_ROOT(file);
786: repo = CVS_DIR_REPO(file);
787:
1.36 deraadt 788: l = snprintf(buf, len, "%s/%s/%s%s",
1.35 xsa 789: root->cr_dir, repo, file->cf_name, RCS_FILE_EXT);
790: if (l == -1 || l >= (int)len) {
791: errno = ENAMETOOLONG;
792: cvs_log(LP_ERRNO, "%s", buf);
793: return (NULL);
794: }
795:
796: return (buf);
1.51 xsa 797: }
798:
799: /*
800: * cvs_write_tagfile()
801: *
802: * Write the CVS/Tag file for current directory.
803: */
804: void
805: cvs_write_tagfile(char *tag, char *date, int nb)
806: {
807: FILE *fp;
808: char tagpath[MAXPATHLEN];
809:
810: if (cvs_noexec == 1)
811: return;
812:
813: if (strlcpy(tagpath, CVS_PATH_TAG, sizeof(tagpath)) >= sizeof(tagpath))
814: return;
815:
816: if ((tag != NULL) || (date != NULL)) {
817: fp = fopen(tagpath, "w+");
818: if (fp == NULL) {
819: if (errno != ENOENT)
820: cvs_log(LP_NOTICE,
821: "failed to open `%s' : %s", tagpath,
822: strerror(errno));
823: return;
824: }
825: if (tag != NULL) {
826: if (nb != 0)
827: fprintf(fp, "N%s\n", tag);
828: else
829: fprintf(fp, "T%s\n", tag);
830: } else {
831: fprintf(fp, "D%s\n", date);
832: }
833: (void)fclose(fp);
834: } else {
835: cvs_unlink(tagpath);
836: return;
837: }
838: }
839:
840: /*
841: * cvs_parse_tagfile()
842: *
843: * Parse the CVS/Tag file for current directory.
844: *
845: * If it contains a branch tag, sets <tagp>.
846: * If it contains a date, sets <datep>.
847: * If it contains a non-branch tag, sets <nbp>.
848: *
849: * Returns nothing but an error message, and sets <tagp>, <datep> to NULL
850: * and <nbp> to 0.
851: */
852: void
853: cvs_parse_tagfile(char **tagp, char **datep, int *nbp)
854: {
855: FILE *fp;
856: int linenum;
857: size_t len;
858: char linebuf[128], tagpath[MAXPATHLEN];
859:
860: if (tagp != NULL)
861: *tagp = (char *)NULL;
862:
863: if (datep != NULL)
864: *datep = (char *)NULL;
865:
866: if (nbp != NULL)
867: *nbp = 0;
868:
869: if (strlcpy(tagpath, CVS_PATH_TAG, sizeof(tagpath)) >= sizeof(tagpath))
870: return;
871:
872: fp = fopen(tagpath, "r");
873: if (fp == NULL) {
874: if (errno != ENOENT)
875: cvs_log(LP_NOTICE, "failed to open `%s' : %s", tagpath,
876: strerror(errno));
877: return;
878: }
879:
880: linenum = 0;
881:
882: while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
883: linenum++;
884: if ((len = strlen(linebuf)) == 0)
885: continue;
886: if (linebuf[len -1] != '\n') {
887: cvs_log(LP_WARN, "line too long in `%s:%d'", tagpath,
888: linenum);
889: break;
890: }
891: linebuf[--len] = '\0';
892:
1.53 ! reyk 893: switch (*linebuf) {
1.51 xsa 894: case 'T':
895: if (tagp != NULL)
896: *tagp = strdup(linebuf);
897: break;
898: case 'D':
899: if (datep != NULL)
900: *datep = strdup(linebuf);
901: break;
902: case 'N':
903: if (tagp != NULL)
904: *tagp = strdup(linebuf);
905: if (nbp != NULL)
906: *nbp = 1;
907: break;
908: default:
909: break;
910: }
911: }
912: if (ferror(fp))
913: cvs_log(LP_NOTICE, "failed to read line from `%s'", tagpath);
914:
915: (void)fclose(fp);
1.29 jfb 916: }