Annotation of src/usr.bin/cvs/client.c, Revision 1.89
1.89 ! tobias 1: /* $OpenBSD: client.c,v 1.88 2008/01/10 10:08:22 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.89 ! tobias 95: { "rtag", 0, cvs_server_rtag, 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) {
1.88 tobias 479: timebuf[strcspn(timebuf, "\n")] = '\0';
1.9 joris 480: } else {
481: len = strlcpy(timebuf, cf->file_ent->ce_conflict,
482: sizeof(timebuf));
483: if (len >= sizeof(timebuf))
484: fatal("cvs_client_sendfile: truncation");
1.56 joris 485: len = strlcat(timebuf, "+", sizeof(timebuf));
486: if (len >= sizeof(timebuf))
487: fatal("cvs_client_sendfile: truncation");
1.9 joris 488: }
1.1 jfb 489:
1.9 joris 490: sticky[0] = '\0';
491: if (cf->file_ent->ce_tag != NULL) {
1.57 xsa 492: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.9 joris 493: cf->file_ent->ce_tag);
1.1 jfb 494: }
495:
1.55 otto 496: cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
1.9 joris 497: cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
1.53 xsa 498: rev, timebuf, cf->file_ent->ce_opts ?
499: cf->file_ent->ce_opts : "", sticky);
1.9 joris 500: }
501:
1.84 tobias 502: if (cvs_cmdop == CVS_OP_ADD)
503: cf->file_status = FILE_MODIFIED;
504:
1.9 joris 505: switch (cf->file_status) {
506: case FILE_UNKNOWN:
507: if (cf->fd != -1)
508: cvs_client_send_request("Questionable %s",
509: cf->file_name);
510: break;
511: case FILE_ADDED:
512: case FILE_MODIFIED:
513: cvs_client_send_request("Modified %s", cf->file_name);
514: cvs_remote_send_file(cf->file_path);
515: break;
516: case FILE_UPTODATE:
517: cvs_client_send_request("Unchanged %s", cf->file_name);
518: break;
1.1 jfb 519: }
1.9 joris 520: }
1.1 jfb 521:
1.9 joris 522: void
523: cvs_client_send_files(char **argv, int argc)
524: {
525: int i;
1.1 jfb 526:
1.9 joris 527: for (i = 0; i < argc; i++)
528: cvs_client_send_request("Argument %s", argv[i]);
529: }
1.1 jfb 530:
1.9 joris 531: void
532: cvs_client_ok(char *data)
533: {
534: end_of_response = 1;
1.37 joris 535: server_response = SERVER_OK;
1.1 jfb 536: }
537:
1.9 joris 538: void
539: cvs_client_error(char *data)
540: {
541: end_of_response = 1;
1.37 joris 542: server_response = SERVER_ERROR;
1.9 joris 543: }
1.1 jfb 544:
1.9 joris 545: void
546: cvs_client_validreq(char *data)
1.1 jfb 547: {
1.9 joris 548: int i;
549: char *sp, *ep;
550: struct cvs_req *req;
551:
1.62 ray 552: if ((sp = data) == NULL)
553: fatal("Missing argument for Valid-requests");
554:
1.9 joris 555: do {
556: if ((ep = strchr(sp, ' ')) != NULL)
557: *ep = '\0';
558:
559: req = cvs_remote_get_request_info(sp);
560: if (req != NULL)
561: req->supported = 1;
562:
563: if (ep != NULL)
564: sp = ep + 1;
565: } while (ep != NULL);
566:
567: for (i = 0; cvs_requests[i].supported != -1; i++) {
568: req = &cvs_requests[i];
569: if ((req->flags & REQ_NEEDED) &&
570: req->supported != 1) {
571: fatal("server does not support required '%s'",
572: req->name);
573: }
1.1 jfb 574: }
1.9 joris 575: }
1.1 jfb 576:
1.9 joris 577: void
578: cvs_client_e(char *data)
579: {
1.62 ray 580: if (data == NULL)
581: fatal("Missing argument for E");
582:
1.80 tobias 583: fprintf(stderr, "%s\n", data);
1.9 joris 584: }
1.1 jfb 585:
1.9 joris 586: void
587: cvs_client_m(char *data)
588: {
1.62 ray 589: if (data == NULL)
590: fatal("Missing argument for M");
591:
1.80 tobias 592: puts(data);
1.9 joris 593: }
1.1 jfb 594:
1.9 joris 595: void
596: cvs_client_checkedin(char *data)
597: {
598: CVSENTRIES *entlist;
599: struct cvs_ent *ent, *newent;
1.84 tobias 600: size_t len;
1.67 xsa 601: char *dir, *e, entry[CVS_ENT_MAXLINELEN], rev[CVS_REV_BUFSZ];
602: char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
1.1 jfb 603:
1.62 ray 604: if (data == NULL)
605: fatal("Missing argument for Checked-in");
606:
1.9 joris 607: dir = cvs_remote_input();
1.54 otto 608: e = cvs_remote_input();
1.9 joris 609: xfree(dir);
1.1 jfb 610:
1.9 joris 611: entlist = cvs_ent_open(data);
1.54 otto 612: newent = cvs_ent_parse(e);
1.9 joris 613: ent = cvs_ent_get(entlist, newent->ce_name);
1.54 otto 614: xfree(e);
1.1 jfb 615:
1.9 joris 616: rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
1.1 jfb 617:
1.9 joris 618: sticky[0] = '\0';
1.84 tobias 619: if (ent == NULL) {
620: len = strlcpy(rev, "0", sizeof(rev));
621: if (len >= sizeof(rev))
622: fatal("cvs_client_sendfile: truncation");
623:
624: len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
625: if (len >= sizeof(timebuf))
626: fatal("cvs_client_sendfile: truncation");
627:
628: len = strlcat(timebuf, newent->ce_name, sizeof(timebuf));
629: if (len >= sizeof(timebuf))
630: fatal("cvs_client_sendfile: truncation");
631: } else {
632: ctime_r(&ent->ce_mtime, timebuf);
633: timebuf[strcspn(timebuf, "\n")] = '\0';
634:
635: if (newent->ce_tag != NULL)
636: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
637: ent->ce_tag);
638: newent->ce_opts = ent->ce_opts;
639:
640: cvs_ent_free(ent);
641: }
1.1 jfb 642:
1.57 xsa 643: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s%s/%s/%s/%s",
1.44 joris 644: newent->ce_name, (newent->ce_status == CVS_ENT_REMOVED) ? "-" : "",
1.84 tobias 645: rev, timebuf, newent->ce_opts ? newent->ce_opts : "", sticky);
1.9 joris 646:
647: cvs_ent_free(newent);
648: cvs_ent_add(entlist, entry);
649: cvs_ent_close(entlist, ENT_SYNC);
1.1 jfb 650: }
651:
1.9 joris 652: void
653: cvs_client_updated(char *data)
654: {
1.57 xsa 655: int fd;
1.9 joris 656: time_t now;
1.51 otto 657: mode_t fmode, mask;
1.9 joris 658: size_t flen;
659: CVSENTRIES *ent;
660: struct cvs_ent *e;
661: const char *errstr;
662: struct timeval tv[2];
1.67 xsa 663: char repo[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
664: char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
665: char *en, *mode, *len, *fpath, *rpath, *wdir;
1.69 joris 666: char sticky[CVS_ENT_MAXLINELEN];
1.9 joris 667:
1.62 ray 668: if (data == NULL)
669: fatal("Missing argument for Updated");
670:
1.10 joris 671: client_check_directory(data);
1.9 joris 672:
673: rpath = cvs_remote_input();
1.54 otto 674: en = cvs_remote_input();
1.9 joris 675: mode = cvs_remote_input();
676: len = cvs_remote_input();
677:
678: cvs_get_repository_path(".", repo, MAXPATHLEN);
1.48 joris 679:
680: STRIP_SLASH(repo);
1.9 joris 681:
682: if (strlen(repo) + 1 > strlen(rpath))
683: fatal("received a repository path that is too short");
684:
685: fpath = rpath + strlen(repo) + 1;
686: if ((wdir = dirname(fpath)) == NULL)
687: fatal("cvs_client_updated: dirname: %s", strerror(errno));
688:
689: flen = strtonum(len, 0, INT_MAX, &errstr);
690: if (errstr != NULL)
691: fatal("cvs_client_updated: %s: %s", len, errstr);
692: xfree(len);
693:
694: cvs_strtomode(mode, &fmode);
695: xfree(mode);
1.51 otto 696: mask = umask(0);
697: umask(mask);
698: fmode &= ~mask;
1.9 joris 699:
700: time(&now);
1.52 joris 701: asctime_r(gmtime(&now), timebuf);
1.88 tobias 702: timebuf[strcspn(timebuf, "\n")] = '\0';
1.9 joris 703:
1.54 otto 704: e = cvs_ent_parse(en);
705: xfree(en);
1.69 joris 706:
707: sticky[0] = '\0';
708: if (e->ce_tag != NULL)
709: (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
710:
1.9 joris 711: rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
1.69 joris 712: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
713: e->ce_name, revbuf, timebuf,
714: e->ce_opts ? e->ce_opts : "", sticky);
1.9 joris 715:
716: cvs_ent_free(e);
717: ent = cvs_ent_open(wdir);
718: cvs_ent_add(ent, entry);
719: cvs_ent_close(ent, ENT_SYNC);
720:
721: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
722: fatal("cvs_client_updated: open: %s: %s",
723: fpath, strerror(errno));
1.1 jfb 724:
1.46 joris 725: cvs_remote_receive_file(fd, flen);
1.1 jfb 726:
1.9 joris 727: tv[0].tv_sec = now;
728: tv[0].tv_usec = 0;
729: tv[1] = tv[0];
1.1 jfb 730:
1.9 joris 731: if (futimes(fd, tv) == -1)
732: fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1 jfb 733:
1.9 joris 734: if (fchmod(fd, fmode) == -1)
735: fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1 jfb 736:
1.9 joris 737: (void)close(fd);
1.1 jfb 738:
1.9 joris 739: xfree(rpath);
1.1 jfb 740: }
741:
1.9 joris 742: void
743: cvs_client_merged(char *data)
744: {
1.56 joris 745: int fd;
746: time_t now;
1.83 tobias 747: mode_t fmode, mask;
1.56 joris 748: size_t flen;
749: CVSENTRIES *ent;
750: const char *errstr;
751: struct timeval tv[2];
1.67 xsa 752: char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
1.56 joris 753: char *len, *fpath, *wdir;
754:
1.62 ray 755: if (data == NULL)
756: fatal("Missing argument for Merged");
757:
1.56 joris 758: client_check_directory(data);
759:
760: rpath = cvs_remote_input();
761: entry = cvs_remote_input();
762: mode = cvs_remote_input();
763: len = cvs_remote_input();
764:
765: repo = xmalloc(MAXPATHLEN);
766: cvs_get_repository_path(".", repo, MAXPATHLEN);
767:
768: STRIP_SLASH(repo);
769:
770: if (strlen(repo) + 1 > strlen(rpath))
771: fatal("received a repository path that is too short");
772:
773: fpath = rpath + strlen(repo) + 1;
774: if ((wdir = dirname(fpath)) == NULL)
775: fatal("cvs_client_merged: dirname: %s", strerror(errno));
776: xfree(repo);
777:
778: flen = strtonum(len, 0, INT_MAX, &errstr);
779: if (errstr != NULL)
780: fatal("cvs_client_merged: %s: %s", len, errstr);
781: xfree(len);
782:
783: cvs_strtomode(mode, &fmode);
784: xfree(mode);
1.83 tobias 785: mask = umask(0);
786: umask(mask);
787: fmode &= ~mask;
1.56 joris 788:
789: time(&now);
790: asctime_r(gmtime(&now), timebuf);
1.88 tobias 791: timebuf[strcspn(timebuf, "\n")] = '\0';
1.56 joris 792:
793: ent = cvs_ent_open(wdir);
794: cvs_ent_add(ent, entry);
795: cvs_ent_close(ent, ENT_SYNC);
1.86 tobias 796: xfree(entry);
1.56 joris 797:
798: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
799: fatal("cvs_client_merged: open: %s: %s",
800: fpath, strerror(errno));
801:
802: cvs_remote_receive_file(fd, flen);
803:
804: tv[0].tv_sec = now;
805: tv[0].tv_usec = 0;
806: tv[1] = tv[0];
807:
808: if (futimes(fd, tv) == -1)
809: fatal("cvs_client_merged: futimes: %s", strerror(errno));
810:
811: if (fchmod(fd, fmode) == -1)
812: fatal("cvs_client_merged: fchmod: %s", strerror(errno));
813:
814: (void)close(fd);
815:
816: xfree(rpath);
1.9 joris 817: }
1.1 jfb 818:
1.9 joris 819: void
820: cvs_client_removed(char *data)
821: {
1.65 joris 822: CVSENTRIES *entlist;
823: char *rpath, *filename, fpath[MAXPATHLEN];
824:
825: rpath = cvs_remote_input();
826: if ((filename = strrchr(rpath, '/')) == NULL)
827: fatal("bad rpath in cvs_client_removed: %s", rpath);
828: filename++;
829:
830: entlist = cvs_ent_open(data);
831: cvs_ent_remove(entlist, filename);
832: cvs_ent_close(entlist, ENT_SYNC);
833:
834: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
835: (void)unlink(fpath);
1.13 joris 836:
1.65 joris 837: xfree(rpath);
1.9 joris 838: }
1.1 jfb 839:
1.9 joris 840: void
841: cvs_client_remove_entry(char *data)
1.1 jfb 842: {
1.38 xsa 843: CVSENTRIES *entlist;
1.44 joris 844: char *filename, *rpath;
1.41 joris 845:
1.62 ray 846: if (data == NULL)
847: fatal("Missing argument for Remove-entry");
848:
1.41 joris 849: rpath = cvs_remote_input();
850: if ((filename = strrchr(rpath, '/')) == NULL)
851: fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
1.61 ray 852: filename++;
1.1 jfb 853:
1.38 xsa 854: entlist = cvs_ent_open(data);
1.44 joris 855: cvs_ent_remove(entlist, filename);
1.38 xsa 856: cvs_ent_close(entlist, ENT_SYNC);
857:
1.44 joris 858: xfree(rpath);
1.36 xsa 859: }
860:
861: void
862: cvs_client_set_static_directory(char *data)
863: {
864: FILE *fp;
1.50 otto 865: char *dir, fpath[MAXPATHLEN];
1.36 xsa 866:
867: if (cvs_cmdop == CVS_OP_EXPORT)
868: return;
869:
1.62 ray 870: if (data == NULL)
871: fatal("Missing argument for Set-static-directory");
872:
1.36 xsa 873: STRIP_SLASH(data);
874:
875: dir = cvs_remote_input();
876: xfree(dir);
877:
1.58 xsa 878: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
879: data, CVS_PATH_STATICENTRIES);
1.36 xsa 880:
881: if ((fp = fopen(fpath, "w+")) == NULL) {
882: cvs_log(LP_ERRNO, "%s", fpath);
1.50 otto 883: return;
1.36 xsa 884: }
885: (void)fclose(fp);
1.16 xsa 886: }
1.35 xsa 887:
888: void
889: cvs_client_clear_static_directory(char *data)
890: {
1.50 otto 891: char *dir, fpath[MAXPATHLEN];
1.35 xsa 892:
893: if (cvs_cmdop == CVS_OP_EXPORT)
894: return;
895:
1.62 ray 896: if (data == NULL)
897: fatal("Missing argument for Clear-static-directory");
898:
1.35 xsa 899: STRIP_SLASH(data);
900:
901: dir = cvs_remote_input();
902: xfree(dir);
903:
1.58 xsa 904: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
905: data, CVS_PATH_STATICENTRIES);
1.35 xsa 906:
907: (void)cvs_unlink(fpath);
908: }
909:
910: void
911: cvs_client_set_sticky(char *data)
912: {
913: FILE *fp;
1.50 otto 914: char *dir, *tag, tagpath[MAXPATHLEN];
1.35 xsa 915:
1.47 xsa 916: if (cvs_cmdop == CVS_OP_EXPORT)
917: return;
918:
1.62 ray 919: if (data == NULL)
920: fatal("Missing argument for Set-sticky");
921:
1.35 xsa 922: STRIP_SLASH(data);
923:
924: dir = cvs_remote_input();
925: xfree(dir);
926: tag = cvs_remote_input();
927:
1.69 joris 928: client_check_directory(data);
929:
1.58 xsa 930: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.35 xsa 931:
932: if ((fp = fopen(tagpath, "w+")) == NULL) {
933: cvs_log(LP_ERRNO, "%s", tagpath);
934: goto out;
935: }
936:
937: (void)fprintf(fp, "%s\n", tag);
938: (void)fclose(fp);
939: out:
940: xfree(tag);
941: }
942:
943: void
944: cvs_client_clear_sticky(char *data)
945: {
1.50 otto 946: char *dir, tagpath[MAXPATHLEN];
1.47 xsa 947:
948: if (cvs_cmdop == CVS_OP_EXPORT)
949: return;
1.62 ray 950:
951: if (data == NULL)
952: fatal("Missing argument for Clear-sticky");
1.35 xsa 953:
954: STRIP_SLASH(data);
955:
956: dir = cvs_remote_input();
957: xfree(dir);
958:
1.69 joris 959: client_check_directory(data);
960:
1.58 xsa 961: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.69 joris 962: (void)unlink(tagpath);
1.35 xsa 963: }
964:
965:
1.16 xsa 966: /*
967: * cvs_client_initlog()
968: *
969: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
970: * set. In this case, the variable's value is used as a path to which the
971: * appropriate suffix is added (".in" for client input and ".out" for server
972: * output).
973: */
974: static void
975: cvs_client_initlog(void)
976: {
977: u_int i;
978: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
979: char rpath[MAXPATHLEN], *s;
980: struct stat st;
981: time_t now;
982: struct passwd *pwd;
983:
984: /* avoid doing it more than once */
985: if (cvs_client_logon)
986: return;
987:
988: if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
989: return;
990:
991: envdup = xstrdup(env);
992: if ((s = strchr(envdup, '%')) != NULL)
993: *s = '\0';
994:
995: if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
996: fatal("cvs_client_initlog: truncation");
997:
998: if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
999: fatal("cvs_client_initlog: truncation");
1000:
1001: xfree(envdup);
1002:
1003: s = buf;
1004: while ((s = strchr(s, '%')) != NULL) {
1005: s++;
1006: switch (*s) {
1007: case 'c':
1.87 tobias 1008: if (strlcpy(fpath, cmdp->cmd_name, sizeof(fpath)) >=
1.16 xsa 1009: sizeof(fpath))
1010: fatal("cvs_client_initlog: truncation");
1011: break;
1012: case 'd':
1013: time(&now);
1014: if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
1015: sizeof(fpath))
1016: fatal("cvs_client_initlog: truncation");
1017: break;
1018: case 'p':
1.57 xsa 1019: (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1.16 xsa 1020: break;
1021: case 'u':
1022: if ((pwd = getpwuid(getuid())) != NULL) {
1023: if (strlcpy(fpath, pwd->pw_name,
1024: sizeof(fpath)) >= sizeof(fpath))
1025: fatal("cvs_client_initlog: truncation");
1026: } else {
1027: fpath[0] = '\0';
1028: }
1029: endpwent();
1030: break;
1031: default:
1032: fpath[0] = '\0';
1033: break;
1034: }
1035:
1036: if (fpath[0] != '\0') {
1037: if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
1038: fatal("cvs_client_initlog: truncation");
1039:
1040: if (strlcat(rpath, fpath, sizeof(rpath))
1041: >= sizeof(rpath))
1042: fatal("cvs_client_initlog: truncation");
1043: }
1044: }
1045:
1046: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1047: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.16 xsa 1048:
1049: if (stat(fpath, &st) != -1)
1050: continue;
1051:
1052: if (errno != ENOENT)
1053: fatal("cvs_client_initlog() stat failed '%s'",
1054: strerror(errno));
1055:
1056: break;
1057: }
1058:
1059: if ((cvs_client_inlog_fd = open(fpath,
1.60 xsa 1060: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1061: fatal("cvs_client_initlog: open `%s': %s",
1062: fpath, strerror(errno));
1063: }
1064:
1065: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1066: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.16 xsa 1067:
1068: if (stat(fpath, &st) != -1)
1069: continue;
1070:
1071: if (errno != ENOENT)
1072: fatal("cvs_client_initlog() stat failed '%s'",
1073: strerror(errno));
1074:
1075: break;
1076: }
1077:
1078: if ((cvs_client_outlog_fd = open(fpath,
1.60 xsa 1079: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1080: fatal("cvs_client_initlog: open `%s': %s",
1081: fpath, strerror(errno));
1082: }
1083:
1084: cvs_client_logon = 1;
1.1 jfb 1085: }