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

Annotation of src/usr.bin/cvs/update.c, Revision 1.48

1.48    ! xsa         1: /*     $OpenBSD: update.c,v 1.47 2005/12/03 15:31:53 joris Exp $       */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.11      tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.11      tedu        6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.11      tedu       10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.11      tedu       13:  *    derived from this software without specific prior written permission.
1.1       jfb        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
1.11      tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/stat.h>
                     28:
                     29: #include <errno.h>
1.34      xsa        30: #include <fcntl.h>
1.1       jfb        31: #include <stdio.h>
                     32: #include <stdlib.h>
1.34      xsa        33: #include <string.h>
1.1       jfb        34: #include <unistd.h>
                     35:
                     36: #include "cvs.h"
                     37: #include "log.h"
1.4       jfb        38: #include "proto.h"
1.45      joris      39: #include "diff.h"
1.1       jfb        40:
1.42      xsa        41: static int     cvs_update_init(struct cvs_cmd *, int, char **, int *);
                     42: static int     cvs_update_pre_exec(struct cvsroot *);
                     43: static int     cvs_update_remote(CVSFILE *, void *);
                     44: static int     cvs_update_local(CVSFILE *, void *);
1.18      joris      45:
1.29      jfb        46: struct cvs_cmd cvs_cmd_update = {
                     47:        CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
                     48:        { "up", "upd" },
                     49:        "Bring work tree in sync with repository",
                     50:        "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
                     51:        "[-t id] ...",
1.35      xsa        52:        "ACD:dfI:j:k:lPpQqRr:t:",
1.29      jfb        53:        NULL,
1.37      joris      54:        CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.29      jfb        55:        cvs_update_init,
                     56:        cvs_update_pre_exec,
1.26      jfb        57:        cvs_update_remote,
1.29      jfb        58:        cvs_update_local,
                     59:        NULL,
                     60:        NULL,
1.18      joris      61:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
                     62: };
1.2       jfb        63:
1.35      xsa        64: static char *date, *rev, *koptstr;
1.31      joris      65: static int dflag, Aflag;
1.35      xsa        66: static int kflag = RCS_KWEXP_DEFAULT;
1.24      joris      67:
1.26      jfb        68: static int
1.29      jfb        69: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        70: {
1.18      joris      71:        int ch;
1.2       jfb        72:
1.31      joris      73:        dflag = Aflag = 0;
1.32      xsa        74:        date = NULL;
                     75:        rev = NULL;
1.24      joris      76:
1.29      jfb        77:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        78:                switch (ch) {
                     79:                case 'A':
1.24      joris      80:                        Aflag = 1;
                     81:                        break;
1.1       jfb        82:                case 'C':
                     83:                case 'D':
1.32      xsa        84:                        date = optarg;
                     85:                        break;
1.1       jfb        86:                case 'd':
1.24      joris      87:                        dflag = 1;
                     88:                        break;
1.1       jfb        89:                case 'f':
1.2       jfb        90:                        break;
1.35      xsa        91:                case 'I':
                     92:                        break;
                     93:                case 'j':
                     94:                        break;
                     95:                case 'k':
                     96:                        koptstr = optarg;
                     97:                        kflag = rcs_kflag_get(koptstr);
                     98:                        if (RCS_KWEXP_INVAL(kflag)) {
                     99:                                cvs_log(LP_ERR,
                    100:                                    "invalid RCS keyword expansion mode");
                    101:                                rcs_kflag_usage();
                    102:                                return (CVS_EX_USAGE);
                    103:                        }
                    104:                        break;
1.1       jfb       105:                case 'l':
1.29      jfb       106:                        cmd->file_flags &= ~CF_RECURSE;
1.2       jfb       107:                        break;
1.1       jfb       108:                case 'P':
1.31      joris     109:                        cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24      joris     110:                        break;
1.1       jfb       111:                case 'p':
1.27      xsa       112:                        cvs_noexec = 1; /* no locks will be created */
                    113:                        break;
1.1       jfb       114:                case 'Q':
                    115:                case 'q':
1.2       jfb       116:                        break;
1.1       jfb       117:                case 'R':
1.29      jfb       118:                        cmd->file_flags |= CF_RECURSE;
1.2       jfb       119:                        break;
1.1       jfb       120:                case 'r':
1.32      xsa       121:                        rev = optarg;
1.1       jfb       122:                        break;
                    123:                default:
1.21      joris     124:                        return (CVS_EX_USAGE);
1.1       jfb       125:                }
1.32      xsa       126:        }
                    127:
1.18      joris     128:        *arg = optind;
1.2       jfb       129:        return (0);
                    130: }
                    131:
