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

Annotation of src/usr.bin/cvs/admin.c, Revision 1.16

1.16    ! jfb         1: /*     $OpenBSD: admin.c,v 1.15 2005/05/24 04:12:25 jfb Exp $  */
1.1       joris       2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * Copyright (c) 2005 Joris Vink <joris@openbsd.org>
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  *
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. The name of the author may not be used to endorse or promote products
                     14:  *    derived from this software without specific prior written permission.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     17:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     18:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     19:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     20:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     21:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     22:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     23:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     24:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     25:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
                     28: #include <sys/types.h>
                     29: #include <sys/stat.h>
                     30:
                     31: #include <errno.h>
                     32: #include <stdio.h>
                     33: #include <fcntl.h>
                     34: #include <stdlib.h>
                     35: #include <unistd.h>
                     36: #include <string.h>
                     37:
1.16    ! jfb        38: #include "rcs.h"
1.1       joris      39: #include "cvs.h"
                     40: #include "log.h"
                     41: #include "proto.h"
                     42:
                     43:
                     44: #define LOCK_SET       0x01
                     45: #define LOCK_REMOVE    0x02
                     46:
                     47: #define FLAG_BRANCH            0x01
                     48: #define FLAG_DELUSER           0x02
                     49: #define FLAG_INTERACTIVE       0x04
                     50: #define FLAG_QUIET             0x08
                     51:
1.15      jfb        52: static int cvs_admin_init     (struct cvs_cmd *, int, char **, int *);
                     53: static int cvs_admin_pre_exec (struct cvsroot *);
1.16    ! jfb        54: static int cvs_admin_remote   (CVSFILE *, void *);
        !            55: static int cvs_admin_local    (CVSFILE *, void *);
1.15      jfb        56:
                     57: struct cvs_cmd cvs_cmd_admin = {
                     58:        CVS_OP_ADMIN, CVS_REQ_ADMIN, "admin",
                     59:        { "adm", "rcs" },
                     60:        "Administrative front-end for RCS",
                     61:        "",
                     62:        "a:A:b::c:e::Ik:l::Lm:n:N:o:qs:t:u::U",
                     63:        NULL,
                     64:        CF_SORT | CF_IGNORE | CF_RECURSE,
                     65:        cvs_admin_init,
                     66:        cvs_admin_pre_exec,
1.16    ! jfb        67:        cvs_admin_remote,
        !            68:        cvs_admin_local,
1.15      jfb        69:        NULL,
                     70:        NULL,
1.7       joris      71:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
                     72: };
                     73:
                     74: static char *q, *Ntag, *ntag, *comment, *replace_msg;
                     75: static char *alist, *subst, *lockrev_arg, *unlockrev_arg;
                     76: static char *state, *userfile, *branch_arg, *elist, *range;
1.16    ! jfb        77: static int runflags, kflag, lockrev, lkmode;
        !            78:
        !            79: /* flag as invalid */
        !            80: static int kflag = RCS_KWEXP_ERR;
        !            81: static int lkmode = RCS_LOCK_INVAL;
