Annotation of src/usr.bin/cvs/util.c, Revision 1.134
1.134 ! joris 1: /* $OpenBSD: util.c,v 1.133 2008/02/09 16:52:27 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.81 joris 469: }
470:
471: void
472: cvs_get_repository_name(const char *dir, char *dst, size_t len)
1.51 xsa 473: {
474: FILE *fp;
1.123 tobias 475: char fpath[MAXPATHLEN];
1.116 tobias 476:
1.127 tobias 477: dst[0] = '\0';
478:
479: if (!(cmdp->cmd_flags & CVS_USE_WDIR)) {
480: if (strlcpy(dst, dir, len) >= len)
1.116 tobias 481: fatal("cvs_get_repository_name: truncation");
482: return;
483: }
1.79 joris 484:
1.127 tobias 485: switch (cvs_cmdop) {
486: case CVS_OP_EXPORT:
487: if (strcmp(dir, "."))
488: if (strlcpy(dst, dir, len) >= len)
489: fatal("cvs_get_repository_name: truncation");
490: break;
491: case CVS_OP_IMPORT:
492: if (strlcpy(dst, import_repository, len) >= len)
493: fatal("cvs_get_repository_name: truncation");
494: if (strlcat(dst, "/", len) >= len)
495: fatal("cvs_get_repository_name: truncation");
1.84 joris 496:
1.127 tobias 497: if (strcmp(dir, "."))
498: if (strlcat(dst, dir, len) >= len)
1.85 joris 499: fatal("cvs_get_repository_name: truncation");
1.127 tobias 500: break;
501: default:
502: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
503: dir, CVS_PATH_REPOSITORY);
504: if ((fp = fopen(fpath, "r")) != NULL) {
505: if ((fgets(dst, len, fp)) == NULL)
506: fatal("%s: bad repository file", fpath);
507: dst[strcspn(dst, "\n")] = '\0';
508: (void)fclose(fp);
509: } else if (cvs_cmdop != CVS_OP_CHECKOUT)
510: fatal("%s is missing", fpath);
511: break;
1.51 xsa 512: }
513: }
514:
515: void
1.87 xsa 516: cvs_mkadmin(const char *path, const char *root, const char *repo,
1.131 tobias 517: char *tag, char *date)
1.51 xsa 518: {
519: FILE *fp;
1.79 joris 520: char buf[MAXPATHLEN];
1.51 xsa 521:
1.90 joris 522: if (cvs_server_active == 0)
1.131 tobias 523: cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s)",
1.90 joris 524: path, root, repo, (tag != NULL) ? tag : "",
1.131 tobias 525: (date != NULL) ? date : "");
1.51 xsa 526:
1.105 xsa 527: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_CVSDIR);
1.51 xsa 528:
1.79 joris 529: if (mkdir(buf, 0755) == -1 && errno != EEXIST)
530: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
531:
1.105 xsa 532: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_ROOTSPEC);
1.79 joris 533:
534: if ((fp = fopen(buf, "w")) == NULL)
535: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
536:
537: fprintf(fp, "%s\n", root);
538: (void)fclose(fp);
1.51 xsa 539:
1.105 xsa 540: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_REPOSITORY);
1.51 xsa 541:
1.79 joris 542: if ((fp = fopen(buf, "w")) == NULL)
543: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51 xsa 544:
1.79 joris 545: fprintf(fp, "%s\n", repo);
1.51 xsa 546: (void)fclose(fp);
1.87 xsa 547:
1.131 tobias 548: cvs_write_tagfile(path, tag, date);
1.54 joris 549: }
550:
1.79 joris 551: void
1.112 joris 552: cvs_mkpath(const char *path, char *tag)
1.72 niallo 553: {
1.122 tobias 554: CVSENTRIES *ent;
1.79 joris 555: FILE *fp;
556: size_t len;
1.122 tobias 557: char entry[CVS_ENT_MAXLINELEN], sticky[CVS_REV_BUFSZ];
558: char *sp, *dp, *dir, *p, rpath[MAXPATHLEN], repo[MAXPATHLEN];
1.79 joris 559:
560: dir = xstrdup(path);
561:
562: STRIP_SLASH(dir);
1.90 joris 563:
564: if (cvs_server_active == 0)
565: cvs_log(LP_TRACE, "cvs_mkpath(%s)", dir);
1.72 niallo 566:
1.79 joris 567: repo[0] = '\0';
568: rpath[0] = '\0';
1.72 niallo 569:
1.79 joris 570: if (cvs_cmdop == CVS_OP_UPDATE) {
571: if ((fp = fopen(CVS_PATH_REPOSITORY, "r")) != NULL) {
1.91 thib 572: if ((fgets(repo, sizeof(repo), fp)) == NULL)
573: fatal("cvs_mkpath: bad repository file");
1.120 gilles 574: repo[strcspn(repo, "\n")] = '\0';
1.79 joris 575: (void)fclose(fp);
576: }
1.72 niallo 577: }
578:
1.79 joris 579: for (sp = dir; sp != NULL; sp = dp) {
580: dp = strchr(sp, '/');
581: if (dp != NULL)
582: *(dp++) = '\0';
583:
1.130 joris 584: if (sp == dir && module_repo_root != NULL) {
585: len = strlcpy(repo, module_repo_root, sizeof(repo));
586: if (len >= (int)sizeof(repo))
587: fatal("cvs_mkpath: overflow");
588: } else {
589: if (repo[0] != '\0') {
590: len = strlcat(repo, "/", sizeof(repo));
591: if (len >= (int)sizeof(repo))
592: fatal("cvs_mkpath: overflow");
593: }
594:
595: len = strlcat(repo, sp, sizeof(repo));
1.79 joris 596: if (len >= (int)sizeof(repo))
597: fatal("cvs_mkpath: overflow");
598: }
599:
600: if (rpath[0] != '\0') {
601: len = strlcat(rpath, "/", sizeof(rpath));
602: if (len >= (int)sizeof(rpath))
603: fatal("cvs_mkpath: overflow");
604: }
605:
606: len = strlcat(rpath, sp, sizeof(rpath));
607: if (len >= (int)sizeof(rpath))
608: fatal("cvs_mkpath: overflow");
609:
610: if (mkdir(rpath, 0755) == -1 && errno != EEXIST)
611: fatal("cvs_mkpath: %s: %s", rpath, strerror(errno));
1.125 tobias 612:
613: if (cvs_cmdop == CVS_OP_EXPORT && !cvs_server_active)
614: continue;
1.72 niallo 615:
1.98 joris 616: cvs_mkadmin(rpath, current_cvsroot->cr_str, repo,
1.131 tobias 617: tag, NULL);
1.122 tobias 618:
619: if (dp != NULL) {
620: if ((p = strchr(dp, '/')) != NULL)
621: *p = '\0';
622: ent = cvs_ent_open(rpath);
1.126 tobias 623: xsnprintf(entry, sizeof(entry), "D/%s////", dp);
1.122 tobias 624: cvs_ent_add(ent, entry);
625: cvs_ent_close(ent, ENT_SYNC);
626: if (p != NULL)
627: *p = '/';
628: }
1.112 joris 629:
630: if (cvs_server_active == 1 && strcmp(rpath, ".")) {
1.119 joris 631: if (tag != NULL) {
632: (void)xsnprintf(sticky, sizeof(sticky),
633: "T%s", tag);
634: cvs_server_set_sticky(rpath, sticky);
635: }
1.112 joris 636: }
1.79 joris 637: }
1.72 niallo 638:
1.79 joris 639: xfree(dir);
1.72 niallo 640: }
1.54 joris 641:
642: /*
643: * Split the contents of a file into a list of lines.
644: */
645: struct cvs_lines *
1.106 otto 646: cvs_splitlines(u_char *data, size_t len)
1.54 joris 647: {
1.97 niallo 648: u_char *p, *c;
649: size_t i, tlen;
1.54 joris 650: struct cvs_lines *lines;
651: struct cvs_line *lp;
652:
1.124 tobias 653: lines = xcalloc(1, sizeof(*lines));
1.54 joris 654: TAILQ_INIT(&(lines->l_lines));
655:
1.124 tobias 656: lp = xcalloc(1, sizeof(*lp));
1.54 joris 657: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
658:
1.97 niallo 659: p = c = data;
660: for (i = 0; i < len; i++) {
661: if (*p == '\n' || (i == len - 1)) {
662: tlen = p - c + 1;
1.124 tobias 663: lp = xcalloc(1, sizeof(*lp));
1.97 niallo 664: lp->l_line = c;
665: lp->l_len = tlen;
666: lp->l_lineno = ++(lines->l_nblines);
667: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
668: c = p + 1;
669: }
670: p++;
1.54 joris 671: }
672:
673: return (lines);
674: }
675:
676: void
677: cvs_freelines(struct cvs_lines *lines)
678: {
679: struct cvs_line *lp;
680:
681: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
682: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.102 niallo 683: if (lp->l_needsfree == 1)
684: xfree(lp->l_line);
1.59 joris 685: xfree(lp);
1.54 joris 686: }
687:
1.59 joris 688: xfree(lines);
1.70 niallo 689: }
690:
1.71 xsa 691: /*
692: * cvs_strsplit()
693: *
694: * Split a string <str> of <sep>-separated values and allocate
695: * an argument vector for the values found.
696: */
1.73 pat 697: struct cvs_argvector *
1.71 xsa 698: cvs_strsplit(char *str, const char *sep)
699: {
1.73 pat 700: struct cvs_argvector *av;
1.76 ray 701: size_t i = 0;
1.71 xsa 702: char *cp, *p;
703:
704: cp = xstrdup(str);
1.77 ray 705: av = xmalloc(sizeof(*av));
1.73 pat 706: av->str = cp;
1.113 ray 707: av->argv = xmalloc(sizeof(*(av->argv)));
1.71 xsa 708:
709: while ((p = strsep(&cp, sep)) != NULL) {
1.73 pat 710: av->argv[i++] = p;
1.111 ray 711: av->argv = xrealloc(av->argv,
1.77 ray 712: i + 1, sizeof(*(av->argv)));
1.71 xsa 713: }
1.73 pat 714: av->argv[i] = NULL;
715:
716: return (av);
717: }
1.71 xsa 718:
1.73 pat 719: /*
720: * cvs_argv_destroy()
721: *
722: * Free an argument vector previously allocated by cvs_strsplit().
723: */
724: void
725: cvs_argv_destroy(struct cvs_argvector *av)
726: {
727: xfree(av->str);
728: xfree(av->argv);
729: xfree(av);
1.82 joris 730: }
731:
732: u_int
733: cvs_revision_select(RCSFILE *file, char *range)
734: {
735: int i;
736: u_int nrev;
737: char *lstr, *rstr;
738: struct rcs_delta *rdp;
739: struct cvs_argvector *revargv, *revrange;
740: RCSNUM *lnum, *rnum;
741:
742: nrev = 0;
743: lnum = rnum = NULL;
744:
745: revargv = cvs_strsplit(range, ",");
746: for (i = 0; revargv->argv[i] != NULL; i++) {
747: revrange = cvs_strsplit(revargv->argv[i], ":");
748: if (revrange->argv[0] == NULL)
749: fatal("invalid revision range: %s", revargv->argv[i]);
750: else if (revrange->argv[1] == NULL)
751: lstr = rstr = revrange->argv[0];
752: else {
753: if (revrange->argv[2] != NULL)
754: fatal("invalid revision range: %s",
755: revargv->argv[i]);
756:
757: lstr = revrange->argv[0];
758: rstr = revrange->argv[1];
759:
760: if (strcmp(lstr, "") == 0)
761: lstr = NULL;
762: if (strcmp(rstr, "") == 0)
763: rstr = NULL;
764: }
765:
766: if (lstr == NULL)
767: lstr = RCS_HEAD_INIT;
768:
1.100 niallo 769: if ((lnum = rcs_translate_tag(lstr, file)) == NULL)
770: fatal("cvs_revision_select: could not translate tag `%s'", lstr);
1.82 joris 771:
772: if (rstr != NULL) {
1.100 niallo 773: if ((rnum = rcs_translate_tag(rstr, file)) == NULL)
774: fatal("cvs_revision_select: could not translate tag `%s'", rstr);
1.82 joris 775: } else {
776: rnum = rcsnum_alloc();
777: rcsnum_cpy(file->rf_head, rnum, 0);
778: }
779:
780: cvs_argv_destroy(revrange);
781:
782: TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
783: if (rcsnum_cmp(rdp->rd_num, lnum, 0) <= 0 &&
784: rcsnum_cmp(rdp->rd_num, rnum, 0) >= 0 &&
785: !(rdp->rd_flags & RCS_RD_SELECT)) {
786: rdp->rd_flags |= RCS_RD_SELECT;
787: nrev++;
788: }
789: }
1.129 joris 790:
791: rcsnum_free(lnum);
792: rcsnum_free(rnum);
1.82 joris 793: }
794:
795: cvs_argv_destroy(revargv);
796:
797: return (nrev);
1.95 xsa 798: }
799:
800: int
801: cvs_yesno(void)
802: {
803: int c, ret;
804:
805: ret = 0;
806:
807: fflush(stderr);
808: fflush(stdout);
809:
810: if ((c = getchar()) != 'y' && c != 'Y')
811: ret = -1;
812: else
813: while (c != EOF && c != '\n')
814: c = getchar();
815:
816: return (ret);
1.132 joris 817: }
818:
819: void
820: cvs_exec(const char *prog)
821: {
822: pid_t pid;
823: char *argp[] = { "sh", "-c", NULL, NULL };
824:
825: argp[2] = prog;
826:
827: if ((pid = fork()) == -1) {
828: cvs_log(LP_ERR, "cvs_exec: fork failed");
829: return;
830: } else if (pid == 0) {
831: execv(_PATH_BSHELL, argp);
832: cvs_log(LP_ERR, "failed to run '%s'", prog);
833: _exit(127);
834: }
1.71 xsa 835: }