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