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