Annotation of src/usr.bin/cvs/util.c, Revision 1.117
1.117 ! tobias 1: /* $OpenBSD: util.c,v 1.116 2007/09/09 20:24:06 tobias 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.117 ! tobias 231: char qbuf[128], *linebuf, *lp, *cp, *arg;
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;) {
241: if (*lp == '"') {
242: /* double-quoted string */
243: lp++;
244: i = 0;
245: memset(qbuf, 0, sizeof(qbuf));
246: while (*lp != '"') {
1.16 jfb 247: if (*lp == '\\')
248: lp++;
1.1 jfb 249: if (*lp == '\0') {
250: cvs_log(LP_ERR, "no terminating quote");
1.67 xsa 251: error++;
1.1 jfb 252: break;
1.16 jfb 253: }
1.1 jfb 254:
1.9 jfb 255: qbuf[i++] = *lp++;
1.110 ray 256: if (i == sizeof(qbuf) - 1) {
1.67 xsa 257: error++;
1.1 jfb 258: break;
259: }
260: }
1.114 tobias 261: if (error)
262: break;
263: lp++;
1.1 jfb 264:
265: arg = qbuf;
1.15 deraadt 266: } else {
1.1 jfb 267: cp = strsep(&lp, " \t");
268: if (cp == NULL)
269: break;
270: else if (*cp == '\0')
271: continue;
272:
273: arg = cp;
274: }
275:
1.16 jfb 276: if (argc == argvlen) {
1.67 xsa 277: error++;
1.16 jfb 278: break;
279: }
280:
1.59 joris 281: argv[argc] = xstrdup(arg);
1.1 jfb 282: argc++;
283: }
284:
1.67 xsa 285: if (error != 0) {
1.1 jfb 286: /* ditch the argument vector */
287: for (i = 0; i < (u_int)argc; i++)
1.59 joris 288: xfree(argv[i]);
1.1 jfb 289: argc = -1;
290: }
291:
1.117 ! tobias 292: xfree(linebuf);
1.1 jfb 293: return (argc);
1.17 jfb 294: }
295:
296: /*
297: * cvs_makeargv()
298: *
1.20 david 299: * Allocate an argument vector large enough to accommodate for all the
1.17 jfb 300: * arguments found in <line> and return it.
301: */
1.42 xsa 302: char **
1.17 jfb 303: cvs_makeargv(const char *line, int *argc)
304: {
305: int i, ret;
306: char *argv[1024], **copy;
307:
308: ret = cvs_getargv(line, argv, 1024);
309: if (ret == -1)
310: return (NULL);
311:
1.77 ray 312: copy = xcalloc(ret + 1, sizeof(char *));
1.17 jfb 313:
314: for (i = 0; i < ret; i++)
315: copy[i] = argv[i];
316: copy[ret] = NULL;
317:
318: *argc = ret;
319: return (copy);
1.1 jfb 320: }
321:
322: /*
323: * cvs_freeargv()
324: *
325: * Free an argument vector previously generated by cvs_getargv().
326: */
327: void
328: cvs_freeargv(char **argv, int argc)
329: {
330: int i;
331:
332: for (i = 0; i < argc; i++)
1.16 jfb 333: if (argv[i] != NULL)
1.59 joris 334: xfree(argv[i]);
1.38 xsa 335: }
336:
337: /*
338: * cvs_chdir()
339: *
1.63 xsa 340: * Change to directory <path>.
341: * If <rm> is equal to `1', <path> is removed if chdir() fails so we
342: * do not have temporary directories leftovers.
1.60 xsa 343: * Returns 0 on success.
1.50 xsa 344: */
1.38 xsa 345: int
1.63 xsa 346: cvs_chdir(const char *path, int rm)
1.38 xsa 347: {
1.63 xsa 348: if (chdir(path) == -1) {
349: if (rm == 1)
350: cvs_unlink(path);
1.60 xsa 351: fatal("cvs_chdir: `%s': %s", path, strerror(errno));
1.63 xsa 352: }
1.49 xsa 353:
354: return (0);
355: }
356:
357: /*
358: * cvs_rename()
359: * Change the name of a file.
360: * rename() wrapper with an error message.
1.60 xsa 361: * Returns 0 on success.
1.49 xsa 362: */
363: int
364: cvs_rename(const char *from, const char *to)
365: {
1.90 joris 366: if (cvs_server_active == 0)
367: cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
1.49 xsa 368:
369: if (cvs_noexec == 1)
370: return (0);
371:
1.60 xsa 372: if (rename(from, to) == -1)
373: fatal("cvs_rename: `%s'->`%s': %s", from, to, strerror(errno));
1.40 xsa 374:
375: return (0);
376: }
377:
378: /*
379: * cvs_unlink()
380: *
381: * Removes the link named by <path>.
382: * unlink() wrapper with an error message.
383: * Returns 0 on success, or -1 on failure.
384: */
385: int
386: cvs_unlink(const char *path)
387: {
1.90 joris 388: if (cvs_server_active == 0)
389: cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
1.40 xsa 390:
391: if (cvs_noexec == 1)
392: return (0);
393:
1.78 deraadt 394: if (unlink(path) == -1 && errno != ENOENT) {
1.94 xsa 395: cvs_log(LP_ERRNO, "%s", path);
1.38 xsa 396: return (-1);
397: }
398:
399: return (0);
1.1 jfb 400: }
1.24 joris 401:
402: /*
1.45 xsa 403: * cvs_rmdir()
1.30 jfb 404: *
405: * Remove a directory tree from disk.
406: * Returns 0 on success, or -1 on failure.
1.24 joris 407: */
408: int
1.45 xsa 409: cvs_rmdir(const char *path)
1.24 joris 410: {
1.99 todd 411: int type, ret = -1;
1.24 joris 412: DIR *dirp;
413: struct dirent *ent;
1.99 todd 414: struct stat st;
1.24 joris 415: char fpath[MAXPATHLEN];
1.46 xsa 416:
1.90 joris 417: if (cvs_server_active == 0)
418: cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
1.46 xsa 419:
420: if (cvs_noexec == 1)
421: return (0);
1.24 joris 422:
423: if ((dirp = opendir(path)) == NULL) {
1.79 joris 424: cvs_log(LP_ERR, "failed to open '%s'", path);
1.30 jfb 425: return (-1);
1.24 joris 426: }
427:
428: while ((ent = readdir(dirp)) != NULL) {
429: if (!strcmp(ent->d_name, ".") ||
430: !strcmp(ent->d_name, ".."))
431: continue;
432:
1.105 xsa 433: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
434: path, ent->d_name);
1.24 joris 435:
1.99 todd 436: if (ent->d_type == DT_UNKNOWN) {
1.104 todd 437: if (lstat(fpath, &st) == -1)
1.99 todd 438: fatal("'%s': %s", fpath, strerror(errno));
439:
440: switch (st.st_mode & S_IFMT) {
441: case S_IFDIR:
442: type = CVS_DIR;
443: break;
444: case S_IFREG:
445: type = CVS_FILE;
446: break;
447: default:
448: fatal("'%s': Unknown file type in copy",
449: fpath);
450: }
451: } else {
452: switch (ent->d_type) {
453: case DT_DIR:
454: type = CVS_DIR;
455: break;
456: case DT_REG:
457: type = CVS_FILE;
458: break;
459: default:
460: fatal("'%s': Unknown file type in copy",
461: fpath);
462: }
463: }
464: switch (type) {
465: case CVS_DIR:
1.45 xsa 466: if (cvs_rmdir(fpath) == -1)
1.33 pat 467: goto done;
1.99 todd 468: break;
469: case CVS_FILE:
470: if (cvs_unlink(fpath) == -1 && errno != ENOENT)
471: goto done;
472: break;
473: default:
474: fatal("type %d unknown, shouldn't happen", type);
475: }
1.24 joris 476: }
477:
478:
1.78 deraadt 479: if (rmdir(path) == -1 && errno != ENOENT) {
1.93 xsa 480: cvs_log(LP_ERRNO, "%s", path);
1.33 pat 481: goto done;
1.30 jfb 482: }
1.24 joris 483:
1.33 pat 484: ret = 0;
485: done:
486: closedir(dirp);
1.34 joris 487: return (ret);
488: }
489:
1.51 xsa 490: void
1.81 joris 491: cvs_get_repository_path(const char *dir, char *dst, size_t len)
492: {
493: char buf[MAXPATHLEN];
494:
495: cvs_get_repository_name(dir, buf, sizeof(buf));
1.105 xsa 496: (void)xsnprintf(dst, len, "%s/%s", current_cvsroot->cr_dir, buf);
1.81 joris 497: }
498:
499: void
500: cvs_get_repository_name(const char *dir, char *dst, size_t len)
1.51 xsa 501: {
502: FILE *fp;
1.81 joris 503: char *s, fpath[MAXPATHLEN];
1.116 tobias 504:
505: /* During checkout -p, do not use any locally available files. */
506: if (cvs_cmdop == CVS_OP_CHECKOUT && print_stdout) {
507: dst[0] = '\0';
508: if (strlcat(dst, dir, len) >= len)
509: fatal("cvs_get_repository_name: truncation");
510: return;
511: }
1.79 joris 512:
1.105 xsa 513: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
514: dir, CVS_PATH_REPOSITORY);
1.79 joris 515:
516: if ((fp = fopen(fpath, "r")) != NULL) {
1.101 thib 517: if ((fgets(dst, len, fp)) == NULL)
518: fatal("cvs_get_repository_name: bad repository file");
1.51 xsa 519:
1.81 joris 520: if ((s = strchr(dst, '\n')) != NULL)
1.79 joris 521: *s = '\0';
1.51 xsa 522:
523: (void)fclose(fp);
1.81 joris 524: } else {
1.84 joris 525: dst[0] = '\0';
526:
527: if (cvs_cmdop == CVS_OP_IMPORT) {
528: if (strlcpy(dst, import_repository, len) >= len)
1.85 joris 529: fatal("cvs_get_repository_name: truncation");
1.84 joris 530: if (strlcat(dst, "/", len) >= len)
1.85 joris 531: fatal("cvs_get_repository_name: truncation");
532:
533: if (strcmp(dir, ".")) {
534: if (strlcat(dst, dir, len) >= len) {
535: fatal("cvs_get_repository_name: "
536: "truncation");
537: }
538: }
539: } else {
1.98 joris 540: if (cvs_cmdop != CVS_OP_CHECKOUT) {
541: if (strlcat(dst, dir, len) >= len)
542: fatal("cvs_get_repository_name: "
543: "truncation");
544: }
1.84 joris 545: }
1.51 xsa 546: }
547: }
548:
549: void
1.87 xsa 550: cvs_mkadmin(const char *path, const char *root, const char *repo,
551: char *tag, char *date, int nb)
1.51 xsa 552: {
553: FILE *fp;
1.79 joris 554: struct stat st;
555: char buf[MAXPATHLEN];
1.51 xsa 556:
1.90 joris 557: if (cvs_server_active == 0)
558: cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s, %d)",
559: path, root, repo, (tag != NULL) ? tag : "",
560: (date != NULL) ? date : "", nb);
1.51 xsa 561:
1.105 xsa 562: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_CVSDIR);
1.51 xsa 563:
1.79 joris 564: if (stat(buf, &st) != -1)
565: return;
1.51 xsa 566:
1.79 joris 567: if (mkdir(buf, 0755) == -1 && errno != EEXIST)
568: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
569:
1.105 xsa 570: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_ROOTSPEC);
1.79 joris 571:
572: if ((fp = fopen(buf, "w")) == NULL)
573: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
574:
575: fprintf(fp, "%s\n", root);
576: (void)fclose(fp);
1.51 xsa 577:
1.105 xsa 578: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_REPOSITORY);
1.51 xsa 579:
1.79 joris 580: if ((fp = fopen(buf, "w")) == NULL)
581: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51 xsa 582:
1.79 joris 583: fprintf(fp, "%s\n", repo);
584: (void)fclose(fp);
1.51 xsa 585:
1.105 xsa 586: (void)xsnprintf(buf, sizeof(buf), "%s/%s", path, CVS_PATH_ENTRIES);
1.51 xsa 587:
1.79 joris 588: if ((fp = fopen(buf, "w")) == NULL)
589: fatal("cvs_mkadmin: %s: %s", buf, strerror(errno));
1.51 xsa 590: (void)fclose(fp);
1.87 xsa 591:
592: cvs_write_tagfile(path, tag, date, nb);
1.54 joris 593: }
594:
1.79 joris 595: void
1.112 joris 596: cvs_mkpath(const char *path, char *tag)
1.72 niallo 597: {
1.79 joris 598: FILE *fp;
599: size_t len;
600: char *sp, *dp, *dir, rpath[MAXPATHLEN], repo[MAXPATHLEN];
601:
602: dir = xstrdup(path);
603:
604: STRIP_SLASH(dir);
1.90 joris 605:
606: if (cvs_server_active == 0)
607: cvs_log(LP_TRACE, "cvs_mkpath(%s)", dir);
1.72 niallo 608:
1.79 joris 609: repo[0] = '\0';
610: rpath[0] = '\0';
1.72 niallo 611:
1.79 joris 612: if (cvs_cmdop == CVS_OP_UPDATE) {
613: if ((fp = fopen(CVS_PATH_REPOSITORY, "r")) != NULL) {
1.91 thib 614: if ((fgets(repo, sizeof(repo), fp)) == NULL)
615: fatal("cvs_mkpath: bad repository file");
616: if ((len = strlen(repo)) && repo[len - 1] == '\n')
617: repo[len - 1] = '\0';
1.79 joris 618: (void)fclose(fp);
619: }
1.72 niallo 620: }
621:
1.79 joris 622: for (sp = dir; sp != NULL; sp = dp) {
623: dp = strchr(sp, '/');
624: if (dp != NULL)
625: *(dp++) = '\0';
626:
627: if (repo[0] != '\0') {
628: len = strlcat(repo, "/", sizeof(repo));
629: if (len >= (int)sizeof(repo))
630: fatal("cvs_mkpath: overflow");
631: }
632:
633: len = strlcat(repo, sp, sizeof(repo));
634: if (len >= (int)sizeof(repo))
635: fatal("cvs_mkpath: overflow");
636:
637: if (rpath[0] != '\0') {
638: len = strlcat(rpath, "/", sizeof(rpath));
639: if (len >= (int)sizeof(rpath))
640: fatal("cvs_mkpath: overflow");
641: }
642:
643: len = strlcat(rpath, sp, sizeof(rpath));
644: if (len >= (int)sizeof(rpath))
645: fatal("cvs_mkpath: overflow");
646:
647: if (mkdir(rpath, 0755) == -1 && errno != EEXIST)
648: fatal("cvs_mkpath: %s: %s", rpath, strerror(errno));
1.72 niallo 649:
1.98 joris 650: cvs_mkadmin(rpath, current_cvsroot->cr_str, repo,
1.112 joris 651: tag, NULL, 0);
652:
653: if (cvs_server_active == 1 && strcmp(rpath, ".")) {
654: if (tag != NULL)
655: cvs_server_set_sticky(rpath, tag);
656: }
1.79 joris 657: }
1.72 niallo 658:
1.79 joris 659: xfree(dir);
1.72 niallo 660: }
1.54 joris 661:
662: /*
663: * Split the contents of a file into a list of lines.
664: */
665: struct cvs_lines *
1.106 otto 666: cvs_splitlines(u_char *data, size_t len)
1.54 joris 667: {
1.97 niallo 668: u_char *p, *c;
669: size_t i, tlen;
1.54 joris 670: struct cvs_lines *lines;
671: struct cvs_line *lp;
672:
1.77 ray 673: lines = xmalloc(sizeof(*lines));
1.97 niallo 674: memset(lines, 0, sizeof(*lines));
1.54 joris 675: TAILQ_INIT(&(lines->l_lines));
676:
1.77 ray 677: lp = xmalloc(sizeof(*lp));
1.97 niallo 678: memset(lp, 0, sizeof(*lp));
1.54 joris 679: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
680:
1.97 niallo 681: p = c = data;
682: for (i = 0; i < len; i++) {
683: if (*p == '\n' || (i == len - 1)) {
684: tlen = p - c + 1;
685: lp = xmalloc(sizeof(*lp));
1.102 niallo 686: memset(lp, 0, sizeof(*lp));
1.97 niallo 687: lp->l_line = c;
688: lp->l_len = tlen;
689: lp->l_lineno = ++(lines->l_nblines);
690: TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
691: c = p + 1;
692: }
693: p++;
1.54 joris 694: }
695:
696: return (lines);
697: }
698:
699: void
700: cvs_freelines(struct cvs_lines *lines)
701: {
702: struct cvs_line *lp;
703:
704: while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
705: TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
1.102 niallo 706: if (lp->l_needsfree == 1)
707: xfree(lp->l_line);
1.59 joris 708: xfree(lp);
1.54 joris 709: }
710:
1.59 joris 711: xfree(lines);
1.70 niallo 712: }
713:
1.71 xsa 714: /*
715: * cvs_strsplit()
716: *
717: * Split a string <str> of <sep>-separated values and allocate
718: * an argument vector for the values found.
719: */
1.73 pat 720: struct cvs_argvector *
1.71 xsa 721: cvs_strsplit(char *str, const char *sep)
722: {
1.73 pat 723: struct cvs_argvector *av;
1.76 ray 724: size_t i = 0;
1.71 xsa 725: char *cp, *p;
726:
727: cp = xstrdup(str);
1.77 ray 728: av = xmalloc(sizeof(*av));
1.73 pat 729: av->str = cp;
1.113 ray 730: av->argv = xmalloc(sizeof(*(av->argv)));
1.71 xsa 731:
732: while ((p = strsep(&cp, sep)) != NULL) {
1.73 pat 733: av->argv[i++] = p;
1.111 ray 734: av->argv = xrealloc(av->argv,
1.77 ray 735: i + 1, sizeof(*(av->argv)));
1.71 xsa 736: }
1.73 pat 737: av->argv[i] = NULL;
738:
739: return (av);
740: }
1.71 xsa 741:
1.73 pat 742: /*
743: * cvs_argv_destroy()
744: *
745: * Free an argument vector previously allocated by cvs_strsplit().
746: */
747: void
748: cvs_argv_destroy(struct cvs_argvector *av)
749: {
750: xfree(av->str);
751: xfree(av->argv);
752: xfree(av);
1.82 joris 753: }
754:
755: u_int
756: cvs_revision_select(RCSFILE *file, char *range)
757: {
758: int i;
759: u_int nrev;
760: char *lstr, *rstr;
761: struct rcs_delta *rdp;
762: struct cvs_argvector *revargv, *revrange;
763: RCSNUM *lnum, *rnum;
764:
765: nrev = 0;
766: lnum = rnum = NULL;
767:
768: revargv = cvs_strsplit(range, ",");
769: for (i = 0; revargv->argv[i] != NULL; i++) {
770: revrange = cvs_strsplit(revargv->argv[i], ":");
771: if (revrange->argv[0] == NULL)
772: fatal("invalid revision range: %s", revargv->argv[i]);
773: else if (revrange->argv[1] == NULL)
774: lstr = rstr = revrange->argv[0];
775: else {
776: if (revrange->argv[2] != NULL)
777: fatal("invalid revision range: %s",
778: revargv->argv[i]);
779:
780: lstr = revrange->argv[0];
781: rstr = revrange->argv[1];
782:
783: if (strcmp(lstr, "") == 0)
784: lstr = NULL;
785: if (strcmp(rstr, "") == 0)
786: rstr = NULL;
787: }
788:
789: if (lstr == NULL)
790: lstr = RCS_HEAD_INIT;
791:
1.100 niallo 792: if ((lnum = rcs_translate_tag(lstr, file)) == NULL)
793: fatal("cvs_revision_select: could not translate tag `%s'", lstr);
1.82 joris 794:
795: if (rstr != NULL) {
1.100 niallo 796: if ((rnum = rcs_translate_tag(rstr, file)) == NULL)
797: fatal("cvs_revision_select: could not translate tag `%s'", rstr);
1.82 joris 798: } else {
799: rnum = rcsnum_alloc();
800: rcsnum_cpy(file->rf_head, rnum, 0);
801: }
802:
803: cvs_argv_destroy(revrange);
804:
805: TAILQ_FOREACH(rdp, &(file->rf_delta), rd_list) {
806: if (rcsnum_cmp(rdp->rd_num, lnum, 0) <= 0 &&
807: rcsnum_cmp(rdp->rd_num, rnum, 0) >= 0 &&
808: !(rdp->rd_flags & RCS_RD_SELECT)) {
809: rdp->rd_flags |= RCS_RD_SELECT;
810: nrev++;
811: }
812: }
813: }
814:
815: cvs_argv_destroy(revargv);
816:
817: if (lnum != NULL)
818: rcsnum_free(lnum);
819: if (rnum != NULL)
820: rcsnum_free(rnum);
821:
822: return (nrev);
1.95 xsa 823: }
824:
825: int
826: cvs_yesno(void)
827: {
828: int c, ret;
829:
830: ret = 0;
831:
832: fflush(stderr);
833: fflush(stdout);
834:
835: if ((c = getchar()) != 'y' && c != 'Y')
836: ret = -1;
837: else
838: while (c != EOF && c != '\n')
839: c = getchar();
840:
841: return (ret);
1.71 xsa 842: }