[BACK]Return to resp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/resp.c, Revision 1.37

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