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