Annotation of src/usr.bin/cvs/client.c, Revision 1.85
1.85 ! tobias 1: /* $OpenBSD: client.c,v 1.84 2008/01/10 09:41:52 tobias 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.75 tobias 109: { "init", 0, cvs_server_init, 0 },
1.66 joris 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:
1.76 tobias 282: switch (verbosity) {
283: case 0:
1.19 xsa 284: cvs_client_send_request("Global_option -Q");
1.76 tobias 285: break;
286: case 1:
287: /* Be quiet. This is the default in OpenCVS. */
288: cvs_client_send_request("Global_option -q");
289: break;
290: default:
291: break;
292: }
1.19 xsa 293:
294: if (cvs_readonly == 1)
295: cvs_client_send_request("Global_option -r");
296:
297: if (cvs_trace == 1)
1.9 joris 298: cvs_client_send_request("Global_option -t");
1.26 xsa 299:
300: /* XXX: If 'Set' is supported? */
301: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
302: cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1 jfb 303: }
304:
1.9 joris 305: void
306: cvs_client_send_request(char *fmt, ...)
307: {
308: va_list ap;
309: char *data, *s;
310: struct cvs_req *req;
311:
312: va_start(ap, fmt);
313: vasprintf(&data, fmt, ap);
314: va_end(ap);
1.1 jfb 315:
1.9 joris 316: if ((s = strchr(data, ' ')) != NULL)
317: *s = '\0';
1.1 jfb 318:
1.9 joris 319: req = cvs_remote_get_request_info(data);
320: if (req == NULL)
321: fatal("'%s' is an unknown request", data);
1.1 jfb 322:
1.9 joris 323: if (req->supported != 1)
324: fatal("remote cvs server does not support '%s'", data);
1.1 jfb 325:
1.9 joris 326: if (s != NULL)
327: *s = ' ';
1.1 jfb 328:
1.12 joris 329: cvs_log(LP_TRACE, "%s", data);
1.16 xsa 330:
1.9 joris 331: cvs_remote_output(data);
332: xfree(data);
333: }
1.1 jfb 334:
1.9 joris 335: void
336: cvs_client_read_response(void)
1.1 jfb 337: {
1.9 joris 338: char *cmd, *data;
339: struct cvs_resp *resp;
1.1 jfb 340:
1.9 joris 341: cmd = cvs_remote_input();
342: if ((data = strchr(cmd, ' ')) != NULL)
343: (*data++) = '\0';
1.1 jfb 344:
1.9 joris 345: resp = cvs_remote_get_response_info(cmd);
346: if (resp == NULL)
347: fatal("response '%s' is not supported by our client", cmd);
1.1 jfb 348:
1.9 joris 349: if (resp->hdlr == NULL)
350: fatal("opencvs client does not support '%s'", cmd);
1.16 xsa 351:
1.9 joris 352: (*resp->hdlr)(data);
1.16 xsa 353:
1.9 joris 354: xfree(cmd);
1.1 jfb 355: }
356:
1.9 joris 357: void
358: cvs_client_get_responses(void)
359: {
360: while (end_of_response != 1)
361: cvs_client_read_response();
1.1 jfb 362:
1.9 joris 363: end_of_response = 0;
1.72 joris 364: }
365:
366: void
367: cvs_client_send_logmsg(char *msg)
368: {
369: char *buf, *p, *q;
370:
371: (void)xasprintf(&buf, "%s\n", msg);
372:
373: cvs_client_send_request("Argument -m");
374: if ((p = strchr(buf, '\n')) != NULL)
375: *p++ = '\0';
376: cvs_client_send_request("Argument %s", buf);
377: for (q = p; p != NULL; q = p) {
378: if ((p = strchr(q, '\n')) != NULL) {
379: *p++ = '\0';
380: cvs_client_send_request("Argumentx %s", q);
381: }
382: }
383:
1.73 joris 384: xfree(buf);
1.9 joris 385: }
1.1 jfb 386:
1.9 joris 387: void
388: cvs_client_senddir(const char *dir)
1.1 jfb 389: {
1.34 xsa 390: struct stat st;
1.29 xsa 391: int nb;
1.50 otto 392: char *d, *date, fpath[MAXPATHLEN], repo[MAXPATHLEN], *tag;
1.29 xsa 393:
394: d = NULL;
1.9 joris 395:
396: if (lastdir != NULL && !strcmp(dir, lastdir))
397: return;
1.1 jfb 398:
1.9 joris 399: cvs_get_repository_path(dir, repo, MAXPATHLEN);
1.31 xsa 400:
1.81 tobias 401: if (cvs_cmdop != CVS_OP_RLOG)
1.70 xsa 402: cvs_client_send_request("Directory %s\n%s", dir, repo);
1.31 xsa 403:
1.58 xsa 404: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
405: dir, CVS_PATH_STATICENTRIES);
1.34 xsa 406:
407: if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
408: cvs_client_send_request("Static-directory");
409:
1.29 xsa 410: d = xstrdup(dir);
411: cvs_parse_tagfile(d, &tag, &date, &nb);
412:
413: if (tag != NULL || date != NULL) {
414: char buf[128];
415:
1.82 chl 416: if (tag != NULL && nb != 0) {
1.29 xsa 417: if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
418: fatal("cvs_client_senddir: truncation");
419: } else if (tag != NULL) {
420: if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
421: fatal("cvs_client_senddir: truncation");
422: } else {
423: if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
424: fatal("cvs_client_senddir: truncation");
425: }
426:
427: if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
428: fatal("cvs_client_senddir: truncation");
429:
430: cvs_client_send_request("Sticky %s", buf);
431:
432: if (tag != NULL)
433: xfree(tag);
434: if (date != NULL)
435: xfree(date);
436: }
437: if (d != NULL)
438: xfree(d);
1.1 jfb 439:
1.9 joris 440: if (lastdir != NULL)
441: xfree(lastdir);
442: lastdir = xstrdup(dir);
443: }
1.1 jfb 444:
1.9 joris 445: void
446: cvs_client_sendfile(struct cvs_file *cf)
1.1 jfb 447: {
1.9 joris 448: size_t len;
1.67 xsa 449: char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[32];
1.1 jfb 450:
1.9 joris 451: if (cf->file_type != CVS_FILE)
452: return;
1.1 jfb 453:
1.9 joris 454: cvs_client_senddir(cf->file_wd);
455: cvs_remote_classify_file(cf);
456:
1.10 joris 457: if (cf->file_type == CVS_DIR)
458: return;
459:
1.85 ! tobias 460: if (cf->file_ent != NULL && cvs_cmdop != CVS_OP_IMPORT) {
1.9 joris 461: if (cf->file_status == FILE_ADDED) {
462: len = strlcpy(rev, "0", sizeof(rev));
463: if (len >= sizeof(rev))
464: fatal("cvs_client_sendfile: truncation");
465:
466: len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
467: if (len >= sizeof(timebuf))
468: fatal("cvs_client_sendfile: truncation");
469:
470: len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
471: if (len >= sizeof(timebuf))
472: fatal("cvs_client_sendfile: truncation");
473: } else {
474: rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
475: ctime_r(&cf->file_ent->ce_mtime, timebuf);
476: }
1.1 jfb 477:
1.9 joris 478: if (cf->file_ent->ce_conflict == NULL) {
479: if (timebuf[strlen(timebuf) - 1] == '\n')
480: timebuf[strlen(timebuf) - 1] = '\0';
481: } else {
482: len = strlcpy(timebuf, cf->file_ent->ce_conflict,
483: sizeof(timebuf));
484: if (len >= sizeof(timebuf))
485: fatal("cvs_client_sendfile: truncation");
1.56 joris 486: len = strlcat(timebuf, "+", sizeof(timebuf));
487: if (len >= sizeof(timebuf))
488: fatal("cvs_client_sendfile: truncation");
1.9 joris 489: }
1.1 jfb 490:
1.9 joris 491: sticky[0] = '\0';
492: if (cf->file_ent->ce_tag != NULL) {
1.57 xsa 493: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.9 joris 494: cf->file_ent->ce_tag);
1.1 jfb 495: }
496:
1.55 otto 497: cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
1.9 joris 498: cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
1.53 xsa 499: rev, timebuf, cf->file_ent->ce_opts ?
500: cf->file_ent->ce_opts : "", sticky);
1.9 joris 501: }
502:
1.84 tobias 503: if (cvs_cmdop == CVS_OP_ADD)
504: cf->file_status = FILE_MODIFIED;
505:
1.9 joris 506: switch (cf->file_status) {
507: case FILE_UNKNOWN:
508: if (cf->fd != -1)
509: cvs_client_send_request("Questionable %s",
510: cf->file_name);
511: break;
512: case FILE_ADDED:
513: case FILE_MODIFIED:
514: cvs_client_send_request("Modified %s", cf->file_name);
515: cvs_remote_send_file(cf->file_path);
516: break;
517: case FILE_UPTODATE:
518: cvs_client_send_request("Unchanged %s", cf->file_name);
519: break;
1.1 jfb 520: }
1.9 joris 521: }
1.1 jfb 522:
1.9 joris 523: void
524: cvs_client_send_files(char **argv, int argc)
525: {
526: int i;
1.1 jfb 527:
1.9 joris 528: for (i = 0; i < argc; i++)
529: cvs_client_send_request("Argument %s", argv[i]);
530: }
1.1 jfb 531:
1.9 joris 532: void
533: cvs_client_ok(char *data)
534: {
535: end_of_response = 1;
1.37 joris 536: server_response = SERVER_OK;
1.1 jfb 537: }
538:
1.9 joris 539: void
540: cvs_client_error(char *data)
541: {
542: end_of_response = 1;
1.37 joris 543: server_response = SERVER_ERROR;
1.9 joris 544: }
1.1 jfb 545:
1.9 joris 546: void
547: cvs_client_validreq(char *data)
1.1 jfb 548: {
1.9 joris 549: int i;
550: char *sp, *ep;
551: struct cvs_req *req;
552:
1.62 ray 553: if ((sp = data) == NULL)
554: fatal("Missing argument for Valid-requests");
555:
1.9 joris 556: do {
557: if ((ep = strchr(sp, ' ')) != NULL)
558: *ep = '\0';
559:
560: req = cvs_remote_get_request_info(sp);
561: if (req != NULL)
562: req->supported = 1;
563:
564: if (ep != NULL)
565: sp = ep + 1;
566: } while (ep != NULL);
567:
568: for (i = 0; cvs_requests[i].supported != -1; i++) {
569: req = &cvs_requests[i];
570: if ((req->flags & REQ_NEEDED) &&
571: req->supported != 1) {
572: fatal("server does not support required '%s'",
573: req->name);
574: }
1.1 jfb 575: }
1.9 joris 576: }
1.1 jfb 577:
1.9 joris 578: void
579: cvs_client_e(char *data)
580: {
1.62 ray 581: if (data == NULL)
582: fatal("Missing argument for E");
583:
1.80 tobias 584: fprintf(stderr, "%s\n", data);
1.9 joris 585: }
1.1 jfb 586:
1.9 joris 587: void
588: cvs_client_m(char *data)
589: {
1.62 ray 590: if (data == NULL)
591: fatal("Missing argument for M");
592:
1.80 tobias 593: puts(data);
1.9 joris 594: }
1.1 jfb 595:
1.9 joris 596: void
597: cvs_client_checkedin(char *data)
598: {
599: CVSENTRIES *entlist;
600: struct cvs_ent *ent, *newent;
1.84 tobias 601: size_t len;
1.67 xsa 602: char *dir, *e, entry[CVS_ENT_MAXLINELEN], rev[CVS_REV_BUFSZ];
603: char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
1.1 jfb 604:
1.62 ray 605: if (data == NULL)
606: fatal("Missing argument for Checked-in");
607:
1.9 joris 608: dir = cvs_remote_input();
1.54 otto 609: e = cvs_remote_input();
1.9 joris 610: xfree(dir);
1.1 jfb 611:
1.9 joris 612: entlist = cvs_ent_open(data);
1.54 otto 613: newent = cvs_ent_parse(e);
1.9 joris 614: ent = cvs_ent_get(entlist, newent->ce_name);
1.54 otto 615: xfree(e);
1.1 jfb 616:
1.9 joris 617: rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
1.1 jfb 618:
1.9 joris 619: sticky[0] = '\0';
1.84 tobias 620: if (ent == NULL) {
621: len = strlcpy(rev, "0", sizeof(rev));
622: if (len >= sizeof(rev))
623: fatal("cvs_client_sendfile: truncation");
624:
625: len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
626: if (len >= sizeof(timebuf))
627: fatal("cvs_client_sendfile: truncation");
628:
629: len = strlcat(timebuf, newent->ce_name, sizeof(timebuf));
630: if (len >= sizeof(timebuf))
631: fatal("cvs_client_sendfile: truncation");
632: } else {
633: ctime_r(&ent->ce_mtime, timebuf);
634: timebuf[strcspn(timebuf, "\n")] = '\0';
635:
636: if (newent->ce_tag != NULL)
637: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
638: ent->ce_tag);
639: newent->ce_opts = ent->ce_opts;
640:
641: cvs_ent_free(ent);
642: }
1.1 jfb 643:
1.57 xsa 644: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s%s/%s/%s/%s",
1.44 joris 645: newent->ce_name, (newent->ce_status == CVS_ENT_REMOVED) ? "-" : "",
1.84 tobias 646: rev, timebuf, newent->ce_opts ? newent->ce_opts : "", sticky);
1.9 joris 647:
648: cvs_ent_free(newent);
649: cvs_ent_add(entlist, entry);
650: cvs_ent_close(entlist, ENT_SYNC);
1.1 jfb 651: }
652:
1.9 joris 653: void
654: cvs_client_updated(char *data)
655: {
1.57 xsa 656: int fd;
1.9 joris 657: time_t now;
1.51 otto 658: mode_t fmode, mask;
1.9 joris 659: size_t flen;
660: CVSENTRIES *ent;
661: struct cvs_ent *e;
662: const char *errstr;
663: struct timeval tv[2];
1.67 xsa 664: char repo[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
665: char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
666: char *en, *mode, *len, *fpath, *rpath, *wdir;
1.69 joris 667: char sticky[CVS_ENT_MAXLINELEN];
1.9 joris 668:
1.62 ray 669: if (data == NULL)
670: fatal("Missing argument for Updated");
671:
1.10 joris 672: client_check_directory(data);
1.9 joris 673:
674: rpath = cvs_remote_input();
1.54 otto 675: en = cvs_remote_input();
1.9 joris 676: mode = cvs_remote_input();
677: len = cvs_remote_input();
678:
679: cvs_get_repository_path(".", repo, MAXPATHLEN);
1.48 joris 680:
681: STRIP_SLASH(repo);
1.9 joris 682:
683: if (strlen(repo) + 1 > strlen(rpath))
684: fatal("received a repository path that is too short");
685:
686: fpath = rpath + strlen(repo) + 1;
687: if ((wdir = dirname(fpath)) == NULL)
688: fatal("cvs_client_updated: dirname: %s", strerror(errno));
689:
690: flen = strtonum(len, 0, INT_MAX, &errstr);
691: if (errstr != NULL)
692: fatal("cvs_client_updated: %s: %s", len, errstr);
693: xfree(len);
694:
695: cvs_strtomode(mode, &fmode);
696: xfree(mode);
1.51 otto 697: mask = umask(0);
698: umask(mask);
699: fmode &= ~mask;
1.9 joris 700:
701: time(&now);
1.52 joris 702: asctime_r(gmtime(&now), timebuf);
1.9 joris 703: if (timebuf[strlen(timebuf) - 1] == '\n')
704: timebuf[strlen(timebuf) - 1] = '\0';
705:
1.54 otto 706: e = cvs_ent_parse(en);
707: xfree(en);
1.69 joris 708:
709: sticky[0] = '\0';
710: if (e->ce_tag != NULL)
711: (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
712:
1.9 joris 713: rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
1.69 joris 714: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
715: e->ce_name, revbuf, timebuf,
716: e->ce_opts ? e->ce_opts : "", sticky);
1.9 joris 717:
718: cvs_ent_free(e);
719: ent = cvs_ent_open(wdir);
720: cvs_ent_add(ent, entry);
721: cvs_ent_close(ent, ENT_SYNC);
722:
723: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
724: fatal("cvs_client_updated: open: %s: %s",
725: fpath, strerror(errno));
1.1 jfb 726:
1.46 joris 727: cvs_remote_receive_file(fd, flen);
1.1 jfb 728:
1.9 joris 729: tv[0].tv_sec = now;
730: tv[0].tv_usec = 0;
731: tv[1] = tv[0];
1.1 jfb 732:
1.9 joris 733: if (futimes(fd, tv) == -1)
734: fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1 jfb 735:
1.9 joris 736: if (fchmod(fd, fmode) == -1)
737: fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1 jfb 738:
1.9 joris 739: (void)close(fd);
1.1 jfb 740:
1.9 joris 741: xfree(rpath);
1.1 jfb 742: }
743:
1.9 joris 744: void
745: cvs_client_merged(char *data)
746: {
1.56 joris 747: int fd;
748: time_t now;
1.83 tobias 749: mode_t fmode, mask;
1.56 joris 750: size_t flen;
751: CVSENTRIES *ent;
752: const char *errstr;
753: struct timeval tv[2];
1.67 xsa 754: char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
1.56 joris 755: char *len, *fpath, *wdir;
756:
1.62 ray 757: if (data == NULL)
758: fatal("Missing argument for Merged");
759:
1.56 joris 760: client_check_directory(data);
761:
762: rpath = cvs_remote_input();
763: entry = cvs_remote_input();
764: mode = cvs_remote_input();
765: len = cvs_remote_input();
766:
767: repo = xmalloc(MAXPATHLEN);
768: cvs_get_repository_path(".", repo, MAXPATHLEN);
769:
770: STRIP_SLASH(repo);
771:
772: if (strlen(repo) + 1 > strlen(rpath))
773: fatal("received a repository path that is too short");
774:
775: fpath = rpath + strlen(repo) + 1;
776: if ((wdir = dirname(fpath)) == NULL)
777: fatal("cvs_client_merged: dirname: %s", strerror(errno));
778: xfree(repo);
779:
780: flen = strtonum(len, 0, INT_MAX, &errstr);
781: if (errstr != NULL)
782: fatal("cvs_client_merged: %s: %s", len, errstr);
783: xfree(len);
784:
785: cvs_strtomode(mode, &fmode);
786: xfree(mode);
1.83 tobias 787: mask = umask(0);
788: umask(mask);
789: fmode &= ~mask;
1.56 joris 790:
791: time(&now);
792: asctime_r(gmtime(&now), timebuf);
793: if (timebuf[strlen(timebuf) - 1] == '\n')
794: timebuf[strlen(timebuf) - 1] = '\0';
795:
796: ent = cvs_ent_open(wdir);
797: cvs_ent_add(ent, entry);
798: cvs_ent_close(ent, ENT_SYNC);
799:
800: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
801: fatal("cvs_client_merged: open: %s: %s",
802: fpath, strerror(errno));
803:
804: cvs_remote_receive_file(fd, flen);
805:
806: tv[0].tv_sec = now;
807: tv[0].tv_usec = 0;
808: tv[1] = tv[0];
809:
810: if (futimes(fd, tv) == -1)
811: fatal("cvs_client_merged: futimes: %s", strerror(errno));
812:
813: if (fchmod(fd, fmode) == -1)
814: fatal("cvs_client_merged: fchmod: %s", strerror(errno));
815:
816: (void)close(fd);
817:
818: xfree(rpath);
1.9 joris 819: }
1.1 jfb 820:
1.9 joris 821: void
822: cvs_client_removed(char *data)
823: {
1.65 joris 824: CVSENTRIES *entlist;
825: char *rpath, *filename, fpath[MAXPATHLEN];
826:
827: rpath = cvs_remote_input();
828: if ((filename = strrchr(rpath, '/')) == NULL)
829: fatal("bad rpath in cvs_client_removed: %s", rpath);
830: filename++;
831:
832: entlist = cvs_ent_open(data);
833: cvs_ent_remove(entlist, filename);
834: cvs_ent_close(entlist, ENT_SYNC);
835:
836: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
837: (void)unlink(fpath);
1.13 joris 838:
1.65 joris 839: xfree(rpath);
1.9 joris 840: }
1.1 jfb 841:
1.9 joris 842: void
843: cvs_client_remove_entry(char *data)
1.1 jfb 844: {
1.38 xsa 845: CVSENTRIES *entlist;
1.44 joris 846: char *filename, *rpath;
1.41 joris 847:
1.62 ray 848: if (data == NULL)
849: fatal("Missing argument for Remove-entry");
850:
1.41 joris 851: rpath = cvs_remote_input();
852: if ((filename = strrchr(rpath, '/')) == NULL)
853: fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
1.61 ray 854: filename++;
1.1 jfb 855:
1.38 xsa 856: entlist = cvs_ent_open(data);
1.44 joris 857: cvs_ent_remove(entlist, filename);
1.38 xsa 858: cvs_ent_close(entlist, ENT_SYNC);
859:
1.44 joris 860: xfree(rpath);
1.36 xsa 861: }
862:
863: void
864: cvs_client_set_static_directory(char *data)
865: {
866: FILE *fp;
1.50 otto 867: char *dir, fpath[MAXPATHLEN];
1.36 xsa 868:
869: if (cvs_cmdop == CVS_OP_EXPORT)
870: return;
871:
1.62 ray 872: if (data == NULL)
873: fatal("Missing argument for Set-static-directory");
874:
1.36 xsa 875: STRIP_SLASH(data);
876:
877: dir = cvs_remote_input();
878: xfree(dir);
879:
1.58 xsa 880: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
881: data, CVS_PATH_STATICENTRIES);
1.36 xsa 882:
883: if ((fp = fopen(fpath, "w+")) == NULL) {
884: cvs_log(LP_ERRNO, "%s", fpath);
1.50 otto 885: return;
1.36 xsa 886: }
887: (void)fclose(fp);
1.16 xsa 888: }
1.35 xsa 889:
890: void
891: cvs_client_clear_static_directory(char *data)
892: {
1.50 otto 893: char *dir, fpath[MAXPATHLEN];
1.35 xsa 894:
895: if (cvs_cmdop == CVS_OP_EXPORT)
896: return;
897:
1.62 ray 898: if (data == NULL)
899: fatal("Missing argument for Clear-static-directory");
900:
1.35 xsa 901: STRIP_SLASH(data);
902:
903: dir = cvs_remote_input();
904: xfree(dir);
905:
1.58 xsa 906: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
907: data, CVS_PATH_STATICENTRIES);
1.35 xsa 908:
909: (void)cvs_unlink(fpath);
910: }
911:
912: void
913: cvs_client_set_sticky(char *data)
914: {
915: FILE *fp;
1.50 otto 916: char *dir, *tag, tagpath[MAXPATHLEN];
1.35 xsa 917:
1.47 xsa 918: if (cvs_cmdop == CVS_OP_EXPORT)
919: return;
920:
1.62 ray 921: if (data == NULL)
922: fatal("Missing argument for Set-sticky");
923:
1.35 xsa 924: STRIP_SLASH(data);
925:
926: dir = cvs_remote_input();
927: xfree(dir);
928: tag = cvs_remote_input();
929:
1.69 joris 930: client_check_directory(data);
931:
1.58 xsa 932: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.35 xsa 933:
934: if ((fp = fopen(tagpath, "w+")) == NULL) {
935: cvs_log(LP_ERRNO, "%s", tagpath);
936: goto out;
937: }
938:
939: (void)fprintf(fp, "%s\n", tag);
940: (void)fclose(fp);
941: out:
942: xfree(tag);
943: }
944:
945: void
946: cvs_client_clear_sticky(char *data)
947: {
1.50 otto 948: char *dir, tagpath[MAXPATHLEN];
1.47 xsa 949:
950: if (cvs_cmdop == CVS_OP_EXPORT)
951: return;
1.62 ray 952:
953: if (data == NULL)
954: fatal("Missing argument for Clear-sticky");
1.35 xsa 955:
956: STRIP_SLASH(data);
957:
958: dir = cvs_remote_input();
959: xfree(dir);
960:
1.69 joris 961: client_check_directory(data);
962:
1.58 xsa 963: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.69 joris 964: (void)unlink(tagpath);
1.35 xsa 965: }
966:
967:
1.16 xsa 968: /*
969: * cvs_client_initlog()
970: *
971: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
972: * set. In this case, the variable's value is used as a path to which the
973: * appropriate suffix is added (".in" for client input and ".out" for server
974: * output).
975: */
976: static void
977: cvs_client_initlog(void)
978: {
979: u_int i;
980: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
981: char rpath[MAXPATHLEN], *s;
982: struct stat st;
983: time_t now;
984: struct passwd *pwd;
985:
986: /* avoid doing it more than once */
987: if (cvs_client_logon)
988: return;
989:
990: if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
991: return;
992:
993: envdup = xstrdup(env);
994: if ((s = strchr(envdup, '%')) != NULL)
995: *s = '\0';
996:
997: if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
998: fatal("cvs_client_initlog: truncation");
999:
1000: if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
1001: fatal("cvs_client_initlog: truncation");
1002:
1003: xfree(envdup);
1004:
1005: s = buf;
1006: while ((s = strchr(s, '%')) != NULL) {
1007: s++;
1008: switch (*s) {
1009: case 'c':
1010: if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
1011: sizeof(fpath))
1012: fatal("cvs_client_initlog: truncation");
1013: break;
1014: case 'd':
1015: time(&now);
1016: if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
1017: sizeof(fpath))
1018: fatal("cvs_client_initlog: truncation");
1019: break;
1020: case 'p':
1.57 xsa 1021: (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1.16 xsa 1022: break;
1023: case 'u':
1024: if ((pwd = getpwuid(getuid())) != NULL) {
1025: if (strlcpy(fpath, pwd->pw_name,
1026: sizeof(fpath)) >= sizeof(fpath))
1027: fatal("cvs_client_initlog: truncation");
1028: } else {
1029: fpath[0] = '\0';
1030: }
1031: endpwent();
1032: break;
1033: default:
1034: fpath[0] = '\0';
1035: break;
1036: }
1037:
1038: if (fpath[0] != '\0') {
1039: if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
1040: fatal("cvs_client_initlog: truncation");
1041:
1042: if (strlcat(rpath, fpath, sizeof(rpath))
1043: >= sizeof(rpath))
1044: fatal("cvs_client_initlog: truncation");
1045: }
1046: }
1047:
1048: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1049: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.16 xsa 1050:
1051: if (stat(fpath, &st) != -1)
1052: continue;
1053:
1054: if (errno != ENOENT)
1055: fatal("cvs_client_initlog() stat failed '%s'",
1056: strerror(errno));
1057:
1058: break;
1059: }
1060:
1061: if ((cvs_client_inlog_fd = open(fpath,
1.60 xsa 1062: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1063: fatal("cvs_client_initlog: open `%s': %s",
1064: fpath, strerror(errno));
1065: }
1066:
1067: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1068: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.16 xsa 1069:
1070: if (stat(fpath, &st) != -1)
1071: continue;
1072:
1073: if (errno != ENOENT)
1074: fatal("cvs_client_initlog() stat failed '%s'",
1075: strerror(errno));
1076:
1077: break;
1078: }
1079:
1080: if ((cvs_client_outlog_fd = open(fpath,
1.60 xsa 1081: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1082: fatal("cvs_client_initlog: open `%s': %s",
1083: fpath, strerror(errno));
1084: }
1085:
1086: cvs_client_logon = 1;
1.1 jfb 1087: }