Annotation of src/usr.bin/cvs/resp.c, Revision 1.67
1.67 ! joris 1: /* $OpenBSD: resp.c,v 1.66 2005/12/29 21:28:26 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:
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 */
1.67 ! joris 339: cvs_getln(root, rpath, sizeof(rpath));
1.1 jfb 340:
1.36 jfb 341: STRIP_SLASH(line);
342:
1.47 joris 343: /*
344: * Create the directory if it does not exist.
345: */
346: if (cvs_resp_createdir(line) < 0)
347: return (-1);
1.36 jfb 348:
1.24 jfb 349: len = snprintf(statpath, sizeof(statpath), "%s/%s", line,
1.1 jfb 350: CVS_PATH_STATICENTRIES);
1.24 jfb 351: if (len == -1 || len >= (int)sizeof(statpath)) {
352: cvs_log(LP_ERR,
353: "path overflow for Entries.static specification");
354: return (-1);
355: }
1.1 jfb 356:
1.48 xsa 357: if (cvs_noexec == 0) {
1.35 xsa 358: if ((type == CVS_RESP_CLRSTATDIR) &&
1.51 xsa 359: (cvs_unlink(statpath) == -1)) {
1.1 jfb 360: return (-1);
1.35 xsa 361: } else if (type == CVS_RESP_SETSTATDIR) {
1.40 joris 362: fd = open(statpath, O_CREAT|O_TRUNC|O_WRONLY, 0644);
1.35 xsa 363: if (fd == -1) {
364: cvs_log(LP_ERRNO,
365: "failed to set static directory on %s",
366: line);
367: return (-1);
368: }
369: (void)close(fd);
370:
1.1 jfb 371: }
372: }
373:
374: return (0);
375: }
376:
377: /*
378: * cvs_resp_sticky()
379: *
1.4 jfb 380: * Handler for the `Clear-sticky' and `Set-sticky' responses. If the
1.47 joris 381: * specified directory doesn't exist, we create it.
1.1 jfb 382: */
383: static int
384: cvs_resp_sticky(struct cvsroot *root, int type, char *line)
385: {
1.47 joris 386: char buf[MAXPATHLEN];
1.4 jfb 387:
388: /* get the remote path */
1.67 ! joris 389: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 390:
1.4 jfb 391: STRIP_SLASH(line);
1.36 jfb 392:
1.47 joris 393: if (cvs_resp_createdir(line) < 0)
394: return (-1);
395:
396: return (0);
397: }
398:
399: /*
400: * Shared code for cvs_resp[static, sticky]
401: *
402: * Looks if the directory requested exists, if it doesn't it will
403: * create it plus all administrative files as well.
404: */
405: static int
406: cvs_resp_createdir(char *line)
407: {
1.53 joris 408: int l;
1.47 joris 409: CVSFILE *base, *cf;
410: CVSENTRIES *entf;
411: struct stat st;
412: struct cvs_ent *ent;
1.56 xsa 413: char *file, subdir[MAXPATHLEN], buf[CVS_ENT_MAXLINELEN];
1.47 joris 414:
1.52 joris 415: entf = NULL;
416: cf = NULL;
1.54 joris 417:
418: /*
419: * we do not want to handle the '.' case,
420: * so return early.
421: */
422: if (!strcmp(line, "."))
423: return (0);
424:
1.4 jfb 425: cvs_splitpath(line, subdir, sizeof(subdir), &file);
1.47 joris 426: base = cvs_file_loadinfo(subdir, CF_NOFILES, NULL, NULL, 1);
427: if (base == NULL)
1.4 jfb 428: return (-1);
1.1 jfb 429:
1.47 joris 430: /*
431: * If <line> doesn't exist, we create it.
432: */
433: if (stat(line, &st) == -1) {
434: if (errno != ENOENT) {
1.62 xsa 435: cvs_log(LP_ERRNO, "failed to stat `%s'", line);
1.19 jfb 436: return (-1);
437: }
1.1 jfb 438:
1.47 joris 439: cf = cvs_file_create(base, line, DT_DIR, 0755);
1.52 joris 440: } else {
441: cf = cvs_file_loadinfo(line, CF_NOFILES, NULL, NULL, 1);
442: }
443:
444: if (cf == NULL) {
445: cvs_file_free(base);
446: return (-1);
447: }
448:
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);
1.5 jfb 458:
1.52 joris 459: /*
460: * see if the entry is still present. If not, we add it again.
461: */
462: if (entf != NULL) {
463: if ((ent = cvs_ent_get(entf, cf->cf_name)) == NULL) {
464: l = snprintf(buf, sizeof(buf), "D/%s////", cf->cf_name);
465: if (l == -1 || l >= (int)sizeof(buf)) {
466: cvs_file_free(cf);
467: cvs_file_free(base);
468: return (-1);
1.40 joris 469: }
1.44 joris 470:
1.52 joris 471: ent = cvs_ent_parse(buf);
472: if (ent == NULL)
473: cvs_log(LP_ERR,
474: "failed to create directory entry");
475: else
476: cvs_ent_add(entf, ent);
1.5 jfb 477: }
1.47 joris 478:
1.52 joris 479: if (strcmp(subdir, cvs_resp_lastdir))
480: cvs_ent_close(entf);
1.1 jfb 481: }
482:
1.52 joris 483: cvs_file_free(cf);
1.47 joris 484: cvs_file_free(base);
1.1 jfb 485: return (0);
486: }
487:
488:
489: /*
490: * cvs_resp_newentry()
491: *
492: * Handler for the `New-entry' response and `Checked-in' responses.
1.9 jfb 493: * In the case of `New-entry', we expect the entry line
1.1 jfb 494: */
495: static int
496: cvs_resp_newentry(struct cvsroot *root, int type, char *line)
497: {
1.55 xsa 498: char entbuf[CVS_ENT_MAXLINELEN];
1.9 jfb 499: struct cvs_ent *ent;
1.1 jfb 500:
501: /* get the remote path */
1.67 ! joris 502: cvs_getln(root, entbuf, sizeof(entbuf));
1.1 jfb 503:
504: /* get the new Entries line */
1.67 ! joris 505: cvs_getln(root, entbuf, sizeof(entbuf));
1.1 jfb 506:
1.52 joris 507: if (resp_check_dir(root, line) < 0)
1.1 jfb 508: return (-1);
1.42 joris 509:
1.9 jfb 510: if (type == CVS_RESP_NEWENTRY) {
1.42 joris 511: cvs_ent_addln(cvs_resp_lastent, entbuf);
1.11 deraadt 512: } else if (type == CVS_RESP_CHECKEDIN) {
1.9 jfb 513: ent = cvs_ent_parse(entbuf);
514: if (ent == NULL) {
515: cvs_log(LP_ERR, "failed to parse entry");
516: return (-1);
517: }
518:
519: /* timestamp it to now */
520: ent->ce_mtime = time(&(ent->ce_mtime));
521:
522: /* replace the current entry with the one we just received */
1.63 joris 523: (void)cvs_ent_remove(cvs_resp_lastent, ent->ce_name, 0);
1.9 jfb 524:
1.42 joris 525: cvs_ent_add(cvs_resp_lastent, ent);
1.9 jfb 526: }
1.1 jfb 527:
528: return (0);
529: }
530:
531:
532: /*
533: * cvs_resp_cksum()
534: *
535: * Handler for the `Checksum' response. We store the checksum received for
536: * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
537: * Upon next file reception, the handler checks to see if there is a stored
538: * checksum.
539: * The file handler must make sure that the checksums match and free the
540: * checksum buffer once it's done to indicate there is no further checksum.
541: */
542: static int
543: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
544: {
545: if (cvs_fcksum != NULL) {
546: cvs_log(LP_WARN, "unused checksum");
1.64 joris 547: xfree(cvs_fcksum);
1.1 jfb 548: }
549:
1.64 joris 550: cvs_fcksum = xstrdup(line);
1.14 jfb 551:
552: return (0);
553: }
554:
555:
556: /*
557: * cvs_resp_copyfile()
558: *
559: * Handler for the `Copy-file' response, which is used to copy the contents
560: * of a file to another file for which the name is provided. The CVS protocol
561: * documentation states that this response is only used prior to a `Merged'
562: * response to create a backup of the file.
563: */
564: static int
565: cvs_resp_copyfile(struct cvsroot *root, int type, char *line)
566: {
1.24 jfb 567: int len;
1.67 ! joris 568: char path[MAXPATHLEN], newpath[MAXPATHLEN];
! 569: char newname[MAXNAMLEN], *file;
1.14 jfb 570:
1.16 jfb 571: /* read the remote path of the file to copy and its new name */
1.67 ! joris 572: cvs_getln(root, path, sizeof(path));
! 573: cvs_getln(root, newname, sizeof(newname));
1.14 jfb 574:
1.67 ! joris 575: if ((file = basename(path)) == NULL)
! 576: fatal("no base file name in Copy-file path");
1.16 jfb 577:
1.24 jfb 578: len = snprintf(path, sizeof(path), "%s%s", line, file);
1.67 ! joris 579: if (len == -1 || len >= (int)sizeof(path))
! 580: fatal("source path overflow in Copy-file response");
! 581:
1.24 jfb 582: len = snprintf(newpath, sizeof(newpath), "%s%s", line, newname);
1.67 ! joris 583: if (len == -1 || len >= (int)sizeof(path))
! 584: fatal("destination path overflow in Copy-file response");
1.16 jfb 585:
586: if (rename(path, newpath) == -1) {
1.67 ! joris 587: fatal("failed to rename %s to %s: %s",
! 588: path, newpath, strerror(errno));
1.1 jfb 589: }
590:
591: return (0);
592: }
593:
594:
595: /*
596: * cvs_resp_modtime()
597: *
598: * Handler for the `Mod-time' file update modifying response. The timestamp
599: * given is used to set the last modification time on the next file that
600: * will be received.
601: */
602: static int
603: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
604: {
1.34 jfb 605: cvs_modtime = cvs_date_parse(line);
1.1 jfb 606: return (0);
607: }
608:
609:
610: /*
611: * cvs_resp_updated()
612: *
1.4 jfb 613: * Handler for the `Updated', `Update-existing', `Created', `Merged' and
614: * `Patched' responses, which all have a very similar format.
1.1 jfb 615: */
616: static int
617: cvs_resp_updated(struct cvsroot *root, int type, char *line)
618: {
1.17 jfb 619: int ret;
1.1 jfb 620: mode_t fmode;
1.4 jfb 621: char path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.1 jfb 622: BUF *fbuf;
1.45 xsa 623: struct cvs_ent *ent;
1.2 jfb 624: struct timeval tv[2];
1.1 jfb 625:
1.4 jfb 626: STRIP_SLASH(line);
627:
1.1 jfb 628: /* read the remote path of the file */
1.67 ! joris 629: cvs_getln(root, path, sizeof(path));
1.1 jfb 630:
631: /* read the new entry */
1.67 ! joris 632: cvs_getln(root, path, sizeof(path));
1.4 jfb 633:
1.45 xsa 634: if ((ent = cvs_ent_parse(path)) == NULL)
1.4 jfb 635: return (-1);
1.67 ! joris 636:
1.45 xsa 637: ret = snprintf(path, sizeof(path), "%s/%s", line, ent->ce_name);
1.67 ! joris 638: if (ret == -1 || ret >= (int)sizeof(path))
! 639: fatal("Entries path overflow in response");
! 640:
1.25 joris 641: ret = 0;
1.1 jfb 642:
1.52 joris 643: /*
644: * Please be sure the directory does exist.
645: */
646: if (cvs_resp_createdir(line) < 0)
647: return (-1);
648:
649: if (resp_check_dir(root, line) < 0)
1.12 jfb 650: return (-1);
651:
1.15 jfb 652: if (cvs_modtime != CVS_DATE_DMSEC) {
1.45 xsa 653: ent->ce_mtime = cvs_modtime;
1.15 jfb 654: } else
1.45 xsa 655: ent->ce_mtime = time(&(ent->ce_mtime));
1.15 jfb 656:
657: if ((type == CVS_RESP_UPDEXIST) || (type == CVS_RESP_UPDATED) ||
1.18 jfb 658: (type == CVS_RESP_MERGED) || (type == CVS_RESP_CREATED)) {
1.63 joris 659: if ((cvs_ent_remove(cvs_resp_lastent, ent->ce_name, 0) < 0) &&
1.30 joris 660: (type != CVS_RESP_CREATED)) {
661: cvs_log(LP_WARN, "failed to remove entry for '%s`",
1.45 xsa 662: ent->ce_name);
1.30 joris 663: }
1.15 jfb 664: }
1.12 jfb 665:
1.67 ! joris 666: cvs_ent_add(cvs_resp_lastent, ent);
1.15 jfb 667:
1.67 ! joris 668: fbuf = cvs_recvfile(root, &fmode);
1.65 xsa 669: cvs_buf_write(fbuf, path, fmode);
1.17 jfb 670: cvs_buf_free(fbuf);
1.2 jfb 671:
1.12 jfb 672: if (cvs_modtime != CVS_DATE_DMSEC) {
673: tv[0].tv_sec = (long)cvs_modtime;
674: tv[0].tv_usec = 0;
675: tv[1].tv_sec = (long)cvs_modtime;
676: tv[1].tv_usec = 0;
677: if (utimes(path, tv) == -1)
678: cvs_log(LP_ERRNO, "failed to set file timestamps");
679: }
1.34 jfb 680:
681: /* invalidate last received timestamp */
682: cvs_modtime = CVS_DATE_DMSEC;
1.1 jfb 683:
684: /* now see if there is a checksum */
685: if (cvs_fcksum != NULL) {
1.17 jfb 686: if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0)
687: ret = -1;
688: else if (strcmp(cksum_buf, cvs_fcksum) != 0) {
1.1 jfb 689: cvs_log(LP_ERR, "checksum error on received file");
690: (void)unlink(line);
1.17 jfb 691: ret = -1;
1.1 jfb 692: }
693:
1.64 joris 694: xfree(cvs_fcksum);
1.1 jfb 695: cvs_fcksum = NULL;
696: }
697:
1.17 jfb 698: return (ret);
1.1 jfb 699: }
700:
701:
702: /*
703: * cvs_resp_removed()
704: *
1.10 jfb 705: * Handler for the `Removed' and `Remove-entry' responses. The `Removed'
706: * response is received when both a file and its entry need to be removed from
707: * the local copy. The `Remove-entry' is received in cases where the file is
708: * already gone but there is still an entry to remove in the Entries file.
1.1 jfb 709: */
710: static int
711: cvs_resp_removed(struct cvsroot *root, int type, char *line)
712: {
1.29 joris 713: int l;
1.67 ! joris 714: char buf[MAXPATHLEN], base[MAXPATHLEN];
! 715: char fpath[MAXPATHLEN], *file;
1.1 jfb 716:
1.67 ! joris 717: cvs_getln(root, buf, sizeof(buf));
1.29 joris 718:
719: cvs_splitpath(buf, base, sizeof(base), &file);
720: l = snprintf(fpath, sizeof(fpath), "%s/%s", line, file);
1.67 ! joris 721: if (l == -1 || l >= (int)sizeof(fpath))
! 722: fatal("cvs_resp_removed: overflow in path");
1.29 joris 723:
1.52 joris 724: if (resp_check_dir(root, line) < 0)
1.42 joris 725: return (-1);
1.10 jfb 726:
1.63 joris 727: (void)cvs_ent_remove(cvs_resp_lastent, file, 0);
1.32 joris 728: if ((type == CVS_RESP_REMOVED) && ((unlink(fpath) == -1) &&
729: errno != ENOENT)) {
1.29 joris 730: cvs_log(LP_ERRNO, "failed to unlink `%s'", file);
1.1 jfb 731: return (-1);
1.10 jfb 732: }
1.1 jfb 733:
734: return (0);
735: }
736:
737:
738: /*
739: * cvs_resp_mode()
740: *
741: * Handler for the `Mode' response.
742: */
743: static int
744: cvs_resp_mode(struct cvsroot *root, int type, char *line)
745: {
1.67 ! joris 746: cvs_strtomode(line, &cvs_lastmode);
1.1 jfb 747: return (0);
748: }
749:
750:
751: /*
752: * cvs_resp_modxpand()
753: *
754: * Handler for the `Module-expansion' response.
755: */
756: static int
757: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
758: {
759: return (0);
760: }
761:
762: /*
763: * cvs_resp_rcsdiff()
764: *
765: * Handler for the `Rcs-diff' response.
766: */
767: static int
768: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
769: {
1.24 jfb 770: int len;
1.56 xsa 771: char file[MAXPATHLEN];
772: char buf[CVS_ENT_MAXLINELEN], cksum_buf[CVS_CKSUM_LEN];
1.1 jfb 773: char *fname, *orig, *patch;
774: mode_t fmode;
775: BUF *res, *fcont, *patchbuf;
776: CVSENTRIES *entf;
777: struct cvs_ent *ent;
778:
779: /* get remote path and build local path of file to be patched */
1.67 ! joris 780: cvs_getln(root, buf, sizeof(buf));
1.43 joris 781:
1.1 jfb 782: fname = strrchr(buf, '/');
783: if (fname == NULL)
784: fname = buf;
1.24 jfb 785: len = snprintf(file, sizeof(file), "%s%s", line, fname);
1.67 ! joris 786: if (len == -1 || len >= (int)sizeof(file))
! 787: fatal("path overflow in Rcs-diff response");
1.1 jfb 788:
789: /* get updated entry fields */
1.67 ! joris 790: cvs_getln(root, buf, sizeof(buf));
1.43 joris 791:
1.1 jfb 792: ent = cvs_ent_parse(buf);
1.13 tedu 793: if (ent == NULL)
1.1 jfb 794: return (-1);
795:
796: patchbuf = cvs_recvfile(root, &fmode);
797: fcont = cvs_buf_load(file, BUF_AUTOEXT);
798: if (fcont == NULL)
799: return (-1);
800:
801: cvs_buf_putc(patchbuf, '\0');
802: cvs_buf_putc(fcont, '\0');
803: orig = cvs_buf_release(fcont);
804: patch = cvs_buf_release(patchbuf);
805:
1.61 joris 806: res = cvs_patchfile(orig, patch, rcs_patch_lines);
1.1 jfb 807: if (res == NULL)
808: return (-1);
809:
810: cvs_buf_write(res, file, fmode);
811:
812: /* now see if there is a checksum */
813: if (cvs_fcksum != NULL) {
814: if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
815: }
816:
817: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
818: cvs_log(LP_ERR, "checksum error on received file");
819: (void)unlink(file);
820: }
821:
1.64 joris 822: xfree(cvs_fcksum);
1.1 jfb 823: cvs_fcksum = NULL;
824: }
825:
826: /* update revision in entries */
827: entf = cvs_ent_open(line, O_WRONLY);
828: if (entf == NULL)
829: return (-1);
830:
831: cvs_ent_close(entf);
832:
833: return (0);
834: }
835:
836:
837: /*
838: * cvs_resp_template()
839: *
840: * Handler for the `Template' response.
841: */
842: static int
843: cvs_resp_template(struct cvsroot *root, int type, char *line)
844: {
845: mode_t mode;
846: BUF *tmpl;
847:
848: tmpl = cvs_recvfile(root, &mode);
1.67 ! joris 849: cvs_buf_free(tmpl);
1.42 joris 850:
851: return (0);
852: }
853:
854: /*
855: * Check if <dir> is the same as the last
856: * received directory, if it's not, switch Entry files.
857: */
858: static int
1.52 joris 859: resp_check_dir(struct cvsroot *root, const char *dir)
1.42 joris 860: {
1.52 joris 861: int l;
1.42 joris 862: size_t len;
1.52 joris 863: char cvspath[MAXPATHLEN], repo[MAXPATHLEN];
864: struct stat st;
865:
866: /*
867: * Make sure the CVS directory exists.
868: */
869: l = snprintf(cvspath, sizeof(cvspath), "%s/%s", dir, CVS_PATH_CVSDIR);
870: if (l == -1 || l >= (int)sizeof(cvspath))
1.67 ! joris 871: fatal("resp_check_dir: path overflow");
1.52 joris 872:
873: if (stat(cvspath, &st) == -1) {
874: if (errno != ENOENT)
875: return (-1);
876: if (cvs_repo_base != NULL) {
877: l = snprintf(repo, sizeof(repo), "%s/%s", cvs_repo_base,
878: dir);
879: if (l == -1 || l >= (int)sizeof(repo))
1.67 ! joris 880: fatal("resp_check_dir: path overflow");
1.52 joris 881: } else {
1.67 ! joris 882: len = strlcpy(repo, dir, sizeof(repo));
! 883: if (len >= sizeof(repo))
! 884: fatal("resp_check_dir: path truncation");
1.52 joris 885: }
886:
1.58 xsa 887: if (cvs_mkadmin(dir, root->cr_str, repo, NULL, NULL, 0) < 0)
1.52 joris 888: return (-1);
889: }
1.42 joris 890:
891: if (strcmp(dir, cvs_resp_lastdir)) {
892: if (cvs_resp_lastent != NULL)
893: cvs_ent_close(cvs_resp_lastent);
894: cvs_resp_lastent = cvs_ent_open(dir, O_WRONLY);
895: if (cvs_resp_lastent == NULL)
896: return (-1);
897:
898: len = strlcpy(cvs_resp_lastdir, dir, sizeof(cvs_resp_lastdir));
1.67 ! joris 899: if (len >= sizeof(cvs_resp_lastdir))
! 900: fatal("resp_check_dir: path truncation");
1.42 joris 901: } else {
902: /* make sure the old one is still open */
903: if (cvs_resp_lastent == NULL) {
904: cvs_resp_lastent = cvs_ent_open(cvs_resp_lastdir,
905: O_WRONLY);
906: if (cvs_resp_lastent == NULL)
907: return (-1);
908: }
909: }
1.1 jfb 910:
911: return (0);
912: }