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

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