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

Annotation of src/usr.bin/cvs/checkout.c, Revision 1.100

1.100   ! tobias      1: /*     $OpenBSD: checkout.c,v 1.99 2007/07/25 08:45:24 xsa Exp $       */
1.1       jfb         2: /*
1.53      joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.53      joris       5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       jfb         8:  *
1.53      joris       9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       jfb        16:  */
                     17:
1.92      otto       18: #include <sys/param.h>
                     19: #include <sys/dirent.h>
                     20: #include <sys/stat.h>
                     21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
1.100   ! tobias     24: #include <libgen.h>
1.92      otto       25: #include <string.h>
                     26: #include <unistd.h>
1.1       jfb        27:
                     28: #include "cvs.h"
1.53      joris      29: #include "diff.h"
1.66      joris      30: #include "remote.h"
1.72      joris      31:
1.61      xsa        32: static void checkout_check_repository(int, char **);
1.53      joris      33: static void checkout_repository(const char *, const char *);
1.14      joris      34:
1.54      joris      35: extern int prune_dirs;
1.57      joris      36: extern int build_dirs;
1.54      joris      37:
1.78      xsa        38: static int flags = CR_REPO | CR_RECURSE_DIRS;
                     39:
1.22      jfb        40: struct cvs_cmd cvs_cmd_checkout = {
1.63      joris      41:        CVS_OP_CHECKOUT, 0, "checkout",
1.22      jfb        42:        { "co", "get" },
1.53      joris      43:        "Checkout a working copy of a repository",
1.33      xsa        44:        "[-AcflNnPpRs] [-D date | -r tag] [-d dir] [-j rev] [-k mode] "
1.22      jfb        45:        "[-t id] module ...",
1.77      xsa        46:        "AcD:d:fj:k:lNnPpRr:st:",
1.59      joris      47:        NULL,
                     48:        cvs_checkout
                     49: };
                     50:
                     51: struct cvs_cmd cvs_cmd_export = {
1.63      joris      52:        CVS_OP_EXPORT, 0, "export",
1.59      joris      53:        { "exp", "ex" },
                     54:        "Export sources from CVS, similar to checkout",
1.61      xsa        55:        "[-flNnR] [-d dir] [-k mode] -D date | -r rev module ...",
                     56:        "D:d:k:flNnRr:",
1.22      jfb        57:        NULL,
1.61      xsa        58:        cvs_export
1.33      xsa        59: };
                     60:
1.53      joris      61: int
                     62: cvs_checkout(int argc, char **argv)
