Annotation of src/usr.bin/cvs/client.c, Revision 1.72
1.72 ! joris 1: /* $OpenBSD: client.c,v 1.71 2007/07/17 20:29:58 xsa Exp $ */
1.1 jfb 2: /*
1.9 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.9 joris 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.59 otto 18: #include <sys/param.h>
19: #include <sys/dirent.h>
20: #include <sys/stat.h>
21:
22: #include <errno.h>
23: #include <fcntl.h>
24: #include <libgen.h>
25: #include <limits.h>
26: #include <pwd.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
1.1 jfb 30:
31: #include "cvs.h"
1.9 joris 32: #include "remote.h"
1.1 jfb 33:
1.9 joris 34: struct cvs_req cvs_requests[] = {
35: /* this is what our client will use, the server should support it */
36: { "Root", 1, cvs_server_root, REQ_NEEDED },
37: { "Valid-responses", 1, cvs_server_validresp, REQ_NEEDED },
38: { "valid-requests", 1, cvs_server_validreq, REQ_NEEDED },
39: { "Directory", 0, cvs_server_directory, REQ_NEEDED },
1.66 joris 40: { "Static-directory", 0, cvs_server_static_directory,
41: REQ_NEEDED | REQ_NEEDDIR },
42: { "Sticky", 0, cvs_server_sticky,
43: REQ_NEEDED | REQ_NEEDDIR },
44: { "Entry", 0, cvs_server_entry,
45: REQ_NEEDED | REQ_NEEDDIR },
46: { "Modified", 0, cvs_server_modified,
47: REQ_NEEDED | REQ_NEEDDIR },
1.9 joris 48: { "UseUnchanged", 0, cvs_server_useunchanged, REQ_NEEDED },
1.66 joris 49: { "Unchanged", 0, cvs_server_unchanged,
50: REQ_NEEDED | REQ_NEEDDIR },
1.9 joris 51: { "Questionable", 0, cvs_server_questionable, REQ_NEEDED },
52: { "Argument", 0, cvs_server_argument, REQ_NEEDED },
1.24 xsa 53: { "Argumentx", 0, cvs_server_argumentx, REQ_NEEDED },
54: { "Global_option", 0, cvs_server_globalopt, REQ_NEEDED },
1.26 xsa 55: { "Set", 0, cvs_server_set, REQ_NEEDED },
1.1 jfb 56:
1.9 joris 57: /*
58: * used to tell the server what is going on in our
59: * working copy, unsupported until we are told otherwise
60: */
61: { "Max-dotdot", 0, NULL, 0 },
62: { "Checkin-prog", 0, NULL, 0 },
63: { "Update-prog", 0, NULL, 0 },
64: { "Kopt", 0, NULL, 0 },
65: { "Checkin-time", 0, NULL, 0 },
66: { "Is-modified", 0, NULL, 0 },
67: { "Notify", 0, NULL, 0 },
68: { "Case", 0, NULL, 0 },
69: { "Gzip-stream", 0, NULL, 0 },
1.25 xsa 70: { "wrapper-sendme-rcsOptions", 0, NULL, 0 },
1.9 joris 71: { "Kerberos-encrypt", 0, NULL, 0 },
72: { "Gssapi-encrypt", 0, NULL, 0 },
73: { "Gssapi-authenticate", 0, NULL, 0 },
74: { "expand-modules", 0, NULL, 0 },
75:
76: /* commands that might be supported */
1.66 joris 77: { "ci", 0, cvs_server_commit,
78: REQ_NEEDDIR },
79: { "co", 0, cvs_server_checkout,
80: REQ_NEEDDIR },
81: { "update", 0, cvs_server_update,
82: REQ_NEEDDIR },
83: { "diff", 0, cvs_server_diff, REQ_NEEDDIR },
84: { "log", 0, cvs_server_log, REQ_NEEDDIR },
1.71 xsa 85: { "rlog", 0, cvs_server_rlog, 0 },
1.66 joris 86: { "add", 0, cvs_server_add, REQ_NEEDDIR },
87: { "remove", 0, cvs_server_remove,
88: REQ_NEEDDIR },
1.33 xsa 89: { "update-patches", 0, cvs_server_update_patches, 0 },
1.25 xsa 90: { "gzip-file-contents", 0, NULL, 0 },
1.66 joris 91: { "status", 0, cvs_server_status,
92: REQ_NEEDDIR },
1.9 joris 93: { "rdiff", 0, NULL, 0 },
1.66 joris 94: { "tag", 0, cvs_server_tag, REQ_NEEDDIR },
1.9 joris 95: { "rtag", 0, NULL, 0 },
1.66 joris 96: { "import", 0, cvs_server_import,
97: REQ_NEEDDIR },
98: { "admin", 0, cvs_server_admin, REQ_NEEDDIR },
1.25 xsa 99: { "export", 0, NULL, 0 },
100: { "history", 0, NULL, 0 },
1.68 xsa 101: { "release", 0, cvs_server_release,
102: REQ_NEEDDIR },
1.9 joris 103: { "watch-on", 0, NULL, 0 },
104: { "watch-off", 0, NULL, 0 },
105: { "watch-add", 0, NULL, 0 },
106: { "watch-remove", 0, NULL, 0 },
1.25 xsa 107: { "watchers", 0, NULL, 0 },
108: { "editors", 0, NULL, 0 },
1.66 joris 109: { "init", 0, cvs_server_init, REQ_NEEDDIR },
110: { "annotate", 0, cvs_server_annotate,
111: REQ_NEEDDIR },
1.25 xsa 112: { "rannotate", 0, NULL, 0 },
1.9 joris 113: { "noop", 0, NULL, 0 },
1.25 xsa 114: { "version", 0, cvs_server_version, 0 },
1.9 joris 115: { "", -1, NULL, 0 }
116: };
117:
1.16 xsa 118: static void client_check_directory(char *);
119: static char *client_get_supported_responses(void);
120: static char *lastdir = NULL;
121: static int end_of_response = 0;
122:
123: static void cvs_client_initlog(void);
124:
125: /*
126: * File descriptors for protocol logging when the CVS_CLIENT_LOG environment
127: * variable is set.
128: */
129: static int cvs_client_logon = 0;
1.42 joris 130: int cvs_client_inlog_fd = -1;
131: int cvs_client_outlog_fd = -1;
1.16 xsa 132:
1.9 joris 133:
1.37 joris 134: int server_response = SERVER_OK;
135:
1.9 joris 136: static char *
137: client_get_supported_responses(void)
138: {
139: BUF *bp;
140: char *d;
141: int i, first;
142:
143: first = 0;
144: bp = cvs_buf_alloc(512, BUF_AUTOEXT);
145: for (i = 0; cvs_responses[i].supported != -1; i++) {
146: if (cvs_responses[i].hdlr == NULL)
147: continue;
148:
149: if (first != 0)
150: cvs_buf_append(bp, " ", 1);
151: else
152: first++;
153: cvs_buf_append(bp, cvs_responses[i].name,
154: strlen(cvs_responses[i].name));
155: }
156:
157: cvs_buf_putc(bp, '\0');
158: d = cvs_buf_release(bp);
159: return (d);
160: }
1.1 jfb 161:
1.10 joris 162: static void
163: client_check_directory(char *data)
164: {
165: CVSENTRIES *entlist;
1.54 otto 166: char entry[CVS_ENT_MAXLINELEN], *parent, *base;
1.10 joris 167:
168: STRIP_SLASH(data);
169:
1.69 joris 170: cvs_mkpath(data, NULL);
1.10 joris 171:
172: if ((base = basename(data)) == NULL)
173: fatal("client_check_directory: overflow");
174:
175: if ((parent = dirname(data)) == NULL)
176: fatal("client_check_directory: overflow");
1.11 joris 177:
178: if (!strcmp(parent, "."))
179: return;
1.10 joris 180:
1.57 xsa 181: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////", base);
1.10 joris 182:
183: entlist = cvs_ent_open(parent);
184: cvs_ent_add(entlist, entry);
185: cvs_ent_close(entlist, ENT_SYNC);
186: }
187:
1.9 joris 188: void
189: cvs_client_connect_to_server(void)
190: {
1.26 xsa 191: struct cvs_var *vp;
1.9 joris 192: char *cmd, *argv[9], *resp;
193: int ifd[2], ofd[2], argc;
1.45 joris 194:
195: if (cvs_server_active == 1)
196: fatal("cvs_client_connect: I was already connected to server");
1.1 jfb 197:
1.9 joris 198: switch (current_cvsroot->cr_method) {
199: case CVS_METHOD_PSERVER:
200: case CVS_METHOD_KSERVER:
201: case CVS_METHOD_GSERVER:
202: case CVS_METHOD_FORK:
203: case CVS_METHOD_EXT:
1.14 joris 204: fatal("the specified connection method is not supported");
1.9 joris 205: default:
206: break;
207: }
208:
209: if (pipe(ifd) == -1)
210: fatal("cvs_client_connect: %s", strerror(errno));
211: if (pipe(ofd) == -1)
212: fatal("cvs_client_connect: %s", strerror(errno));
213:
214: switch (fork()) {
215: case -1:
216: fatal("cvs_client_connect: fork failed: %s", strerror(errno));
217: case 0:
218: if (dup2(ifd[0], STDIN_FILENO) == -1)
219: fatal("cvs_client_connect: %s", strerror(errno));
220: if (dup2(ofd[1], STDOUT_FILENO) == -1)
221: fatal("cvs_client_connect: %s", strerror(errno));
1.1 jfb 222:
1.9 joris 223: close(ifd[1]);
224: close(ofd[0]);
1.1 jfb 225:
1.9 joris 226: if ((cmd = getenv("CVS_SERVER")) == NULL)
227: cmd = CVS_SERVER_DEFAULT;
1.1 jfb 228:
229: argc = 0;
230: argv[argc++] = cvs_rsh;
231:
1.9 joris 232: if (current_cvsroot->cr_user != NULL) {
1.1 jfb 233: argv[argc++] = "-l";
1.9 joris 234: argv[argc++] = current_cvsroot->cr_user;
1.1 jfb 235: }
236:
1.9 joris 237: argv[argc++] = current_cvsroot->cr_host;
238: argv[argc++] = cmd;
1.1 jfb 239: argv[argc++] = "server";
240: argv[argc] = NULL;
241:
1.9 joris 242: cvs_log(LP_TRACE, "connecting to server %s",
243: current_cvsroot->cr_host);
244:
1.1 jfb 245: execvp(argv[0], argv);
1.9 joris 246: fatal("cvs_client_connect: failed to execute cvs server");
247: default:
248: break;
1.1 jfb 249: }
250:
1.9 joris 251: close(ifd[0]);
252: close(ofd[1]);
253:
254: if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
255: fatal("cvs_client_connect: %s", strerror(errno));
256: if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
257: fatal("cvs_client_connect: %s", strerror(errno));
1.1 jfb 258:
1.9 joris 259: setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
260: setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
1.1 jfb 261:
1.16 xsa 262: cvs_client_initlog();
263:
1.18 xsa 264: if (cvs_cmdop != CVS_OP_INIT)
265: cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
1.1 jfb 266:
1.9 joris 267: resp = client_get_supported_responses();
268: cvs_client_send_request("Valid-responses %s", resp);
269: xfree(resp);
1.1 jfb 270:
1.9 joris 271: cvs_client_send_request("valid-requests");
272: cvs_client_get_responses();
1.1 jfb 273:
1.28 xsa 274: cvs_client_send_request("UseUnchanged");
275:
1.20 xsa 276: if (cvs_nolog == 1)
277: cvs_client_send_request("Global_option -l");
278:
1.19 xsa 279: if (cvs_noexec == 1)
280: cvs_client_send_request("Global_option -n");
281:
282: if (verbosity == 0)
283: cvs_client_send_request("Global_option -Q");
1.20 xsa 284:
285: /* Be quiet. This is the default in OpenCVS. */
286: cvs_client_send_request("Global_option -q");
1.19 xsa 287:
288: if (cvs_readonly == 1)
289: cvs_client_send_request("Global_option -r");
290:
291: if (cvs_trace == 1)
1.9 joris 292: cvs_client_send_request("Global_option -t");
1.1 jfb 293:
1.9 joris 294: if (verbosity == 2)
295: cvs_client_send_request("Global_option -V");
1.26 xsa 296:
297: /* XXX: If 'Set' is supported? */
298: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
299: cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1 jfb 300: }
301:
1.9 joris 302: void
303: cvs_client_send_request(char *fmt, ...)
304: {
305: va_list ap;
306: char *data, *s;
307: struct cvs_req *req;
308:
309: va_start(ap, fmt);
310: vasprintf(&data, fmt, ap);
311: va_end(ap);
1.1 jfb 312:
1.9 joris 313: if ((s = strchr(data, ' ')) != NULL)
314: *s = '\0';
1.1 jfb 315:
1.9 joris 316: req = cvs_remote_get_request_info(data);
317: if (req == NULL)
318: fatal("'%s' is an unknown request", data);
1.1 jfb 319:
1.9 joris 320: if (req->supported != 1)
321: fatal("remote cvs server does not support '%s'", data);
1.1 jfb 322:
1.9 joris 323: if (s != NULL)
324: *s = ' ';
1.1 jfb 325:
1.12 joris 326: cvs_log(LP_TRACE, "%s", data);
1.16 xsa 327:
328: if (cvs_client_inlog_fd != -1) {
329: BUF *bp;
330:
331: bp = cvs_buf_alloc(strlen(data), BUF_AUTOEXT);
332:
333: if (cvs_buf_append(bp, data, strlen(data)) < 0)
334: fatal("cvs_client_send_request: cvs_buf_append");
335:
336: cvs_buf_putc(bp, '\n');
337:
338: if (cvs_buf_write_fd(bp, cvs_client_inlog_fd) < 0)
339: fatal("cvs_client_send_request: cvs_buf_write_fd");
340:
341: cvs_buf_free(bp);
342: }
343:
1.9 joris 344: cvs_remote_output(data);
345: xfree(data);
346: }
1.1 jfb 347:
1.9 joris 348: void
349: cvs_client_read_response(void)
1.1 jfb 350: {
1.9 joris 351: char *cmd, *data;
352: struct cvs_resp *resp;
1.1 jfb 353:
1.9 joris 354: cmd = cvs_remote_input();
355: if ((data = strchr(cmd, ' ')) != NULL)
356: (*data++) = '\0';
1.1 jfb 357:
1.9 joris 358: resp = cvs_remote_get_response_info(cmd);
359: if (resp == NULL)
360: fatal("response '%s' is not supported by our client", cmd);
1.1 jfb 361:
1.9 joris 362: if (resp->hdlr == NULL)
363: fatal("opencvs client does not support '%s'", cmd);
1.16 xsa 364:
1.9 joris 365: (*resp->hdlr)(data);
1.16 xsa 366:
1.9 joris 367: xfree(cmd);
1.1 jfb 368: }
369:
1.9 joris 370: void
371: cvs_client_get_responses(void)
372: {
373: while (end_of_response != 1)
374: cvs_client_read_response();
1.1 jfb 375:
1.9 joris 376: end_of_response = 0;
1.72 ! joris 377: }
! 378:
! 379: void
! 380: cvs_client_send_logmsg(char *msg)
! 381: {
! 382: char *buf, *p, *q;
! 383:
! 384: (void)xasprintf(&buf, "%s\n", msg);
! 385:
! 386: cvs_client_send_request("Argument -m");
! 387: if ((p = strchr(buf, '\n')) != NULL)
! 388: *p++ = '\0';
! 389: cvs_client_send_request("Argument %s", buf);
! 390: for (q = p; p != NULL; q = p) {
! 391: if ((p = strchr(q, '\n')) != NULL) {
! 392: *p++ = '\0';
! 393: cvs_client_send_request("Argumentx %s", q);
! 394: }
! 395: }
! 396:
! 397: free(buf);
1.9 joris 398: }
1.1 jfb 399:
1.9 joris 400: void
401: cvs_client_senddir(const char *dir)
1.1 jfb 402: {
1.34 xsa 403: struct stat st;
1.29 xsa 404: int nb;
1.50 otto 405: char *d, *date, fpath[MAXPATHLEN], repo[MAXPATHLEN], *tag;
1.29 xsa 406:
407: d = NULL;
1.9 joris 408:
409: if (lastdir != NULL && !strcmp(dir, lastdir))
410: return;
1.1 jfb 411:
1.9 joris 412: cvs_get_repository_path(dir, repo, MAXPATHLEN);
1.31 xsa 413:
1.70 xsa 414: if (cvs_cmdop != CVS_OP_RLOG)
415: cvs_client_send_request("Directory %s\n%s", dir, repo);
1.31 xsa 416:
1.58 xsa 417: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
418: dir, CVS_PATH_STATICENTRIES);
1.34 xsa 419:
420: if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
421: cvs_client_send_request("Static-directory");
422:
1.29 xsa 423: d = xstrdup(dir);
424: cvs_parse_tagfile(d, &tag, &date, &nb);
425:
426: if (tag != NULL || date != NULL) {
427: char buf[128];
428:
429: if (tag != NULL && nb != NULL) {
430: if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
431: fatal("cvs_client_senddir: truncation");
432: } else if (tag != NULL) {
433: if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
434: fatal("cvs_client_senddir: truncation");
435: } else {
436: if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
437: fatal("cvs_client_senddir: truncation");
438: }
439:
440: if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
441: fatal("cvs_client_senddir: truncation");
442:
443: cvs_client_send_request("Sticky %s", buf);
444:
445: if (tag != NULL)
446: xfree(tag);
447: if (date != NULL)
448: xfree(date);
449: }
450: if (d != NULL)
451: xfree(d);
1.1 jfb 452:
1.9 joris 453: if (lastdir != NULL)
454: xfree(lastdir);
455: lastdir = xstrdup(dir);
456: }
1.1 jfb 457:
1.9 joris 458: void
459: cvs_client_sendfile(struct cvs_file *cf)
1.1 jfb 460: {
1.9 joris 461: size_t len;
1.67 xsa 462: char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[32];
1.1 jfb 463:
1.9 joris 464: if (cf->file_type != CVS_FILE)
465: return;
1.1 jfb 466:
1.9 joris 467: cvs_client_senddir(cf->file_wd);
468: cvs_remote_classify_file(cf);
469:
1.10 joris 470: if (cf->file_type == CVS_DIR)
471: return;
472:
1.9 joris 473: if (cf->file_ent != NULL) {
474: if (cf->file_status == FILE_ADDED) {
475: len = strlcpy(rev, "0", sizeof(rev));
476: if (len >= sizeof(rev))
477: fatal("cvs_client_sendfile: truncation");
478:
479: len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
480: if (len >= sizeof(timebuf))
481: fatal("cvs_client_sendfile: truncation");
482:
483: len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
484: if (len >= sizeof(timebuf))
485: fatal("cvs_client_sendfile: truncation");
486: } else {
487: rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
488: ctime_r(&cf->file_ent->ce_mtime, timebuf);
489: }
1.1 jfb 490:
1.9 joris 491: if (cf->file_ent->ce_conflict == NULL) {
492: if (timebuf[strlen(timebuf) - 1] == '\n')
493: timebuf[strlen(timebuf) - 1] = '\0';
494: } else {
495: len = strlcpy(timebuf, cf->file_ent->ce_conflict,
496: sizeof(timebuf));
497: if (len >= sizeof(timebuf))
498: fatal("cvs_client_sendfile: truncation");
1.56 joris 499: len = strlcat(timebuf, "+", sizeof(timebuf));
500: if (len >= sizeof(timebuf))
501: fatal("cvs_client_sendfile: truncation");
1.9 joris 502: }
1.1 jfb 503:
1.9 joris 504: sticky[0] = '\0';
505: if (cf->file_ent->ce_tag != NULL) {
1.57 xsa 506: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.9 joris 507: cf->file_ent->ce_tag);
1.1 jfb 508: }
509:
1.55 otto 510: cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
1.9 joris 511: cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
1.53 xsa 512: rev, timebuf, cf->file_ent->ce_opts ?
513: cf->file_ent->ce_opts : "", sticky);
1.9 joris 514: }
515:
516: switch (cf->file_status) {
517: case FILE_UNKNOWN:
518: if (cf->fd != -1)
519: cvs_client_send_request("Questionable %s",
520: cf->file_name);
521: break;
522: case FILE_ADDED:
523: case FILE_MODIFIED:
524: cvs_client_send_request("Modified %s", cf->file_name);
525: cvs_remote_send_file(cf->file_path);
526: break;
527: case FILE_UPTODATE:
528: cvs_client_send_request("Unchanged %s", cf->file_name);
529: break;
1.1 jfb 530: }
1.9 joris 531: }
1.1 jfb 532:
1.9 joris 533: void
534: cvs_client_send_files(char **argv, int argc)
535: {
536: int i;
1.1 jfb 537:
1.9 joris 538: for (i = 0; i < argc; i++)
539: cvs_client_send_request("Argument %s", argv[i]);
540: }
1.1 jfb 541:
1.9 joris 542: void
543: cvs_client_ok(char *data)
544: {
545: end_of_response = 1;
1.37 joris 546: server_response = SERVER_OK;
1.1 jfb 547: }
548:
1.9 joris 549: void
550: cvs_client_error(char *data)
551: {
552: end_of_response = 1;
1.37 joris 553: server_response = SERVER_ERROR;
1.9 joris 554: }
1.1 jfb 555:
1.9 joris 556: void
557: cvs_client_validreq(char *data)
1.1 jfb 558: {
1.9 joris 559: int i;
560: char *sp, *ep;
561: struct cvs_req *req;
562:
1.62 ray 563: if ((sp = data) == NULL)
564: fatal("Missing argument for Valid-requests");
565:
1.9 joris 566: do {
567: if ((ep = strchr(sp, ' ')) != NULL)
568: *ep = '\0';
569:
570: req = cvs_remote_get_request_info(sp);
571: if (req != NULL)
572: req->supported = 1;
573:
574: if (ep != NULL)
575: sp = ep + 1;
576: } while (ep != NULL);
577:
578: for (i = 0; cvs_requests[i].supported != -1; i++) {
579: req = &cvs_requests[i];
580: if ((req->flags & REQ_NEEDED) &&
581: req->supported != 1) {
582: fatal("server does not support required '%s'",
583: req->name);
584: }
1.1 jfb 585: }
1.9 joris 586: }
1.1 jfb 587:
1.9 joris 588: void
589: cvs_client_e(char *data)
590: {
1.62 ray 591: if (data == NULL)
592: fatal("Missing argument for E");
593:
1.9 joris 594: cvs_printf("%s\n", data);
595: }
1.1 jfb 596:
1.9 joris 597: void
598: cvs_client_m(char *data)
599: {
1.62 ray 600: if (data == NULL)
601: fatal("Missing argument for M");
602:
1.9 joris 603: cvs_printf("%s\n", data);
604: }
1.1 jfb 605:
1.9 joris 606: void
607: cvs_client_checkedin(char *data)
608: {
609: CVSENTRIES *entlist;
610: struct cvs_ent *ent, *newent;
1.67 xsa 611: char *dir, *e, entry[CVS_ENT_MAXLINELEN], rev[CVS_REV_BUFSZ];
612: char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
1.1 jfb 613:
1.62 ray 614: if (data == NULL)
615: fatal("Missing argument for Checked-in");
616:
1.9 joris 617: dir = cvs_remote_input();
1.54 otto 618: e = cvs_remote_input();
1.9 joris 619: xfree(dir);
1.1 jfb 620:
1.9 joris 621: entlist = cvs_ent_open(data);
1.54 otto 622: newent = cvs_ent_parse(e);
1.9 joris 623: ent = cvs_ent_get(entlist, newent->ce_name);
1.54 otto 624: xfree(e);
1.1 jfb 625:
1.9 joris 626: rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
627: ctime_r(&ent->ce_mtime, timebuf);
628: if (timebuf[strlen(timebuf) - 1] == '\n')
629: timebuf[strlen(timebuf) - 1] = '\0';
1.1 jfb 630:
1.9 joris 631: sticky[0] = '\0';
1.57 xsa 632: if (ent->ce_tag != NULL)
633: (void)xsnprintf(sticky, sizeof(sticky), "T%s", ent->ce_tag);
1.1 jfb 634:
1.57 xsa 635: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s%s/%s/%s/%s",
1.44 joris 636: newent->ce_name, (newent->ce_status == CVS_ENT_REMOVED) ? "-" : "",
1.53 xsa 637: rev, timebuf, ent->ce_opts ? ent->ce_opts : "", sticky);
1.9 joris 638:
639: cvs_ent_free(ent);
640: cvs_ent_free(newent);
641: cvs_ent_add(entlist, entry);
642: cvs_ent_close(entlist, ENT_SYNC);
1.1 jfb 643: }
644:
1.9 joris 645: void
646: cvs_client_updated(char *data)
647: {
1.57 xsa 648: int fd;
1.9 joris 649: time_t now;
1.51 otto 650: mode_t fmode, mask;
1.9 joris 651: size_t flen;
652: CVSENTRIES *ent;
653: struct cvs_ent *e;
654: const char *errstr;
655: struct timeval tv[2];
1.67 xsa 656: char repo[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
657: char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
658: char *en, *mode, *len, *fpath, *rpath, *wdir;
1.69 joris 659: char sticky[CVS_ENT_MAXLINELEN];
1.9 joris 660:
1.62 ray 661: if (data == NULL)
662: fatal("Missing argument for Updated");
663:
1.10 joris 664: client_check_directory(data);
1.9 joris 665:
666: rpath = cvs_remote_input();
1.54 otto 667: en = cvs_remote_input();
1.9 joris 668: mode = cvs_remote_input();
669: len = cvs_remote_input();
670:
671: cvs_get_repository_path(".", repo, MAXPATHLEN);
1.48 joris 672:
673: STRIP_SLASH(repo);
1.9 joris 674:
675: if (strlen(repo) + 1 > strlen(rpath))
676: fatal("received a repository path that is too short");
677:
678: fpath = rpath + strlen(repo) + 1;
679: if ((wdir = dirname(fpath)) == NULL)
680: fatal("cvs_client_updated: dirname: %s", strerror(errno));
681:
682: flen = strtonum(len, 0, INT_MAX, &errstr);
683: if (errstr != NULL)
684: fatal("cvs_client_updated: %s: %s", len, errstr);
685: xfree(len);
686:
687: cvs_strtomode(mode, &fmode);
688: xfree(mode);
1.51 otto 689: mask = umask(0);
690: umask(mask);
691: fmode &= ~mask;
1.9 joris 692:
693: time(&now);
1.52 joris 694: asctime_r(gmtime(&now), timebuf);
1.9 joris 695: if (timebuf[strlen(timebuf) - 1] == '\n')
696: timebuf[strlen(timebuf) - 1] = '\0';
697:
1.54 otto 698: e = cvs_ent_parse(en);
699: xfree(en);
1.69 joris 700:
701: sticky[0] = '\0';
702: if (e->ce_tag != NULL)
703: (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
704:
1.9 joris 705: rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
1.69 joris 706: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
707: e->ce_name, revbuf, timebuf,
708: e->ce_opts ? e->ce_opts : "", sticky);
1.9 joris 709:
710: cvs_ent_free(e);
711: ent = cvs_ent_open(wdir);
712: cvs_ent_add(ent, entry);
713: cvs_ent_close(ent, ENT_SYNC);
714:
715: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
716: fatal("cvs_client_updated: open: %s: %s",
717: fpath, strerror(errno));
1.1 jfb 718:
1.46 joris 719: cvs_remote_receive_file(fd, flen);
1.1 jfb 720:
1.9 joris 721: tv[0].tv_sec = now;
722: tv[0].tv_usec = 0;
723: tv[1] = tv[0];
1.1 jfb 724:
1.9 joris 725: if (futimes(fd, tv) == -1)
726: fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1 jfb 727:
1.9 joris 728: if (fchmod(fd, fmode) == -1)
729: fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1 jfb 730:
1.9 joris 731: (void)close(fd);
1.1 jfb 732:
1.9 joris 733: xfree(rpath);
1.1 jfb 734: }
735:
1.9 joris 736: void
737: cvs_client_merged(char *data)
738: {
1.56 joris 739: int fd;
740: time_t now;
741: mode_t fmode;
742: size_t flen;
743: CVSENTRIES *ent;
744: const char *errstr;
745: struct timeval tv[2];
1.67 xsa 746: char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
1.56 joris 747: char *len, *fpath, *wdir;
748:
1.62 ray 749: if (data == NULL)
750: fatal("Missing argument for Merged");
751:
1.56 joris 752: client_check_directory(data);
753:
754: rpath = cvs_remote_input();
755: entry = cvs_remote_input();
756: mode = cvs_remote_input();
757: len = cvs_remote_input();
758:
759: repo = xmalloc(MAXPATHLEN);
760: cvs_get_repository_path(".", repo, MAXPATHLEN);
761:
762: STRIP_SLASH(repo);
763:
764: if (strlen(repo) + 1 > strlen(rpath))
765: fatal("received a repository path that is too short");
766:
767: fpath = rpath + strlen(repo) + 1;
768: if ((wdir = dirname(fpath)) == NULL)
769: fatal("cvs_client_merged: dirname: %s", strerror(errno));
770: xfree(repo);
771:
772: flen = strtonum(len, 0, INT_MAX, &errstr);
773: if (errstr != NULL)
774: fatal("cvs_client_merged: %s: %s", len, errstr);
775: xfree(len);
776:
777: cvs_strtomode(mode, &fmode);
778: xfree(mode);
779:
780: time(&now);
781: asctime_r(gmtime(&now), timebuf);
782: if (timebuf[strlen(timebuf) - 1] == '\n')
783: timebuf[strlen(timebuf) - 1] = '\0';
784:
785: ent = cvs_ent_open(wdir);
786: cvs_ent_add(ent, entry);
787: cvs_ent_close(ent, ENT_SYNC);
788:
789: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
790: fatal("cvs_client_merged: open: %s: %s",
791: fpath, strerror(errno));
792:
793: cvs_remote_receive_file(fd, flen);
794:
795: tv[0].tv_sec = now;
796: tv[0].tv_usec = 0;
797: tv[1] = tv[0];
798:
799: if (futimes(fd, tv) == -1)
800: fatal("cvs_client_merged: futimes: %s", strerror(errno));
801:
802: if (fchmod(fd, fmode) == -1)
803: fatal("cvs_client_merged: fchmod: %s", strerror(errno));
804:
805: (void)close(fd);
806:
807: xfree(rpath);
1.9 joris 808: }
1.1 jfb 809:
1.9 joris 810: void
811: cvs_client_removed(char *data)
812: {
1.65 joris 813: CVSENTRIES *entlist;
814: char *rpath, *filename, fpath[MAXPATHLEN];
815:
816: rpath = cvs_remote_input();
817: if ((filename = strrchr(rpath, '/')) == NULL)
818: fatal("bad rpath in cvs_client_removed: %s", rpath);
819: filename++;
820:
821: entlist = cvs_ent_open(data);
822: cvs_ent_remove(entlist, filename);
823: cvs_ent_close(entlist, ENT_SYNC);
824:
825: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
826: (void)unlink(fpath);
1.13 joris 827:
1.65 joris 828: xfree(rpath);
1.9 joris 829: }
1.1 jfb 830:
1.9 joris 831: void
832: cvs_client_remove_entry(char *data)
1.1 jfb 833: {
1.38 xsa 834: CVSENTRIES *entlist;
1.44 joris 835: char *filename, *rpath;
1.41 joris 836:
1.62 ray 837: if (data == NULL)
838: fatal("Missing argument for Remove-entry");
839:
1.41 joris 840: rpath = cvs_remote_input();
841: if ((filename = strrchr(rpath, '/')) == NULL)
842: fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
1.61 ray 843: filename++;
1.1 jfb 844:
1.38 xsa 845: entlist = cvs_ent_open(data);
1.44 joris 846: cvs_ent_remove(entlist, filename);
1.38 xsa 847: cvs_ent_close(entlist, ENT_SYNC);
848:
1.44 joris 849: xfree(rpath);
1.36 xsa 850: }
851:
852: void
853: cvs_client_set_static_directory(char *data)
854: {
855: FILE *fp;
1.50 otto 856: char *dir, fpath[MAXPATHLEN];
1.36 xsa 857:
858: if (cvs_cmdop == CVS_OP_EXPORT)
859: return;
860:
1.62 ray 861: if (data == NULL)
862: fatal("Missing argument for Set-static-directory");
863:
1.36 xsa 864: STRIP_SLASH(data);
865:
866: dir = cvs_remote_input();
867: xfree(dir);
868:
1.58 xsa 869: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
870: data, CVS_PATH_STATICENTRIES);
1.36 xsa 871:
872: if ((fp = fopen(fpath, "w+")) == NULL) {
873: cvs_log(LP_ERRNO, "%s", fpath);
1.50 otto 874: return;
1.36 xsa 875: }
876: (void)fclose(fp);
1.16 xsa 877: }
1.35 xsa 878:
879: void
880: cvs_client_clear_static_directory(char *data)
881: {
1.50 otto 882: char *dir, fpath[MAXPATHLEN];
1.35 xsa 883:
884: if (cvs_cmdop == CVS_OP_EXPORT)
885: return;
886:
1.62 ray 887: if (data == NULL)
888: fatal("Missing argument for Clear-static-directory");
889:
1.35 xsa 890: STRIP_SLASH(data);
891:
892: dir = cvs_remote_input();
893: xfree(dir);
894:
1.58 xsa 895: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
896: data, CVS_PATH_STATICENTRIES);
1.35 xsa 897:
898: (void)cvs_unlink(fpath);
899: }
900:
901: void
902: cvs_client_set_sticky(char *data)
903: {
904: FILE *fp;
1.50 otto 905: char *dir, *tag, tagpath[MAXPATHLEN];
1.35 xsa 906:
1.47 xsa 907: if (cvs_cmdop == CVS_OP_EXPORT)
908: return;
909:
1.62 ray 910: if (data == NULL)
911: fatal("Missing argument for Set-sticky");
912:
1.35 xsa 913: STRIP_SLASH(data);
914:
915: dir = cvs_remote_input();
916: xfree(dir);
917: tag = cvs_remote_input();
918:
1.69 joris 919: client_check_directory(data);
920:
1.58 xsa 921: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.35 xsa 922:
923: if ((fp = fopen(tagpath, "w+")) == NULL) {
924: cvs_log(LP_ERRNO, "%s", tagpath);
925: goto out;
926: }
927:
928: (void)fprintf(fp, "%s\n", tag);
929: (void)fclose(fp);
930: out:
931: xfree(tag);
932: }
933:
934: void
935: cvs_client_clear_sticky(char *data)
936: {
1.50 otto 937: char *dir, tagpath[MAXPATHLEN];
1.47 xsa 938:
939: if (cvs_cmdop == CVS_OP_EXPORT)
940: return;
1.62 ray 941:
942: if (data == NULL)
943: fatal("Missing argument for Clear-sticky");
1.35 xsa 944:
945: STRIP_SLASH(data);
946:
947: dir = cvs_remote_input();
948: xfree(dir);
949:
1.69 joris 950: client_check_directory(data);
951:
1.58 xsa 952: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.69 joris 953: (void)unlink(tagpath);
1.35 xsa 954: }
955:
956:
1.16 xsa 957: /*
958: * cvs_client_initlog()
959: *
960: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
961: * set. In this case, the variable's value is used as a path to which the
962: * appropriate suffix is added (".in" for client input and ".out" for server
963: * output).
964: */
965: static void
966: cvs_client_initlog(void)
967: {
968: u_int i;
969: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
970: char rpath[MAXPATHLEN], *s;
971: struct stat st;
972: time_t now;
973: struct passwd *pwd;
974:
975: /* avoid doing it more than once */
976: if (cvs_client_logon)
977: return;
978:
979: if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
980: return;
981:
982: envdup = xstrdup(env);
983: if ((s = strchr(envdup, '%')) != NULL)
984: *s = '\0';
985:
986: if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
987: fatal("cvs_client_initlog: truncation");
988:
989: if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
990: fatal("cvs_client_initlog: truncation");
991:
992: xfree(envdup);
993:
994: s = buf;
995: while ((s = strchr(s, '%')) != NULL) {
996: s++;
997: switch (*s) {
998: case 'c':
999: if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
1000: sizeof(fpath))
1001: fatal("cvs_client_initlog: truncation");
1002: break;
1003: case 'd':
1004: time(&now);
1005: if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
1006: sizeof(fpath))
1007: fatal("cvs_client_initlog: truncation");
1008: break;
1009: case 'p':
1.57 xsa 1010: (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1.16 xsa 1011: break;
1012: case 'u':
1013: if ((pwd = getpwuid(getuid())) != NULL) {
1014: if (strlcpy(fpath, pwd->pw_name,
1015: sizeof(fpath)) >= sizeof(fpath))
1016: fatal("cvs_client_initlog: truncation");
1017: } else {
1018: fpath[0] = '\0';
1019: }
1020: endpwent();
1021: break;
1022: default:
1023: fpath[0] = '\0';
1024: break;
1025: }
1026:
1027: if (fpath[0] != '\0') {
1028: if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
1029: fatal("cvs_client_initlog: truncation");
1030:
1031: if (strlcat(rpath, fpath, sizeof(rpath))
1032: >= sizeof(rpath))
1033: fatal("cvs_client_initlog: truncation");
1034: }
1035: }
1036:
1037: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1038: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.16 xsa 1039:
1040: if (stat(fpath, &st) != -1)
1041: continue;
1042:
1043: if (errno != ENOENT)
1044: fatal("cvs_client_initlog() stat failed '%s'",
1045: strerror(errno));
1046:
1047: break;
1048: }
1049:
1050: if ((cvs_client_inlog_fd = open(fpath,
1.60 xsa 1051: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1052: fatal("cvs_client_initlog: open `%s': %s",
1053: fpath, strerror(errno));
1054: }
1055:
1056: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1057: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.16 xsa 1058:
1059: if (stat(fpath, &st) != -1)
1060: continue;
1061:
1062: if (errno != ENOENT)
1063: fatal("cvs_client_initlog() stat failed '%s'",
1064: strerror(errno));
1065:
1066: break;
1067: }
1068:
1069: if ((cvs_client_outlog_fd = open(fpath,
1.60 xsa 1070: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1071: fatal("cvs_client_initlog: open `%s': %s",
1072: fpath, strerror(errno));
1073: }
1074:
1075: cvs_client_logon = 1;
1.1 jfb 1076: }