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