Annotation of src/usr.bin/cvs/client.c, Revision 1.50
1.50 ! otto 1: /* $OpenBSD: client.c,v 1.49 2007/01/18 16:45:52 joris 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;
610: mode_t fmode;
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);
644:
645: time(&now);
646: now = cvs_hack_time(now, 0);
647: ctime_r(&now, timebuf);
648: if (timebuf[strlen(timebuf) - 1] == '\n')
649: timebuf[strlen(timebuf) - 1] = '\0';
650:
651: e = cvs_ent_parse(entry);
652: xfree(entry);
653: rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
654: entry = xmalloc(CVS_ENT_MAXLINELEN);
655: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//", e->ce_name,
656: revbuf, timebuf);
657: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
658: fatal("cvs_client_updated: overflow");
659:
660: cvs_ent_free(e);
661: ent = cvs_ent_open(wdir);
662: cvs_ent_add(ent, entry);
663: cvs_ent_close(ent, ENT_SYNC);
664: xfree(entry);
665:
666: if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
667: fatal("cvs_client_updated: open: %s: %s",
668: fpath, strerror(errno));
1.1 jfb 669:
1.46 joris 670: cvs_remote_receive_file(fd, flen);
1.1 jfb 671:
1.9 joris 672: now = cvs_hack_time(now, 0);
673: tv[0].tv_sec = now;
674: tv[0].tv_usec = 0;
675: tv[1] = tv[0];
1.1 jfb 676:
1.9 joris 677: if (futimes(fd, tv) == -1)
678: fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1 jfb 679:
1.9 joris 680: if (fchmod(fd, fmode) == -1)
681: fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1 jfb 682:
1.9 joris 683: (void)close(fd);
1.1 jfb 684:
1.9 joris 685: xfree(rpath);
1.1 jfb 686: }
687:
1.9 joris 688: void
689: cvs_client_merged(char *data)
690: {
691: }
1.1 jfb 692:
1.9 joris 693: void
694: cvs_client_removed(char *data)
695: {
1.13 joris 696: char *dir;
697:
698: dir = cvs_remote_input();
699: xfree(dir);
1.9 joris 700: }
1.1 jfb 701:
1.9 joris 702: void
703: cvs_client_remove_entry(char *data)
1.1 jfb 704: {
1.38 xsa 705: CVSENTRIES *entlist;
1.44 joris 706: char *filename, *rpath;
1.41 joris 707:
708: rpath = cvs_remote_input();
709: if ((filename = strrchr(rpath, '/')) == NULL)
710: fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
711: *filename++;
1.1 jfb 712:
1.38 xsa 713: entlist = cvs_ent_open(data);
1.44 joris 714: cvs_ent_remove(entlist, filename);
1.38 xsa 715: cvs_ent_close(entlist, ENT_SYNC);
716:
1.44 joris 717: xfree(rpath);
1.36 xsa 718: }
719:
720: void
721: cvs_client_set_static_directory(char *data)
722: {
723: FILE *fp;
1.50 ! otto 724: char *dir, fpath[MAXPATHLEN];
1.36 xsa 725:
726: if (cvs_cmdop == CVS_OP_EXPORT)
727: return;
728:
729: STRIP_SLASH(data);
730:
731: dir = cvs_remote_input();
732: xfree(dir);
733:
734: if (cvs_path_cat(data, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
735: MAXPATHLEN)
736: fatal("cvs_client_set_static_directory: truncation");
737:
738: if ((fp = fopen(fpath, "w+")) == NULL) {
739: cvs_log(LP_ERRNO, "%s", fpath);
1.50 ! otto 740: return;
1.36 xsa 741: }
742: (void)fclose(fp);
1.16 xsa 743: }
1.35 xsa 744:
745: void
746: cvs_client_clear_static_directory(char *data)
747: {
1.50 ! otto 748: char *dir, fpath[MAXPATHLEN];
1.35 xsa 749:
750: if (cvs_cmdop == CVS_OP_EXPORT)
751: return;
752:
753: STRIP_SLASH(data);
754:
755: dir = cvs_remote_input();
756: xfree(dir);
757:
758: if (cvs_path_cat(data, CVS_PATH_STATICENTRIES, fpath, MAXPATHLEN) >=
759: MAXPATHLEN)
760: fatal("cvs_client_clear_static_directory: truncation");
761:
762: (void)cvs_unlink(fpath);
763: }
764:
765: void
766: cvs_client_set_sticky(char *data)
767: {
768: FILE *fp;
1.50 ! otto 769: char *dir, *tag, tagpath[MAXPATHLEN];
1.35 xsa 770:
1.47 xsa 771: if (cvs_cmdop == CVS_OP_EXPORT)
772: return;
773:
1.35 xsa 774: STRIP_SLASH(data);
775:
776: dir = cvs_remote_input();
777: xfree(dir);
778: tag = cvs_remote_input();
779:
780: if (cvs_path_cat(data, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
781: fatal("cvs_client_clear_sticky: truncation");
782:
783: if ((fp = fopen(tagpath, "w+")) == NULL) {
784: cvs_log(LP_ERRNO, "%s", tagpath);
785: goto out;
786: }
787:
788: (void)fprintf(fp, "%s\n", tag);
789: (void)fclose(fp);
790: out:
791: xfree(tag);
792: }
793:
794: void
795: cvs_client_clear_sticky(char *data)
796: {
1.50 ! otto 797: char *dir, tagpath[MAXPATHLEN];
1.47 xsa 798:
799: if (cvs_cmdop == CVS_OP_EXPORT)
800: return;
1.35 xsa 801:
802: STRIP_SLASH(data);
803:
804: dir = cvs_remote_input();
805: xfree(dir);
806:
807: if (cvs_path_cat(data, CVS_PATH_TAG, tagpath, MAXPATHLEN) >= MAXPATHLEN)
808: fatal("cvs_client_clear_sticky: truncation");
809:
810: (void)cvs_unlink(tagpath);
811: }
812:
813:
1.16 xsa 814: /*
815: * cvs_client_initlog()
816: *
817: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
818: * set. In this case, the variable's value is used as a path to which the
819: * appropriate suffix is added (".in" for client input and ".out" for server
820: * output).
821: */
822: static void
823: cvs_client_initlog(void)
824: {
825: int l;
826: u_int i;
827: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
828: char rpath[MAXPATHLEN], *s;
829: struct stat st;
830: time_t now;
831: struct passwd *pwd;
832:
833: /* avoid doing it more than once */
834: if (cvs_client_logon)
835: return;
836:
837: if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
838: return;
839:
840: envdup = xstrdup(env);
841: if ((s = strchr(envdup, '%')) != NULL)
842: *s = '\0';
843:
844: if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
845: fatal("cvs_client_initlog: truncation");
846:
847: if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
848: fatal("cvs_client_initlog: truncation");
849:
850: xfree(envdup);
851:
852: s = buf;
853: while ((s = strchr(s, '%')) != NULL) {
854: s++;
855: switch (*s) {
856: case 'c':
857: if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
858: sizeof(fpath))
859: fatal("cvs_client_initlog: truncation");
860: break;
861: case 'd':
862: time(&now);
863: if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
864: sizeof(fpath))
865: fatal("cvs_client_initlog: truncation");
866: break;
867: case 'p':
868: snprintf(fpath, sizeof(fpath), "%d", getpid());
869: break;
870: case 'u':
871: if ((pwd = getpwuid(getuid())) != NULL) {
872: if (strlcpy(fpath, pwd->pw_name,
873: sizeof(fpath)) >= sizeof(fpath))
874: fatal("cvs_client_initlog: truncation");
875: } else {
876: fpath[0] = '\0';
877: }
878: endpwent();
879: break;
880: default:
881: fpath[0] = '\0';
882: break;
883: }
884:
885: if (fpath[0] != '\0') {
886: if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
887: fatal("cvs_client_initlog: truncation");
888:
889: if (strlcat(rpath, fpath, sizeof(rpath))
890: >= sizeof(rpath))
891: fatal("cvs_client_initlog: truncation");
892: }
893: }
894:
895: for (i = 0; i < UINT_MAX; i++) {
896: l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
897: if (l == -1 || l >= (int)sizeof(fpath))
898: fatal("cvs_client_initlog: overflow");
899:
900: if (stat(fpath, &st) != -1)
901: continue;
902:
903: if (errno != ENOENT)
904: fatal("cvs_client_initlog() stat failed '%s'",
905: strerror(errno));
906:
907: break;
908: }
909:
910: if ((cvs_client_inlog_fd = open(fpath,
911: O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
912: fatal("cvs_client_initlog: open `%s': %s",
913: fpath, strerror(errno));
914: }
915:
916: for (i = 0; i < UINT_MAX; i++) {
917: l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
918: if (l == -1 || l >= (int)sizeof(fpath))
919: fatal("cvs_client_initlog: overflow");
920:
921: if (stat(fpath, &st) != -1)
922: continue;
923:
924: if (errno != ENOENT)
925: fatal("cvs_client_initlog() stat failed '%s'",
926: strerror(errno));
927:
928: break;
929: }
930:
931: if ((cvs_client_outlog_fd = open(fpath,
932: O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
933: fatal("cvs_client_initlog: open `%s': %s",
934: fpath, strerror(errno));
935: }
936:
937: cvs_client_logon = 1;
1.1 jfb 938: }