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

Annotation of src/usr.bin/rcs/co.c, Revision 1.45

1.45    ! joris       1: /*     $OpenBSD: co.c,v 1.44 2005/12/08 18:56:10 joris Exp $   */
1.1       joris       2: /*
                      3:  * Copyright (c) 2005 Joris Vink <joris@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: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
1.4       joris      33: #include <unistd.h>
1.1       joris      34:
                     35: #include "log.h"
                     36: #include "rcs.h"
                     37: #include "rcsprog.h"
                     38:
1.38      xsa        39: #define CO_OPTSTRING   "f::k:l::M::p::q::r::s:Tu::Vw::x:"
                     40:
1.43      xsa        41: static void    checkout_err_nobranch(RCSFILE *, const char *, const char *,
                     42:     const char *, int);
1.4       joris      43:
1.1       joris      44: int
                     45: checkout_main(int argc, char **argv)
                     46: {
1.33      xsa        47:        int i, ch, flags, kflag;
1.4       joris      48:        RCSNUM *frev, *rev;
1.1       joris      49:        RCSFILE *file;
1.45    ! joris      50:        char fpath[MAXPATHLEN];
1.44      joris      51:        char *author, *username;
1.38      xsa        52:        const char *state;
1.37      xsa        53:        time_t rcs_mtime = -1;
1.1       joris      54:
1.24      niallo     55:        flags = 0;
1.33      xsa        56:        kflag = RCS_KWEXP_ERR;
1.1       joris      57:        rev = RCS_HEAD_REV;
1.6       joris      58:        frev = NULL;
1.44      joris      59:        state = NULL;
                     60:        author = NULL;
1.4       joris      61:
1.38      xsa        62:        while ((ch = rcs_getopt(argc, argv, CO_OPTSTRING)) != -1) {
1.1       joris      63:                switch (ch) {
1.18      joris      64:                case 'f':
1.19      joris      65:                        rcs_set_rev(rcs_optarg, &rev);
1.24      niallo     66:                        flags |= FORCE;
1.18      joris      67:                        break;
1.33      xsa        68:                case 'k':
                     69:                        kflag = rcs_kflag_get(rcs_optarg);
                     70:                        if (RCS_KWEXP_INVAL(kflag)) {
                     71:                                cvs_log(LP_ERR,
                     72:                                    "invalid RCS keyword expansion mode");
                     73:                                (usage)();
                     74:                                exit(1);
                     75:                        }
                     76:                        break;
1.4       joris      77:                case 'l':
1.19      joris      78:                        rcs_set_rev(rcs_optarg, &rev);
1.24      niallo     79:                        flags |= CO_LOCK;
1.26      niallo     80:                        break;
                     81:                case 'M':
                     82:                        rcs_set_rev(rcs_optarg, &rev);
                     83:                        flags |= CO_REVDATE;
1.4       joris      84:                        break;
1.20      joris      85:                case 'p':
                     86:                        rcs_set_rev(rcs_optarg, &rev);
                     87:                        pipeout = 1;
                     88:                        break;
1.3       joris      89:                case 'q':
1.32      xsa        90:                        rcs_set_rev(rcs_optarg, &rev);
1.3       joris      91:                        verbose = 0;
                     92:                        break;
1.1       joris      93:                case 'r':
1.19      joris      94:                        rcs_set_rev(rcs_optarg, &rev);
1.4       joris      95:                        break;
1.24      niallo     96:                case 's':
                     97:                        if ((state = strdup(rcs_optarg)) == NULL) {
                     98:                                cvs_log(LP_ERRNO, "out of memory");
                     99:                                exit(1);
                    100:                        }
                    101:                        flags |= CO_STATE;
1.34      xsa       102:                        break;
                    103:                case 'T':
                    104:                        flags |= PRESERVETIME;
1.24      niallo    105:                        break;
1.4       joris     106:                case 'u':
1.19      joris     107:                        rcs_set_rev(rcs_optarg, &rev);
1.24      niallo    108:                        flags |= CO_UNLOCK;
1.1       joris     109:                        break;
1.7       joris     110:                case 'V':
                    111:                        printf("%s\n", rcs_version);
                    112:                        exit(0);
1.38      xsa       113:                case 'w':
1.44      joris     114:                        /* if no argument, assume current user */
                    115:                        if (rcs_optarg == NULL) {
                    116:                                if ((author = getlogin()) == NULL) {
                    117:                                        cvs_log(LP_ERRNO,
                    118:                                            "could not get login");
                    119:                                        exit(1);
                    120:                                }
                    121:                        } else if ((author = strdup(rcs_optarg)) == NULL) {
                    122:                                cvs_log(LP_ERRNO, "out of memory");
                    123:                                exit(1);
                    124:                        }
                    125:
