[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.19

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