1.1       jfb        63: {
1.78      xsa        64:        int ch;
1.13      jfb        65:
1.53      joris      66:        while ((ch = getopt(argc, argv, cvs_cmd_checkout.cmd_opts)) != -1) {
1.1       jfb        67:                switch (ch) {
1.76      xsa        68:                case 'A':
                     69:                        reset_stickies = 1;
                     70:                        break;
1.77      xsa        71:                case 'l':
                     72:                        flags &= ~CR_RECURSE_DIRS;
                     73:                        break;
1.54      joris      74:                case 'P':
                     75:                        prune_dirs = 1;
1.77      xsa        76:                        break;
                     77:                case 'R':
1.54      joris      78:                        break;
1.93      niallo     79:                case 'r':
1.97      joris      80:                        cvs_specified_tag = optarg;
1.93      niallo     81:                        break;
1.1       jfb        82:                default:
1.53      joris      83:                        fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.1       jfb        84:                }
                     85:        }
                     86:
                     87:        argc -= optind;
                     88:        argv += optind;
                     89:
1.53      joris      90:        if (argc == 0)
                     91:                fatal("%s", cvs_cmd_checkout.cmd_synopsis);
1.22      jfb        92:
1.61      xsa        93:        checkout_check_repository(argc, argv);
                     94:
                     95:        return (0);
                     96: }
                     97:
                     98: int
                     99: cvs_export(int argc, char **argv)
                    100: {
1.78      xsa       101:        int ch;
1.61      xsa       102:
                    103:        prune_dirs = 1;
                    104:
                    105:        while ((ch = getopt(argc, argv, cvs_cmd_export.cmd_opts)) != -1) {
                    106:                switch (ch) {
                    107:                case 'l':
                    108:                        flags &= ~CR_RECURSE_DIRS;
                    109:                        break;
                    110:                case 'R':
1.98      xsa       111:                        break;
                    112:                case 'r':
                    113:                        cvs_specified_tag = optarg;
1.61      xsa       114:                        break;
                    115:                default:
                    116:                        fatal("%s", cvs_cmd_export.cmd_synopsis);
                    117:                }
                    118:        }
                    119:
                    120:        argc -= optind;
                    121:        argv += optind;
                    122:
                    123:        if (argc == 0)
                    124:                fatal("%s", cvs_cmd_export.cmd_synopsis);
                    125:
                    126:        checkout_check_repository(argc, argv);
                    127:
                    128:        return (0);
                    129: }
                    130:
                    131: static void
                    132: checkout_check_repository(int argc, char **argv)
                    133: {
1.67      xsa       134:        int i;
1.61      xsa       135:        char repo[MAXPATHLEN];
                    136:        struct stat st;
1.79      joris     137:        struct cvs_recursion cr;
                    138:
                    139:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    140:                cvs_client_connect_to_server();
                    141:
1.97      joris     142:                if (cvs_specified_tag != NULL)
                    143:                        cvs_client_send_request("Argument -r%s",
                    144:                            cvs_specified_tag);
1.79      joris     145:                if (reset_stickies == 1)
                    146:                        cvs_client_send_request("Argument -A");
1.80      xsa       147:
                    148:                if (!(flags & CR_RECURSE_DIRS))
                    149:                        cvs_client_send_request("Argument -l");
1.79      joris     150:
                    151:                if (cvs_cmdop == CVS_OP_CHECKOUT && prune_dirs == 1)
                    152:                        cvs_client_send_request("Argument -P");
                    153:
                    154:                cr.enterdir = NULL;
                    155:                cr.leavedir = NULL;
                    156:                cr.fileproc = cvs_client_sendfile;
                    157:                cr.flags = flags;
                    158:
                    159:                cvs_file_run(argc, argv, &cr);
                    160:
                    161:                cvs_client_send_files(argv, argc);
                    162:                cvs_client_senddir(".");
                    163:
                    164:                cvs_client_send_request("%s",
                    165:                    (cvs_cmdop == CVS_OP_CHECKOUT) ? "co" : "export");
                    166:
                    167:                cvs_client_get_responses();
                    168:
                    169:                return;
                    170:        }
1.61      xsa       171:
1.53      joris     172:        for (i = 0; i < argc; i++) {
1.90      xsa       173:                (void)xsnprintf(repo, sizeof(repo), "%s/%s",
                    174:                    current_cvsroot->cr_dir, argv[i]);
1.13      jfb       175:
1.53      joris     176:                if (stat(repo, &st) == -1) {
1.100   ! tobias    177:                        /* check if a single file was requested */
        !           178:                        strlcat(repo, RCS_FILE_EXT, MAXPATHLEN);
        !           179:
        !           180:                        if (stat(repo, &st) == -1) {
        !           181:                                cvs_log(LP_ERR,
        !           182:                                    "cannot find module `%s' - ignored",
        !           183:                                    argv[i]);
        !           184:                                continue;
        !           185:                        }
        !           186:
        !           187:                        cr.fileproc = cvs_update_local;
        !           188:                        cr.flags = flags;
        !           189:                        cvs_mkpath(dirname(argv[i]), cvs_specified_tag);
        !           190:                        cvs_file_run(1, &(argv[i]), &cr);
        !           191:
1.53      joris     192:                        continue;
                    193:                }
1.97      joris     194:
                    195:                cvs_mkpath(argv[i], cvs_specified_tag);
1.53      joris     196:                checkout_repository(repo, argv[i]);
1.33      xsa       197:        }
1.14      joris     198: }
1.9       jfb       199:
1.53      joris     200: static void
                    201: checkout_repository(const char *repobase, const char *wdbase)