1.26      jfb       132: static int
1.29      jfb       133: cvs_update_pre_exec(struct cvsroot *root)
1.24      joris     134: {
1.36      joris     135:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.38      joris     136:                if ((cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS) &&
1.36      joris     137:                    (cvs_sendarg(root, "-P", 0) < 0))
                    138:                        return (CVS_EX_PROTO);
                    139:                if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
                    140:                        return (CVS_EX_PROTO);
                    141:                if (dflag && cvs_sendarg(root, "-d", 0) < 0)
                    142:                        return (CVS_EX_PROTO);
1.33      xsa       143:
1.36      joris     144:                if ((rev != NULL) && ((cvs_sendarg(root, "-r", 0) < 0) ||
                    145:                    (cvs_sendarg(root, rev, 0) < 0)))
1.33      xsa       146:                        return (CVS_EX_PROTO);
1.35      xsa       147:
1.36      joris     148:                if ((date != NULL) && ((cvs_sendarg(root, "-D", 0) < 0) ||
                    149:                    (cvs_sendarg(root, date, 0) < 0)))
                    150:                        return (CVS_EX_PROTO);
                    151:        }
1.35      xsa       152:
1.24      joris     153:        return (0);
                    154: }
1.2       jfb       155:
                    156: /*
1.26      jfb       157:  * cvs_update_remote()
1.2       jfb       158:  *
1.12      jfb       159:  * Update a single file.  In the case where we act as client, send any
                    160:  * pertinent information about that file to the server.
1.2       jfb       161:  */
1.26      jfb       162: static int
                    163: cvs_update_remote(CVSFILE *cf, void *arg)
1.2       jfb       164: {
1.26      jfb       165:        int ret;
1.44      xsa       166:        char fpath[MAXPATHLEN];
1.2       jfb       167:        struct cvsroot *root;
                    168:
1.12      jfb       169:        ret = 0;
                    170:        root = CVS_DIR_ROOT(cf);
1.9       jfb       171:
1.13      jfb       172:        if (cf->cf_type == DT_DIR) {
1.26      jfb       173:                if (cf->cf_cvstat == CVS_FST_UNKNOWN)
                    174:                        ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
                    175:                            cf->cf_name);
                    176:                else
                    177:                        ret = cvs_senddir(root, cf);
1.13      jfb       178:
1.28      joris     179:                if (ret == -1)
                    180:                        ret = CVS_EX_PROTO;
                    181:
1.12      jfb       182:                return (ret);
1.2       jfb       183:        }
                    184:
1.12      jfb       185:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2       jfb       186:
1.26      jfb       187:        if (cvs_sendentry(root, cf) < 0)
                    188:                return (CVS_EX_PROTO);
1.41      joris     189:
                    190:        if (!(cf->cf_flags & CVS_FILE_ONDISK))
                    191:                return (0);
1.2       jfb       192:
1.26      jfb       193:        switch (cf->cf_cvstat) {
                    194:        case CVS_FST_UNKNOWN:
                    195:                ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
                    196:                break;
                    197:        case CVS_FST_UPTODATE:
                    198:                ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
                    199:                break;
                    200:        case CVS_FST_ADDED:
                    201:        case CVS_FST_MODIFIED:
                    202:                ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
                    203:                if (ret == 0)
                    204:                        ret = cvs_sendfile(root, fpath);
                    205:                break;
                    206:        default:
                    207:                break;
1.2       jfb       208:        }
                    209:
1.28      joris     210:        if (ret == -1)
                    211:                ret = CVS_EX_PROTO;
                    212:
1.12      jfb       213:        return (ret);
1.2       jfb       214: }
                    215:
                    216: /*
1.26      jfb       217:  * cvs_update_local()
1.2       jfb       218:  */
1.26      jfb       219: static int
                    220: cvs_update_local(CVSFILE *cf, void *arg)
1.2       jfb       221: {
1.45      joris     222:        int ret, islocal, revdiff;
1.39      xsa       223:        char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.45      joris     224:        char *repo;
1.26      jfb       225:        RCSFILE *rf;
1.45      joris     226:        RCSNUM *frev;
                    227:        BUF *fbuf;
                    228:        struct cvsroot *root;
1.26      jfb       229:
1.45      joris     230:        revdiff = ret = 0;
1.26      jfb       231:        rf = NULL;
1.45      joris     232:        islocal = (cvs_cmdop != CVS_OP_SERVER);
1.26      jfb       233:
1.45      joris     234:        root = CVS_DIR_ROOT(cf);
                    235:        repo = CVS_DIR_REPO(cf);
1.29      jfb       236:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    237:
1.37      joris     238:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.46      joris     239:                if (verbosity > 1)
                    240:                        cvs_printf("? %s\n", fpath);
1.37      joris     241:                return (CVS_EX_OK);
                    242:        }
                    243:
1.26      jfb       244:        if (cf->cf_type == DT_DIR) {
1.45      joris     245:                if (verbosity > 1)
                    246:                        cvs_log(LP_NOTICE, "Updating %s", fpath);
1.26      jfb       247:                return (CVS_EX_OK);
                    248:        }
                    249:
1.48    ! xsa       250:        cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.1       jfb       251:
1.45      joris     252:        /*
                    253:         * Only open the RCS file for files that have not been added.
                    254:         */
                    255:        if (cf->cf_cvstat != CVS_FST_ADDED) {
                    256:                rf = rcs_open(rcspath, RCS_READ);
                    257:
                    258:                /*
                    259:                 * If there is no RCS file available in the repository
                    260:                 * directory that matches this file, it's gone.
                    261:                 * XXX: so what about the Attic?
                    262:                 */
                    263:                if (rf == NULL) {
                    264:                        cvs_log(LP_WARN, "%s is no longer in the repository",
                    265:                            fpath);
                    266:                        if (cvs_checkout_rev(NULL, NULL, cf, fpath,
                    267:                            islocal, CHECKOUT_REV_REMOVED) < 0)
                    268:                                return (CVS_EX_FILE);
                    269:                        return (CVS_EX_OK);
                    270:                }
                    271:        }
                    272:
                    273:        /* set keyword expansion */
                    274:        /* XXX look at cf->cf_opts as well for this */
                    275:        if (rcs_kwexp_set(rf, kflag) < 0) {
                    276:                if (rf != NULL)
                    277:                        rcs_close(rf);
1.26      jfb       278:                return (CVS_EX_DATA);
                    279:        }
                    280:
1.45      joris     281:        /* fill in the correct revision */
                    282:        if (rev != NULL) {
                    283:                if ((frev = rcsnum_parse(rev)) == NULL) {
                    284:                        if (rf != NULL)
                    285:                                rcs_close(rf);
                    286:                        return (CVS_EX_DATA);
                    287:                }
                    288:        } else {
                    289:                frev = rf->rf_head;
                    290:        }
1.26      jfb       291:
1.45      joris     292:        /*
                    293:         * Compare the headrevision with the revision we currently have.
                    294:         */
                    295:        if (rf != NULL && cf->cf_lrev != NULL)
                    296:                revdiff = rcsnum_cmp(cf->cf_lrev, frev, 0);
                    297:
                    298:        switch (cf->cf_cvstat) {
                    299:        case CVS_FST_MODIFIED:
                    300:                /*
                    301:                 * If the file has been modified but there is a newer version
                    302:                 * available, we try to merge it into the existing changes.
                    303:                 */
                    304:                if (revdiff == 1) {
                    305:                        fbuf = cvs_diff3(rf, fpath, cf->cf_lrev, frev);
                    306:                        if (fbuf == NULL) {
                    307:                                cvs_log(LP_ERR, "merge failed");
                    308:                                break;
                    309:                        }
                    310:
                    311:                        /*
                    312:                         * Please note fbuf will be free'd in cvs_checkout_rev
                    313:                         */
                    314:                        if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
                    315:                            CHECKOUT_REV_MERGED, fbuf) != -1) {
                    316:                                cvs_printf("%c %s\n",
                    317:                                    (diff3_conflicts > 0) ? 'C' : 'M',
                    318:                                    fpath);
                    319:                                if (diff3_conflicts > 0)
                    320:                                        cf->cf_cvstat = CVS_FST_CONFLICT;
                    321:                        }
                    322:                } else {
                    323:                        cvs_printf("M %s\n", fpath);
                    324:                }
                    325:                break;
                    326:        case CVS_FST_ADDED:
                    327:                cvs_printf("A %s\n", fpath);
                    328:                break;
                    329:        case CVS_FST_REMOVED:
                    330:                cvs_printf("R %s\n", fpath);
                    331:                break;
                    332:        case CVS_FST_CONFLICT:
                    333:                cvs_printf("C %s\n", fpath);
                    334:                break;
                    335:        case CVS_FST_LOST:
                    336:                if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47      joris     337:                    CHECKOUT_REV_UPDATED) != -1) {
1.45      joris     338:                        cf->cf_cvstat = CVS_FST_UPTODATE;
                    339:                        cvs_printf("U %s\n", fpath);
                    340:                }
                    341:                break;
                    342:        case CVS_FST_UPTODATE:
                    343:                if (revdiff == 1) {
                    344:                        if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47      joris     345:                            CHECKOUT_REV_UPDATED) != -1)
1.45      joris     346:                                cvs_printf("P %s\n", fpath);
                    347:                }
                    348:                break;
                    349:        default:
                    350:                break;
                    351:        }
                    352:
                    353:        if ((frev != NULL) && (frev != rf->rf_head))
                    354:                rcsnum_free(frev);
                    355:        if (rf != NULL)
                    356:                rcs_close(rf);
                    357:
                    358:        return (CVS_EX_OK);
1.1       jfb       359: }