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