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

1.34    ! xsa         1: /*     $OpenBSD: update.c,v 1.33 2005/05/30 08:27:03 xsa 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/param.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <errno.h>
1.34    ! xsa        31: #include <fcntl.h>
1.1       jfb        32: #include <stdio.h>
                     33: #include <stdlib.h>
1.34    ! xsa        34: #include <string.h>
1.1       jfb        35: #include <unistd.h>
                     36:
                     37: #include "cvs.h"
                     38: #include "log.h"
1.4       jfb        39: #include "proto.h"
1.1       jfb        40:
                     41:
1.29      jfb        42: static int cvs_update_init     (struct cvs_cmd *, int, char **, int *);
                     43: static int cvs_update_pre_exec (struct cvsroot *);
1.26      jfb        44: static int cvs_update_remote    (CVSFILE *, void *);
                     45: static int cvs_update_local     (CVSFILE *, void *);
1.18      joris      46:
1.29      jfb        47:
                     48: struct cvs_cmd cvs_cmd_update = {
                     49:        CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
                     50:        { "up", "upd" },
                     51:        "Bring work tree in sync with repository",
                     52:        "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
                     53:        "[-t id] ...",
                     54:        "ACD:dflPpQqRr:",
                     55:        NULL,
                     56:        CF_SORT | CF_RECURSE | CF_IGNORE | CF_KNOWN | CF_NOSYMS,
                     57:        cvs_update_init,
                     58:        cvs_update_pre_exec,
1.26      jfb        59:        cvs_update_remote,
1.29      jfb        60:        cvs_update_local,
                     61:        NULL,
                     62:        NULL,
1.18      joris      63:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
                     64: };
1.2       jfb        65:
1.32      xsa        66: static char *date, *rev;
1.31      joris      67: static int dflag, Aflag;
1.24      joris      68:
1.26      jfb        69: static int
1.29      jfb        70: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        71: {
1.18      joris      72:        int ch;
1.2       jfb        73:
1.31      joris      74:        dflag = Aflag = 0;
1.32      xsa        75:        date = NULL;
                     76:        rev = NULL;
1.24      joris      77:
1.29      jfb        78:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        79:                switch (ch) {
                     80:                case 'A':
1.24      joris      81:                        Aflag = 1;
                     82:                        break;
1.1       jfb        83:                case 'C':
                     84:                case 'D':
1.32      xsa        85:                        date = optarg;
                     86:                        break;
1.1       jfb        87:                case 'd':
1.24      joris      88:                        dflag = 1;
                     89:                        break;
1.1       jfb        90:                case 'f':
1.2       jfb        91:                        break;
1.1       jfb        92:                case 'l':
1.29      jfb        93:                        cmd->file_flags &= ~CF_RECURSE;
1.2       jfb        94:                        break;
1.1       jfb        95:                case 'P':
1.31      joris      96:                        cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24      joris      97:                        break;
1.1       jfb        98:                case 'p':
1.27      xsa        99:                        cvs_noexec = 1; /* no locks will be created */
                    100:                        break;
1.1       jfb       101:                case 'Q':
                    102:                case 'q':
1.2       jfb       103:                        break;
1.1       jfb       104:                case 'R':
1.29      jfb       105:                        cmd->file_flags |= CF_RECURSE;
1.2       jfb       106:                        break;
1.1       jfb       107:                case 'r':
1.32      xsa       108:                        rev = optarg;
1.1       jfb       109:                        break;
                    110:                default:
1.21      joris     111:                        return (CVS_EX_USAGE);
1.1       jfb       112:                }
1.32      xsa       113:        }
                    114:
1.18      joris     115:        *arg = optind;
1.2       jfb       116:        return (0);
                    117: }
                    118:
1.26      jfb       119: static int
1.29      jfb       120: cvs_update_pre_exec(struct cvsroot *root)
1.24      joris     121: {
1.31      joris     122:        if ((cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS) &&
                    123:            (cvs_sendarg(root, "-P", 0) < 0))
1.24      joris     124:                return (CVS_EX_PROTO);
1.25      joris     125:        if (Aflag && cvs_sendarg(root, "-A", 0) < 0)
1.24      joris     126:                return (CVS_EX_PROTO);
                    127:        if (dflag && cvs_sendarg(root, "-d", 0) < 0)
                    128:                return (CVS_EX_PROTO);
1.33      xsa       129:
                    130:        if (rev != NULL) {
                    131:                if ((cvs_sendarg(root, "-r", 0) < 0) ||
                    132:                    (cvs_sendarg(root, rev, 0) < 0))
                    133:                        return (CVS_EX_PROTO);
                    134:        }
1.24      joris     135:        return (0);
                    136: }