1.14      joris     202: {
1.53      joris     203:        struct cvs_flisthead fl, dl;
                    204:        struct cvs_recursion cr;
                    205:
                    206:        TAILQ_INIT(&fl);
                    207:        TAILQ_INIT(&dl);
1.94      joris     208:
1.99      xsa       209:        cvs_history_add((cvs_cmdop == CVS_OP_CHECKOUT) ?
                    210:            CVS_HISTORY_CHECKOUT : CVS_HISTORY_EXPORT, NULL, wdbase);
1.41      joris     211:
1.57      joris     212:        build_dirs = 1;
1.53      joris     213:        cr.enterdir = cvs_update_enterdir;
1.54      joris     214:        cr.leavedir = cvs_update_leavedir;
1.64      joris     215:        cr.fileproc = cvs_update_local;
1.78      xsa       216:        cr.flags = flags;
1.41      joris     217:
1.53      joris     218:        cvs_repository_lock(repobase);
1.56      joris     219:        cvs_repository_getdir(repobase, wdbase, &fl, &dl, 1);
1.1       jfb       220:
1.53      joris     221:        cvs_file_walklist(&fl, &cr);
                    222:        cvs_file_freelist(&fl);
1.23      joris     223:
1.53      joris     224:        cvs_repository_unlock(repobase);
1.23      joris     225:
1.53      joris     226:        cvs_file_walklist(&dl, &cr);
                    227:        cvs_file_freelist(&dl);
                    228: }