1.43      xsa       126:                        flags |= CO_AUTHOR;
1.38      xsa       127:                        break;
1.30      xsa       128:                case 'x':
                    129:                        rcs_suffixes = rcs_optarg;
                    130:                        break;
1.1       joris     131:                default:
                    132:                        (usage)();
                    133:                        exit(1);
                    134:                }
                    135:        }
                    136:
1.13      joris     137:        argc -= rcs_optind;
                    138:        argv += rcs_optind;
1.1       joris     139:
                    140:        if (argc == 0) {
                    141:                cvs_log(LP_ERR, "no input file");
                    142:                (usage)();
                    143:                exit (1);
                    144:        }
1.11      deraadt   145:
1.44      joris     146:        if ((username = getlogin()) == NULL) {
1.38      xsa       147:                cvs_log(LP_ERRNO, "failed to get username");
                    148:                exit (1);
                    149:        }
                    150:
1.1       joris     151:        for (i = 0; i < argc; i++) {
                    152:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    153:                        continue;
                    154:
1.21      niallo    155:                if (verbose == 1)
1.22      xsa       156:                        printf("%s  -->  %s\n", fpath,
                    157:                            (pipeout == 1) ? "standard output" : argv[i]);
1.35      xsa       158:
                    159:                if ((flags & CO_LOCK) && (kflag & RCS_KWEXP_VAL)) {
                    160:                        cvs_log(LP_ERR, "%s: cannot combine -kv and -l", fpath);
                    161:                        continue;
                    162:                }
1.21      niallo    163:
1.4       joris     164:                if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.1       joris     165:                        continue;
                    166:
1.37      xsa       167:                if (flags & PRESERVETIME)
                    168:                        rcs_mtime = rcs_get_mtime(file->rf_path);
                    169:
                    170:                if (kflag != RCS_KWEXP_ERR)
                    171:                        rcs_kwexp_set(file, kflag);
                    172:
1.4       joris     173:                if (rev == RCS_HEAD_REV)
                    174:                        frev = file->rf_head;
                    175:                else
                    176:                        frev = rev;
1.17      joris     177:
1.44      joris     178:                if (checkout_rev(file, frev, argv[i], flags,
                    179:                    username, author, state) < 0) {
1.24      niallo    180:                                rcs_close(file);
                    181:                                continue;
1.8       niallo    182:                }
1.17      joris     183:
1.1       joris     184:                rcs_close(file);
1.37      xsa       185:
                    186:                if (flags & PRESERVETIME)
                    187:                        rcs_set_mtime(fpath, rcs_mtime);
1.1       joris     188:        }
                    189:
                    190:        if (rev != RCS_HEAD_REV)
1.5       joris     191:                rcsnum_free(frev);
1.1       joris     192:
                    193:        return (0);
                    194: }
                    195:
                    196: void
                    197: checkout_usage(void)
                    198: {
1.11      deraadt   199:        fprintf(stderr,
1.42      xsa       200:            "usage: co [-TV] [-ddate] [-f[rev]] [-I[rev]] [-kmode] [-l[rev]]\n"
                    201:            "          [-M[rev]] [-p[rev]] [-q[rev]] [-r[rev]] [-sstate]\n"
                    202:            "          [-u[rev]] [-w[user]] [-xsuffixes] [-ztz] file ...\n");
1.1       joris     203: }
1.14      niallo    204:
                    205: /*
                    206:  * Checkout revision <rev> from RCSFILE <file>, writing it to the path <dst>
1.29      xsa       207:  * Currenly recognised <flags> are CO_LOCK, CO_UNLOCK and CO_REVDATE.
1.14      niallo    208:  *
1.44      joris     209:  * Looks up revision based upon <lockname>, <author>, <state>
                    210:  *
1.14      niallo    211:  * Returns 0 on success, -1 on failure.
                    212:  */
                    213: int