1.7       joris      82:
1.15      jfb        83: static int
                     84: cvs_admin_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       joris      85: {
1.7       joris      86:        int ch;
1.1       joris      87:        RCSNUM *rcs;
                     88:
1.16    ! jfb        89:        runflags = lockrev = 0;
1.5       joris      90:        Ntag = ntag = comment = replace_msg = NULL;
                     91:        state = alist = subst = elist = lockrev_arg = NULL;
                     92:        range = userfile = branch_arg = unlockrev_arg = NULL;
1.1       joris      93:
                     94:        /* option-o-rama ! */
1.15      jfb        95:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       joris      96:                switch (ch) {
                     97:                case 'a':
                     98:                        alist = optarg;
                     99:                        break;
                    100:                case 'A':
                    101:                        userfile = optarg;
                    102:                        break;
                    103:                case 'b':
                    104:                        runflags |= FLAG_BRANCH;
                    105:                        if (optarg)
                    106:                                branch_arg = optarg;
                    107:                        break;
                    108:                case 'c':
                    109:                        comment = optarg;
                    110:                        break;
                    111:                case 'e':
                    112:                        runflags |= FLAG_DELUSER;
                    113:                        if (optarg)
                    114:                                elist = optarg;
                    115:                        break;
                    116:                case 'I':
                    117:                        runflags |= FLAG_INTERACTIVE;
                    118:                        break;
                    119:                case 'k':
                    120:                        subst = optarg;
1.3       joris     121:                        kflag = rcs_kflag_get(subst);
                    122:                        if (RCS_KWEXP_INVAL(kflag)) {
                    123:                                cvs_log(LP_ERR,
                    124:                                    "invalid RCS keyword expansion mode");
                    125:                                rcs_kflag_usage();
1.11      joris     126:                                return (CVS_EX_USAGE);
1.3       joris     127:                        }
1.1       joris     128:                        break;
                    129:                case 'l':
                    130:                        lockrev |= LOCK_SET;
                    131:                        if (optarg)
                    132:                                lockrev_arg = optarg;
                    133:                        break;
                    134:                case 'L':
1.16    ! jfb       135:                        lkmode = RCS_LOCK_STRICT;
1.1       joris     136:                        break;
                    137:                case 'm':
                    138:                        replace_msg = optarg;
                    139:                        break;
                    140:                case 'n':
1.5       joris     141:                        ntag = optarg;
1.1       joris     142:                        break;
                    143:                case 'N':
1.5       joris     144:                        Ntag = optarg;
1.1       joris     145:                        break;
                    146:                case 'o':
1.5       joris     147:                        range = optarg;
1.1       joris     148:                        break;
                    149:                case 'q':
                    150:                        runflags |= FLAG_QUIET;
                    151:                        break;
                    152:                case 's':
1.5       joris     153:                        state = optarg;
1.1       joris     154:                        break;
                    155:                case 't':
                    156:                        break;
                    157:                case 'u':
                    158:                        lockrev |= LOCK_REMOVE;
                    159:                        if (optarg)
                    160:                                unlockrev_arg = optarg;
                    161:                        break;
                    162:                case 'U':
1.16    ! jfb       163:                        if (lkmode != RCS_LOCK_INVAL) {
        !           164:                                cvs_log(LP_ERR, "-L and -U are incompatible");
        !           165:                                return (CVS_EX_USAGE);
        !           166:                        }
        !           167:                        lkmode = RCS_LOCK_LOOSE;
1.1       joris     168:                        break;
                    169:                default:
1.11      joris     170:                        return (CVS_EX_USAGE);
1.1       joris     171:                }
                    172:        }
                    173:
                    174:        argc -= optind;
                    175:        argv += optind;
                    176:
                    177:        if (lockrev_arg != NULL) {
                    178:                if ((rcs = rcsnum_parse(lockrev_arg)) == NULL) {
                    179:                        cvs_log(LP_ERR, "%s is not a numeric branch",
                    180:                            lockrev_arg);
1.11      joris     181:                        return (CVS_EX_USAGE);
1.1       joris     182:                }
                    183:                rcsnum_free(rcs);
                    184:        }
                    185:
                    186:        if (unlockrev_arg != NULL) {
                    187:                if ((rcs = rcsnum_parse(unlockrev_arg)) == NULL) {
                    188:                        cvs_log(LP_ERR, "%s is not a numeric branch",
                    189:                            unlockrev_arg);
1.11      joris     190:                        return (CVS_EX_USAGE);
1.1       joris     191:                }
                    192:                rcsnum_free(rcs);
                    193:        }
                    194:
                    195:        if (replace_msg != NULL) {
                    196:                if ((q = strchr(replace_msg, ':')) == NULL) {
                    197:                        cvs_log(LP_ERR, "invalid option for -m");
1.11      joris     198:                        return (CVS_EX_USAGE);
1.1       joris     199:                }
                    200:                *q = '\0';
                    201:                if ((rcs = rcsnum_parse(replace_msg)) == NULL) {
                    202:                        cvs_log(LP_ERR, "%s is not a numeric revision",
                    203:                            replace_msg);
1.11      joris     204:                        return (CVS_EX_USAGE);
1.1       joris     205:                }
1.2       joris     206:                rcsnum_free(rcs);
1.1       joris     207:                *q = ':';
                    208:        }
                    209:
1.7       joris     210:        *arg = optind;
                    211:        return (0);
                    212: }
1.1       joris     213:
1.15      jfb       214: static int
                    215: cvs_admin_pre_exec(struct cvsroot *root)
