Annotation of src/usr.bin/cvs/resp.c, Revision 1.6
1.1 jfb 1: /* $OpenBSD$ */
2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27:
28: #include <sys/types.h>
29: #include <sys/stat.h>
1.2 jfb 30: #include <sys/time.h>
1.1 jfb 31:
32: #include <fcntl.h>
33: #include <stdio.h>
34: #include <errno.h>
35: #include <dirent.h>
36: #include <stdlib.h>
37: #include <unistd.h>
38: #include <string.h>
39: #include <sysexits.h>
40:
41: #include "buf.h"
42: #include "cvs.h"
43: #include "log.h"
44: #include "file.h"
45: #include "proto.h"
46:
47:
48: #define CVS_MTSTK_MAXDEPTH 16
49:
50:
1.4 jfb 51: #define STRIP_SLASH(p) \
52: do { \
53: size_t len; \
54: len = strlen(p); \
55: while ((len > 0) && (p[len - 1] == '/')) \
56: p[--len] = '\0'; \
57: } while (0)
58:
59:
60:
1.1 jfb 61: static int cvs_resp_validreq (struct cvsroot *, int, char *);
62: static int cvs_resp_cksum (struct cvsroot *, int, char *);
63: static int cvs_resp_modtime (struct cvsroot *, int, char *);
64: static int cvs_resp_m (struct cvsroot *, int, char *);
65: static int cvs_resp_ok (struct cvsroot *, int, char *);
66: static int cvs_resp_error (struct cvsroot *, int, char *);
67: static int cvs_resp_statdir (struct cvsroot *, int, char *);
68: static int cvs_resp_sticky (struct cvsroot *, int, char *);
69: static int cvs_resp_newentry (struct cvsroot *, int, char *);
70: static int cvs_resp_updated (struct cvsroot *, int, char *);
71: static int cvs_resp_removed (struct cvsroot *, int, char *);
72: static int cvs_resp_mode (struct cvsroot *, int, char *);
73: static int cvs_resp_modxpand (struct cvsroot *, int, char *);
74: static int cvs_resp_rcsdiff (struct cvsroot *, int, char *);
75: static int cvs_resp_template (struct cvsroot *, int, char *);
76:
77:
78: struct cvs_resphdlr {
79: int (*hdlr)(struct cvsroot *, int, char *);
80: } cvs_resp_swtab[CVS_RESP_MAX + 1] = {
81: { NULL },
82: { cvs_resp_ok },
83: { cvs_resp_error },
84: { cvs_resp_validreq },
85: { cvs_resp_newentry },
86: { cvs_resp_newentry },
87: { cvs_resp_cksum },
88: { NULL },
89: { cvs_resp_updated },
90: { cvs_resp_updated },
91: { cvs_resp_updated }, /* 10 */
92: { cvs_resp_updated },
93: { cvs_resp_updated },
94: { cvs_resp_rcsdiff },
95: { cvs_resp_mode },
96: { cvs_resp_modtime },
97: { cvs_resp_removed },
98: { cvs_resp_removed },
99: { cvs_resp_statdir },
100: { cvs_resp_statdir },
101: { cvs_resp_sticky }, /* 20 */
102: { cvs_resp_sticky },
103: { cvs_resp_template },
104: { NULL },
105: { NULL },
106: { NULL },
107: { cvs_resp_modxpand },
108: { NULL },
109: { cvs_resp_m },
110: { cvs_resp_m },
111: { cvs_resp_m }, /* 30 */
112: { cvs_resp_m },
113: { cvs_resp_m },
114: };
115:
116:
117:
118: /*
119: * The MT command uses scoping to tag the data. Whenever we encouter a '+',
120: * we push the name of the tag on the stack, and we pop it when we encounter
121: * a '-' with the same name.
122: */
123:
124: static char *cvs_mt_stack[CVS_MTSTK_MAXDEPTH];
125: static u_int cvs_mtstk_depth = 0;
126:
127: static time_t cvs_modtime = 0;
128:
129:
130: /* last checksum received */
131: char *cvs_fcksum = NULL;
132:
133: mode_t cvs_lastmode = 0;
134:
135: /* hack to receive the remote version without outputting it */
136: extern u_int cvs_version_sent;
137:
138:
139: /*
140: * cvs_resp_handle()
141: *
142: * Generic response handler dispatcher. The handler expects the first line
143: * of the command as single argument.
144: * Returns the return value of the command on success, or -1 on failure.
145: */
146:
147: int
148: cvs_resp_handle(struct cvsroot *root, char *line)
149: {
150: char *cp, *cmd;
151: struct cvs_resp *resp;
152:
153: cmd = line;
154:
155: cp = strchr(cmd, ' ');
156: if (cp != NULL)
157: *(cp++) = '\0';
158:
159: resp = cvs_resp_getbyname(cmd);
160: if (resp == NULL) {
161: return (-1);
162: }
163: else if (cvs_resp_swtab[resp->resp_id].hdlr == NULL) {
164: cvs_log(LP_ERRNO, "handler for `%s' not implemented", cmd);
165: return (-1);
166: }
167:
168: return (*cvs_resp_swtab[resp->resp_id].hdlr)(root, resp->resp_id, cp);
169: }
170:
171:
172: /*
173: * cvs_resp_validreq()
174: *
175: * Handler for the `Valid-requests' response. The list of valid requests is
176: * split on spaces and each request's entry in the valid request array is set
177: * to 1 to indicate the validity.
178: * Returns 0 on success, or -1 on failure.
179: */
180:
181: static int
182: cvs_resp_validreq(struct cvsroot *root, int type, char *line)
183: {
184: char *sp, *ep;
185: struct cvs_req *req;
186:
187: /* parse the requests */
188: sp = line;
189: do {
190: ep = strchr(sp, ' ');
191: if (ep != NULL)
192: *ep = '\0';
193:
194: req = cvs_req_getbyname(sp);
195: if (req != NULL)
196: CVS_SETVR(root, req->req_id);
197:
198: if (ep != NULL)
199: sp = ep + 1;
200: } while (ep != NULL);
201:
202: return (0);
203: }
204:
205:
206: /*
207: * cvs_resp_m()
208: *
209: * Handler for the `M', 'MT', `F' and `E' responses.
210: */
211:
212: static int
213: cvs_resp_m(struct cvsroot *root, int type, char *line)
214: {
215: char *cp;
216: FILE *stream;
217:
218: stream = NULL;
219:
220: switch (type) {
221: case CVS_RESP_F:
222: fflush(stderr);
223: return (0);
224: case CVS_RESP_M:
225: if (cvs_version_sent) {
226: /*
227: * Instead of outputting the line, we save it as the
228: * remote server's version string.
229: */
230: cvs_version_sent = 0;
231: root->cr_version = strdup(line);
232: return (0);
233: }
234: stream = stdout;
235: break;
236: case CVS_RESP_E:
237: stream = stderr;
238: break;
239: case CVS_RESP_MT:
240: if (*line == '+') {
241: if (cvs_mtstk_depth == CVS_MTSTK_MAXDEPTH) {
242: cvs_log(LP_ERR,
243: "MT scope stack has reached max depth");
244: return (-1);
245: }
246: cvs_mt_stack[cvs_mtstk_depth] = strdup(line + 1);
247: if (cvs_mt_stack[cvs_mtstk_depth] == NULL)
248: return (-1);
249: cvs_mtstk_depth++;
250: }
251: else if (*line == '-') {
252: if (cvs_mtstk_depth == 0) {
253: cvs_log(LP_ERR, "MT scope stack underflow");
254: return (-1);
255: }
256: else if (strcmp(line + 1,
257: cvs_mt_stack[cvs_mtstk_depth - 1]) != 0) {
258: cvs_log(LP_ERR, "mismatch in MT scope stack");
259: return (-1);
260: }
261: free(cvs_mt_stack[cvs_mtstk_depth--]);
262: }
263: else {
264: if (strcmp(line, "newline") == 0)
265: putc('\n', stdout);
266: else if (strncmp(line, "fname ", 6) == 0)
267: printf("%s", line + 6);
268: else {
269: /* assume text */
270: cp = strchr(line, ' ');
271: if (cp != NULL)
272: printf("%s", cp + 1);
273: }
274: }
275:
276: return (0);
277: case CVS_RESP_MBINARY:
278: cvs_log(LP_WARN, "Mbinary not supported in client yet");
279: break;
280: }
281:
282: fputs(line, stream);
283: fputc('\n', stream);
284:
285: return (0);
286: }
287:
288:
289: /*
290: * cvs_resp_ok()
291: *
292: * Handler for the `ok' response. This handler's job is to
293: */
294:
295: static int
296: cvs_resp_ok(struct cvsroot *root, int type, char *line)
297: {
298: return (1);
299: }
300:
301:
302: /*
303: * cvs_resp_error()
304: *
305: * Handler for the `error' response. This handler's job is to
306: */
307:
308: static int
309: cvs_resp_error(struct cvsroot *root, int type, char *line)
310: {
311: return (1);
312: }
313:
314:
315: /*
316: * cvs_resp_statdir()
317: *
318: * Handler for the `Clear-static-directory' and `Set-static-directory'
319: * responses.
320: */
321:
322: static int
323: cvs_resp_statdir(struct cvsroot *root, int type, char *line)
324: {
325: int fd;
326: char rpath[MAXPATHLEN], statpath[MAXPATHLEN];
327:
1.3 jfb 328: /* remote directory line */
329: if (cvs_getln(root, rpath, sizeof(rpath)) < 0)
330: return (-1);
1.1 jfb 331:
332: snprintf(statpath, sizeof(statpath), "%s/%s", line,
333: CVS_PATH_STATICENTRIES);
334:
335: if ((type == CVS_RESP_CLRSTATDIR) &&
336: (unlink(statpath) == -1) && (errno != ENOENT)) {
337: cvs_log(LP_ERRNO, "failed to unlink %s file",
338: CVS_PATH_STATICENTRIES);
339: return (-1);
340: }
341: else if (type == CVS_RESP_SETSTATDIR) {
342: fd = open(statpath, O_CREAT|O_TRUNC|O_WRONLY, 0400);
343: if (fd == -1) {
1.3 jfb 344: cvs_log(LP_ERRNO,
345: "failed to set static directory on %s", line);
1.1 jfb 346: return (-1);
347: }
348: (void)close(fd);
349:
350: }
351:
352: return (0);
353: }
354:
355: /*
356: * cvs_resp_sticky()
357: *
1.4 jfb 358: * Handler for the `Clear-sticky' and `Set-sticky' responses. If the
359: * specified directory doesn't exist, we create it and attach it to the
360: * global file structure.
1.1 jfb 361: */
362:
363: static int
364: cvs_resp_sticky(struct cvsroot *root, int type, char *line)
365: {
1.5 jfb 366: char buf[MAXPATHLEN], subdir[MAXPATHLEN], *file;
367: struct cvs_ent *ent;
1.4 jfb 368: CVSFILE *cf, *sdir;
369:
370: /* get the remote path */
1.5 jfb 371: if (cvs_getln(root, buf, sizeof(buf)) < 0)
1.4 jfb 372: return (-1);
1.1 jfb 373:
1.4 jfb 374: STRIP_SLASH(line);
1.1 jfb 375:
1.4 jfb 376: cvs_splitpath(line, subdir, sizeof(subdir), &file);
377: sdir = cvs_file_find(cvs_files, subdir);
378: if (sdir == NULL) {
379: cvs_log(LP_ERR, "failed to find %s", subdir);
380: return (-1);
381: }
1.1 jfb 382:
1.4 jfb 383: cf = cvs_file_find(sdir, file);
384: if (cf == NULL) {
1.1 jfb 385: /* attempt to create it */
1.4 jfb 386: cf = cvs_file_create(line, DT_DIR, 0755);
387: if (cf == NULL)
388: return (-1);
389: cf->cf_ddat->cd_repo = strdup(line);
390: cf->cf_ddat->cd_root = root;
391: root->cr_ref++;
1.1 jfb 392:
1.4 jfb 393: cvs_file_attach(sdir, cf);
1.5 jfb 394:
395: /* add a directory entry to the parent */
396: if (CVS_DIR_ENTRIES(sdir) != NULL) {
397: snprintf(buf, sizeof(buf), "D/%s////", cf->cf_name);
398: ent = cvs_ent_parse(buf);
399: if (ent == NULL)
400: cvs_log(LP_ERR,
401: "failed to create directory entry");
402: else
403: cvs_ent_add(CVS_DIR_ENTRIES(sdir), ent);
404: }
1.1 jfb 405: }
406:
1.4 jfb 407: if (type == CVS_RESP_CLRSTICKY)
408: cf->cf_ddat->cd_flags &= ~CVS_DIRF_STICKY;
409: else if (type == CVS_RESP_SETSTICKY)
410: cf->cf_ddat->cd_flags |= CVS_DIRF_STICKY;
1.1 jfb 411:
412: return (0);
413: }
414:
415:
416: /*
417: * cvs_resp_newentry()
418: *
419: * Handler for the `New-entry' response and `Checked-in' responses.
420: */
421:
422: static int
423: cvs_resp_newentry(struct cvsroot *root, int type, char *line)
424: {
425: char entbuf[128];
426: CVSENTRIES *entfile;
427:
428: /* get the remote path */
429: cvs_getln(root, entbuf, sizeof(entbuf));
430:
431: /* get the new Entries line */
432: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
433: return (-1);
434:
435: entfile = cvs_ent_open(line, O_WRONLY);
436: if (entfile == NULL)
437: return (-1);
438: cvs_ent_addln(entfile, entbuf);
439: cvs_ent_close(entfile);
440:
441: return (0);
442: }
443:
444:
445: /*
446: * cvs_resp_cksum()
447: *
448: * Handler for the `Checksum' response. We store the checksum received for
449: * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
450: * Upon next file reception, the handler checks to see if there is a stored
451: * checksum.
452: * The file handler must make sure that the checksums match and free the
453: * checksum buffer once it's done to indicate there is no further checksum.
454: */
455:
456: static int
457: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
458: {
459: if (cvs_fcksum != NULL) {
460: cvs_log(LP_WARN, "unused checksum");
461: free(cvs_fcksum);
462: }
463:
464: cvs_fcksum = strdup(line);
465: if (cvs_fcksum == NULL) {
466: cvs_log(LP_ERRNO, "failed to copy checksum string");
467: return (-1);
468: }
469:
470: return (0);
471: }
472:
473:
474: /*
475: * cvs_resp_modtime()
476: *
477: * Handler for the `Mod-time' file update modifying response. The timestamp
478: * given is used to set the last modification time on the next file that
479: * will be received.
480: */
481:
482: static int
483: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
484: {
1.4 jfb 485: cvs_modtime = cvs_datesec(line, CVS_DATE_RFC822, 1);
1.1 jfb 486: return (0);
487: }
488:
489:
490: /*
491: * cvs_resp_updated()
492: *
1.4 jfb 493: * Handler for the `Updated', `Update-existing', `Created', `Merged' and
494: * `Patched' responses, which all have a very similar format.
1.1 jfb 495: */
496:
497: static int
498: cvs_resp_updated(struct cvsroot *root, int type, char *line)
499: {
500: mode_t fmode;
1.4 jfb 501: char path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.1 jfb 502: BUF *fbuf;
1.4 jfb 503: CVSFILE *cf;
1.1 jfb 504: struct cvs_ent *ep;
1.2 jfb 505: struct timeval tv[2];
1.1 jfb 506:
1.4 jfb 507: STRIP_SLASH(line);
508:
509: /* find parent directory of file */
510: cf = cvs_file_find(cvs_files, line);
511: if (cf == NULL) {
512: cvs_log(LP_ERR, "failed to find directory %s", line);
513: return (-1);
514: }
1.1 jfb 515:
516: /* read the remote path of the file */
1.4 jfb 517: if (cvs_getln(root, path, sizeof(path)) < 0)
518: return (-1);
1.1 jfb 519:
520: /* read the new entry */
1.4 jfb 521: if (cvs_getln(root, path, sizeof(path)) < 0)
1.1 jfb 522: return (-1);
1.4 jfb 523:
524: if ((ep = cvs_ent_parse(path)) == NULL)
525: return (-1);
526: snprintf(path, sizeof(path), "%s/%s", line, ep->ce_name);
1.1 jfb 527:
528: if (type == CVS_RESP_CREATED) {
529: /* set the timestamp as the last one received from Mod-time */
1.4 jfb 530: ep->ce_mtime = cvs_modtime;
531: cvs_ent_add(cf->cf_ddat->cd_ent, ep);
1.1 jfb 532: }
533: else if (type == CVS_RESP_UPDEXIST) {
534: }
535: else if (type == CVS_RESP_UPDATED) {
536: }
537:
538: fbuf = cvs_recvfile(root, &fmode);
539: if (fbuf == NULL)
540: return (-1);
541:
542: cvs_buf_write(fbuf, path, fmode);
1.2 jfb 543:
544: tv[0].tv_sec = (long)cvs_modtime;
545: tv[0].tv_usec = 0;
546: tv[1].tv_sec = (long)cvs_modtime;
547: tv[1].tv_usec = 0;
548: if (utimes(path, tv) == -1)
549: cvs_log(LP_ERRNO, "failed to set file timestamps");
1.1 jfb 550:
551: /* now see if there is a checksum */
552: if (cvs_fcksum != NULL) {
553: if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0) {
554: }
555:
556: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
557: cvs_log(LP_ERR, "checksum error on received file");
558: (void)unlink(line);
559: }
560:
561: free(cvs_fcksum);
562: cvs_fcksum = NULL;
563: }
564:
565: return (0);
566: }
567:
568:
569: /*
570: * cvs_resp_removed()
571: *
572: * Handler for the `Removed' and `Remove-entry' responses.
573: */
574:
575: static int
576: cvs_resp_removed(struct cvsroot *root, int type, char *line)
577: {
1.3 jfb 578: char base[MAXPATHLEN], *file;
1.1 jfb 579: CVSENTRIES *ef;
580:
1.3 jfb 581: cvs_splitpath(line, base, sizeof(base), &file);
1.1 jfb 582: ef = cvs_ent_open(base, O_RDWR);
583: if (ef == NULL)
584: return (-1);
585:
586: printf("Received a `Remove' on %s\n", line);
587: cvs_ent_remove(ef, file);
588: cvs_ent_close(ef);
589:
590: return (0);
591: }
592:
593:
594: /*
595: * cvs_resp_mode()
596: *
597: * Handler for the `Mode' response.
598: */
599:
600: static int
601: cvs_resp_mode(struct cvsroot *root, int type, char *line)
602: {
603: if (cvs_strtomode(line, &cvs_lastmode) < 0) {
604: return (-1);
605: }
606: return (0);
607: }
608:
609:
610: /*
611: * cvs_resp_modxpand()
612: *
613: * Handler for the `Module-expansion' response.
614: */
615:
616: static int
617: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
618: {
619: return (0);
620: }
621:
622: /*
623: * cvs_resp_rcsdiff()
624: *
625: * Handler for the `Rcs-diff' response.
626: */
627:
628: static int
629: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
630: {
631: char file[MAXPATHLEN], buf[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
632: char *fname, *orig, *patch;
633: mode_t fmode;
634: BUF *res, *fcont, *patchbuf;
635: CVSENTRIES *entf;
636: struct cvs_ent *ent;
637:
638: /* get remote path and build local path of file to be patched */
639: cvs_getln(root, buf, sizeof(buf));
640: fname = strrchr(buf, '/');
641: if (fname == NULL)
642: fname = buf;
643: snprintf(file, sizeof(file), "%s%s", line, fname);
644:
645: /* get updated entry fields */
646: cvs_getln(root, buf, sizeof(buf));
647: ent = cvs_ent_parse(buf);
648: if (ent == NULL) {
649: return (-1);
650: }
651:
652: patchbuf = cvs_recvfile(root, &fmode);
653: fcont = cvs_buf_load(file, BUF_AUTOEXT);
654: if (fcont == NULL)
655: return (-1);
656:
657: cvs_buf_putc(patchbuf, '\0');
658: cvs_buf_putc(fcont, '\0');
659: orig = cvs_buf_release(fcont);
660: patch = cvs_buf_release(patchbuf);
661:
662: res = rcs_patch(orig, patch);
663: if (res == NULL)
664: return (-1);
665:
666: cvs_buf_write(res, file, fmode);
667:
668: /* now see if there is a checksum */
669: if (cvs_fcksum != NULL) {
670: if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
671: }
672:
673: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
674: cvs_log(LP_ERR, "checksum error on received file");
675: (void)unlink(file);
676: }
677:
678: free(cvs_fcksum);
679: cvs_fcksum = NULL;
680: }
681:
682: /* update revision in entries */
683: entf = cvs_ent_open(line, O_WRONLY);
684: if (entf == NULL)
685: return (-1);
686:
687: cvs_ent_close(entf);
688:
689: return (0);
690: }
691:
692:
693: /*
694: * cvs_resp_template()
695: *
696: * Handler for the `Template' response.
697: */
698:
699: static int
700: cvs_resp_template(struct cvsroot *root, int type, char *line)
701: {
702: mode_t mode;
703: BUF *tmpl;
704:
705: tmpl = cvs_recvfile(root, &mode);
706: if (tmpl == NULL)
707: return (-1);
708:
709: return (0);
710: }