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