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