1.7       joris     216: {
1.16    ! jfb       217:        if (root->cr_method == CVS_METHOD_LOCAL)
        !           218:                return (0);
        !           219:
1.7       joris     220:        if ((alist != NULL) && ((cvs_sendarg(root, "-a", 0) < 0) ||
                    221:            (cvs_sendarg(root, alist, 0) < 0)))
1.11      joris     222:                return (CVS_EX_PROTO);
1.1       joris     223:
1.7       joris     224:        if ((userfile != NULL) && ((cvs_sendarg(root, "-A", 0) < 0) ||
                    225:            (cvs_sendarg(root, userfile, 0) < 0)))
1.11      joris     226:                return (CVS_EX_PROTO);
1.1       joris     227:
1.7       joris     228:        if (runflags & FLAG_BRANCH) {
                    229:                if (cvs_sendarg(root, "-b", 0) < 0)
1.11      joris     230:                        return (CVS_EX_PROTO);
1.7       joris     231:                if ((branch_arg != NULL) &&
                    232:                    (cvs_sendarg(root, branch_arg, 0) < 0))
1.11      joris     233:                        return (CVS_EX_PROTO);
1.7       joris     234:        }
1.1       joris     235:
1.7       joris     236:        if ((comment != NULL) && ((cvs_sendarg(root, "-c", 0) < 0) ||
                    237:            (cvs_sendarg(root, comment, 0) < 0)))
1.11      joris     238:                return (CVS_EX_PROTO);
1.1       joris     239:
1.7       joris     240:        if (runflags & FLAG_DELUSER)  {
                    241:                if (cvs_sendarg(root, "-e", 0) < 0)
1.11      joris     242:                        return (CVS_EX_PROTO);
1.7       joris     243:                if ((elist != NULL) &&
                    244:                    (cvs_sendarg(root, elist, 0) < 0))
1.11      joris     245:                        return (CVS_EX_PROTO);
1.7       joris     246:        }
1.1       joris     247:
1.7       joris     248:        if (runflags & FLAG_INTERACTIVE) {
                    249:                if (cvs_sendarg(root, "-I", 0) < 0)
1.11      joris     250:                        return (CVS_EX_PROTO);
1.7       joris     251:        }
1.1       joris     252:
1.7       joris     253:        if ((subst != NULL) && ((cvs_sendarg(root, "-k", 0) < 0) ||
                    254:            (cvs_sendarg(root, subst, 0) < 0)))
1.11      joris     255:                return (CVS_EX_PROTO);
1.5       joris     256:
1.7       joris     257:        if (lockrev & LOCK_SET) {
                    258:                if (cvs_sendarg(root, "-l", 0) < 0)
1.11      joris     259:                        return (CVS_EX_PROTO);
1.7       joris     260:                if ((lockrev_arg != NULL) &&
                    261:                    (cvs_sendarg(root, lockrev_arg, 0) < 0))
1.11      joris     262:                        return (CVS_EX_PROTO);
1.7       joris     263:        }
1.5       joris     264:
1.16    ! jfb       265:        if ((lkmode == RCS_LOCK_STRICT) && (cvs_sendarg(root, "-L", 0) < 0))
        !           266:                return (CVS_EX_PROTO);
        !           267:        else if ((lkmode == RCS_LOCK_LOOSE) && (cvs_sendarg(root, "-U", 0) < 0))
1.11      joris     268:                return (CVS_EX_PROTO);
1.5       joris     269:
1.7       joris     270:        if ((replace_msg != NULL) && ((cvs_sendarg(root, "-m", 0) < 0)
                    271:            || (cvs_sendarg(root, replace_msg, 0) < 0)))
1.11      joris     272:                return (CVS_EX_PROTO);
1.5       joris     273:
1.7       joris     274:        if ((ntag != NULL) && ((cvs_sendarg(root, "-n", 0) < 0) ||
                    275:            (cvs_sendarg(root, ntag, 0) < 0)))
1.11      joris     276:                return (CVS_EX_PROTO);
1.1       joris     277:
1.7       joris     278:        if ((Ntag != NULL) && ((cvs_sendarg(root, "-N", 0) < 0) ||
                    279:            (cvs_sendarg(root, Ntag, 0) < 0)))
1.11      joris     280:                return (CVS_EX_PROTO);
1.1       joris     281:
1.7       joris     282:        if ((range != NULL) && ((cvs_sendarg(root, "-o", 0) < 0) ||
                    283:            (cvs_sendarg(root, range, 0) < 0)))
1.11      joris     284:                return (CVS_EX_PROTO);
1.1       joris     285:
1.7       joris     286:        if ((state != NULL) && ((cvs_sendarg(root, "-s", 0) < 0) ||
                    287:            (cvs_sendarg(root, state, 0) < 0)))
1.11      joris     288:                return (CVS_EX_PROTO);
1.1       joris     289:
1.7       joris     290:        if (lockrev & LOCK_REMOVE) {
                    291:                if (cvs_sendarg(root, "-u", 0) < 0)
1.11      joris     292:                        return (CVS_EX_PROTO);
1.7       joris     293:                if ((unlockrev_arg != NULL) &&
                    294:                    (cvs_sendarg(root, unlockrev_arg, 0) < 0))
1.11      joris     295:                        return (CVS_EX_PROTO);
1.1       joris     296:        }
                    297:
                    298:        return (0);
                    299: }
                    300:
                    301: /*
1.16    ! jfb       302:  * cvs_admin_remote()
1.1       joris     303:  *
                    304:  * Perform admin commands on each file.
                    305:  */
