Annotation of src/usr.bin/cvs/client.c, Revision 1.69
1.69 ! joris 1: /* $OpenBSD: client.c,v 1.68 2007/06/29 12:42:05 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:
1.69 ! joris 170: cvs_mkpath(data, NULL);
1.10 joris 171:
172: if ((base = basename(data)) == NULL)
173: fatal("client_check_directory: overflow");
174:
175: if ((parent = dirname(data)) == NULL)
176: fatal("client_check_directory: overflow");
1.11 joris 177:
178: if (!strcmp(parent, "."))
179: return;
1.10 joris 180:
1.57 xsa 181: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////", base);
1.10 joris 182:
183: entlist = cvs_ent_open(parent);
184: cvs_ent_add(entlist, entry);
185: cvs_ent_close(entlist, ENT_SYNC);
186: }
187:
1.9 joris 188: void
189: cvs_client_connect_to_server(void)
190: {
1.26 xsa 191: struct cvs_var *vp;
1.9 joris 192: char *cmd, *argv[9], *resp;
193: int ifd[2], ofd[2], argc;
1.45 joris 194:
195: if (cvs_server_active == 1)
196: fatal("cvs_client_connect: I was already connected to server");
1.1 jfb 197:
1.9 joris 198: switch (current_cvsroot->cr_method) {
199: case CVS_METHOD_PSERVER:
200: case CVS_METHOD_KSERVER:
201: case CVS_METHOD_GSERVER:
202: case CVS_METHOD_FORK:
203: case CVS_METHOD_EXT:
1.14 joris 204: fatal("the specified connection method is not supported");
1.9 joris 205: default:
206: break;
207: }
208:
209: if (pipe(ifd) == -1)
210: fatal("cvs_client_connect: %s", strerror(errno));
211: if (pipe(ofd) == -1)
212: fatal("cvs_client_connect: %s", strerror(errno));
213:
214: switch (fork()) {
215: case -1:
216: fatal("cvs_client_connect: fork failed: %s", strerror(errno));
217: case 0:
218: if (dup2(ifd[0], STDIN_FILENO) == -1)
219: fatal("cvs_client_connect: %s", strerror(errno));
220: if (dup2(ofd[1], STDOUT_FILENO) == -1)
221: fatal("cvs_client_connect: %s", strerror(errno));
1.1 jfb 222:
1.9 joris 223: close(ifd[1]);
224: close(ofd[0]);
1.1 jfb 225:
1.9 joris 226: if ((cmd = getenv("CVS_SERVER")) == NULL)
227: cmd = CVS_SERVER_DEFAULT;
1.1 jfb 228:
229: argc = 0;
230: argv[argc++] = cvs_rsh;
231:
1.9 joris 232: if (current_cvsroot->cr_user != NULL) {
1.1 jfb 233: argv[argc++] = "-l";
1.9 joris 234: argv[argc++] = current_cvsroot->cr_user;
1.1 jfb 235: }
236:
1.9 joris 237: argv[argc++] = current_cvsroot->cr_host;
238: argv[argc++] = cmd;
1.1 jfb 239: argv[argc++] = "server";
240: argv[argc] = NULL;
241:
1.9 joris 242: cvs_log(LP_TRACE, "connecting to server %s",
243: current_cvsroot->cr_host);
244:
1.1 jfb 245: execvp(argv[0], argv);
1.9 joris 246: fatal("cvs_client_connect: failed to execute cvs server");
247: default:
248: break;
1.1 jfb 249: }
250:
1.9 joris 251: close(ifd[0]);
252: close(ofd[1]);
253:
254: if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
255: fatal("cvs_client_connect: %s", strerror(errno));
256: if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
257: fatal("cvs_client_connect: %s", strerror(errno));
1.1 jfb 258:
1.9 joris 259: setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
260: setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
1.1 jfb 261:
1.16 xsa 262: cvs_client_initlog();
263:
1.18 xsa 264: if (cvs_cmdop != CVS_OP_INIT)
265: cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
1.1 jfb 266:
1.9 joris 267: resp = client_get_supported_responses();
268: cvs_client_send_request("Valid-responses %s", resp);
269: xfree(resp);
1.1 jfb 270:
1.9 joris 271: cvs_client_send_request("valid-requests");
272: cvs_client_get_responses();
1.1 jfb 273:
1.28 xsa 274: cvs_client_send_request("UseUnchanged");
275:
1.20 xsa 276: if (cvs_nolog == 1)
277: cvs_client_send_request("Global_option -l");
278:
1.19 xsa 279: if (cvs_noexec == 1)
280: cvs_client_send_request("Global_option -n");
281:
282: if (verbosity == 0)
283: cvs_client_send_request("Global_option -Q");
1.20 xsa 284:
285: /* Be quiet. This is the default in OpenCVS. */
286: cvs_client_send_request("Global_option -q");
1.19 xsa 287:
288: if (cvs_readonly == 1)
289: cvs_client_send_request("Global_option -r");
290:
291: if (cvs_trace == 1)
1.9 joris 292: cvs_client_send_request("Global_option -t");
1.1 jfb 293:
1.9 joris 294: if (verbosity == 2)
295: cvs_client_send_request("Global_option -V");
1.26 xsa 296:
297: /* XXX: If 'Set' is supported? */
298: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
299: cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1 jfb 300: }
301:
1.9 joris 302: void
303: cvs_client_send_request(char *fmt, ...)
304: {
305: va_list ap;
306: char *data, *s;
307: struct cvs_req *req;
308:
309: va_start(ap, fmt);
310: vasprintf(&data, fmt, ap);
311: va_end(ap);
1.1 jfb 312:
1.9 joris 313: if ((s = strchr(data, ' ')) != NULL)
314: *s = '\0';
1.1 jfb 315:
1.9 joris 316: req = cvs_remote_get_request_info(data);
317: if (req == NULL)
318: fatal("'%s' is an unknown request", data);
1.1 jfb 319:
1.9 joris 320: if (req->supported != 1)
321: fatal("remote cvs server does not support '%s'", data);
1.1 jfb 322:
1.9 joris 323: if (s != NULL)
324: *s = ' ';
1.1 jfb 325:
1.12 joris 326: cvs_log(LP_TRACE, "%s", data);
1.16 xsa 327:
328: if (cvs_client_inlog_fd != -1) {
329: BUF *bp;
330:
331: bp = cvs_buf_alloc(strlen(data), BUF_AUTOEXT);
332:
333: if (cvs_buf_append(bp, data, strlen(data)) < 0)
334: fatal("cvs_client_send_request: cvs_buf_append");
335:
336: cvs_buf_putc(bp, '\n');
337:
338: if (cvs_buf_write_fd(bp, cvs_client_inlog_fd) < 0)
339: fatal("cvs_client_send_request: cvs_buf_write_fd");
340:
341: cvs_buf_free(bp);
342: }
343:
1.9 joris 344: cvs_remote_output(data);
345: xfree(data);
346: }
1.1 jfb 347:
1.9 joris 348: void
349: cvs_client_read_response(void)
1.1 jfb 350: {
1.9 joris 351: char *cmd, *data;
352: struct cvs_resp *resp;
1.1 jfb 353:
1.9 joris 354: cmd = cvs_remote_input();
355: if ((data = strchr(cmd, ' ')) != NULL)
356: (*data++) = '\0';
1.1 jfb 357:
1.9 joris 358: resp = cvs_remote_get_response_info(cmd);
359: if (resp == NULL)
360: fatal("response '%s' is not supported by our client", cmd);
1.1 jfb 361:
1.9 joris 362: if (resp->hdlr == NULL)
363: fatal("opencvs client does not support '%s'", cmd);
1.16 xsa 364:
1.9 joris 365: (*resp->hdlr)(data);
1.16 xsa 366:
1.9 joris 367: xfree(cmd);
1.1 jfb 368: }
369:
1.9 joris 370: void
371: cvs_client_get_responses(void)
372: {
373: while (end_of_response != 1)
374: cvs_client_read_response();
1.1 jfb 375:
1.9 joris 376: end_of_response = 0;
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.69 ! joris 637: char sticky[CVS_ENT_MAXLINELEN];
1.9 joris 638:
1.62 ray 639: if (data == NULL)
640: fatal("Missing argument for Updated");
641:
1.10 joris 642: client_check_directory(data);
1.9 joris 643:
644: rpath = cvs_remote_input();
1.54 otto 645: en = cvs_remote_input();
1.9 joris 646: mode = cvs_remote_input();
647: len = cvs_remote_input();
648:
649: cvs_get_repository_path(".", repo, MAXPATHLEN);
1.48 joris 650:
651: STRIP_SLASH(repo);
1.9 joris 652:
653: if (strlen(repo) + 1 > strlen(rpath))
654: fatal("received a repository path that is too short");
655:
656: fpath = rpath + strlen(repo) + 1;
657: if ((wdir = dirname(fpath)) == NULL)
658: fatal("cvs_client_updated: dirname: %s", strerror(errno));
659:
660: flen = strtonum(len, 0, INT_MAX, &errstr);
661: if (errstr != NULL)
662: fatal("cvs_client_updated: %s: %s", len, errstr);
663: xfree(len);
664:
665: cvs_strtomode(mode, &fmode);
666: xfree(mode);
1.51 otto 667: mask = umask(0);
668: umask(mask);
669: fmode &= ~mask;
1.9 joris 670:
671: time(&now);
1.52 joris 672: asctime_r(gmtime(&now), timebuf);
1.9 joris 673: if (timebuf[strlen(timebuf) - 1] == '\n')
674: timebuf[strlen(timebuf) - 1] = '\0';
675:
1.54 otto 676: e = cvs_ent_parse(en);
677: xfree(en);
1.69 ! joris 678:
! 679: sticky[0] = '\0';
! 680: if (e->ce_tag != NULL)
! 681: (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
! 682:
1.9 joris 683: rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
1.69 ! joris 684: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
! 685: e->ce_name, revbuf, timebuf,
! 686: e->ce_opts ? e->ce_opts : "", sticky);
1.9 joris 687:
688: cvs_ent_free(e);
689: ent = cvs_ent_open(wdir);
690: cvs_ent_add(ent, entry);
691: cvs_ent_close(ent, ENT_SYNC);
692:
693: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
694: fatal("cvs_client_updated: open: %s: %s",
695: fpath, strerror(errno));
1.1 jfb 696:
1.46 joris 697: cvs_remote_receive_file(fd, flen);
1.1 jfb 698:
1.9 joris 699: tv[0].tv_sec = now;
700: tv[0].tv_usec = 0;
701: tv[1] = tv[0];
1.1 jfb 702:
1.9 joris 703: if (futimes(fd, tv) == -1)
704: fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1 jfb 705:
1.9 joris 706: if (fchmod(fd, fmode) == -1)
707: fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1 jfb 708:
1.9 joris 709: (void)close(fd);
1.1 jfb 710:
1.9 joris 711: xfree(rpath);
1.1 jfb 712: }
713:
1.9 joris 714: void
715: cvs_client_merged(char *data)
716: {
1.56 joris 717: int fd;
718: time_t now;
719: mode_t fmode;
720: size_t flen;
721: CVSENTRIES *ent;
722: const char *errstr;
723: struct timeval tv[2];
1.67 xsa 724: char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
1.56 joris 725: char *len, *fpath, *wdir;
726:
1.62 ray 727: if (data == NULL)
728: fatal("Missing argument for Merged");
729:
1.56 joris 730: client_check_directory(data);
731:
732: rpath = cvs_remote_input();
733: entry = cvs_remote_input();
734: mode = cvs_remote_input();
735: len = cvs_remote_input();
736:
737: repo = xmalloc(MAXPATHLEN);
738: cvs_get_repository_path(".", repo, MAXPATHLEN);
739:
740: STRIP_SLASH(repo);
741:
742: if (strlen(repo) + 1 > strlen(rpath))
743: fatal("received a repository path that is too short");
744:
745: fpath = rpath + strlen(repo) + 1;
746: if ((wdir = dirname(fpath)) == NULL)
747: fatal("cvs_client_merged: dirname: %s", strerror(errno));
748: xfree(repo);
749:
750: flen = strtonum(len, 0, INT_MAX, &errstr);
751: if (errstr != NULL)
752: fatal("cvs_client_merged: %s: %s", len, errstr);
753: xfree(len);
754:
755: cvs_strtomode(mode, &fmode);
756: xfree(mode);
757:
758: time(&now);
759: asctime_r(gmtime(&now), timebuf);
760: if (timebuf[strlen(timebuf) - 1] == '\n')
761: timebuf[strlen(timebuf) - 1] = '\0';
762:
763: ent = cvs_ent_open(wdir);
764: cvs_ent_add(ent, entry);
765: cvs_ent_close(ent, ENT_SYNC);
766:
767: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
768: fatal("cvs_client_merged: open: %s: %s",
769: fpath, strerror(errno));
770:
771: cvs_remote_receive_file(fd, flen);
772:
773: tv[0].tv_sec = now;
774: tv[0].tv_usec = 0;
775: tv[1] = tv[0];
776:
777: if (futimes(fd, tv) == -1)
778: fatal("cvs_client_merged: futimes: %s", strerror(errno));
779:
780: if (fchmod(fd, fmode) == -1)
781: fatal("cvs_client_merged: fchmod: %s", strerror(errno));
782:
783: (void)close(fd);
784:
785: xfree(rpath);
1.9 joris 786: }
1.1 jfb 787:
1.9 joris 788: void
789: cvs_client_removed(char *data)
790: {
1.65 joris 791: CVSENTRIES *entlist;
792: char *rpath, *filename, fpath[MAXPATHLEN];
793:
794: rpath = cvs_remote_input();
795: if ((filename = strrchr(rpath, '/')) == NULL)
796: fatal("bad rpath in cvs_client_removed: %s", rpath);
797: filename++;
798:
799: entlist = cvs_ent_open(data);
800: cvs_ent_remove(entlist, filename);
801: cvs_ent_close(entlist, ENT_SYNC);
802:
803: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
804: (void)unlink(fpath);
1.13 joris 805:
1.65 joris 806: xfree(rpath);
1.9 joris 807: }
1.1 jfb 808:
1.9 joris 809: void
810: cvs_client_remove_entry(char *data)
1.1 jfb 811: {
1.38 xsa 812: CVSENTRIES *entlist;
1.44 joris 813: char *filename, *rpath;
1.41 joris 814:
1.62 ray 815: if (data == NULL)
816: fatal("Missing argument for Remove-entry");
817:
1.41 joris 818: rpath = cvs_remote_input();
819: if ((filename = strrchr(rpath, '/')) == NULL)
820: fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
1.61 ray 821: filename++;
1.1 jfb 822:
1.38 xsa 823: entlist = cvs_ent_open(data);
1.44 joris 824: cvs_ent_remove(entlist, filename);
1.38 xsa 825: cvs_ent_close(entlist, ENT_SYNC);
826:
1.44 joris 827: xfree(rpath);
1.36 xsa 828: }
829:
830: void
831: cvs_client_set_static_directory(char *data)
832: {
833: FILE *fp;
1.50 otto 834: char *dir, fpath[MAXPATHLEN];
1.36 xsa 835:
836: if (cvs_cmdop == CVS_OP_EXPORT)
837: return;
838:
1.62 ray 839: if (data == NULL)
840: fatal("Missing argument for Set-static-directory");
841:
1.36 xsa 842: STRIP_SLASH(data);
843:
844: dir = cvs_remote_input();
845: xfree(dir);
846:
1.58 xsa 847: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
848: data, CVS_PATH_STATICENTRIES);
1.36 xsa 849:
850: if ((fp = fopen(fpath, "w+")) == NULL) {
851: cvs_log(LP_ERRNO, "%s", fpath);
1.50 otto 852: return;
1.36 xsa 853: }
854: (void)fclose(fp);
1.16 xsa 855: }
1.35 xsa 856:
857: void
858: cvs_client_clear_static_directory(char *data)
859: {
1.50 otto 860: char *dir, fpath[MAXPATHLEN];
1.35 xsa 861:
862: if (cvs_cmdop == CVS_OP_EXPORT)
863: return;
864:
1.62 ray 865: if (data == NULL)
866: fatal("Missing argument for Clear-static-directory");
867:
1.35 xsa 868: STRIP_SLASH(data);
869:
870: dir = cvs_remote_input();
871: xfree(dir);
872:
1.58 xsa 873: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
874: data, CVS_PATH_STATICENTRIES);
1.35 xsa 875:
876: (void)cvs_unlink(fpath);
877: }
878:
879: void
880: cvs_client_set_sticky(char *data)
881: {
882: FILE *fp;
1.50 otto 883: char *dir, *tag, tagpath[MAXPATHLEN];
1.35 xsa 884:
1.47 xsa 885: if (cvs_cmdop == CVS_OP_EXPORT)
886: return;
887:
1.62 ray 888: if (data == NULL)
889: fatal("Missing argument for Set-sticky");
890:
1.35 xsa 891: STRIP_SLASH(data);
892:
893: dir = cvs_remote_input();
894: xfree(dir);
895: tag = cvs_remote_input();
896:
1.69 ! joris 897: client_check_directory(data);
! 898:
1.58 xsa 899: (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.35 xsa 900:
901: if ((fp = fopen(tagpath, "w+")) == NULL) {
902: cvs_log(LP_ERRNO, "%s", tagpath);
903: goto out;
904: }
905:
906: (void)fprintf(fp, "%s\n", tag);
907: (void)fclose(fp);
908: out:
909: xfree(tag);
910: }
911:
912: void
913: cvs_client_clear_sticky(char *data)
914: {
1.50 otto 915: char *dir, tagpath[MAXPATHLEN];
1.47 xsa 916:
917: if (cvs_cmdop == CVS_OP_EXPORT)
918: return;
1.62 ray 919:
920: if (data == NULL)
921: fatal("Missing argument for Clear-sticky");
1.35 xsa 922:
923: STRIP_SLASH(data);
924:
925: dir = cvs_remote_input();
926: xfree(dir);
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.69 ! joris 931: (void)unlink(tagpath);
1.35 xsa 932: }
933:
934:
1.16 xsa 935: /*
936: * cvs_client_initlog()
937: *
938: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
939: * set. In this case, the variable's value is used as a path to which the
940: * appropriate suffix is added (".in" for client input and ".out" for server
941: * output).
942: */
943: static void
944: cvs_client_initlog(void)
945: {
946: u_int i;
947: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
948: char rpath[MAXPATHLEN], *s;
949: struct stat st;
950: time_t now;
951: struct passwd *pwd;
952:
953: /* avoid doing it more than once */
954: if (cvs_client_logon)
955: return;
956:
957: if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
958: return;
959:
960: envdup = xstrdup(env);
961: if ((s = strchr(envdup, '%')) != NULL)
962: *s = '\0';
963:
964: if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
965: fatal("cvs_client_initlog: truncation");
966:
967: if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
968: fatal("cvs_client_initlog: truncation");
969:
970: xfree(envdup);
971:
972: s = buf;
973: while ((s = strchr(s, '%')) != NULL) {
974: s++;
975: switch (*s) {
976: case 'c':
977: if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
978: sizeof(fpath))
979: fatal("cvs_client_initlog: truncation");
980: break;
981: case 'd':
982: time(&now);
983: if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
984: sizeof(fpath))
985: fatal("cvs_client_initlog: truncation");
986: break;
987: case 'p':
1.57 xsa 988: (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1.16 xsa 989: break;
990: case 'u':
991: if ((pwd = getpwuid(getuid())) != NULL) {
992: if (strlcpy(fpath, pwd->pw_name,
993: sizeof(fpath)) >= sizeof(fpath))
994: fatal("cvs_client_initlog: truncation");
995: } else {
996: fpath[0] = '\0';
997: }
998: endpwent();
999: break;
1000: default:
1001: fpath[0] = '\0';
1002: break;
1003: }
1004:
1005: if (fpath[0] != '\0') {
1006: if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
1007: fatal("cvs_client_initlog: truncation");
1008:
1009: if (strlcat(rpath, fpath, sizeof(rpath))
1010: >= sizeof(rpath))
1011: fatal("cvs_client_initlog: truncation");
1012: }
1013: }
1014:
1015: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1016: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.16 xsa 1017:
1018: if (stat(fpath, &st) != -1)
1019: continue;
1020:
1021: if (errno != ENOENT)
1022: fatal("cvs_client_initlog() stat failed '%s'",
1023: strerror(errno));
1024:
1025: break;
1026: }
1027:
1028: if ((cvs_client_inlog_fd = open(fpath,
1.60 xsa 1029: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1030: fatal("cvs_client_initlog: open `%s': %s",
1031: fpath, strerror(errno));
1032: }
1033:
1034: for (i = 0; i < UINT_MAX; i++) {
1.57 xsa 1035: (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.16 xsa 1036:
1037: if (stat(fpath, &st) != -1)
1038: continue;
1039:
1040: if (errno != ENOENT)
1041: fatal("cvs_client_initlog() stat failed '%s'",
1042: strerror(errno));
1043:
1044: break;
1045: }
1046:
1047: if ((cvs_client_outlog_fd = open(fpath,
1.60 xsa 1048: O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16 xsa 1049: fatal("cvs_client_initlog: open `%s': %s",
1050: fpath, strerror(errno));
1051: }
1052:
1053: cvs_client_logon = 1;
1.1 jfb 1054: }