1.2       jfb       137:
                    138: /*
1.26      jfb       139:  * cvs_update_remote()
1.2       jfb       140:  *
1.12      jfb       141:  * Update a single file.  In the case where we act as client, send any
                    142:  * pertinent information about that file to the server.
1.2       jfb       143:  */
1.26      jfb       144: static int
                    145: cvs_update_remote(CVSFILE *cf, void *arg)
1.2       jfb       146: {
1.26      jfb       147:        int ret;
                    148:        char *repo, fpath[MAXPATHLEN];
1.2       jfb       149:        struct cvsroot *root;
                    150:
1.12      jfb       151:        ret = 0;
                    152:        root = CVS_DIR_ROOT(cf);
                    153:        repo = CVS_DIR_REPO(cf);
1.9       jfb       154:
1.13      jfb       155:        if (cf->cf_type == DT_DIR) {
1.26      jfb       156:                if (cf->cf_cvstat == CVS_FST_UNKNOWN)
                    157:                        ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
                    158:                            cf->cf_name);
                    159:                else
                    160:                        ret = cvs_senddir(root, cf);
1.13      jfb       161:
1.28      joris     162:                if (ret == -1)
                    163:                        ret = CVS_EX_PROTO;
                    164:
1.12      jfb       165:                return (ret);
1.2       jfb       166:        }
                    167:
1.12      jfb       168:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2       jfb       169:
1.26      jfb       170:        if (cvs_sendentry(root, cf) < 0)
                    171:                return (CVS_EX_PROTO);
1.2       jfb       172:
1.26      jfb       173:        switch (cf->cf_cvstat) {
                    174:        case CVS_FST_UNKNOWN:
                    175:                ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
                    176:                break;
                    177:        case CVS_FST_UPTODATE:
                    178:                ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
                    179:                break;
                    180:        case CVS_FST_ADDED:
                    181:        case CVS_FST_MODIFIED:
                    182:                ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
                    183:                if (ret == 0)
                    184:                        ret = cvs_sendfile(root, fpath);
                    185:                break;
                    186:        default:
                    187:                break;
1.2       jfb       188:        }
                    189:
1.28      joris     190:        if (ret == -1)
                    191:                ret = CVS_EX_PROTO;
                    192:
1.12      jfb       193:        return (ret);
1.2       jfb       194: }
                    195:
                    196: /*
1.26      jfb       197:  * cvs_update_local()
1.2       jfb       198:  */
1.26      jfb       199: static int
                    200: cvs_update_local(CVSFILE *cf, void *arg)
1.2       jfb       201: {
1.26      jfb       202:        int ret, l;
                    203:        char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
                    204:        RCSFILE *rf;
                    205:        struct cvsroot *root;
                    206:
                    207:        ret = 0;
                    208:        rf = NULL;
                    209:        root = CVS_DIR_ROOT(cf);
                    210:        repo = CVS_DIR_REPO(cf);
                    211:
1.29      jfb       212:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    213:
1.26      jfb       214:        if (cf->cf_type == DT_DIR) {
1.29      jfb       215:                cvs_log(LP_INFO, "Updating %s", fpath);
1.26      jfb       216:                return (CVS_EX_OK);
                    217:        }
                    218:
                    219:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
                    220:                cvs_printf("? %s\n", fpath);
                    221:                return (0);
                    222:        }
                    223:
                    224:        l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
                    225:            root->cr_dir, repo, cf->cf_name, RCS_FILE_EXT);
                    226:        if (l == -1 || l >= (int)sizeof(rcspath)) {
                    227:                errno = ENAMETOOLONG;
                    228:                cvs_log(LP_ERRNO, "%s", rcspath);
1.28      joris     229:                return (CVS_EX_DATA);
1.26      jfb       230:        }
1.1       jfb       231:
1.26      jfb       232:        rf = rcs_open(rcspath, RCS_RDWR);
                    233:        if (rf == NULL) {
1.29      jfb       234:                printf("failed here?\n");
1.26      jfb       235:                return (CVS_EX_DATA);
                    236:        }
                    237:
                    238:        rcs_close(rf);
                    239:
                    240:        return (ret);
1.1       jfb       241: }