Annotation of src/usr.bin/cvs/resp.c, Revision 1.8
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: {
1.7 jfb 311: fprintf(stderr, "%s\n", line);
1.1 jfb 312: return (1);
313: }
314:
315:
316: /*
317: * cvs_resp_statdir()
318: *
319: * Handler for the `Clear-static-directory' and `Set-static-directory'
320: * responses.
321: */
322:
323: static int
324: cvs_resp_statdir(struct cvsroot *root, int type, char *line)
325: {
326: int fd;
327: char rpath[MAXPATHLEN], statpath[MAXPATHLEN];
328:
1.3 jfb 329: /* remote directory line */
330: if (cvs_getln(root, rpath, sizeof(rpath)) < 0)
331: return (-1);
1.1 jfb 332:
333: snprintf(statpath, sizeof(statpath), "%s/%s", line,
334: CVS_PATH_STATICENTRIES);
335:
336: if ((type == CVS_RESP_CLRSTATDIR) &&
337: (unlink(statpath) == -1) && (errno != ENOENT)) {
338: cvs_log(LP_ERRNO, "failed to unlink %s file",
339: CVS_PATH_STATICENTRIES);
340: return (-1);
341: }
342: else if (type == CVS_RESP_SETSTATDIR) {
343: fd = open(statpath, O_CREAT|O_TRUNC|O_WRONLY, 0400);
344: if (fd == -1) {
1.3 jfb 345: cvs_log(LP_ERRNO,
346: "failed to set static directory on %s", line);
1.1 jfb 347: return (-1);
348: }
349: (void)close(fd);
350:
351: }
352:
353: return (0);
354: }
355:
356: /*
357: * cvs_resp_sticky()
358: *
1.4 jfb 359: * Handler for the `Clear-sticky' and `Set-sticky' responses. If the
360: * specified directory doesn't exist, we create it and attach it to the
361: * global file structure.
1.1 jfb 362: */
363:
364: static int
365: cvs_resp_sticky(struct cvsroot *root, int type, char *line)
366: {
1.5 jfb 367: char buf[MAXPATHLEN], subdir[MAXPATHLEN], *file;
368: struct cvs_ent *ent;
1.4 jfb 369: CVSFILE *cf, *sdir;
370:
371: /* get the remote path */
1.5 jfb 372: if (cvs_getln(root, buf, sizeof(buf)) < 0)
1.4 jfb 373: return (-1);
1.1 jfb 374:
1.4 jfb 375: STRIP_SLASH(line);
1.1 jfb 376:
1.4 jfb 377: cvs_splitpath(line, subdir, sizeof(subdir), &file);
378: sdir = cvs_file_find(cvs_files, subdir);
379: if (sdir == NULL) {
380: cvs_log(LP_ERR, "failed to find %s", subdir);
381: return (-1);
382: }
1.1 jfb 383:
1.4 jfb 384: cf = cvs_file_find(sdir, file);
385: if (cf == NULL) {
1.1 jfb 386: /* attempt to create it */
1.8 ! jfb 387: cf = cvs_file_create(sdir, line, DT_DIR, 0755);
1.4 jfb 388: if (cf == NULL)
389: return (-1);
390: cf->cf_ddat->cd_repo = strdup(line);
391: cf->cf_ddat->cd_root = root;
392: root->cr_ref++;
1.1 jfb 393:
1.4 jfb 394: cvs_file_attach(sdir, cf);
1.5 jfb 395:
396: /* add a directory entry to the parent */
397: if (CVS_DIR_ENTRIES(sdir) != NULL) {
1.8 ! jfb 398: snprintf(buf, sizeof(buf), "D/%s////",
! 399: CVS_FILE_NAME(cf));
1.5 jfb 400: ent = cvs_ent_parse(buf);
401: if (ent == NULL)
402: cvs_log(LP_ERR,
403: "failed to create directory entry");
404: else
405: cvs_ent_add(CVS_DIR_ENTRIES(sdir), ent);
406: }
1.1 jfb 407: }
408:
1.4 jfb 409: if (type == CVS_RESP_CLRSTICKY)
410: cf->cf_ddat->cd_flags &= ~CVS_DIRF_STICKY;
411: else if (type == CVS_RESP_SETSTICKY)
412: cf->cf_ddat->cd_flags |= CVS_DIRF_STICKY;
1.1 jfb 413:
414: return (0);
415: }
416:
417:
418: /*
419: * cvs_resp_newentry()
420: *
421: * Handler for the `New-entry' response and `Checked-in' responses.
422: */
423:
424: static int
425: cvs_resp_newentry(struct cvsroot *root, int type, char *line)
426: {
427: char entbuf[128];
428: CVSENTRIES *entfile;
429:
430: /* get the remote path */
431: cvs_getln(root, entbuf, sizeof(entbuf));
432:
433: /* get the new Entries line */
434: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
435: return (-1);
436:
437: entfile = cvs_ent_open(line, O_WRONLY);
438: if (entfile == NULL)
439: return (-1);
440: cvs_ent_addln(entfile, entbuf);
441: cvs_ent_close(entfile);
442:
443: return (0);
444: }
445:
446:
447: /*
448: * cvs_resp_cksum()
449: *
450: * Handler for the `Checksum' response. We store the checksum received for
451: * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
452: * Upon next file reception, the handler checks to see if there is a stored
453: * checksum.
454: * The file handler must make sure that the checksums match and free the
455: * checksum buffer once it's done to indicate there is no further checksum.
456: */
457:
458: static int
459: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
460: {
461: if (cvs_fcksum != NULL) {
462: cvs_log(LP_WARN, "unused checksum");
463: free(cvs_fcksum);
464: }
465:
466: cvs_fcksum = strdup(line);
467: if (cvs_fcksum == NULL) {
468: cvs_log(LP_ERRNO, "failed to copy checksum string");
469: return (-1);
470: }
471:
472: return (0);
473: }
474:
475:
476: /*
477: * cvs_resp_modtime()
478: *
479: * Handler for the `Mod-time' file update modifying response. The timestamp
480: * given is used to set the last modification time on the next file that
481: * will be received.
482: */
483:
484: static int
485: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
486: {
1.4 jfb 487: cvs_modtime = cvs_datesec(line, CVS_DATE_RFC822, 1);
1.1 jfb 488: return (0);
489: }
490:
491:
492: /*
493: * cvs_resp_updated()
494: *
1.4 jfb 495: * Handler for the `Updated', `Update-existing', `Created', `Merged' and
496: * `Patched' responses, which all have a very similar format.
1.1 jfb 497: */
498:
499: static int
500: cvs_resp_updated(struct cvsroot *root, int type, char *line)
501: {
502: mode_t fmode;
1.4 jfb 503: char path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.1 jfb 504: BUF *fbuf;
1.4 jfb 505: CVSFILE *cf;
1.1 jfb 506: struct cvs_ent *ep;
1.2 jfb 507: struct timeval tv[2];
1.1 jfb 508:
1.4 jfb 509: STRIP_SLASH(line);
510:
511: /* find parent directory of file */
512: cf = cvs_file_find(cvs_files, line);
513: if (cf == NULL) {
514: cvs_log(LP_ERR, "failed to find directory %s", line);
515: return (-1);
516: }
1.1 jfb 517:
518: /* read the remote path of the file */
1.4 jfb 519: if (cvs_getln(root, path, sizeof(path)) < 0)
520: return (-1);
1.1 jfb 521:
522: /* read the new entry */
1.4 jfb 523: if (cvs_getln(root, path, sizeof(path)) < 0)
1.1 jfb 524: return (-1);
1.4 jfb 525:
526: if ((ep = cvs_ent_parse(path)) == NULL)
527: return (-1);
528: snprintf(path, sizeof(path), "%s/%s", line, ep->ce_name);
1.1 jfb 529:
530: if (type == CVS_RESP_CREATED) {
531: /* set the timestamp as the last one received from Mod-time */
1.4 jfb 532: ep->ce_mtime = cvs_modtime;
533: cvs_ent_add(cf->cf_ddat->cd_ent, ep);
1.1 jfb 534: }
535: else if (type == CVS_RESP_UPDEXIST) {
536: }
537: else if (type == CVS_RESP_UPDATED) {
538: }
539:
540: fbuf = cvs_recvfile(root, &fmode);
541: if (fbuf == NULL)
542: return (-1);
543:
544: cvs_buf_write(fbuf, path, fmode);
1.2 jfb 545:
546: tv[0].tv_sec = (long)cvs_modtime;
547: tv[0].tv_usec = 0;
548: tv[1].tv_sec = (long)cvs_modtime;
549: tv[1].tv_usec = 0;
550: if (utimes(path, tv) == -1)
551: cvs_log(LP_ERRNO, "failed to set file timestamps");
1.1 jfb 552:
553: /* now see if there is a checksum */
554: if (cvs_fcksum != NULL) {
555: if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0) {
556: }
557:
558: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
559: cvs_log(LP_ERR, "checksum error on received file");
560: (void)unlink(line);
561: }
562:
563: free(cvs_fcksum);
564: cvs_fcksum = NULL;
565: }
566:
567: return (0);
568: }
569:
570:
571: /*
572: * cvs_resp_removed()
573: *
574: * Handler for the `Removed' and `Remove-entry' responses.
575: */
576:
577: static int
578: cvs_resp_removed(struct cvsroot *root, int type, char *line)
579: {
1.3 jfb 580: char base[MAXPATHLEN], *file;
1.1 jfb 581: CVSENTRIES *ef;
582:
1.3 jfb 583: cvs_splitpath(line, base, sizeof(base), &file);
1.1 jfb 584: ef = cvs_ent_open(base, O_RDWR);
585: if (ef == NULL)
586: return (-1);
587:
588: printf("Received a `Remove' on %s\n", line);
589: cvs_ent_remove(ef, file);
590: cvs_ent_close(ef);
591:
592: return (0);
593: }
594:
595:
596: /*
597: * cvs_resp_mode()
598: *
599: * Handler for the `Mode' response.
600: */
601:
602: static int
603: cvs_resp_mode(struct cvsroot *root, int type, char *line)
604: {
605: if (cvs_strtomode(line, &cvs_lastmode) < 0) {
606: return (-1);
607: }
608: return (0);
609: }
610:
611:
612: /*
613: * cvs_resp_modxpand()
614: *
615: * Handler for the `Module-expansion' response.
616: */
617:
618: static int
619: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
620: {
621: return (0);
622: }
623:
624: /*
625: * cvs_resp_rcsdiff()
626: *
627: * Handler for the `Rcs-diff' response.
628: */
629:
630: static int
631: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
632: {
633: char file[MAXPATHLEN], buf[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
634: char *fname, *orig, *patch;
635: mode_t fmode;
636: BUF *res, *fcont, *patchbuf;
637: CVSENTRIES *entf;
638: struct cvs_ent *ent;
639:
640: /* get remote path and build local path of file to be patched */
641: cvs_getln(root, buf, sizeof(buf));
642: fname = strrchr(buf, '/');
643: if (fname == NULL)
644: fname = buf;
645: snprintf(file, sizeof(file), "%s%s", line, fname);
646:
647: /* get updated entry fields */
648: cvs_getln(root, buf, sizeof(buf));
649: ent = cvs_ent_parse(buf);
650: if (ent == NULL) {
651: return (-1);
652: }
653:
654: patchbuf = cvs_recvfile(root, &fmode);
655: fcont = cvs_buf_load(file, BUF_AUTOEXT);
656: if (fcont == NULL)
657: return (-1);
658:
659: cvs_buf_putc(patchbuf, '\0');
660: cvs_buf_putc(fcont, '\0');
661: orig = cvs_buf_release(fcont);
662: patch = cvs_buf_release(patchbuf);
663:
664: res = rcs_patch(orig, patch);
665: if (res == NULL)
666: return (-1);
667:
668: cvs_buf_write(res, file, fmode);
669:
670: /* now see if there is a checksum */
671: if (cvs_fcksum != NULL) {
672: if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
673: }
674:
675: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
676: cvs_log(LP_ERR, "checksum error on received file");
677: (void)unlink(file);
678: }
679:
680: free(cvs_fcksum);
681: cvs_fcksum = NULL;
682: }
683:
684: /* update revision in entries */
685: entf = cvs_ent_open(line, O_WRONLY);
686: if (entf == NULL)
687: return (-1);
688:
689: cvs_ent_close(entf);
690:
691: return (0);
692: }
693:
694:
695: /*
696: * cvs_resp_template()
697: *
698: * Handler for the `Template' response.
699: */
700:
701: static int
702: cvs_resp_template(struct cvsroot *root, int type, char *line)
703: {
704: mode_t mode;
705: BUF *tmpl;
706:
707: tmpl = cvs_recvfile(root, &mode);
708: if (tmpl == NULL)
709: return (-1);
710:
711: return (0);
712: }