Annotation of src/usr.bin/cvs/resp.c, Revision 1.60
1.60 ! reyk 1: /* $OpenBSD: resp.c,v 1.59 2005/09/15 22:12:23 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;
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++;
1.11 deraadt 250: } else if (*line == '-') {
1.1 jfb 251: if (cvs_mtstk_depth == 0) {
252: cvs_log(LP_ERR, "MT scope stack underflow");
253: return (-1);
1.11 deraadt 254: } else if (strcmp(line + 1,
1.1 jfb 255: cvs_mt_stack[cvs_mtstk_depth - 1]) != 0) {
256: cvs_log(LP_ERR, "mismatch in MT scope stack");
257: return (-1);
258: }
259: free(cvs_mt_stack[cvs_mtstk_depth--]);
1.11 deraadt 260: } else {
1.1 jfb 261: if (strcmp(line, "newline") == 0)
262: putc('\n', stdout);
1.57 xsa 263: else if (strncmp(line, "fname ", (size_t)6) == 0)
1.1 jfb 264: printf("%s", line + 6);
265: else {
266: /* assume text */
267: cp = strchr(line, ' ');
268: if (cp != NULL)
269: printf("%s", cp + 1);
270: }
271: }
272:
273: return (0);
274: case CVS_RESP_MBINARY:
275: cvs_log(LP_WARN, "Mbinary not supported in client yet");
276: break;
277: }
278:
279: fputs(line, stream);
280: fputc('\n', stream);
281:
282: return (0);
283: }
284:
285:
286: /*
287: * cvs_resp_ok()
288: *
1.13 tedu 289: * Handler for the `ok' response. This handler's job is to
1.1 jfb 290: */
291: static int
292: cvs_resp_ok(struct cvsroot *root, int type, char *line)
293: {
1.42 joris 294: /*
295: * If we still have an Entry file open, close it now.
296: */
297: if (cvs_resp_lastent != NULL)
298: cvs_ent_close(cvs_resp_lastent);
1.13 tedu 299:
1.1 jfb 300: return (1);
301: }
302:
303:
304: /*
305: * cvs_resp_error()
306: *
1.13 tedu 307: * Handler for the `error' response. This handler's job is to
1.31 joris 308: * show the error message given by the server.
1.1 jfb 309: */
310: static int
311: cvs_resp_error(struct cvsroot *root, int type, char *line)
312: {
1.33 joris 313: if (line == NULL)
314: return (1);
1.31 joris 315:
316: /* XXX - GNU cvs sends an empty error message
317: * at the end of the diff command, even for successfull
318: * diff.
319: */
320: if ((strlen(line) == 1) && (*line == ' '))
321: return (1);
1.13 tedu 322:
1.7 jfb 323: fprintf(stderr, "%s\n", line);
1.1 jfb 324: return (1);
325: }
326:
327:
328: /*
329: * cvs_resp_statdir()
330: *
331: * Handler for the `Clear-static-directory' and `Set-static-directory'
332: * responses.
333: */
334: static int
335: cvs_resp_statdir(struct cvsroot *root, int type, char *line)
336: {
1.24 jfb 337: int fd, len;
1.1 jfb 338: char rpath[MAXPATHLEN], statpath[MAXPATHLEN];
339:
1.3 jfb 340: /* remote directory line */
341: if (cvs_getln(root, rpath, sizeof(rpath)) < 0)
342: return (-1);
1.1 jfb 343:
1.36 jfb 344: STRIP_SLASH(line);
345:
1.47 joris 346: /*
347: * Create the directory if it does not exist.
348: */
349: if (cvs_resp_createdir(line) < 0)
350: return (-1);
1.36 jfb 351:
1.24 jfb 352: len = snprintf(statpath, sizeof(statpath), "%s/%s", line,
1.1 jfb 353: CVS_PATH_STATICENTRIES);
1.24 jfb 354: if (len == -1 || len >= (int)sizeof(statpath)) {
355: cvs_log(LP_ERR,
356: "path overflow for Entries.static specification");
357: return (-1);
358: }
1.1 jfb 359:
1.48 xsa 360: if (cvs_noexec == 0) {
1.35 xsa 361: if ((type == CVS_RESP_CLRSTATDIR) &&
1.51 xsa 362: (cvs_unlink(statpath) == -1)) {
1.1 jfb 363: return (-1);
1.35 xsa 364: } else if (type == CVS_RESP_SETSTATDIR) {
1.40 joris 365: fd = open(statpath, O_CREAT|O_TRUNC|O_WRONLY, 0644);
1.35 xsa 366: if (fd == -1) {
367: cvs_log(LP_ERRNO,
368: "failed to set static directory on %s",
369: line);
370: return (-1);
371: }
372: (void)close(fd);
373:
1.1 jfb 374: }
375: }
376:
377: return (0);
378: }
379:
380: /*
381: * cvs_resp_sticky()
382: *
1.4 jfb 383: * Handler for the `Clear-sticky' and `Set-sticky' responses. If the
1.47 joris 384: * specified directory doesn't exist, we create it.
1.1 jfb 385: */
386: static int
387: cvs_resp_sticky(struct cvsroot *root, int type, char *line)
388: {
1.47 joris 389: char buf[MAXPATHLEN];
1.4 jfb 390:
391: /* get the remote path */
1.5 jfb 392: if (cvs_getln(root, buf, sizeof(buf)) < 0)
1.4 jfb 393: return (-1);
1.1 jfb 394:
1.4 jfb 395: STRIP_SLASH(line);
1.36 jfb 396:
1.47 joris 397: if (cvs_resp_createdir(line) < 0)
398: return (-1);
399:
400: return (0);
401: }
402:
403: /*
404: * Shared code for cvs_resp[static, sticky]
405: *
406: * Looks if the directory requested exists, if it doesn't it will
407: * create it plus all administrative files as well.
408: */
409: static int
410: cvs_resp_createdir(char *line)
411: {
1.53 joris 412: int l;
1.47 joris 413: CVSFILE *base, *cf;
414: CVSENTRIES *entf;
415: struct stat st;
416: struct cvs_ent *ent;
1.56 xsa 417: char *file, subdir[MAXPATHLEN], buf[CVS_ENT_MAXLINELEN];
1.47 joris 418:
1.52 joris 419: entf = NULL;
420: cf = NULL;
1.54 joris 421:
422: /*
423: * we do not want to handle the '.' case,
424: * so return early.
425: */
426: if (!strcmp(line, "."))
427: return (0);
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);
1.52 joris 444: } else {
445: cf = cvs_file_loadinfo(line, CF_NOFILES, NULL, NULL, 1);
446: }
447:
448: if (cf == NULL) {
449: cvs_file_free(base);
450: return (-1);
451: }
452:
453: /*
454: * If the Entries file for the parent is already
455: * open, operate on that, instead of reopening it
456: * and invalidating the opened list.
457: */
458: if (!strcmp(subdir, cvs_resp_lastdir))
459: entf = cvs_resp_lastent;
460: else
461: entf = cvs_ent_open(subdir, O_WRONLY);
1.5 jfb 462:
1.52 joris 463: /*
464: * see if the entry is still present. If not, we add it again.
465: */
466: if (entf != NULL) {
467: if ((ent = cvs_ent_get(entf, cf->cf_name)) == NULL) {
468: l = snprintf(buf, sizeof(buf), "D/%s////", cf->cf_name);
469: if (l == -1 || l >= (int)sizeof(buf)) {
470: cvs_file_free(cf);
471: cvs_file_free(base);
472: return (-1);
1.40 joris 473: }
1.44 joris 474:
1.52 joris 475: ent = cvs_ent_parse(buf);
476: if (ent == NULL)
477: cvs_log(LP_ERR,
478: "failed to create directory entry");
479: else
480: cvs_ent_add(entf, ent);
1.5 jfb 481: }
1.47 joris 482:
1.52 joris 483: if (strcmp(subdir, cvs_resp_lastdir))
484: cvs_ent_close(entf);
1.1 jfb 485: }
486:
1.52 joris 487: cvs_file_free(cf);
1.47 joris 488: cvs_file_free(base);
1.1 jfb 489: return (0);
490: }
491:
492:
493: /*
494: * cvs_resp_newentry()
495: *
496: * Handler for the `New-entry' response and `Checked-in' responses.
1.9 jfb 497: * In the case of `New-entry', we expect the entry line
1.1 jfb 498: */
499: static int
500: cvs_resp_newentry(struct cvsroot *root, int type, char *line)
501: {
1.55 xsa 502: char entbuf[CVS_ENT_MAXLINELEN];
1.9 jfb 503: struct cvs_ent *ent;
1.1 jfb 504:
505: /* get the remote path */
1.43 joris 506: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
507: return (-1);
1.1 jfb 508:
509: /* get the new Entries line */
510: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
511: return (-1);
512:
1.52 joris 513: if (resp_check_dir(root, line) < 0)
1.1 jfb 514: return (-1);
1.42 joris 515:
1.9 jfb 516: if (type == CVS_RESP_NEWENTRY) {
1.42 joris 517: cvs_ent_addln(cvs_resp_lastent, entbuf);
1.11 deraadt 518: } else if (type == CVS_RESP_CHECKEDIN) {
1.9 jfb 519: ent = cvs_ent_parse(entbuf);
520: if (ent == NULL) {
521: cvs_log(LP_ERR, "failed to parse entry");
522: return (-1);
523: }
524:
525: /* timestamp it to now */
526: ent->ce_mtime = time(&(ent->ce_mtime));
527:
528: /* replace the current entry with the one we just received */
1.42 joris 529: (void)cvs_ent_remove(cvs_resp_lastent, ent->ce_name);
1.9 jfb 530:
1.42 joris 531: cvs_ent_add(cvs_resp_lastent, ent);
1.9 jfb 532: }
1.1 jfb 533:
534: return (0);
535: }
536:
537:
538: /*
539: * cvs_resp_cksum()
540: *
541: * Handler for the `Checksum' response. We store the checksum received for
542: * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
543: * Upon next file reception, the handler checks to see if there is a stored
544: * checksum.
545: * The file handler must make sure that the checksums match and free the
546: * checksum buffer once it's done to indicate there is no further checksum.
547: */
548: static int
549: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
550: {
551: if (cvs_fcksum != NULL) {
552: cvs_log(LP_WARN, "unused checksum");
553: free(cvs_fcksum);
554: }
555:
556: cvs_fcksum = strdup(line);
557: if (cvs_fcksum == NULL) {
558: cvs_log(LP_ERRNO, "failed to copy checksum string");
1.14 jfb 559: return (-1);
560: }
561:
562: return (0);
563: }
564:
565:
566: /*
567: * cvs_resp_copyfile()
568: *
569: * Handler for the `Copy-file' response, which is used to copy the contents
570: * of a file to another file for which the name is provided. The CVS protocol
571: * documentation states that this response is only used prior to a `Merged'
572: * response to create a backup of the file.
573: */
574: static int
575: cvs_resp_copyfile(struct cvsroot *root, int type, char *line)
576: {
1.24 jfb 577: int len;
1.16 jfb 578: char path[MAXPATHLEN], newpath[MAXPATHLEN], newname[MAXNAMLEN], *file;
1.14 jfb 579:
1.16 jfb 580: /* read the remote path of the file to copy and its new name */
581: if ((cvs_getln(root, path, sizeof(path)) < 0) ||
582: (cvs_getln(root, newname, sizeof(newname)) < 0))
1.14 jfb 583: return (-1);
584:
1.16 jfb 585: if ((file = basename(path)) == NULL) {
586: cvs_log(LP_ERR, "no base file name in Copy-file path");
587: return (-1);
588: }
589:
1.24 jfb 590: len = snprintf(path, sizeof(path), "%s%s", line, file);
591: if (len == -1 || len >= (int)sizeof(path)) {
592: cvs_log(LP_ERR, "source path overflow in Copy-file response");
593: return (-1);
594: }
595: len = snprintf(newpath, sizeof(newpath), "%s%s", line, newname);
596: if (len == -1 || len >= (int)sizeof(path)) {
597: cvs_log(LP_ERR,
598: "destination path overflow in Copy-file response");
599: return (-1);
600: }
1.16 jfb 601:
602: if (rename(path, newpath) == -1) {
1.27 xsa 603: cvs_log(LP_ERRNO, "failed to rename %s to %s", path, newpath);
1.1 jfb 604: return (-1);
605: }
606:
607: return (0);
608: }
609:
610:
611: /*
612: * cvs_resp_modtime()
613: *
614: * Handler for the `Mod-time' file update modifying response. The timestamp
615: * given is used to set the last modification time on the next file that
616: * will be received.
617: */
618: static int
619: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
620: {
1.34 jfb 621: cvs_modtime = cvs_date_parse(line);
1.1 jfb 622: return (0);
623: }
624:
625:
626: /*
627: * cvs_resp_updated()
628: *
1.4 jfb 629: * Handler for the `Updated', `Update-existing', `Created', `Merged' and
630: * `Patched' responses, which all have a very similar format.
1.1 jfb 631: */
632: static int
633: cvs_resp_updated(struct cvsroot *root, int type, char *line)
634: {
1.17 jfb 635: int ret;
1.1 jfb 636: mode_t fmode;
1.4 jfb 637: char path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.1 jfb 638: BUF *fbuf;
1.45 xsa 639: struct cvs_ent *ent;
1.2 jfb 640: struct timeval tv[2];
1.1 jfb 641:
1.4 jfb 642: STRIP_SLASH(line);
643:
1.1 jfb 644: /* read the remote path of the file */
1.4 jfb 645: if (cvs_getln(root, path, sizeof(path)) < 0)
646: return (-1);
1.1 jfb 647:
648: /* read the new entry */
1.4 jfb 649: if (cvs_getln(root, path, sizeof(path)) < 0)
1.1 jfb 650: return (-1);
1.4 jfb 651:
1.45 xsa 652: if ((ent = cvs_ent_parse(path)) == NULL)
1.4 jfb 653: return (-1);
1.45 xsa 654: ret = snprintf(path, sizeof(path), "%s/%s", line, ent->ce_name);
1.24 jfb 655: if (ret == -1 || ret >= (int)sizeof(path)) {
656: cvs_log(LP_ERR, "Entries path overflow in response");
657: return (-1);
658: }
1.25 joris 659: ret = 0;
1.1 jfb 660:
1.52 joris 661: /*
662: * Please be sure the directory does exist.
663: */
664: if (cvs_resp_createdir(line) < 0)
665: return (-1);
666:
667: if (resp_check_dir(root, line) < 0)
1.12 jfb 668: return (-1);
669:
1.15 jfb 670: if (cvs_modtime != CVS_DATE_DMSEC) {
1.45 xsa 671: ent->ce_mtime = cvs_modtime;
1.15 jfb 672: } else
1.45 xsa 673: ent->ce_mtime = time(&(ent->ce_mtime));
1.15 jfb 674:
675: if ((type == CVS_RESP_UPDEXIST) || (type == CVS_RESP_UPDATED) ||
1.18 jfb 676: (type == CVS_RESP_MERGED) || (type == CVS_RESP_CREATED)) {
1.45 xsa 677: if ((cvs_ent_remove(cvs_resp_lastent, ent->ce_name) < 0) &&
1.30 joris 678: (type != CVS_RESP_CREATED)) {
679: cvs_log(LP_WARN, "failed to remove entry for '%s`",
1.45 xsa 680: ent->ce_name);
1.30 joris 681: }
1.15 jfb 682: }
1.12 jfb 683:
1.45 xsa 684: if (cvs_ent_add(cvs_resp_lastent, ent) < 0) {
685: cvs_ent_free(ent);
1.15 jfb 686: return (-1);
1.1 jfb 687: }
1.15 jfb 688:
1.17 jfb 689: if ((fbuf = cvs_recvfile(root, &fmode)) == NULL)
690: return (-1);
691: if (cvs_buf_write(fbuf, path, fmode) < 0) {
692: cvs_buf_free(fbuf);
1.1 jfb 693: return (-1);
1.17 jfb 694: }
695: cvs_buf_free(fbuf);
1.2 jfb 696:
1.12 jfb 697: if (cvs_modtime != CVS_DATE_DMSEC) {
698: tv[0].tv_sec = (long)cvs_modtime;
699: tv[0].tv_usec = 0;
700: tv[1].tv_sec = (long)cvs_modtime;
701: tv[1].tv_usec = 0;
702: if (utimes(path, tv) == -1)
703: cvs_log(LP_ERRNO, "failed to set file timestamps");
704: }
1.34 jfb 705:
706: /* invalidate last received timestamp */
707: cvs_modtime = CVS_DATE_DMSEC;
1.1 jfb 708:
709: /* now see if there is a checksum */
710: if (cvs_fcksum != NULL) {
1.17 jfb 711: if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0)
712: ret = -1;
713: else if (strcmp(cksum_buf, cvs_fcksum) != 0) {
1.1 jfb 714: cvs_log(LP_ERR, "checksum error on received file");
715: (void)unlink(line);
1.17 jfb 716: ret = -1;
1.1 jfb 717: }
718:
719: free(cvs_fcksum);
720: cvs_fcksum = NULL;
721: }
722:
1.17 jfb 723: return (ret);
1.1 jfb 724: }
725:
726:
727: /*
728: * cvs_resp_removed()
729: *
1.10 jfb 730: * Handler for the `Removed' and `Remove-entry' responses. The `Removed'
731: * response is received when both a file and its entry need to be removed from
732: * the local copy. The `Remove-entry' is received in cases where the file is
733: * already gone but there is still an entry to remove in the Entries file.
1.1 jfb 734: */
735: static int
736: cvs_resp_removed(struct cvsroot *root, int type, char *line)
737: {
1.29 joris 738: int l;
739: char buf[MAXPATHLEN], base[MAXPATHLEN], fpath[MAXPATHLEN], *file;
1.1 jfb 740:
1.29 joris 741: if (cvs_getln(root, buf, sizeof(buf)) < 0)
742: return (-1);
743:
744: cvs_splitpath(buf, base, sizeof(base), &file);
745: l = snprintf(fpath, sizeof(fpath), "%s/%s", line, file);
746: if (l == -1 || l >= (int)sizeof(fpath)) {
747: errno = ENAMETOOLONG;
748: cvs_log(LP_ERRNO, "%s", fpath);
749: return (-1);
750: }
751:
1.52 joris 752: if (resp_check_dir(root, line) < 0)
1.42 joris 753: return (-1);
1.10 jfb 754:
1.42 joris 755: (void)cvs_ent_remove(cvs_resp_lastent, file);
1.32 joris 756: if ((type == CVS_RESP_REMOVED) && ((unlink(fpath) == -1) &&
757: errno != ENOENT)) {
1.29 joris 758: cvs_log(LP_ERRNO, "failed to unlink `%s'", file);
1.1 jfb 759: return (-1);
1.10 jfb 760: }
1.1 jfb 761:
762: return (0);
763: }
764:
765:
766: /*
767: * cvs_resp_mode()
768: *
769: * Handler for the `Mode' response.
770: */
771: static int
772: cvs_resp_mode(struct cvsroot *root, int type, char *line)
773: {
774: if (cvs_strtomode(line, &cvs_lastmode) < 0) {
1.12 jfb 775: cvs_log(LP_ERR, "error handling Mode response");
1.1 jfb 776: return (-1);
777: }
778: return (0);
779: }
780:
781:
782: /*
783: * cvs_resp_modxpand()
784: *
785: * Handler for the `Module-expansion' response.
786: */
787: static int
788: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
789: {
790: return (0);
791: }
792:
793: /*
794: * cvs_resp_rcsdiff()
795: *
796: * Handler for the `Rcs-diff' response.
797: */
798: static int
799: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
800: {
1.24 jfb 801: int len;
1.56 xsa 802: char file[MAXPATHLEN];
803: char buf[CVS_ENT_MAXLINELEN], cksum_buf[CVS_CKSUM_LEN];
1.1 jfb 804: char *fname, *orig, *patch;
805: mode_t fmode;
806: BUF *res, *fcont, *patchbuf;
807: CVSENTRIES *entf;
808: struct cvs_ent *ent;
809:
810: /* get remote path and build local path of file to be patched */
1.43 joris 811: if (cvs_getln(root, buf, sizeof(buf)) < 0)
812: return (-1);
813:
1.1 jfb 814: fname = strrchr(buf, '/');
815: if (fname == NULL)
816: fname = buf;
1.24 jfb 817: len = snprintf(file, sizeof(file), "%s%s", line, fname);
818: if (len == -1 || len >= (int)sizeof(file)) {
819: cvs_log(LP_ERR, "path overflow in Rcs-diff response");
820: return (-1);
821: }
1.1 jfb 822:
823: /* get updated entry fields */
1.43 joris 824: if (cvs_getln(root, buf, sizeof(buf)) < 0)
825: return (-1);
826:
1.1 jfb 827: ent = cvs_ent_parse(buf);
1.13 tedu 828: if (ent == NULL)
1.1 jfb 829: return (-1);
830:
831: patchbuf = cvs_recvfile(root, &fmode);
832: fcont = cvs_buf_load(file, BUF_AUTOEXT);
833: if (fcont == NULL)
834: return (-1);
835:
836: cvs_buf_putc(patchbuf, '\0');
837: cvs_buf_putc(fcont, '\0');
838: orig = cvs_buf_release(fcont);
839: patch = cvs_buf_release(patchbuf);
840:
841: res = rcs_patch(orig, patch);
842: if (res == NULL)
843: return (-1);
844:
845: cvs_buf_write(res, file, fmode);
846:
847: /* now see if there is a checksum */
848: if (cvs_fcksum != NULL) {
849: if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
850: }
851:
852: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
853: cvs_log(LP_ERR, "checksum error on received file");
854: (void)unlink(file);
855: }
856:
857: free(cvs_fcksum);
858: cvs_fcksum = NULL;
859: }
860:
861: /* update revision in entries */
862: entf = cvs_ent_open(line, O_WRONLY);
863: if (entf == NULL)
864: return (-1);
865:
866: cvs_ent_close(entf);
867:
868: return (0);
869: }
870:
871:
872: /*
873: * cvs_resp_template()
874: *
875: * Handler for the `Template' response.
876: */
877: static int
878: cvs_resp_template(struct cvsroot *root, int type, char *line)
879: {
880: mode_t mode;
881: BUF *tmpl;
882:
883: tmpl = cvs_recvfile(root, &mode);
884: if (tmpl == NULL)
885: return (-1);
1.42 joris 886:
887: return (0);
888: }
889:
890: /*
891: * Check if <dir> is the same as the last
892: * received directory, if it's not, switch Entry files.
893: */
894: static int
1.52 joris 895: resp_check_dir(struct cvsroot *root, const char *dir)
1.42 joris 896: {
1.52 joris 897: int l;
1.42 joris 898: size_t len;
1.52 joris 899: char cvspath[MAXPATHLEN], repo[MAXPATHLEN];
900: struct stat st;
901:
902: /*
903: * Make sure the CVS directory exists.
904: */
905: l = snprintf(cvspath, sizeof(cvspath), "%s/%s", dir, CVS_PATH_CVSDIR);
906: if (l == -1 || l >= (int)sizeof(cvspath))
907: return (-1);
908:
909: if (stat(cvspath, &st) == -1) {
910: if (errno != ENOENT)
911: return (-1);
912: if (cvs_repo_base != NULL) {
913: l = snprintf(repo, sizeof(repo), "%s/%s", cvs_repo_base,
914: dir);
915: if (l == -1 || l >= (int)sizeof(repo))
916: return (-1);
917: } else {
918: strlcpy(repo, dir, sizeof(repo));
919: }
920:
1.58 xsa 921: if (cvs_mkadmin(dir, root->cr_str, repo, NULL, NULL, 0) < 0)
1.52 joris 922: return (-1);
923: }
1.42 joris 924:
925: if (strcmp(dir, cvs_resp_lastdir)) {
926: if (cvs_resp_lastent != NULL)
927: cvs_ent_close(cvs_resp_lastent);
928: cvs_resp_lastent = cvs_ent_open(dir, O_WRONLY);
929: if (cvs_resp_lastent == NULL)
930: return (-1);
931:
932: len = strlcpy(cvs_resp_lastdir, dir, sizeof(cvs_resp_lastdir));
933: if (len >= sizeof(cvs_resp_lastdir)) {
934: errno = ENAMETOOLONG;
935: cvs_log(LP_ERRNO, "%s", cvs_resp_lastdir);
936: return (-1);
937: }
938: } else {
939: /* make sure the old one is still open */
940: if (cvs_resp_lastent == NULL) {
941: cvs_resp_lastent = cvs_ent_open(cvs_resp_lastdir,
942: O_WRONLY);
943: if (cvs_resp_lastent == NULL)
944: return (-1);
945: }
946: }
1.1 jfb 947:
948: return (0);
949: }