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