Annotation of src/usr.bin/cvs/util.c, Revision 1.140
1.140 ! joris 1: /* $OpenBSD: util.c,v 1.139 2008/02/10 14:04:40 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.71 xsa 4: * Copyright (c) 2005, 2006 Joris Vink <joris@openbsd.org>
5: * Copyright (c) 2005, 2006 Xavier Santolaria <xsa@openbsd.org>
1.9 jfb 6: * All rights reserved.
1.1 jfb 7: *
1.9 jfb 8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
1.1 jfb 11: *
1.9 jfb 12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
1.1 jfb 14: * 2. The name of the author may not be used to endorse or promote products
1.9 jfb 15: * derived from this software without specific prior written permission.
1.1 jfb 16: *
17: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.9 jfb 26: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 27: */
28:
1.107 otto 29: #include <sys/stat.h>
30: #include <sys/wait.h>
31:
32: #include <errno.h>
33: #include <md5.h>
34: #include <stdlib.h>
35: #include <string.h>
1.132 joris 36: #include <paths.h>
1.107 otto 37: #include <unistd.h>
1.1 jfb 38:
39: #include "cvs.h"
1.112 joris 40: #include "remote.h"
1.1 jfb 41:
1.116 tobias 42: extern int print_stdout;
43:
1.1 jfb 44: /* letter -> mode type map */
45: static const int cvs_modetypes[26] = {
46: -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1,
47: -1, 2, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1,
48: };
49:
50: /* letter -> mode map */
51: static const mode_t cvs_modes[3][26] = {
52: {
53: 0, 0, 0, 0, 0, 0, 0, /* a - g */
54: 0, 0, 0, 0, 0, 0, 0, /* h - m */
55: 0, 0, 0, S_IRUSR, 0, 0, 0, /* n - u */
56: 0, S_IWUSR, S_IXUSR, 0, 0 /* v - z */
57: },
58: {
59: 0, 0, 0, 0, 0, 0, 0, /* a - g */
60: 0, 0, 0, 0, 0, 0, 0, /* h - m */
61: 0, 0, 0, S_IRGRP, 0, 0, 0, /* n - u */
62: 0, S_IWGRP, S_IXGRP, 0, 0 /* v - z */
63: },
64: {
65: 0, 0, 0, 0, 0, 0, 0, /* a - g */
66: 0, 0, 0, 0, 0, 0, 0, /* h - m */
67: 0, 0, 0, S_IROTH, 0, 0, 0, /* n - u */
68: 0, S_IWOTH, S_IXOTH, 0, 0 /* v - z */
69: }
70: };
71:
72:
73: /* octal -> string */
74: static const char *cvs_modestr[8] = {
75: "", "x", "w", "wx", "r", "rx", "rw", "rwx"
76: };
77:
1.9 jfb 78: /*
1.1 jfb 79: * cvs_strtomode()
80: *
81: * Read the contents of the string <str> and generate a permission mode from
82: * the contents of <str>, which is assumed to have the mode format of CVS.
83: * The CVS protocol specification states that any modes or mode types that are
84: * not recognized should be silently ignored. This function does not return
85: * an error in such cases, but will issue warnings.
86: */
1.64 joris 87: void
1.1 jfb 88: cvs_strtomode(const char *str, mode_t *mode)
89: {
1.2 jfb 90: char type;
1.64 joris 91: size_t l;
1.1 jfb 92: mode_t m;
93: char buf[32], ms[4], *sp, *ep;
94:
95: m = 0;
1.64 joris 96: l = strlcpy(buf, str, sizeof(buf));
97: if (l >= sizeof(buf))
98: fatal("cvs_strtomode: string truncation");
99:
1.1 jfb 100: sp = buf;
101: ep = sp;
102:
103: for (sp = buf; ep != NULL; sp = ep + 1) {
104: ep = strchr(sp, ',');
105: if (ep != NULL)
1.2 jfb 106: *ep = '\0';
1.1 jfb 107:
1.14 weingart 108: memset(ms, 0, sizeof ms);
109: if (sscanf(sp, "%c=%3s", &type, ms) != 2 &&
110: sscanf(sp, "%c=", &type) != 1) {
1.89 joris 111: fatal("failed to scan mode string `%s'", sp);
1.1 jfb 112: }
113:
1.78 deraadt 114: if (type <= 'a' || type >= 'z' ||
115: cvs_modetypes[type - 'a'] == -1) {
1.79 joris 116: cvs_log(LP_ERR,
1.1 jfb 117: "invalid mode type `%c'"
118: " (`u', `g' or `o' expected), ignoring", type);
119: continue;
120: }
121:
122: /* make type contain the actual mode index */
123: type = cvs_modetypes[type - 'a'];
124:
125: for (sp = ms; *sp != '\0'; sp++) {
1.78 deraadt 126: if (*sp <= 'a' || *sp >= 'z' ||
127: cvs_modes[(int)type][*sp - 'a'] == 0) {
1.89 joris 128: fatal("invalid permission bit `%c'", *sp);
1.15 deraadt 129: } else
1.5 jfb 130: m |= cvs_modes[(int)type][*sp - 'a'];
1.1 jfb 131: }
132: }
133:
134: *mode = m;
135: }
136:
137: /*
138: * cvs_modetostr()
139: *
1.30 jfb 140: * Generate a CVS-format string to represent the permissions mask on a file
141: * from the mode <mode> and store the result in <buf>, which can accept up to
142: * <len> bytes (including the terminating NUL byte). The result is guaranteed
143: * to be NUL-terminated.
1.1 jfb 144: */
1.65 joris 145: void
1.1 jfb 146: cvs_modetostr(mode_t mode, char *buf, size_t len)
147: {
148: char tmp[16], *bp;
149: mode_t um, gm, om;
150:
151: um = (mode & S_IRWXU) >> 6;
152: gm = (mode & S_IRWXG) >> 3;
153: om = mode & S_IRWXO;
154:
155: bp = buf;
156: *bp = '\0';
157:
158: if (um) {
1.68 xsa 159: if (strlcpy(tmp, "u=", sizeof(tmp)) >= sizeof(tmp) ||
160: strlcat(tmp, cvs_modestr[um], sizeof(tmp)) >= sizeof(tmp))
1.65 joris 161: fatal("cvs_modetostr: overflow for user mode");
162:
1.68 xsa 163: if (strlcat(buf, tmp, len) >= len)
1.65 joris 164: fatal("cvs_modetostr: string truncation");
1.1 jfb 165: }
1.65 joris 166:
1.1 jfb 167: if (gm) {
1.65 joris 168: if (um) {
1.68 xsa 169: if (strlcat(buf, ",", len) >= len)
1.65 joris 170: fatal("cvs_modetostr: string truncation");
171: }
172:
1.68 xsa 173: if (strlcpy(tmp, "g=", sizeof(tmp)) >= sizeof(tmp) ||
174: strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65 joris 175: fatal("cvs_modetostr: overflow for group mode");
176:
1.68 xsa 177: if (strlcat(buf, tmp, len) >= len)
1.65 joris 178: fatal("cvs_modetostr: string truncation");
1.1 jfb 179: }
1.65 joris 180:
1.1 jfb 181: if (om) {
1.65 joris 182: if (um || gm) {
1.68 xsa 183: if (strlcat(buf, ",", len) >= len)
1.65 joris 184: fatal("cvs_modetostr: string truncation");
185: }
186:
1.68 xsa 187: if (strlcpy(tmp, "o=", sizeof(tmp)) >= sizeof(tmp) ||
188: strlcat(tmp, cvs_modestr[gm], sizeof(tmp)) >= sizeof(tmp))
1.65 joris 189: fatal("cvs_modetostr: overflow for others mode");
190:
1.68 xsa 191: if (strlcat(buf, tmp, len) >= len)
1.65 joris 192: fatal("cvs_modetostr: string truncation");
1.1 jfb 193: }
194: }
195:
196: /*
197: * cvs_cksum()
198: *
199: * Calculate the MD5 checksum of the file whose path is <file> and generate
200: * a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
201: * given in <len> and must be at least 33.
202: * Returns 0 on success, or -1 on failure.
203: */
204: int
205: cvs_cksum(const char *file, char *dst, size_t len)
206: {
207: if (len < CVS_CKSUM_LEN) {
1.79 joris 208: cvs_log(LP_ERR, "buffer too small for checksum");
1.1 jfb 209: return (-1);
210: }
211: if (MD5File(file, dst) == NULL) {
1.79 joris 212: cvs_log(LP_ERR, "failed to generate checksum for %s", file);
1.1 jfb 213: return (-1);
214: }
215:
216: return (0);
217: }
218:
219: /*
220: * cvs_getargv()
221: *
222: * Parse a line contained in <line> and generate an argument vector by
223: * splitting the line on spaces and tabs. The resulting vector is stored in
224: * <argv>, which can accept up to <argvlen> entries.
1.20 david 225: * Returns the number of arguments in the vector, or -1 if an error occurred.
1.1 jfb 226: */
227: int
228: cvs_getargv(const char *line, char **argv, int argvlen)
229: {
230: u_int i;
1.67 xsa 231: int argc, error;
1.118 tobias 232: char *linebuf, *lp, *cp;
1.1 jfb 233:
1.117 tobias 234: linebuf = xstrdup(line);
1.65 joris 235:
1.27 pat 236: memset(argv, 0, argvlen * sizeof(char *));
1.1 jfb 237: argc = 0;
238:
239: /* build the argument vector */
1.67 xsa 240: error = 0;
1.1 jfb 241: for (lp = linebuf; lp != NULL;) {
1.118 tobias 242: cp = strsep(&lp, " \t");
243: if (cp == NULL)
244: break;
245: else if (*cp == '\0')
246: continue;
1.1 jfb 247:
1.16 jfb 248: if (argc == argvlen) {
1.67 xsa 249: error++;
1.16 jfb 250: break;
251: }
252:
1.118 tobias 253: argv[argc] = xstrdup(cp);
1.1 jfb 254: argc++;
255: }
256:
1.67 xsa 257: if (error != 0) {
1.1 jfb 258: /* ditch the argument vector */
259: for (i = 0; i < (u_int)argc; i++)
1.59 joris 260: xfree(argv[i]);
1.1 jfb 261: argc = -1;
262: }
263:
1.117 tobias 264: xfree(linebuf);
1.1 jfb 265: return (argc);
1.17 jfb 266: }
267:
268: /*
269: * cvs_makeargv()
270: *
1.20 david 271: * Allocate an argument vector large enough to accommodate for all the
1.17 jfb 272: * arguments found in <line> and return it.
273: */
1.42 xsa 274: char **
1.17 jfb 275: cvs_makeargv(const char *line, int *argc)
276: {
277: int i, ret;
278: char *argv[1024], **copy;
279:
280: ret = cvs_getargv(line, argv, 1024);
281: if (ret == -1)
282: return (NULL);
283:
1.77 ray 284: copy = xcalloc(ret + 1, sizeof(char *));
1.17 jfb 285:
286: for (i = 0; i < ret; i++)
287: copy[i] = argv[i];
288: copy[ret] = NULL;
289:
290: *argc = ret;
291: return (copy);
1.1 jfb 292: }
293:
294: /*
295: * cvs_freeargv()
296: *
297: * Free an argument vector previously generated by cvs_getargv().
298: */
299: void
300: cvs_freeargv(char **argv, int argc)
301: {
302: int i;
303:
304: for (i = 0; i < argc; i++)
1.16 jfb 305: if (argv[i] != NULL)
1.59 joris 306: xfree(argv[i]);
1.38 xsa 307: }
308:
309: /*
310: * cvs_chdir()
311: *
1.63 xsa 312: * Change to directory <path>.
313: * If <rm> is equal to `1', <path> is removed if chdir() fails so we
314: * do not have temporary directories leftovers.
1.60 xsa 315: * Returns 0 on success.
1.50 xsa 316: */
1.38 xsa 317: int
1.63 xsa 318: cvs_chdir(const char *path, int rm)
1.38 xsa 319: {
1.63 xsa 320: if (chdir(path) == -1) {
321: if (rm == 1)
322: cvs_unlink(path);
1.60 xsa 323: fatal("cvs_chdir: `%s': %s", path, strerror(errno));
1.63 xsa 324: }
1.49 xsa 325:
326: return (0);
327: }
328:
329: /*
330: * cvs_rename()
331: * Change the name of a file.
332: * rename() wrapper with an error message.
1.60 xsa 333: * Returns 0 on success.
1.49 xsa 334: */
335: int
336: cvs_rename(const char *from, const char *to)
337: {
1.90 joris 338: if (cvs_server_active == 0)
339: cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
1.49 xsa 340:
341: if (cvs_noexec == 1)
342: return (0);
343:
1.60 xsa 344: if (rename(from, to) == -1)
345: fatal("cvs_rename: `%s'->`%s': %s", from, to, strerror(errno));
1.40 xsa 346:
347: return (0);
348: }
349:
350: /*
351: * cvs_unlink()
352: *
353: * Removes the link named by <path>.
354: * unlink() wrapper with an error message.
355: * Returns 0 on success, or -1 on failure.
356: */
357: int
358: cvs_unlink(const char *path)
359: {
1.90 joris 360: if (cvs_server_active == 0)
361: cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
1.40 xsa 362:
363: if (cvs_noexec == 1)
364: return (0);
365:
1.78 deraadt 366: if (unlink(path) == -1 && errno != ENOENT) {
1.94 xsa 367: cvs_log(LP_ERRNO, "%s", path);
1.38 xsa 368: return (-1);
369: }
370:
371: return (0);
1.1 jfb 372: }
1.24 joris 373:
374: /*
1.45 xsa 375: * cvs_rmdir()
1.30 jfb 376: *
377: * Remove a directory tree from disk.
378: * Returns 0 on success, or -1 on failure.
1.24 joris 379: */
380: int
1.45 xsa 381: cvs_rmdir(const char *path)
1.24 joris 382: {
1.99 todd 383: int type, ret = -1;
1.24 joris 384: DIR *dirp;
385: struct dirent *ent;
1.99 todd 386: struct stat st;
1.24 joris 387: char fpath[MAXPATHLEN];
1.46 xsa 388:
1.90 joris 389: if (cvs_server_active == 0)
390: cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
1.46 xsa 391:
392: if (cvs_noexec == 1)
393: return (0);
1.24 joris 394:
395: if ((dirp = opendir(path)) == NULL) {
1.79 joris 396: cvs_log(LP_ERR, "failed to open '%s'", path);
1.30 jfb 397: return (-1);
1.24 joris 398: }
399:
400: while ((ent = readdir(dirp)) != NULL) {
401: if (!strcmp(ent->d_name, ".") ||
402: !strcmp(ent->d_name, ".."))
403: continue;
404:
1.105 xsa 405: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
406: path, ent->d_name);
1.24 joris 407:
1.99 todd 408: if (ent->d_type == DT_UNKNOWN) {
1.104 todd 409: if (lstat(fpath, &st) == -1)
1.99 todd 410: fatal("'%s': %s", fpath, strerror(errno));
411:
412: switch (st.st_mode & S_IFMT) {
413: case S_IFDIR:
414: type = CVS_DIR;
415: break;
416: case S_IFREG:
417: type = CVS_FILE;
418: break;
419: default:
420: fatal("'%s': Unknown file type in copy",
421: fpath);
422: }
423: } else {
424: switch (ent->d_type) {
425: case DT_DIR:
426: type = CVS_DIR;
427: break;
428: case DT_REG:
429: type = CVS_FILE;
430: break;
431: default:
432: fatal("'%s': Unknown file type in copy",
433: fpath);
434: }
435: }
436: switch (type) {
1.128 xsa 437: case CVS_DIR:
1.45 xsa 438: if (cvs_rmdir(fpath) == -1)
1.33 pat 439: goto done;
1.99 todd 440: break;
441: case CVS_FILE:
442: if (cvs_unlink(fpath) == -1 && errno != ENOENT)
443: goto done;
444: break;
445: default:
446: fatal("type %d unknown, shouldn't happen", type);
447: }
1.24 joris 448: }
449:
450:
1.78 deraadt 451: if (rmdir(path) == -1 && errno != ENOENT) {
1.93 xsa 452: cvs_log(LP_ERRNO, "%s", path);
1.33 pat 453: goto done;
1.30 jfb 454: }
1.24 joris 455:
1.33 pat 456: ret = 0;
457: done:
458: closedir(dirp);
1.34 joris 459: return (ret);
460: }
461:
1.51 xsa 462: void
1.81 joris 463: cvs_get_repository_path(const char *dir, char *dst, size_t len)
464: {
465: char buf[MAXPATHLEN];
466:
467: cvs_get_repository_name(dir, buf, sizeof(buf));
1.105 xsa 468: (void)xsnprintf(dst, len, "%s/%s", current_cvsroot->cr_dir, buf);
1.139 joris 469: cvs_validate_directory(dst);
1.81 joris 470: }
471:
472: void
473: cvs_get_repository_name(const char *dir, char *dst, size_t len)
1.51 xsa 474: {
475: FILE *fp;
1.123 tobias 476: char fpath[MAXPATHLEN];
1.116 tobias 477:
1.127 tobias 478: dst[0] = '\0';
479:
480: if (!(cmdp->cmd_flags & CVS_USE_WDIR)) {
481: if (strlcpy(dst, dir, len) >= len)
1.116 tobias 482: fatal("cvs_get_repository_name: truncation");
483: return;
484: }
1.79 joris 485:
1.127 tobias 486: switch (cvs_cmdop) {
487: case CVS_OP_EXPORT:
488: if (strcmp(dir, "."))
489: if (strlcpy(dst, dir, len) >= len)
490: fatal("cvs_get_repository_name: truncation");
491: break;
492: case CVS_OP_IMPORT:
493: if (strlcpy(dst, import_repository, len) >= len)
494: fatal("cvs_get_repository_name: truncation");
495: if (strlcat(dst, "/", len) >= len)
496: fatal("cvs_get_repository_name: truncation");
1.84 joris 497:
1.127 tobias 498: if (strcmp(dir, "."))
499: if (strlcat(dst, dir, len) >= len)
1.85 joris 500: fatal("cvs_get_repository_name: truncation");
1.127 tobias 501: break;
502: default:
503: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
504: dir, CVS_PATH_REPOSITORY);
505: if ((fp = fopen(fpath, "r")) != NULL) {
506: if ((fgets(dst, len, fp)) == NULL)
507: fatal("%s: bad repository file", fpath);
508: dst[strcspn(dst, "\n")] = '\0';
509: (void)fclose(fp);
510: } else if (cvs_cmdop != CVS_OP_CHECKOUT)
511: fatal("%s is missing", fpath);
512: break;
1.51 xsa 513: }
514: }
515:
516: void
1.87 xsa 517: cvs_mkadmin(const char *path, const char *root, const char *repo,
1.131 tobias 518: char *tag, char *date)
1.51 xsa 519: {
520: FILE *fp;
1.79 joris 521: char buf[MAXPATHLEN];
1.51 xsa 522:
1.90 joris 523: if (cvs_server_active == 0)
1.131 tobias 524: cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s)",
1.90 joris 525: path, root, repo, (tag != NULL) ? tag : "",
1.131 tobias 526: (date != NULL) ? date : "");
1.51 xsa 527:
1.105 xsa 528: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_CVSDIR);
1.51 xsa 529:
1.79 joris 530: if (mkdir(buf, 0755) == -1 && errno != EEXIST)
531: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
532:
1.140 ! joris 533: if (cvs_cmdop == CVS_OP_CHECKOUT) {
! 534: (void)xsnprintf(buf, sizeof(buf), "%s/%s",
! 535: path, CVS_PATH_ROOTSPEC);
1.79 joris 536:
1.140 ! joris 537: if ((fp = fopen(buf, "w")) == NULL)
! 538: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.79 joris 539:
1.140 ! joris 540: fprintf(fp, "%s\n", root);
! 541: (void)fclose(fp);
! 542: }
1.51 xsa 543:
1.105 xsa 544: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_REPOSITORY);
1.51 xsa 545:
1.79 joris 546: if ((fp = fopen(buf, "w")) == NULL)
547: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51 xsa 548:
1.79 joris 549: fprintf(fp, "%s\n", repo);
1.51 xsa 550: (void)fclose(fp);
1.87 xsa 551:
1.131 tobias 552: cvs_write_tagfile(path, tag, date);
1.54 joris 553: }
554:
1.79 joris 555: void
1.112 joris 556: cvs_mkpath(const char *path, char *tag)
1.72 niallo 557: {
1.122 tobias 558: CVSENTRIES *ent;
1.79 joris 559: FILE *fp;
560: size_t len;
1.135 xsa 561: char *entry, sticky[CVS_REV_BUFSZ];
1.122 tobias 562: char *sp, *dp, *dir, *p, rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.138 joris 563:
564: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL ||
565: cvs_server_active == 1)
566: cvs_validate_directory(path);
1.79 joris 567:
568: dir = xstrdup(path);
569:
570: STRIP_SLASH(dir);
1.90 joris 571:
572: if (cvs_server_active == 0)
573: cvs_log(LP_TRACE, "cvs_mkpath(%s)", dir);
1.72 niallo 574:
1.79 joris 575: repo[0] = '\0';
576: rpath[0] = '\0';
1.72 niallo 577:
1.137 tobias 578: if (cvs_cmdop == CVS_OP_UPDATE || cvs_cmdop == CVS_OP_COMMIT) {
1.79 joris 579: if ((fp = fopen(CVS_PATH_REPOSITORY, "r")) != NULL) {
1.91 thib 580: if ((fgets(repo, sizeof(repo), fp)) == NULL)
581: fatal("cvs_mkpath: bad repository file");
1.120 gilles 582: repo[strcspn(repo, "\n")] = '\0';
1.79 joris 583: (void)fclose(fp);
584: }
1.72 niallo 585: }
586:
1.79 joris 587: for (sp = dir; sp != NULL; sp = dp) {
588: dp = strchr(sp, '/');
589: if (dp != NULL)
590: *(dp++) = '\0';
591:
1.130 joris 592: if (sp == dir && module_repo_root != NULL) {
593: len = strlcpy(repo, module_repo_root, sizeof(repo));
594: if (len >= (int)sizeof(repo))
595: fatal("cvs_mkpath: overflow");
1.136 joris 596: } else if (strcmp(sp, ".")) {
1.130 joris 597: if (repo[0] != '\0') {
598: len = strlcat(repo, "/", sizeof(repo));
599: if (len >= (int)sizeof(repo))
600: fatal("cvs_mkpath: overflow");
601: }
602:
603: len = strlcat(repo, sp, sizeof(repo));
1.79 joris 604: if (len >= (int)sizeof(repo))
605: fatal("cvs_mkpath: overflow");
606: }
607:
608: if (rpath[0] != '\0') {
609: len = strlcat(rpath, "/", sizeof(rpath));
610: if (len >= (int)sizeof(rpath))
611: fatal("cvs_mkpath: overflow");
612: }
613:
614: len = strlcat(rpath, sp, sizeof(rpath));
615: if (len >= (int)sizeof(rpath))
616: fatal("cvs_mkpath: overflow");
617:
618: if (mkdir(rpath, 0755) == -1 && errno != EEXIST)
619: fatal("cvs_mkpath: %s: %s", rpath, strerror(errno));
1.125 tobias 620:
621: if (cvs_cmdop == CVS_OP_EXPORT && !cvs_server_active)
622: continue;
1.72 niallo 623:
1.98 joris 624: cvs_mkadmin(rpath, current_cvsroot->cr_str, repo,
1.131 tobias 625: tag, NULL);
1.122 tobias 626:
627: if (dp != NULL) {
628: if ((p = strchr(dp, '/')) != NULL)
629: *p = '\0';
1.135 xsa 630:
631: entry = xmalloc(CVS_ENT_MAXLINELEN);
632: cvs_ent_line_str(dp, NULL, NULL, NULL, NULL, 1, 0,
633: entry, CVS_ENT_MAXLINELEN);
634:
1.122 tobias 635: ent = cvs_ent_open(rpath);
636: cvs_ent_add(ent, entry);
637: cvs_ent_close(ent, ENT_SYNC);
1.135 xsa 638: xfree(entry);
639:
1.122 tobias 640: if (p != NULL)
641: *p = '/';
642: }
1.112 joris 643:
644: if (cvs_server_active == 1 && strcmp(rpath, ".")) {
1.119 joris 645: if (tag != NULL) {
646: (void)xsnprintf(sticky, sizeof(sticky),
647: "T%s", tag);
648: cvs_server_set_sticky(rpath, sticky);
649: }
1.112 joris 650: }
1.79 joris 651: }
1.72 niallo 652:
1.79 joris 653: xfree(dir);
1.72 niallo 654: }
1.54 joris 655:
656: /*
657: * Split the contents of a file into a list of lines.
658: */
659: struct cvs_lines *
1.106 otto 660: cvs_splitlines(u_char *data, size_t len)
1.54 joris 661: {
1.97 niallo 662: u_char *p, *c;
663: size_t i, tlen;
1.54 joris 664: struct cvs_lines *lines;
665: struct cvs_line *lp;
666:
1.124 tobias 667: lines = xcalloc(1, sizeof(*lines));
1.54 joris 668: TAILQ_INIT(&(lines->l_lines));
669:
1.124 tobias 670: lp = xcalloc(1, sizeof(*lp));
1.54 joris 671: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
672:
1.97 niallo 673: p = c = data;
674: for (i = 0; i < len; i++) {
675: if (*p == '\n' || (i == len - 1)) {
676: tlen = p - c + 1;
1.124 tobias 677: lp = xcalloc(1, sizeof(*lp));
1.97 niallo 678: lp->l_line = c;
679: lp->l_len = tlen;
680: lp->l_lineno = ++(lines->l_nblines);
681: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
682: c = p + 1;
683: }
684: p++;
1.54 joris 685: }
686:
687: return (lines);
688: }
689:
690: void
691: cvs_freelines(struct cvs_lines *lines)
692: {
693: struct cvs_line *lp;
694:
695: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
696: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.102 niallo 697: if (lp->l_needsfree == 1)
698: xfree(lp->l_line);
1.59 joris 699: xfree(lp);
1.54 joris 700: }
701:
1.59 joris 702: xfree(lines);
1.70 niallo 703: }
704:
1.71 xsa 705: /*
706: * cvs_strsplit()
707: *
708: * Split a string <str> of <sep>-separated values and allocate
709: * an argument vector for the values found.
710: */
1.73 pat 711: struct cvs_argvector *
1.71 xsa 712: cvs_strsplit(char *str, const char *sep)
713: {
1.73 pat 714: struct cvs_argvector *av;
1.76 ray 715: size_t i = 0;
1.71 xsa 716: char *cp, *p;
717:
718: cp = xstrdup(str);
1.77 ray 719: av = xmalloc(sizeof(*av));
1.73 pat 720: av->str = cp;
1.113 ray 721: av->argv = xmalloc(sizeof(*(av->argv)));
1.71 xsa 722:
723: while ((p = strsep(&cp, sep)) != NULL) {
1.73 pat 724: av->argv[i++] = p;
1.111 ray 725: av->argv = xrealloc(av->argv,
1.77 ray 726: i + 1, sizeof(*(av->argv)));
1.71 xsa 727: }
1.73 pat 728: av->argv[i] = NULL;
729:
730: return (av);
731: }
1.71 xsa 732:
1.73 pat 733: /*
734: * cvs_argv_destroy()
735: *
736: * Free an argument vector previously allocated by cvs_strsplit().
737: */
738: void
739: cvs_argv_destroy(struct cvs_argvector *av)
740: {
741: xfree(av->str);
742: xfree(av->argv);
743: xfree(av);
1.82 joris 744: }
745:
746: u_int
747: cvs_revision_select(RCSFILE *file, char *range)
748: {
749: int i;
750: u_int nrev;
751: char *lstr, *rstr;
752: struct rcs_delta *rdp;
753: struct cvs_argvector *revargv, *revrange;
754: RCSNUM *lnum, *rnum;
755:
756: nrev = 0;
757: lnum = rnum = NULL;
758:
759: revargv = cvs_strsplit(range, ",");
760: for (i = 0; revargv->argv[i] != NULL; i++) {
761: revrange = cvs_strsplit(revargv->argv[i], ":");
762: if (revrange->argv[0] == NULL)
763: fatal("invalid revision range: %s", revargv->argv[i]);
764: else if (revrange->argv[1] == NULL)
765: lstr = rstr = revrange->argv[0];
766: else {
767: if (revrange->argv[2] != NULL)
768: fatal("invalid revision range: %s",
769: revargv->argv[i]);
770:
771: lstr = revrange->argv[0];
772: rstr = revrange->argv[1];
773:
774: if (strcmp(lstr, "") == 0)
775: lstr = NULL;
776: if (strcmp(rstr, "") == 0)
777: rstr = NULL;
778: }
779:
780: if (lstr == NULL)
781: lstr = RCS_HEAD_INIT;
782:
1.100 niallo 783: if ((lnum = rcs_translate_tag(lstr, file)) == NULL)
784: fatal("cvs_revision_select: could not translate tag `%s'", lstr);
1.82 joris 785:
786: if (rstr != NULL) {
1.100 niallo 787: if ((rnum = rcs_translate_tag(rstr, file)) == NULL)
788: fatal("cvs_revision_select: could not translate tag `%s'", rstr);
1.82 joris 789: } else {
790: rnum = rcsnum_alloc();
791: rcsnum_cpy(file->rf_head, rnum, 0);
792: }
793:
794: cvs_argv_destroy(revrange);
795:
796: TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
797: if (rcsnum_cmp(rdp->rd_num, lnum, 0) <= 0 &&
798: rcsnum_cmp(rdp->rd_num, rnum, 0) >= 0 &&
799: !(rdp->rd_flags & RCS_RD_SELECT)) {
800: rdp->rd_flags |= RCS_RD_SELECT;
801: nrev++;
802: }
803: }
1.129 joris 804:
805: rcsnum_free(lnum);
806: rcsnum_free(rnum);
1.82 joris 807: }
808:
809: cvs_argv_destroy(revargv);
810:
811: return (nrev);
1.95 xsa 812: }
813:
814: int
815: cvs_yesno(void)
816: {
817: int c, ret;
818:
819: ret = 0;
820:
821: fflush(stderr);
822: fflush(stdout);
823:
824: if ((c = getchar()) != 'y' && c != 'Y')
825: ret = -1;
826: else
827: while (c != EOF && c != '\n')
828: c = getchar();
829:
830: return (ret);
1.132 joris 831: }
832:
833: void
834: cvs_exec(const char *prog)
835: {
836: pid_t pid;
837: char *argp[] = { "sh", "-c", NULL, NULL };
838:
839: argp[2] = prog;
840:
841: if ((pid = fork()) == -1) {
842: cvs_log(LP_ERR, "cvs_exec: fork failed");
843: return;
844: } else if (pid == 0) {
845: execv(_PATH_BSHELL, argp);
846: cvs_log(LP_ERR, "failed to run '%s'", prog);
847: _exit(127);
848: }
1.71 xsa 849: }