1.24      niallo    214: checkout_rev(RCSFILE *file, RCSNUM *frev, const char *dst, int flags,
1.44      joris     215:     const char *lockname, const char *author, const char *state)
1.14      niallo    216: {
1.44      joris     217:        BUF *bp;
                    218:        int lcount;
1.18      joris     219:        char buf[16], yn;
1.14      niallo    220:        mode_t mode = 0444;
1.18      joris     221:        struct stat st;
1.44      joris     222:        struct rcs_delta *rdp;
                    223:        struct rcs_lock *lkp;
                    224:        char *content, msg[128];
1.14      niallo    225:
1.39      xsa       226:        /* Check out the latest revision if <frev> is greater than HEAD */
1.15      niallo    227:        if (rcsnum_cmp(frev, file->rf_head, 0) == -1)
1.23      xsa       228:                frev = file->rf_head;
1.15      niallo    229:
1.44      joris     230:        lcount = 0;
                    231:        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
                    232:                if (!strcmp(lkp->rl_name, lockname))
                    233:                        lcount++;
                    234:        }
                    235:
                    236:        /*
                    237:         * If the user didn't specify any revision, we cycle through
                    238:         * revisions to lookup the first one that matches what he specified.
                    239:         *
                    240:         * If we cannot find one, we return an error.
                    241:         */
                    242:        rdp = NULL;
                    243:        if (frev == file->rf_head) {
                    244:                if (lcount > 1) {
                    245:                        cvs_log(LP_WARN,
                    246:                            "multiple revisions locked by %s; "
                    247:                            "please specify one", lockname);
                    248:                        return (-1);
                    249:                }
                    250:
                    251:                TAILQ_FOREACH(rdp, &file->rf_delta, rd_list) {
                    252:                        if ((author != NULL) &&
                    253:                            (strcmp(rdp->rd_author, author)))
                    254:                                continue;
                    255:                        if ((state != NULL) &&
                    256:                            (strcmp(rdp->rd_state, state)))
                    257:                                continue;
                    258:
                    259:                        frev = rdp->rd_num;
                    260:                        break;
                    261:                }
                    262:        } else {
                    263:                rdp = rcs_findrev(file, frev);
                    264:        }
                    265:
                    266:        if (rdp == NULL) {
                    267:                checkout_err_nobranch(file, author, NULL, state, flags);
                    268:                return (-1);
                    269:        }
                    270:
1.15      niallo    271:        rcsnum_tostr(frev, buf, sizeof(buf));
                    272:
1.44      joris     273:        if (rdp->rd_locker != NULL) {
                    274:                if (strcmp(lockname, rdp->rd_locker)) {
                    275:                        strlcpy(msg, "Revision %s is already locked by %s; ",
                    276:                            sizeof(msg));
                    277:                        if (flags & CO_UNLOCK)
                    278:                                strlcat(msg, "use co -r or rcs -u", sizeof(msg));
                    279:                        cvs_log(LP_ERR, msg, buf, rdp->rd_locker);
                    280:                        return (-1);
                    281:                }
                    282:        }
                    283:
1.18      joris     284:        if (verbose == 1)
                    285:                printf("revision %s", buf);
                    286:
1.44      joris     287:
1.14      niallo    288:        if ((bp = rcs_getrev(file, frev)) == NULL) {
                    289:                cvs_log(LP_ERR, "cannot find revision `%s'", buf);
                    290:                return (-1);
                    291:        }
                    292:
1.24      niallo    293:        if (flags & CO_LOCK) {
1.44      joris     294:                if ((lockname != NULL)
                    295:                    && (rcs_lock_add(file, lockname, frev) < 0)) {
                    296:                        if (rcs_errno != RCS_ERR_DUPENT)
                    297:                                return (-1);
1.14      niallo    298:                }
1.18      joris     299:
1.14      niallo    300:                mode = 0644;
1.18      joris     301:                if (verbose == 1)
                    302:                        printf(" (locked)");
1.24      niallo    303:        } else if (flags & CO_UNLOCK) {
1.44      joris     304:                if (rcs_lock_remove(file, lockname, frev) < 0)
                    305:                        return (-1);
1.18      joris     306:
1.14      niallo    307:                mode = 0444;
1.18      joris     308:                if (verbose == 1)
                    309:                        printf(" (unlocked)");
                    310:        }
                    311:
                    312:        if (verbose == 1)
                    313:                printf("\n");
                    314:
