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