1.23      joris     229:
1.58      joris     230: void
1.78      xsa       231: cvs_checkout_file(struct cvs_file *cf, RCSNUM *rnum, int co_flags)
1.53      joris     232: {
1.89      xsa       233:        int kflag, oflags, exists;
1.53      joris     234:        time_t rcstime;
                    235:        CVSENTRIES *ent;
                    236:        struct timeval tv[2];
1.87      joris     237:        char *tosend;
1.97      joris     238:        char template[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
1.96      xsa       239:        char kbuf[8], stickytag[32], rev[CVS_REV_BUFSZ];
                    240:        char timebuf[CVS_TIME_BUFSZ], tbuf[CVS_TIME_BUFSZ];
1.22      jfb       241:
1.87      joris     242:        exists = 0;
                    243:        tosend = NULL;
1.53      joris     244:        rcsnum_tostr(rnum, rev, sizeof(rev));
1.41      joris     245:
1.66      joris     246:        cvs_log(LP_TRACE, "cvs_checkout_file(%s, %s, %d) -> %s",
1.78      xsa       247:            cf->file_path, rev, co_flags,
1.66      joris     248:            (cvs_server_active) ? "to client" : "to disk");
1.42      joris     249:
1.78      xsa       250:        if (co_flags & CO_DUMP) {
1.66      joris     251:                if (cvs_server_active) {
                    252:                        cvs_printf("dump file %s to client\n", cf->file_path);
                    253:                } else {
1.71      joris     254:                        rcs_rev_write_fd(cf->file_rcs, rnum,
                    255:                            STDOUT_FILENO, 1);
1.66      joris     256:                }
                    257:
1.65      reyk      258:                return;
                    259:        }
1.87      joris     260:
1.66      joris     261:        if (cvs_server_active == 0) {
1.87      joris     262:                if (!(co_flags & CO_MERGE)) {
                    263:                        oflags = O_WRONLY | O_TRUNC;
                    264:                        if (cf->fd != -1) {
                    265:                                exists = 1;
                    266:                                (void)close(cf->fd);
                    267:                        } else  {
                    268:                                oflags |= O_CREAT;
                    269:                        }
                    270:
                    271:                        cf->fd = open(cf->file_path, oflags);
                    272:                        if (cf->fd == -1)
                    273:                                fatal("cvs_checkout_file: open: %s",
                    274:                                    strerror(errno));
                    275:
                    276:                        rcs_rev_write_fd(cf->file_rcs, rnum, cf->fd, 1);
                    277:                } else {
                    278:                        cvs_merge_file(cf, 1);
1.66      joris     279:                }
                    280:
                    281:                if (fchmod(cf->fd, 0644) == -1)
                    282:                        fatal("cvs_checkout_file: fchmod: %s", strerror(errno));
                    283:
1.87      joris     284:                if ((exists == 0) && (cf->file_ent == NULL) &&
                    285:                    !(co_flags & CO_MERGE))
1.66      joris     286:                        rcstime = rcs_rev_getdate(cf->file_rcs, rnum);
1.84      joris     287:                else
1.66      joris     288:                        time(&rcstime);
                    289:
                    290:                tv[0].tv_sec = rcstime;
                    291:                tv[0].tv_usec = 0;
                    292:                tv[1] = tv[0];
                    293:                if (futimes(cf->fd, tv) == -1)
                    294:                        fatal("cvs_checkout_file: futimes: %s",
                    295:                            strerror(errno));
1.53      joris     296:        } else {
                    297:                time(&rcstime);
1.41      joris     298:        }
                    299:
1.84      joris     300:        asctime_r(gmtime(&rcstime), tbuf);
1.58      joris     301:        if (tbuf[strlen(tbuf) - 1] == '\n')
                    302:                tbuf[strlen(tbuf) - 1] = '\0';
                    303:
1.78      xsa       304:        if (co_flags & CO_MERGE) {
1.89      xsa       305:                (void)xsnprintf(timebuf, sizeof(timebuf), "Result of merge+%s",
1.58      joris     306:                    tbuf);
                    307:        } else {
                    308:                strlcpy(timebuf, tbuf, sizeof(timebuf));
                    309:        }
1.41      joris     310:
1.89      xsa       311:        if (co_flags & CO_SETSTICKY)
1.97      joris     312:                if (cvs_specified_tag != NULL)
1.93      niallo    313:                        (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
1.97      joris     314:                            cvs_specified_tag);
1.93      niallo    315:                else
                    316:                        (void)xsnprintf(stickytag, sizeof(stickytag), "T%s",
                    317:                            rev);
1.89      xsa       318:        else
1.60      joris     319:                stickytag[0] = '\0';
                    320:
1.85      xsa       321:        kbuf[0] = '\0';
                    322:        if (cf->file_ent != NULL) {
                    323:                if (cf->file_ent->ce_opts != NULL)
                    324:                        strlcpy(kbuf, cf->file_ent->ce_opts, sizeof(kbuf));
                    325:        } else if (cf->file_rcs->rf_expand != NULL) {
                    326:                kflag = rcs_kflag_get(cf->file_rcs->rf_expand);
                    327:                if (!(kflag & RCS_KWEXP_DEFAULT))
1.89      xsa       328:                        (void)xsnprintf(kbuf, sizeof(kbuf),
1.85      xsa       329:                            "-k%s", cf->file_rcs->rf_expand);
                    330:        }
                    331:
1.89      xsa       332:        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
1.85      xsa       333:            cf->file_name, rev, timebuf, kbuf, stickytag);
1.41      joris     334:
1.66      joris     335:        if (cvs_server_active == 0) {
1.95      joris     336:                if (!(co_flags & CO_REMOVE)) {
                    337:                        ent = cvs_ent_open(cf->file_wd);
                    338:                        cvs_ent_add(ent, entry);
                    339:                        cvs_ent_close(ent, ENT_SYNC);
                    340:                }
1.66      joris     341:        } else {
1.87      joris     342:                if (co_flags & CO_MERGE) {
                    343:                        cvs_merge_file(cf, 1);
                    344:                        tosend = cf->file_path;
                    345:                }
                    346:
1.78      xsa       347:                if (co_flags & CO_COMMIT)
1.69      joris     348:                        cvs_server_update_entry("Checked-in", cf);
1.87      joris     349:                else if (co_flags & CO_MERGE)
                    350:                        cvs_server_update_entry("Merged", cf);
1.95      joris     351:                else if (co_flags & CO_REMOVE)
                    352:                        cvs_server_update_entry("Removed", cf);
1.69      joris     353:                else
                    354:                        cvs_server_update_entry("Updated", cf);
1.68      joris     355:
1.95      joris     356:                if (!(co_flags & CO_REMOVE))
                    357:                        cvs_remote_output(entry);
1.66      joris     358:
1.95      joris     359:                if (!(co_flags & CO_COMMIT) && !(co_flags & CO_REMOVE)) {
1.87      joris     360:                        if (!(co_flags & CO_MERGE)) {
1.89      xsa       361:                                (void)xsnprintf(template, MAXPATHLEN,
1.87      joris     362:                                    "%s/checkout.XXXXXXXXXX", cvs_tmpdir);
1.89      xsa       363:
1.87      joris     364:                                rcs_rev_write_stmp(cf->file_rcs, rnum,
                    365:                                    template, 0);
                    366:                                tosend = template;
                    367:                        }
                    368:
                    369:                        cvs_remote_send_file(tosend);
                    370:
                    371:                        if (!(co_flags & CO_MERGE)) {
                    372:                                (void)unlink(template);
                    373:                                cvs_worklist_run(&temp_files,
                    374:                                    cvs_worklist_unlink);
                    375:                        }
1.68      joris     376:                }
1.66      joris     377:        }
1.1       jfb       378: }