Annotation of src/usr.bin/cvs/resp.c, Revision 1.48
1.48 ! xsa 1: /* $OpenBSD: resp.c,v 1.47 2005/07/23 11:19:46 joris Exp $ */
1.1 jfb 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:
1.41 xsa 32: #include <dirent.h>
33: #include <errno.h>
1.1 jfb 34: #include <fcntl.h>
1.41 xsa 35: #include <libgen.h>
1.1 jfb 36: #include <stdio.h>
37: #include <stdlib.h>
1.41 xsa 38: #include <string.h>
1.1 jfb 39: #include <unistd.h>
40:
41: #include "buf.h"
42: #include "cvs.h"
43: #include "log.h"
44: #include "proto.h"
45:
46:
47: #define CVS_MTSTK_MAXDEPTH 16
48:
49:
1.4 jfb 50: #define STRIP_SLASH(p) \
51: do { \
1.37 jfb 52: size_t _slen; \
53: _slen = strlen(p); \
54: while ((_slen > 0) && (p[_slen - 1] == '/')) \
55: p[--_slen] = '\0'; \
1.4 jfb 56: } while (0)
57:
58:
1.1 jfb 59: static int cvs_resp_validreq (struct cvsroot *, int, char *);
60: static int cvs_resp_cksum (struct cvsroot *, int, char *);
61: static int cvs_resp_modtime (struct cvsroot *, int, char *);
62: static int cvs_resp_m (struct cvsroot *, int, char *);
63: static int cvs_resp_ok (struct cvsroot *, int, char *);
64: static int cvs_resp_error (struct cvsroot *, int, char *);
65: static int cvs_resp_statdir (struct cvsroot *, int, char *);
66: static int cvs_resp_sticky (struct cvsroot *, int, char *);
67: static int cvs_resp_newentry (struct cvsroot *, int, char *);
68: static int cvs_resp_updated (struct cvsroot *, int, char *);
69: static int cvs_resp_removed (struct cvsroot *, int, char *);
70: static int cvs_resp_mode (struct cvsroot *, int, char *);
71: static int cvs_resp_modxpand (struct cvsroot *, int, char *);
72: static int cvs_resp_rcsdiff (struct cvsroot *, int, char *);
73: static int cvs_resp_template (struct cvsroot *, int, char *);
1.14 jfb 74: static int cvs_resp_copyfile (struct cvsroot *, int, char *);
1.47 joris 75: static int cvs_resp_createdir (char *);
1.1 jfb 76:
77: struct cvs_resphdlr {
78: int (*hdlr)(struct cvsroot *, int, char *);
79: } cvs_resp_swtab[CVS_RESP_MAX + 1] = {
80: { NULL },
81: { cvs_resp_ok },
82: { cvs_resp_error },
83: { cvs_resp_validreq },
84: { cvs_resp_newentry },
85: { cvs_resp_newentry },
86: { cvs_resp_cksum },
1.14 jfb 87: { cvs_resp_copyfile },
1.1 jfb 88: { cvs_resp_updated },
89: { cvs_resp_updated },
90: { cvs_resp_updated }, /* 10 */
91: { cvs_resp_updated },
92: { cvs_resp_updated },
93: { cvs_resp_rcsdiff },
94: { cvs_resp_mode },
95: { cvs_resp_modtime },
96: { cvs_resp_removed },
97: { cvs_resp_removed },
98: { cvs_resp_statdir },
99: { cvs_resp_statdir },
100: { cvs_resp_sticky }, /* 20 */
101: { cvs_resp_sticky },
102: { cvs_resp_template },
103: { NULL },
104: { NULL },
105: { NULL },
106: { cvs_resp_modxpand },
107: { NULL },
108: { cvs_resp_m },
109: { cvs_resp_m },
110: { cvs_resp_m }, /* 30 */
111: { cvs_resp_m },
112: { cvs_resp_m },
113: };
114:
1.42 joris 115: /*
116: * Instead of opening and closing the Entry file all the time,
117: * which caused a huge CPU load and slowed down everything,
118: * we keep the Entry file for the directory we are working in
119: * open until we encounter a new directory.
120: */
121: static char cvs_resp_lastdir[MAXPATHLEN] = "";
122: static CVSENTRIES *cvs_resp_lastent = NULL;
123: static int resp_check_dir(const char *);
1.1 jfb 124:
125: /*
126: * The MT command uses scoping to tag the data. Whenever we encouter a '+',
127: * we push the name of the tag on the stack, and we pop it when we encounter
128: * a '-' with the same name.
129: */
130:
131: static char *cvs_mt_stack[CVS_MTSTK_MAXDEPTH];
132: static u_int cvs_mtstk_depth = 0;
133:
1.12 jfb 134: static time_t cvs_modtime = CVS_DATE_DMSEC;
1.1 jfb 135:
136:
137: /* last checksum received */
138: char *cvs_fcksum = NULL;
139:
140: mode_t cvs_lastmode = 0;
141:
142: /* hack to receive the remote version without outputting it */
143: extern u_int cvs_version_sent;
144:
145:
146: /*
147: * cvs_resp_handle()
148: *
149: * Generic response handler dispatcher. The handler expects the first line
150: * of the command as single argument.
151: * Returns the return value of the command on success, or -1 on failure.
152: */
153: int
154: cvs_resp_handle(struct cvsroot *root, char *line)
155: {
1.15 jfb 156: int ret;
1.1 jfb 157: char *cp, *cmd;
158: struct cvs_resp *resp;
159:
160: cmd = line;
161:
162: cp = strchr(cmd, ' ');
163: if (cp != NULL)
164: *(cp++) = '\0';
165:
166: resp = cvs_resp_getbyname(cmd);
167: if (resp == NULL) {
168: return (-1);
1.11 deraadt 169: } else if (cvs_resp_swtab[resp->resp_id].hdlr == NULL) {
1.20 jfb 170: cvs_log(LP_ERR, "handler for `%s' not implemented", cmd);
1.1 jfb 171: return (-1);
172: }
173:
1.15 jfb 174: ret = (*cvs_resp_swtab[resp->resp_id].hdlr)(root, resp->resp_id, cp);
175:
176: if (ret == -1)
177: cvs_log(LP_ERR, "error in handling of `%s' response", cmd);
178:
179: return (ret);
1.1 jfb 180: }
181:
182:
183: /*
184: * cvs_resp_validreq()
185: *
186: * Handler for the `Valid-requests' response. The list of valid requests is
187: * split on spaces and each request's entry in the valid request array is set
188: * to 1 to indicate the validity.
189: * Returns 0 on success, or -1 on failure.
190: */
191: static int
192: cvs_resp_validreq(struct cvsroot *root, int type, char *line)
193: {
194: char *sp, *ep;
195: struct cvs_req *req;
196:
197: /* parse the requests */
198: sp = line;
199: do {
200: ep = strchr(sp, ' ');
201: if (ep != NULL)
202: *ep = '\0';
203:
204: req = cvs_req_getbyname(sp);
205: if (req != NULL)
206: CVS_SETVR(root, req->req_id);
207:
208: if (ep != NULL)
209: sp = ep + 1;
210: } while (ep != NULL);
211:
212: return (0);
213: }
214:
215:
216: /*
217: * cvs_resp_m()
218: *
219: * Handler for the `M', 'MT', `F' and `E' responses.
220: */
221: static int
222: cvs_resp_m(struct cvsroot *root, int type, char *line)
223: {
224: char *cp;
225: FILE *stream;
226:
227: stream = NULL;
228:
229: switch (type) {
230: case CVS_RESP_F:
231: fflush(stderr);
232: return (0);
233: case CVS_RESP_M:
234: if (cvs_version_sent) {
235: /*
236: * Instead of outputting the line, we save it as the
237: * remote server's version string.
238: */
239: cvs_version_sent = 0;
240: root->cr_version = strdup(line);
241: return (0);
242: }
243: stream = stdout;
244: break;
245: case CVS_RESP_E:
246: stream = stderr;
247: break;
248: case CVS_RESP_MT:
249: if (*line == '+') {
250: if (cvs_mtstk_depth == CVS_MTSTK_MAXDEPTH) {
251: cvs_log(LP_ERR,
252: "MT scope stack has reached max depth");
253: return (-1);
254: }
255: cvs_mt_stack[cvs_mtstk_depth] = strdup(line + 1);
256: if (cvs_mt_stack[cvs_mtstk_depth] == NULL)
257: return (-1);
258: cvs_mtstk_depth++;
1.11 deraadt 259: } else if (*line == '-') {
1.1 jfb 260: if (cvs_mtstk_depth == 0) {
261: cvs_log(LP_ERR, "MT scope stack underflow");
262: return (-1);
1.11 deraadt 263: } else if (strcmp(line + 1,
1.1 jfb 264: cvs_mt_stack[cvs_mtstk_depth - 1]) != 0) {
265: cvs_log(LP_ERR, "mismatch in MT scope stack");
266: return (-1);
267: }
268: free(cvs_mt_stack[cvs_mtstk_depth--]);
1.11 deraadt 269: } else {
1.1 jfb 270: if (strcmp(line, "newline") == 0)
271: putc('\n', stdout);
272: else if (strncmp(line, "fname ", 6) == 0)
273: printf("%s", line + 6);
274: else {
275: /* assume text */
276: cp = strchr(line, ' ');
277: if (cp != NULL)
278: printf("%s", cp + 1);
279: }
280: }
281:
282: return (0);
283: case CVS_RESP_MBINARY:
284: cvs_log(LP_WARN, "Mbinary not supported in client yet");
285: break;
286: }
287:
288: fputs(line, stream);
289: fputc('\n', stream);
290:
291: return (0);
292: }
293:
294:
295: /*
296: * cvs_resp_ok()
297: *
1.13 tedu 298: * Handler for the `ok' response. This handler's job is to
1.1 jfb 299: */
300: static int
301: cvs_resp_ok(struct cvsroot *root, int type, char *line)
302: {
1.42 joris 303: /*
304: * If we still have an Entry file open, close it now.
305: */
306: if (cvs_resp_lastent != NULL)
307: cvs_ent_close(cvs_resp_lastent);
1.13 tedu 308:
1.1 jfb 309: return (1);
310: }
311:
312:
313: /*
314: * cvs_resp_error()
315: *
1.13 tedu 316: * Handler for the `error' response. This handler's job is to
1.31 joris 317: * show the error message given by the server.
1.1 jfb 318: */
319: static int
320: cvs_resp_error(struct cvsroot *root, int type, char *line)
321: {
1.33 joris 322: if (line == NULL)
323: return (1);
1.31 joris 324:
325: /* XXX - GNU cvs sends an empty error message
326: * at the end of the diff command, even for successfull
327: * diff.
328: */
329: if ((strlen(line) == 1) && (*line == ' '))
330: return (1);
1.13 tedu 331:
1.7 jfb 332: fprintf(stderr, "%s\n", line);
1.1 jfb 333: return (1);
334: }
335:
336:
337: /*
338: * cvs_resp_statdir()
339: *
340: * Handler for the `Clear-static-directory' and `Set-static-directory'
341: * responses.
342: */
343: static int
344: cvs_resp_statdir(struct cvsroot *root, int type, char *line)
345: {
1.24 jfb 346: int fd, len;
1.1 jfb 347: char rpath[MAXPATHLEN], statpath[MAXPATHLEN];
348:
1.3 jfb 349: /* remote directory line */
350: if (cvs_getln(root, rpath, sizeof(rpath)) < 0)
351: return (-1);
1.1 jfb 352:
1.36 jfb 353: STRIP_SLASH(line);
354:
1.47 joris 355: /*
356: * Create the directory if it does not exist.
357: */
358: if (cvs_resp_createdir(line) < 0)
359: return (-1);
1.36 jfb 360:
1.24 jfb 361: len = snprintf(statpath, sizeof(statpath), "%s/%s", line,
1.1 jfb 362: CVS_PATH_STATICENTRIES);
1.24 jfb 363: if (len == -1 || len >= (int)sizeof(statpath)) {
364: cvs_log(LP_ERR,
365: "path overflow for Entries.static specification");
366: return (-1);
367: }
1.1 jfb 368:
1.48 ! xsa 369: if (cvs_noexec == 0) {
1.35 xsa 370: if ((type == CVS_RESP_CLRSTATDIR) &&
371: (unlink(statpath) == -1) && (errno != ENOENT)) {
372: cvs_log(LP_ERRNO, "failed to unlink %s file",
373: CVS_PATH_STATICENTRIES);
1.1 jfb 374: return (-1);
1.35 xsa 375: } else if (type == CVS_RESP_SETSTATDIR) {
1.40 joris 376: fd = open(statpath, O_CREAT|O_TRUNC|O_WRONLY, 0644);
1.35 xsa 377: if (fd == -1) {
378: cvs_log(LP_ERRNO,
379: "failed to set static directory on %s",
380: line);
381: return (-1);
382: }
383: (void)close(fd);
384:
1.1 jfb 385: }
386: }
387:
388: return (0);
389: }
390:
391: /*
392: * cvs_resp_sticky()
393: *
1.4 jfb 394: * Handler for the `Clear-sticky' and `Set-sticky' responses. If the
1.47 joris 395: * specified directory doesn't exist, we create it.
1.1 jfb 396: */
397: static int
398: cvs_resp_sticky(struct cvsroot *root, int type, char *line)
399: {
1.47 joris 400: char buf[MAXPATHLEN];
1.4 jfb 401:
402: /* get the remote path */
1.5 jfb 403: if (cvs_getln(root, buf, sizeof(buf)) < 0)
1.4 jfb 404: return (-1);
1.1 jfb 405:
1.4 jfb 406: STRIP_SLASH(line);
1.36 jfb 407:
1.47 joris 408: if (cvs_resp_createdir(line) < 0)
409: return (-1);
410:
411: return (0);
412: }
413:
414: /*
415: * Shared code for cvs_resp[static, sticky]
416: *
417: * Looks if the directory requested exists, if it doesn't it will
418: * create it plus all administrative files as well.
419: */
420: static int
421: cvs_resp_createdir(char *line)
422: {
423: CVSFILE *base, *cf;
424: CVSENTRIES *entf;
425: struct stat st;
426: struct cvs_ent *ent;
427: char *file, subdir[MAXPATHLEN], buf[MAXPATHLEN];
428:
1.4 jfb 429: cvs_splitpath(line, subdir, sizeof(subdir), &file);
1.47 joris 430: base = cvs_file_loadinfo(subdir, CF_NOFILES, NULL, NULL, 1);
431: if (base == NULL)
1.4 jfb 432: return (-1);
1.1 jfb 433:
1.47 joris 434: /*
435: * If <line> doesn't exist, we create it.
436: */
437: if (stat(line, &st) == -1) {
438: if (errno != ENOENT) {
439: cvs_log(LP_ERRNO, "failed to stat '%s'", line);
1.19 jfb 440: return (-1);
441: }
1.1 jfb 442:
1.47 joris 443: cf = cvs_file_create(base, line, DT_DIR, 0755);
444: if (cf == NULL) {
445: cvs_file_free(base);
1.22 joris 446: return (-1);
447: }
1.5 jfb 448:
1.44 joris 449: /*
450: * If the Entries file for the parent is already
451: * open, operate on that, instead of reopening it
452: * and invalidating the opened list.
453: */
454: if (!strcmp(subdir, cvs_resp_lastdir))
455: entf = cvs_resp_lastent;
456: else
457: entf = cvs_ent_open(subdir, O_WRONLY);
458:
1.5 jfb 459: /* add a directory entry to the parent */
1.44 joris 460: if (entf != NULL) {
1.46 joris 461: if ((ent = cvs_ent_get(entf, cf->cf_name)) == NULL) {
1.40 joris 462: snprintf(buf, sizeof(buf), "D/%s////",
1.46 joris 463: cf->cf_name);
1.40 joris 464: ent = cvs_ent_parse(buf);
465: if (ent == NULL)
466: cvs_log(LP_ERR,
467: "failed to create directory entry");
468: else
469: cvs_ent_add(entf, ent);
470: }
1.44 joris 471:
472: if (strcmp(subdir, cvs_resp_lastdir))
473: cvs_ent_close(entf);
1.5 jfb 474: }
1.47 joris 475:
476: cvs_file_free(cf);
1.1 jfb 477: }
478:
1.47 joris 479: cvs_file_free(base);
1.1 jfb 480: return (0);
481: }
482:
483:
484: /*
485: * cvs_resp_newentry()
486: *
487: * Handler for the `New-entry' response and `Checked-in' responses.
1.9 jfb 488: * In the case of `New-entry', we expect the entry line
1.1 jfb 489: */
490: static int
491: cvs_resp_newentry(struct cvsroot *root, int type, char *line)
492: {
493: char entbuf[128];
1.9 jfb 494: struct cvs_ent *ent;
1.1 jfb 495:
496: /* get the remote path */
1.43 joris 497: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
498: return (-1);
1.1 jfb 499:
500: /* get the new Entries line */
501: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
502: return (-1);
503:
1.42 joris 504: if (resp_check_dir(line) < 0)
1.1 jfb 505: return (-1);
1.42 joris 506:
1.9 jfb 507: if (type == CVS_RESP_NEWENTRY) {
1.42 joris 508: cvs_ent_addln(cvs_resp_lastent, entbuf);
1.11 deraadt 509: } else if (type == CVS_RESP_CHECKEDIN) {
1.9 jfb 510: ent = cvs_ent_parse(entbuf);
511: if (ent == NULL) {
512: cvs_log(LP_ERR, "failed to parse entry");
513: return (-1);
514: }
515:
516: /* timestamp it to now */
517: ent->ce_mtime = time(&(ent->ce_mtime));
518:
519: /* replace the current entry with the one we just received */
1.42 joris 520: (void)cvs_ent_remove(cvs_resp_lastent, ent->ce_name);
1.9 jfb 521:
1.42 joris 522: cvs_ent_add(cvs_resp_lastent, ent);
1.9 jfb 523: }
1.1 jfb 524:
525: return (0);
526: }
527:
528:
529: /*
530: * cvs_resp_cksum()
531: *
532: * Handler for the `Checksum' response. We store the checksum received for
533: * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
534: * Upon next file reception, the handler checks to see if there is a stored
535: * checksum.
536: * The file handler must make sure that the checksums match and free the
537: * checksum buffer once it's done to indicate there is no further checksum.
538: */
539: static int
540: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
541: {
542: if (cvs_fcksum != NULL) {
543: cvs_log(LP_WARN, "unused checksum");
544: free(cvs_fcksum);
545: }
546:
547: cvs_fcksum = strdup(line);
548: if (cvs_fcksum == NULL) {
549: cvs_log(LP_ERRNO, "failed to copy checksum string");
1.14 jfb 550: return (-1);
551: }
552:
553: return (0);
554: }
555:
556:
557: /*
558: * cvs_resp_copyfile()
559: *
560: * Handler for the `Copy-file' response, which is used to copy the contents
561: * of a file to another file for which the name is provided. The CVS protocol
562: * documentation states that this response is only used prior to a `Merged'
563: * response to create a backup of the file.
564: */
565: static int
566: cvs_resp_copyfile(struct cvsroot *root, int type, char *line)
567: {
1.24 jfb 568: int len;
1.16 jfb 569: char path[MAXPATHLEN], newpath[MAXPATHLEN], newname[MAXNAMLEN], *file;
1.14 jfb 570:
1.16 jfb 571: /* read the remote path of the file to copy and its new name */
572: if ((cvs_getln(root, path, sizeof(path)) < 0) ||
573: (cvs_getln(root, newname, sizeof(newname)) < 0))
1.14 jfb 574: return (-1);
575:
1.16 jfb 576: if ((file = basename(path)) == NULL) {
577: cvs_log(LP_ERR, "no base file name in Copy-file path");
578: return (-1);
579: }
580:
1.24 jfb 581: len = snprintf(path, sizeof(path), "%s%s", line, file);
582: if (len == -1 || len >= (int)sizeof(path)) {
583: cvs_log(LP_ERR, "source path overflow in Copy-file response");
584: return (-1);
585: }
586: len = snprintf(newpath, sizeof(newpath), "%s%s", line, newname);
587: if (len == -1 || len >= (int)sizeof(path)) {
588: cvs_log(LP_ERR,
589: "destination path overflow in Copy-file response");
590: return (-1);
591: }
1.16 jfb 592:
593: if (rename(path, newpath) == -1) {
1.27 xsa 594: cvs_log(LP_ERRNO, "failed to rename %s to %s", path, newpath);
1.1 jfb 595: return (-1);
596: }
597:
598: return (0);
599: }
600:
601:
602: /*
603: * cvs_resp_modtime()
604: *
605: * Handler for the `Mod-time' file update modifying response. The timestamp
606: * given is used to set the last modification time on the next file that
607: * will be received.
608: */
609: static int
610: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
611: {
1.34 jfb 612: cvs_modtime = cvs_date_parse(line);
1.1 jfb 613: return (0);
614: }
615:
616:
617: /*
618: * cvs_resp_updated()
619: *
1.4 jfb 620: * Handler for the `Updated', `Update-existing', `Created', `Merged' and
621: * `Patched' responses, which all have a very similar format.
1.1 jfb 622: */
623: static int
624: cvs_resp_updated(struct cvsroot *root, int type, char *line)
625: {
1.17 jfb 626: int ret;
1.1 jfb 627: mode_t fmode;
1.4 jfb 628: char path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.1 jfb 629: BUF *fbuf;
1.45 xsa 630: struct cvs_ent *ent;
1.2 jfb 631: struct timeval tv[2];
1.1 jfb 632:
1.4 jfb 633: STRIP_SLASH(line);
634:
1.1 jfb 635: /* read the remote path of the file */
1.4 jfb 636: if (cvs_getln(root, path, sizeof(path)) < 0)
637: return (-1);
1.1 jfb 638:
639: /* read the new entry */
1.4 jfb 640: if (cvs_getln(root, path, sizeof(path)) < 0)
1.1 jfb 641: return (-1);
1.4 jfb 642:
1.45 xsa 643: if ((ent = cvs_ent_parse(path)) == NULL)
1.4 jfb 644: return (-1);
1.45 xsa 645: ret = snprintf(path, sizeof(path), "%s/%s", line, ent->ce_name);
1.24 jfb 646: if (ret == -1 || ret >= (int)sizeof(path)) {
647: cvs_log(LP_ERR, "Entries path overflow in response");
648: return (-1);
649: }
1.25 joris 650: ret = 0;
1.1 jfb 651:
1.42 joris 652: if (resp_check_dir(line) < 0)
1.12 jfb 653: return (-1);
654:
1.15 jfb 655: if (cvs_modtime != CVS_DATE_DMSEC) {
1.45 xsa 656: ent->ce_mtime = cvs_modtime;
1.15 jfb 657: } else
1.45 xsa 658: ent->ce_mtime = time(&(ent->ce_mtime));
1.15 jfb 659:
660: if ((type == CVS_RESP_UPDEXIST) || (type == CVS_RESP_UPDATED) ||
1.18 jfb 661: (type == CVS_RESP_MERGED) || (type == CVS_RESP_CREATED)) {
1.45 xsa 662: if ((cvs_ent_remove(cvs_resp_lastent, ent->ce_name) < 0) &&
1.30 joris 663: (type != CVS_RESP_CREATED)) {
664: cvs_log(LP_WARN, "failed to remove entry for '%s`",
1.45 xsa 665: ent->ce_name);
1.30 joris 666: }
1.15 jfb 667: }
1.12 jfb 668:
1.45 xsa 669: if (cvs_ent_add(cvs_resp_lastent, ent) < 0) {
670: cvs_ent_free(ent);
1.15 jfb 671: return (-1);
1.1 jfb 672: }
1.15 jfb 673:
1.17 jfb 674: if ((fbuf = cvs_recvfile(root, &fmode)) == NULL)
675: return (-1);
676: if (cvs_buf_write(fbuf, path, fmode) < 0) {
677: cvs_buf_free(fbuf);
1.1 jfb 678: return (-1);
1.17 jfb 679: }
680: cvs_buf_free(fbuf);
1.2 jfb 681:
1.12 jfb 682: if (cvs_modtime != CVS_DATE_DMSEC) {
683: tv[0].tv_sec = (long)cvs_modtime;
684: tv[0].tv_usec = 0;
685: tv[1].tv_sec = (long)cvs_modtime;
686: tv[1].tv_usec = 0;
687: if (utimes(path, tv) == -1)
688: cvs_log(LP_ERRNO, "failed to set file timestamps");
689: }
1.34 jfb 690:
691: /* invalidate last received timestamp */
692: cvs_modtime = CVS_DATE_DMSEC;
1.1 jfb 693:
694: /* now see if there is a checksum */
695: if (cvs_fcksum != NULL) {
1.17 jfb 696: if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0)
697: ret = -1;
698: else if (strcmp(cksum_buf, cvs_fcksum) != 0) {
1.1 jfb 699: cvs_log(LP_ERR, "checksum error on received file");
700: (void)unlink(line);
1.17 jfb 701: ret = -1;
1.1 jfb 702: }
703:
704: free(cvs_fcksum);
705: cvs_fcksum = NULL;
706: }
707:
1.17 jfb 708: return (ret);
1.1 jfb 709: }
710:
711:
712: /*
713: * cvs_resp_removed()
714: *
1.10 jfb 715: * Handler for the `Removed' and `Remove-entry' responses. The `Removed'
716: * response is received when both a file and its entry need to be removed from
717: * the local copy. The `Remove-entry' is received in cases where the file is
718: * already gone but there is still an entry to remove in the Entries file.
1.1 jfb 719: */
720: static int
721: cvs_resp_removed(struct cvsroot *root, int type, char *line)
722: {
1.29 joris 723: int l;
724: char buf[MAXPATHLEN], base[MAXPATHLEN], fpath[MAXPATHLEN], *file;
1.1 jfb 725:
1.29 joris 726: if (cvs_getln(root, buf, sizeof(buf)) < 0)
727: return (-1);
728:
729: cvs_splitpath(buf, base, sizeof(base), &file);
730: l = snprintf(fpath, sizeof(fpath), "%s/%s", line, file);
731: if (l == -1 || l >= (int)sizeof(fpath)) {
732: errno = ENAMETOOLONG;
733: cvs_log(LP_ERRNO, "%s", fpath);
734: return (-1);
735: }
736:
1.42 joris 737: if (resp_check_dir(line) < 0)
738: return (-1);
1.10 jfb 739:
1.42 joris 740: (void)cvs_ent_remove(cvs_resp_lastent, file);
1.32 joris 741: if ((type == CVS_RESP_REMOVED) && ((unlink(fpath) == -1) &&
742: errno != ENOENT)) {
1.29 joris 743: cvs_log(LP_ERRNO, "failed to unlink `%s'", file);
1.1 jfb 744: return (-1);
1.10 jfb 745: }
1.1 jfb 746:
747: return (0);
748: }
749:
750:
751: /*
752: * cvs_resp_mode()
753: *
754: * Handler for the `Mode' response.
755: */
756: static int
757: cvs_resp_mode(struct cvsroot *root, int type, char *line)
758: {
759: if (cvs_strtomode(line, &cvs_lastmode) < 0) {
1.12 jfb 760: cvs_log(LP_ERR, "error handling Mode response");
1.1 jfb 761: return (-1);
762: }
763: return (0);
764: }
765:
766:
767: /*
768: * cvs_resp_modxpand()
769: *
770: * Handler for the `Module-expansion' response.
771: */
772: static int
773: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
774: {
775: return (0);
776: }
777:
778: /*
779: * cvs_resp_rcsdiff()
780: *
781: * Handler for the `Rcs-diff' response.
782: */
783: static int
784: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
785: {
1.24 jfb 786: int len;
1.1 jfb 787: char file[MAXPATHLEN], buf[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
788: char *fname, *orig, *patch;
789: mode_t fmode;
790: BUF *res, *fcont, *patchbuf;
791: CVSENTRIES *entf;
792: struct cvs_ent *ent;
793:
794: /* get remote path and build local path of file to be patched */
1.43 joris 795: if (cvs_getln(root, buf, sizeof(buf)) < 0)
796: return (-1);
797:
1.1 jfb 798: fname = strrchr(buf, '/');
799: if (fname == NULL)
800: fname = buf;
1.24 jfb 801: len = snprintf(file, sizeof(file), "%s%s", line, fname);
802: if (len == -1 || len >= (int)sizeof(file)) {
803: cvs_log(LP_ERR, "path overflow in Rcs-diff response");
804: return (-1);
805: }
1.1 jfb 806:
807: /* get updated entry fields */
1.43 joris 808: if (cvs_getln(root, buf, sizeof(buf)) < 0)
809: return (-1);
810:
1.1 jfb 811: ent = cvs_ent_parse(buf);
1.13 tedu 812: if (ent == NULL)
1.1 jfb 813: return (-1);
814:
815: patchbuf = cvs_recvfile(root, &fmode);
816: fcont = cvs_buf_load(file, BUF_AUTOEXT);
817: if (fcont == NULL)
818: return (-1);
819:
820: cvs_buf_putc(patchbuf, '\0');
821: cvs_buf_putc(fcont, '\0');
822: orig = cvs_buf_release(fcont);
823: patch = cvs_buf_release(patchbuf);
824:
825: res = rcs_patch(orig, patch);
826: if (res == NULL)
827: return (-1);
828:
829: cvs_buf_write(res, file, fmode);
830:
831: /* now see if there is a checksum */
832: if (cvs_fcksum != NULL) {
833: if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
834: }
835:
836: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
837: cvs_log(LP_ERR, "checksum error on received file");
838: (void)unlink(file);
839: }
840:
841: free(cvs_fcksum);
842: cvs_fcksum = NULL;
843: }
844:
845: /* update revision in entries */
846: entf = cvs_ent_open(line, O_WRONLY);
847: if (entf == NULL)
848: return (-1);
849:
850: cvs_ent_close(entf);
851:
852: return (0);
853: }
854:
855:
856: /*
857: * cvs_resp_template()
858: *
859: * Handler for the `Template' response.
860: */
861: static int
862: cvs_resp_template(struct cvsroot *root, int type, char *line)
863: {
864: mode_t mode;
865: BUF *tmpl;
866:
867: tmpl = cvs_recvfile(root, &mode);
868: if (tmpl == NULL)
869: return (-1);
1.42 joris 870:
871: return (0);
872: }
873:
874: /*
875: * Check if <dir> is the same as the last
876: * received directory, if it's not, switch Entry files.
877: */
878: static int
879: resp_check_dir(const char *dir)
880: {
881: size_t len;
882:
883: if (strcmp(dir, cvs_resp_lastdir)) {
884: if (cvs_resp_lastent != NULL)
885: cvs_ent_close(cvs_resp_lastent);
886: cvs_resp_lastent = cvs_ent_open(dir, O_WRONLY);
887: if (cvs_resp_lastent == NULL)
888: return (-1);
889:
890: len = strlcpy(cvs_resp_lastdir, dir, sizeof(cvs_resp_lastdir));
891: if (len >= sizeof(cvs_resp_lastdir)) {
892: errno = ENAMETOOLONG;
893: cvs_log(LP_ERRNO, "%s", cvs_resp_lastdir);
894: return (-1);
895: }
896: } else {
897: /* make sure the old one is still open */
898: if (cvs_resp_lastent == NULL) {
899: cvs_resp_lastent = cvs_ent_open(cvs_resp_lastdir,
900: O_WRONLY);
901: if (cvs_resp_lastent == NULL)
902: return (-1);
903: }
904: }
1.1 jfb 905:
906: return (0);
907: }