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

1.21    ! jfb         1: /*     $OpenBSD: resp.c,v 1.20 2005/01/13 05:39:07 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.20      jfb       164:                cvs_log(LP_ERR, "handler for `%s' not implemented", cmd);
1.1       jfb       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;
1.21    ! jfb       367:        CVSENTRIES *entf;
1.4       jfb       368:
                    369:        /* get the remote path */
1.5       jfb       370:        if (cvs_getln(root, buf, sizeof(buf)) < 0)
1.4       jfb       371:                return (-1);
1.1       jfb       372:
1.4       jfb       373:        STRIP_SLASH(line);
1.1       jfb       374:
1.4       jfb       375:        cvs_splitpath(line, subdir, sizeof(subdir), &file);
                    376:        sdir = cvs_file_find(cvs_files, subdir);
                    377:        if (sdir == NULL) {
                    378:                cvs_log(LP_ERR, "failed to find %s", subdir);
                    379:                return (-1);
                    380:        }
1.1       jfb       381:
1.4       jfb       382:        cf = cvs_file_find(sdir, file);
                    383:        if (cf == NULL) {
1.1       jfb       384:                /* attempt to create it */
1.8       jfb       385:                cf = cvs_file_create(sdir, line, DT_DIR, 0755);
1.4       jfb       386:                if (cf == NULL)
                    387:                        return (-1);
                    388:                cf->cf_ddat->cd_repo = strdup(line);
1.19      jfb       389:                if (cf->cf_ddat->cd_repo == NULL) {
                    390:                        cvs_log(LP_ERRNO, "failed to duplicate `%s'", line);
                    391:                        cvs_file_free(cf);
                    392:                        return (-1);
                    393:                }
1.4       jfb       394:                cf->cf_ddat->cd_root = root;
                    395:                root->cr_ref++;
1.1       jfb       396:
1.4       jfb       397:                cvs_file_attach(sdir, cf);
1.5       jfb       398:
                    399:                /* add a directory entry to the parent */
1.21    ! jfb       400:                if ((entf = cvs_ent_open(subdir, O_WRONLY)) != NULL) {
1.8       jfb       401:                        snprintf(buf, sizeof(buf), "D/%s////",
                    402:                            CVS_FILE_NAME(cf));
1.5       jfb       403:                        ent = cvs_ent_parse(buf);
                    404:                        if (ent == NULL)
                    405:                                cvs_log(LP_ERR,
                    406:                                    "failed to create directory entry");
                    407:                        else
1.21    ! jfb       408:                                cvs_ent_add(entf, ent);
        !           409:                        cvs_ent_close(entf);
1.5       jfb       410:                }
1.1       jfb       411:        }
                    412:
1.4       jfb       413:        if (type == CVS_RESP_CLRSTICKY)
                    414:                cf->cf_ddat->cd_flags &= ~CVS_DIRF_STICKY;
                    415:        else if (type == CVS_RESP_SETSTICKY)
                    416:                cf->cf_ddat->cd_flags |= CVS_DIRF_STICKY;
