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