1.44      joris     315:        if (flags & CO_LOCK) {
                    316:                lcount++;
                    317:                if (lcount > 1)
                    318:                        cvs_log(LP_WARN, "You now have %d locks.", lcount);
                    319:        }
                    320:
1.41      xsa       321:        if ((pipeout == 0) && (stat(dst, &st) == 0) && !(flags & FORCE)) {
1.18      joris     322:                if (st.st_mode & S_IWUSR) {
                    323:                        yn = 0;
1.21      niallo    324:                        if (verbose == 0) {
                    325:                                cvs_log(LP_ERR,
1.40      xsa       326:                                    "writable %s exists; checkout aborted",
1.21      niallo    327:                                    dst);
                    328:                                return (-1);
                    329:                        }
1.41      xsa       330:
                    331:                        while ((yn != 'y') && (yn != 'n')) {
                    332:                                printf("writable %s exists%s; ", dst,
                    333:                                    ((uid_t)getuid() == st.st_uid) ? "" :
                    334:                                    ", and you do not own it");
1.21      niallo    335:                                printf("remove it? [ny](n): ");
1.18      joris     336:                                fflush(stdout);
                    337:                                yn = getchar();
                    338:                        }
                    339:
                    340:                        if (yn == 'n') {
1.21      niallo    341:                                cvs_log(LP_ERR, "checkout aborted");
1.18      joris     342:                                return (-1);
                    343:                        }
                    344:                }
1.14      niallo    345:        }
1.17      joris     346:
1.20      joris     347:        if (pipeout == 1) {
                    348:                cvs_buf_putc(bp, '\0');
                    349:                content = cvs_buf_release(bp);
                    350:                printf("%s", content);
                    351:                free(content);
                    352:        } else {
                    353:                if (cvs_buf_write(bp, dst, mode) < 0) {
                    354:                        cvs_log(LP_ERR, "failed to write revision to file");
                    355:                        cvs_buf_free(bp);
                    356:                        return (-1);
                    357:                }
1.14      niallo    358:                cvs_buf_free(bp);
1.24      niallo    359:                if (flags & CO_REVDATE) {
                    360:                        struct timeval tv[2];
1.36      xsa       361:                        memset(&tv, 0, sizeof(tv));
1.24      niallo    362:                        tv[0].tv_sec = (long)rcs_rev_getdate(file, frev);
                    363:                        tv[1].tv_sec = tv[0].tv_sec;
                    364:                        if (utimes(dst, (const struct timeval *)&tv) < 0)
                    365:                                cvs_log(LP_ERRNO, "error setting utimes");
                    366:                }
1.20      joris     367:
                    368:                if (verbose == 1)
                    369:                        printf("done\n");
1.14      niallo    370:        }
1.17      joris     371:
1.14      niallo    372:        return (0);
1.43      xsa       373: }
                    374:
                    375: /*
                    376:  * checkout_err_nobranch()
                    377:  *
                    378:  * XXX - should handle the dates too.
                    379:  */
                    380: static void
                    381: checkout_err_nobranch(RCSFILE *file, const char *author, const char *date,
                    382:     const char *state, int flags)
                    383: {
                    384:        if (!(flags & CO_AUTHOR))
                    385:                author = NULL;
                    386:        if (!(flags & CO_STATE))
                    387:                state = NULL;
                    388:
                    389:        cvs_log(LP_ERR, "%s: No revision on branch has%s%s%s%s%s%s.",
                    390:            file->rf_path,
                    391:            date ? " a date before " : "",
                    392:            date ? date : "",
                    393:            author ? " and author " + (date ? 0:4 ) : "",
                    394:            author ? author : "",
                    395:            state  ? " and state " + (date || author ? 0:4) : "",
                    396:            state  ? state : "");
1.24      niallo    397: }