1.1       jfb       417:
                    418:        return (0);
                    419: }
                    420:
                    421:
                    422: /*
                    423:  * cvs_resp_newentry()
                    424:  *
                    425:  * Handler for the `New-entry' response and `Checked-in' responses.
1.9       jfb       426:  * In the case of `New-entry', we expect the entry line
1.1       jfb       427:  */
                    428: static int
                    429: cvs_resp_newentry(struct cvsroot *root, int type, char *line)
                    430: {
                    431:        char entbuf[128];
1.9       jfb       432:        struct cvs_ent *ent;
1.1       jfb       433:        CVSENTRIES *entfile;
                    434:
                    435:        /* get the remote path */
                    436:        cvs_getln(root, entbuf, sizeof(entbuf));
                    437:
                    438:        /* get the new Entries line */
                    439:        if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
                    440:                return (-1);
                    441:
                    442:        entfile = cvs_ent_open(line, O_WRONLY);
                    443:        if (entfile == NULL)
                    444:                return (-1);
1.9       jfb       445:        if (type == CVS_RESP_NEWENTRY) {
                    446:                cvs_ent_addln(entfile, entbuf);
1.11      deraadt   447:        } else if (type == CVS_RESP_CHECKEDIN) {
1.9       jfb       448:                ent = cvs_ent_parse(entbuf);
                    449:                if (ent == NULL) {
                    450:                        cvs_log(LP_ERR, "failed to parse entry");
                    451:                        cvs_ent_close(entfile);
                    452:                        return (-1);
                    453:                }
                    454:
                    455:                /* timestamp it to now */
                    456:                ent->ce_mtime = time(&(ent->ce_mtime));
                    457:
                    458:                /* replace the current entry with the one we just received */
                    459:                if (cvs_ent_remove(entfile, ent->ce_name) < 0)
                    460:                        cvs_log(LP_WARN, "failed to remove `%s' entry",
                    461:                            ent->ce_name);
                    462:
                    463:                cvs_ent_add(entfile, ent);
                    464:        }
1.1       jfb       465:        cvs_ent_close(entfile);
                    466:
                    467:        return (0);
                    468: }
                    469:
                    470:
                    471: /*
                    472:  * cvs_resp_cksum()
                    473:  *
                    474:  * Handler for the `Checksum' response.  We store the checksum received for
                    475:  * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
                    476:  * Upon next file reception, the handler checks to see if there is a stored
                    477:  * checksum.
                    478:  * The file handler must make sure that the checksums match and free the
                    479:  * checksum buffer once it's done to indicate there is no further checksum.
                    480:  */
                    481: static int
                    482: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
                    483: {
                    484:        if (cvs_fcksum != NULL) {
                    485:                cvs_log(LP_WARN, "unused checksum");
                    486:                free(cvs_fcksum);
                    487:        }
                    488:
                    489:        cvs_fcksum = strdup(line);
                    490:        if (cvs_fcksum == NULL) {
                    491:                cvs_log(LP_ERRNO, "failed to copy checksum string");
1.14      jfb       492:                return (-1);
                    493:        }
                    494:
                    495:        return (0);
                    496: }
                    497:
                    498:
                    499: /*
                    500:  * cvs_resp_copyfile()
                    501:  *
                    502:  * Handler for the `Copy-file' response, which is used to copy the contents
                    503:  * of a file to another file for which the name is provided.  The CVS protocol
                    504:  * documentation states that this response is only used prior to a `Merged'
                    505:  * response to create a backup of the file.
                    506:  */
                    507: static int
                    508: cvs_resp_copyfile(struct cvsroot *root, int type, char *line)
                    509: {
1.16      jfb       510:        char path[MAXPATHLEN], newpath[MAXPATHLEN], newname[MAXNAMLEN], *file;
1.14      jfb       511:
1.16      jfb       512:        /* read the remote path of the file to copy and its new name */
                    513:        if ((cvs_getln(root, path, sizeof(path)) < 0) ||
                    514:            (cvs_getln(root, newname, sizeof(newname)) < 0))
1.14      jfb       515:                return (-1);
                    516:
1.16      jfb       517:        if ((file = basename(path)) == NULL) {
                    518:                cvs_log(LP_ERR, "no base file name in Copy-file path");
                    519:                return (-1);
                    520:        }
                    521:
                    522:        snprintf(path, sizeof(path), "%s%s", line, file);
                    523:        snprintf(newpath, sizeof(newpath), "%s%s", line, newname);
                    524:
                    525:        if (rename(path, newpath) == -1) {
1.14      jfb       526:                cvs_log(LP_ERRNO, "failed to rename %s to %s", line, newname);
1.1       jfb       527:                return (-1);
                    528:        }
                    529:
                    530:        return (0);
                    531: }
                    532:
                    533:
                    534: /*
                    535:  * cvs_resp_modtime()
                    536:  *
                    537:  * Handler for the `Mod-time' file update modifying response.  The timestamp
                    538:  * given is used to set the last modification time on the next file that
                    539:  * will be received.
                    540:  */
                    541: static int
                    542: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
                    543: {
1.4       jfb       544:        cvs_modtime = cvs_datesec(line, CVS_DATE_RFC822, 1);
1.1       jfb       545:        return (0);
                    546: }
                    547:
                    548:
                    549: /*
                    550:  * cvs_resp_updated()
                    551:  *
1.4       jfb       552:  * Handler for the `Updated', `Update-existing', `Created', `Merged' and
                    553:  * `Patched' responses, which all have a very similar format.
1.1       jfb       554:  */
                    555: static int
                    556: cvs_resp_updated(struct cvsroot *root, int type, char *line)
                    557: {
1.17      jfb       558:        int ret;
1.1       jfb       559:        mode_t fmode;
1.4       jfb       560:        char path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.1       jfb       561:        BUF *fbuf;
1.12      jfb       562:        CVSENTRIES *entfile;
1.1       jfb       563:        struct cvs_ent *ep;
1.2       jfb       564:        struct timeval tv[2];
1.1       jfb       565:
1.17      jfb       566:        ret = 0;
1.4       jfb       567:        STRIP_SLASH(line);
                    568:
1.1       jfb       569:        /* read the remote path of the file */
1.4       jfb       570:        if (cvs_getln(root, path, sizeof(path)) < 0)
                    571:                return (-1);
1.1       jfb       572:
                    573:        /* read the new entry */
1.4       jfb       574:        if (cvs_getln(root, path, sizeof(path)) < 0)
1.1       jfb       575:                return (-1);
1.4       jfb       576:
                    577:        if ((ep = cvs_ent_parse(path)) == NULL)
                    578:                return (-1);
                    579:        snprintf(path, sizeof(path), "%s/%s", line, ep->ce_name);
1.1       jfb       580:
1.12      jfb       581:        entfile = cvs_ent_open(line, O_WRONLY);
                    582:        if (entfile == NULL) {
                    583:                cvs_ent_free(ep);
                    584:                return (-1);
                    585:        }
                    586:
1.15      jfb       587:        if (cvs_modtime != CVS_DATE_DMSEC) {
1.4       jfb       588:                ep->ce_mtime = cvs_modtime;
1.15      jfb       589:                cvs_modtime = CVS_DATE_DMSEC;   /* invalidate */
                    590:        } else
                    591:                ep->ce_mtime = time(&(ep->ce_mtime));
                    592:
                    593:        if ((type == CVS_RESP_UPDEXIST) || (type == CVS_RESP_UPDATED) ||
1.18      jfb       594:            (type == CVS_RESP_MERGED) || (type == CVS_RESP_CREATED)) {
1.12      jfb       595:                if (cvs_ent_remove(entfile, ep->ce_name) < 0)
                    596:                        cvs_log(LP_WARN, "failed to remove entry for `%s'",
                    597:                            ep->ce_name);
1.15      jfb       598:        }
1.12      jfb       599:
1.15      jfb       600:        if (cvs_ent_add(entfile, ep) < 0) {
                    601:                cvs_ent_free(ep);
                    602:                cvs_ent_close(entfile);
                    603:                return (-1);
1.1       jfb       604:        }
1.15      jfb       605:
                    606:        cvs_ent_close(entfile);
1.1       jfb       607:
1.17      jfb       608:        if ((fbuf = cvs_recvfile(root, &fmode)) == NULL)
                    609:                return (-1);
                    610:        if (cvs_buf_write(fbuf, path, fmode) < 0) {
                    611:                cvs_buf_free(fbuf);
1.1       jfb       612:                return (-1);
1.17      jfb       613:        }
                    614:        cvs_buf_free(fbuf);
1.2       jfb       615:
1.12      jfb       616:        if (cvs_modtime != CVS_DATE_DMSEC) {
                    617:                tv[0].tv_sec = (long)cvs_modtime;
                    618:                tv[0].tv_usec = 0;
                    619:                tv[1].tv_sec = (long)cvs_modtime;
                    620:                tv[1].tv_usec = 0;
                    621:                if (utimes(path, tv) == -1)
                    622:                        cvs_log(LP_ERRNO, "failed to set file timestamps");
                    623:        }
1.1       jfb       624:
                    625:        /* now see if there is a checksum */
                    626:        if (cvs_fcksum != NULL) {
1.17      jfb       627:                if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0)
                    628:                        ret = -1;
                    629:                else if (strcmp(cksum_buf, cvs_fcksum) != 0) {
1.1       jfb       630:                        cvs_log(LP_ERR, "checksum error on received file");
                    631:                        (void)unlink(line);
1.17      jfb       632:                        ret = -1;
1.1       jfb       633:                }
                    634:
                    635:                free(cvs_fcksum);
                    636:                cvs_fcksum = NULL;
                    637:        }
                    638:
1.17      jfb       639:        return (ret);
1.1       jfb       640: }
                    641:
                    642:
                    643: /*
                    644:  * cvs_resp_removed()
                    645:  *
1.10      jfb       646:  * Handler for the `Removed' and `Remove-entry' responses.  The `Removed'
                    647:  * response is received when both a file and its entry need to be removed from
                    648:  * the local copy.  The `Remove-entry' is received in cases where the file is
                    649:  * already gone but there is still an entry to remove in the Entries file.
1.1       jfb       650:  */
                    651: static int
                    652: cvs_resp_removed(struct cvsroot *root, int type, char *line)
                    653: {
1.3       jfb       654:        char base[MAXPATHLEN], *file;
1.1       jfb       655:        CVSENTRIES *ef;
                    656:
1.3       jfb       657:        cvs_splitpath(line, base, sizeof(base), &file);
1.1       jfb       658:        ef = cvs_ent_open(base, O_RDWR);
1.10      jfb       659:        if (ef == NULL) {
                    660:                cvs_log(LP_ERR, "error handling `Removed' response");
                    661:                if (type == CVS_RESP_RMENTRY)
                    662:                        return (-1);
                    663:        } else {
                    664:                (void)cvs_ent_remove(ef, file);
                    665:                cvs_ent_close(ef);
                    666:        }
                    667:
                    668:        if ((type == CVS_RESP_REMOVED) && (unlink(line) == -1)) {
                    669:                cvs_log(LP_ERRNO, "failed to unlink `%s'", line);
1.1       jfb       670:                return (-1);
1.10      jfb       671:        }
1.1       jfb       672:
                    673:        return (0);
                    674: }
                    675:
                    676:
                    677: /*
                    678:  * cvs_resp_mode()
                    679:  *
                    680:  * Handler for the `Mode' response.
                    681:  */
                    682: static int
                    683: cvs_resp_mode(struct cvsroot *root, int type, char *line)
                    684: {
                    685:        if (cvs_strtomode(line, &cvs_lastmode) < 0) {
1.12      jfb       686:                cvs_log(LP_ERR, "error handling Mode response");
1.1       jfb       687:                return (-1);
                    688:        }
                    689:        return (0);
                    690: }
                    691:
                    692:
                    693: /*
                    694:  * cvs_resp_modxpand()
                    695:  *
                    696:  * Handler for the `Module-expansion' response.
                    697:  */
                    698: static int
                    699: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
                    700: {
                    701:        return (0);
                    702: }
                    703:
                    704: /*
                    705:  * cvs_resp_rcsdiff()
                    706:  *
                    707:  * Handler for the `Rcs-diff' response.
                    708:  */
                    709: static int
                    710: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
                    711: {
                    712:        char file[MAXPATHLEN], buf[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
                    713:        char *fname, *orig, *patch;
                    714:        mode_t fmode;
                    715:        BUF *res, *fcont, *patchbuf;
                    716:        CVSENTRIES *entf;
                    717:        struct cvs_ent *ent;
                    718:
                    719:        /* get remote path and build local path of file to be patched */
                    720:        cvs_getln(root, buf, sizeof(buf));
                    721:        fname = strrchr(buf, '/');
                    722:        if (fname == NULL)
                    723:                fname = buf;
                    724:        snprintf(file, sizeof(file), "%s%s", line, fname);
                    725:
                    726:        /* get updated entry fields */
                    727:        cvs_getln(root, buf, sizeof(buf));
                    728:        ent = cvs_ent_parse(buf);
1.13      tedu      729:        if (ent == NULL)
1.1       jfb       730:                return (-1);
                    731:
                    732:        patchbuf = cvs_recvfile(root, &fmode);
                    733:        fcont = cvs_buf_load(file, BUF_AUTOEXT);
                    734:        if (fcont == NULL)
                    735:                return (-1);
                    736:
                    737:        cvs_buf_putc(patchbuf, '\0');
                    738:        cvs_buf_putc(fcont, '\0');
                    739:        orig = cvs_buf_release(fcont);
                    740:        patch = cvs_buf_release(patchbuf);
                    741:
                    742:        res = rcs_patch(orig, patch);
                    743:        if (res == NULL)
                    744:                return (-1);
                    745:
                    746:        cvs_buf_write(res, file, fmode);
                    747:
                    748:        /* now see if there is a checksum */
                    749:        if (cvs_fcksum != NULL) {
                    750:                if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
                    751:                }
                    752:
                    753:                if (strcmp(cksum_buf, cvs_fcksum) != 0) {
                    754:                        cvs_log(LP_ERR, "checksum error on received file");
                    755:                        (void)unlink(file);
                    756:                }
                    757:
                    758:                free(cvs_fcksum);
                    759:                cvs_fcksum = NULL;
                    760:        }
                    761:
                    762:        /* update revision in entries */
                    763:        entf = cvs_ent_open(line, O_WRONLY);
                    764:        if (entf == NULL)
                    765:                return (-1);
                    766:
                    767:        cvs_ent_close(entf);
                    768:
                    769:        return (0);
                    770: }
                    771:
                    772:
                    773: /*
                    774:  * cvs_resp_template()
                    775:  *
                    776:  * Handler for the `Template' response.
                    777:  */
                    778: static int
                    779: cvs_resp_template(struct cvsroot *root, int type, char *line)
                    780: {
                    781:        mode_t mode;
                    782:        BUF *tmpl;
                    783:
                    784:        tmpl = cvs_recvfile(root, &mode);
                    785:        if (tmpl == NULL)
                    786:                return (-1);
                    787:
                    788:        return (0);
                    789: }