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