1.15      jfb       306: static int
1.16    ! jfb       307: cvs_admin_remote(CVSFILE *cf, void *arg)
1.1       joris     308: {
1.16    ! jfb       309:        int ret;
        !           310:        char *repo, fpath[MAXPATHLEN];
1.1       joris     311:        struct cvsroot *root;
                    312:
                    313:        ret = 0;
1.16    ! jfb       314:        root = CVS_DIR_ROOT(cf);
        !           315:        repo = CVS_DIR_REPO(cf);
1.14      joris     316:
1.16    ! jfb       317:        if (cf->cf_type == DT_DIR) {
        !           318:                if (cf->cf_cvstat == CVS_FST_UNKNOWN)
        !           319:                        ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
        !           320:                            cf->cf_name);
        !           321:                else
        !           322:                        ret = cvs_senddir(root, cf);
        !           323:                if (ret == -1)
        !           324:                        ret = CVS_EX_PROTO;
1.1       joris     325:
                    326:                return (ret);
                    327:        }
                    328:
1.16    ! jfb       329:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       joris     330:
1.16    ! jfb       331:        if (cvs_sendentry(root, cf) < 0)
        !           332:                return (CVS_EX_PROTO);
        !           333:
        !           334:        switch (cf->cf_cvstat) {
        !           335:        case CVS_FST_UNKNOWN:
        !           336:                ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
        !           337:                break;
        !           338:        case CVS_FST_UPTODATE:
        !           339:                ret = cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
        !           340:                break;
        !           341:        case CVS_FST_MODIFIED:
        !           342:                ret = cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
        !           343:                if (ret == 0)
        !           344:                        ret = cvs_sendfile(root, fpath);
        !           345:        default:
        !           346:                break;
        !           347:        }
1.1       joris     348:
1.16    ! jfb       349:        return (ret);
        !           350: }
1.14      joris     351:
1.16    ! jfb       352: /*
        !           353:  * cvs_admin_local()
        !           354:  *
        !           355:  * Perform administrative operations on a local RCS file.
        !           356:  */
        !           357: static int
        !           358: cvs_admin_local(CVSFILE *cf, void *arg)
        !           359: {
        !           360:        int ret, len;
        !           361:        char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
        !           362:        RCSFILE *rf;
        !           363:        struct cvsroot *root;
1.1       joris     364:
1.16    ! jfb       365:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
        !           366:                cvs_log(LP_WARN, "I know nothing about %s", fpath);
        !           367:                return (0);
        !           368:        }
1.1       joris     369:
1.16    ! jfb       370:        cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       joris     371:
1.16    ! jfb       372:        len = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
        !           373:            root->cr_dir, repo, cf->cf_name, RCS_FILE_EXT);
        !           374:        if (len == -1 || len >= (int)sizeof(rcspath)) {
        !           375:                errno = ENAMETOOLONG;
        !           376:                cvs_log(LP_ERRNO, "%s", rcspath);
        !           377:                return (-1);
1.1       joris     378:        }
                    379:
1.16    ! jfb       380:        rf = rcs_open(rcspath, RCS_RDWR);
        !           381:        if (rf == NULL)
        !           382:                return (CVS_EX_DATA);
        !           383:
        !           384:        if (!RCS_KWEXP_INVAL(kflag))
        !           385:                ret = rcs_kwexp_set(rf, kflag);
        !           386:        if (lkmode != RCS_LOCK_INVAL)
        !           387:                ret = rcs_lock_setmode(rf, lkmode);
        !           388:
        !           389:        rcs_close(rf);
        !           390:
        !           391:        return (0);
1.1       joris     392: }