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

Annotation of src/usr.bin/cvs/client.c, Revision 1.71

1.71    ! xsa         1: /*     $OpenBSD: client.c,v 1.70 2007/07/17 16:13:39 xsa Exp $ */
1.1       jfb         2: /*
1.9       joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.9       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.
                      8:  *
                      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.59      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>
                     24: #include <libgen.h>
                     25: #include <limits.h>
                     26: #include <pwd.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29: #include <unistd.h>
1.1       jfb        30:
                     31: #include "cvs.h"
1.9       joris      32: #include "remote.h"
1.1       jfb        33:
1.9       joris      34: struct cvs_req cvs_requests[] = {
                     35:        /* this is what our client will use, the server should support it */
                     36:        { "Root",               1,      cvs_server_root, REQ_NEEDED },
                     37:        { "Valid-responses",    1,      cvs_server_validresp, REQ_NEEDED },
                     38:        { "valid-requests",     1,      cvs_server_validreq, REQ_NEEDED },
                     39:        { "Directory",          0,      cvs_server_directory, REQ_NEEDED },
1.66      joris      40:        { "Static-directory",   0,      cvs_server_static_directory,
                     41:            REQ_NEEDED | REQ_NEEDDIR },
                     42:        { "Sticky",             0,      cvs_server_sticky,
                     43:            REQ_NEEDED | REQ_NEEDDIR },
                     44:        { "Entry",              0,      cvs_server_entry,
                     45:            REQ_NEEDED | REQ_NEEDDIR },
                     46:        { "Modified",           0,      cvs_server_modified,
                     47:            REQ_NEEDED | REQ_NEEDDIR },
1.9       joris      48:        { "UseUnchanged",       0,      cvs_server_useunchanged, REQ_NEEDED },
1.66      joris      49:        { "Unchanged",          0,      cvs_server_unchanged,
                     50:            REQ_NEEDED | REQ_NEEDDIR },
1.9       joris      51:        { "Questionable",       0,      cvs_server_questionable, REQ_NEEDED },
                     52:        { "Argument",           0,      cvs_server_argument, REQ_NEEDED },
1.24      xsa        53:        { "Argumentx",          0,      cvs_server_argumentx, REQ_NEEDED },
                     54:        { "Global_option",      0,      cvs_server_globalopt, REQ_NEEDED },
1.26      xsa        55:        { "Set",                0,      cvs_server_set, REQ_NEEDED },
1.1       jfb        56:
1.9       joris      57:        /*
                     58:         * used to tell the server what is going on in our
                     59:         * working copy, unsupported until we are told otherwise
                     60:         */
                     61:        { "Max-dotdot",                 0,      NULL, 0 },
                     62:        { "Checkin-prog",               0,      NULL, 0 },
                     63:        { "Update-prog",                0,      NULL, 0 },
                     64:        { "Kopt",                       0,      NULL, 0 },
                     65:        { "Checkin-time",               0,      NULL, 0 },
                     66:        { "Is-modified",                0,      NULL, 0 },
                     67:        { "Notify",                     0,      NULL, 0 },
                     68:        { "Case",                       0,      NULL, 0 },
                     69:        { "Gzip-stream",                0,      NULL, 0 },
1.25      xsa        70:        { "wrapper-sendme-rcsOptions",  0,      NULL, 0 },
1.9       joris      71:        { "Kerberos-encrypt",           0,      NULL, 0 },
                     72:        { "Gssapi-encrypt",             0,      NULL, 0 },
                     73:        { "Gssapi-authenticate",        0,      NULL, 0 },
                     74:        { "expand-modules",             0,      NULL, 0 },
                     75:
                     76:        /* commands that might be supported */
1.66      joris      77:        { "ci",                         0,      cvs_server_commit,
                     78:            REQ_NEEDDIR },
                     79:        { "co",                         0,      cvs_server_checkout,
                     80:            REQ_NEEDDIR },
                     81:        { "update",                     0,      cvs_server_update,
                     82:            REQ_NEEDDIR },
                     83:        { "diff",                       0,      cvs_server_diff, REQ_NEEDDIR },
                     84:        { "log",                        0,      cvs_server_log, REQ_NEEDDIR },
1.71    ! xsa        85:        { "rlog",                       0,      cvs_server_rlog, 0 },
1.66      joris      86:        { "add",                        0,      cvs_server_add, REQ_NEEDDIR },
                     87:        { "remove",                     0,      cvs_server_remove,
                     88:            REQ_NEEDDIR },
1.33      xsa        89:        { "update-patches",             0,      cvs_server_update_patches, 0 },
1.25      xsa        90:        { "gzip-file-contents",         0,      NULL, 0 },
1.66      joris      91:        { "status",                     0,      cvs_server_status,
                     92:            REQ_NEEDDIR },
1.9       joris      93:        { "rdiff",                      0,      NULL, 0 },
1.66      joris      94:        { "tag",                        0,      cvs_server_tag, REQ_NEEDDIR },
1.9       joris      95:        { "rtag",                       0,      NULL, 0 },
1.66      joris      96:        { "import",                     0,      cvs_server_import,
                     97:            REQ_NEEDDIR },
                     98:        { "admin",                      0,      cvs_server_admin, REQ_NEEDDIR },
1.25      xsa        99:        { "export",                     0,      NULL, 0 },
                    100:        { "history",                    0,      NULL, 0 },
1.68      xsa       101:        { "release",                    0,      cvs_server_release,
                    102:            REQ_NEEDDIR },
1.9       joris     103:        { "watch-on",                   0,      NULL, 0 },
                    104:        { "watch-off",                  0,      NULL, 0 },
                    105:        { "watch-add",                  0,      NULL, 0 },
                    106:        { "watch-remove",               0,      NULL, 0 },
1.25      xsa       107:        { "watchers",                   0,      NULL, 0 },
                    108:        { "editors",                    0,      NULL, 0 },
1.66      joris     109:        { "init",                       0,      cvs_server_init, REQ_NEEDDIR },
                    110:        { "annotate",                   0,      cvs_server_annotate,
                    111:            REQ_NEEDDIR },
1.25      xsa       112:        { "rannotate",                  0,      NULL, 0 },
1.9       joris     113:        { "noop",                       0,      NULL, 0 },
1.25      xsa       114:        { "version",                    0,      cvs_server_version, 0 },
1.9       joris     115:        { "",                           -1,     NULL, 0 }
                    116: };
                    117:
1.16      xsa       118: static void     client_check_directory(char *);
                    119: static char    *client_get_supported_responses(void);
                    120: static char    *lastdir = NULL;
                    121: static int      end_of_response = 0;
                    122:
                    123: static void    cvs_client_initlog(void);
                    124:
                    125: /*
                    126:  * File descriptors for protocol logging when the CVS_CLIENT_LOG environment
                    127:  * variable is set.
                    128:  */
                    129: static int     cvs_client_logon = 0;
1.42      joris     130: int    cvs_client_inlog_fd = -1;
                    131: int    cvs_client_outlog_fd = -1;
1.16      xsa       132:
1.9       joris     133:
1.37      joris     134: int server_response = SERVER_OK;
                    135:
1.9       joris     136: static char *
                    137: client_get_supported_responses(void)
                    138: {
                    139:        BUF *bp;
                    140:        char *d;
                    141:        int i, first;
                    142:
                    143:        first = 0;
                    144:        bp = cvs_buf_alloc(512, BUF_AUTOEXT);
                    145:        for (i = 0; cvs_responses[i].supported != -1; i++) {
                    146:                if (cvs_responses[i].hdlr == NULL)
                    147:                        continue;
                    148:
                    149:                if (first != 0)
                    150:                        cvs_buf_append(bp, " ", 1);
                    151:                else
                    152:                        first++;
                    153:                cvs_buf_append(bp, cvs_responses[i].name,
                    154:                    strlen(cvs_responses[i].name));
                    155:        }
                    156:
                    157:        cvs_buf_putc(bp, '\0');
                    158:        d = cvs_buf_release(bp);
                    159:        return (d);
                    160: }
1.1       jfb       161:
1.10      joris     162: static void
                    163: client_check_directory(char *data)
                    164: {
                    165:        CVSENTRIES *entlist;
1.54      otto      166:        char entry[CVS_ENT_MAXLINELEN], *parent, *base;
1.10      joris     167:
                    168:        STRIP_SLASH(data);
                    169:
1.69      joris     170:        cvs_mkpath(data, NULL);
1.10      joris     171:
                    172:        if ((base = basename(data)) == NULL)
                    173:                fatal("client_check_directory: overflow");
                    174:
                    175:        if ((parent = dirname(data)) == NULL)
                    176:                fatal("client_check_directory: overflow");
1.11      joris     177:
                    178:        if (!strcmp(parent, "."))
                    179:                return;
1.10      joris     180:
1.57      xsa       181:        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////", base);
1.10      joris     182:
                    183:        entlist = cvs_ent_open(parent);
                    184:        cvs_ent_add(entlist, entry);
                    185:        cvs_ent_close(entlist, ENT_SYNC);
                    186: }
                    187:
1.9       joris     188: void
                    189: cvs_client_connect_to_server(void)
                    190: {
1.26      xsa       191:        struct cvs_var *vp;
1.9       joris     192:        char *cmd, *argv[9], *resp;
                    193:        int ifd[2], ofd[2], argc;
1.45      joris     194:
                    195:        if (cvs_server_active == 1)
                    196:                fatal("cvs_client_connect: I was already connected to server");
1.1       jfb       197:
1.9       joris     198:        switch (current_cvsroot->cr_method) {
                    199:        case CVS_METHOD_PSERVER:
                    200:        case CVS_METHOD_KSERVER:
                    201:        case CVS_METHOD_GSERVER:
                    202:        case CVS_METHOD_FORK:
                    203:        case CVS_METHOD_EXT:
1.14      joris     204:                fatal("the specified connection method is not supported");
1.9       joris     205:        default:
                    206:                break;
                    207:        }
                    208:
                    209:        if (pipe(ifd) == -1)
                    210:                fatal("cvs_client_connect: %s", strerror(errno));
                    211:        if (pipe(ofd) == -1)
                    212:                fatal("cvs_client_connect: %s", strerror(errno));
                    213:
                    214:        switch (fork()) {
                    215:        case -1:
                    216:                fatal("cvs_client_connect: fork failed: %s", strerror(errno));
                    217:        case 0:
                    218:                if (dup2(ifd[0], STDIN_FILENO) == -1)
                    219:                        fatal("cvs_client_connect: %s", strerror(errno));
                    220:                if (dup2(ofd[1], STDOUT_FILENO) == -1)
                    221:                        fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       222:
1.9       joris     223:                close(ifd[1]);
                    224:                close(ofd[0]);
1.1       jfb       225:
1.9       joris     226:                if ((cmd = getenv("CVS_SERVER")) == NULL)
                    227:                        cmd = CVS_SERVER_DEFAULT;
1.1       jfb       228:
                    229:                argc = 0;
                    230:                argv[argc++] = cvs_rsh;
                    231:
1.9       joris     232:                if (current_cvsroot->cr_user != NULL) {
1.1       jfb       233:                        argv[argc++] = "-l";
1.9       joris     234:                        argv[argc++] = current_cvsroot->cr_user;
1.1       jfb       235:                }
                    236:
1.9       joris     237:                argv[argc++] = current_cvsroot->cr_host;
                    238:                argv[argc++] = cmd;
1.1       jfb       239:                argv[argc++] = "server";
                    240:                argv[argc] = NULL;
                    241:
1.9       joris     242:                cvs_log(LP_TRACE, "connecting to server %s",
                    243:                    current_cvsroot->cr_host);
                    244:
1.1       jfb       245:                execvp(argv[0], argv);
1.9       joris     246:                fatal("cvs_client_connect: failed to execute cvs server");
                    247:        default:
                    248:                break;
1.1       jfb       249:        }
                    250:
1.9       joris     251:        close(ifd[0]);
                    252:        close(ofd[1]);
                    253:
                    254:        if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
                    255:                fatal("cvs_client_connect: %s", strerror(errno));
                    256:        if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
                    257:                fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       258:
1.9       joris     259:        setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
                    260:        setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
1.1       jfb       261:
1.16      xsa       262:        cvs_client_initlog();
                    263:
1.18      xsa       264:        if (cvs_cmdop != CVS_OP_INIT)
                    265:                cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
1.1       jfb       266:
1.9       joris     267:        resp = client_get_supported_responses();
                    268:        cvs_client_send_request("Valid-responses %s", resp);
                    269:        xfree(resp);
1.1       jfb       270:
1.9       joris     271:        cvs_client_send_request("valid-requests");
                    272:        cvs_client_get_responses();
1.1       jfb       273:
1.28      xsa       274:        cvs_client_send_request("UseUnchanged");
                    275:
1.20      xsa       276:        if (cvs_nolog == 1)
                    277:                cvs_client_send_request("Global_option -l");
                    278:
1.19      xsa       279:        if (cvs_noexec == 1)
                    280:                cvs_client_send_request("Global_option -n");
                    281:
                    282:        if (verbosity == 0)
                    283:                cvs_client_send_request("Global_option -Q");
1.20      xsa       284:
                    285:        /* Be quiet. This is the default in OpenCVS. */
                    286:        cvs_client_send_request("Global_option -q");
1.19      xsa       287:
                    288:        if (cvs_readonly == 1)
                    289:                cvs_client_send_request("Global_option -r");
                    290:
                    291:        if (cvs_trace == 1)
1.9       joris     292:                cvs_client_send_request("Global_option -t");
1.1       jfb       293:
1.9       joris     294:        if (verbosity == 2)
                    295:                cvs_client_send_request("Global_option -V");
1.26      xsa       296:
                    297:        /* XXX: If 'Set' is supported? */
                    298:        TAILQ_FOREACH(vp, &cvs_variables, cv_link)
                    299:                cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
1.1       jfb       300: }
                    301:
1.9       joris     302: void
                    303: cvs_client_send_request(char *fmt, ...)
                    304: {
                    305:        va_list ap;
                    306:        char *data, *s;
                    307:        struct cvs_req *req;
                    308:
                    309:        va_start(ap, fmt);
                    310:        vasprintf(&data, fmt, ap);
                    311:        va_end(ap);
1.1       jfb       312:
1.9       joris     313:        if ((s = strchr(data, ' ')) != NULL)
                    314:                *s = '\0';
1.1       jfb       315:
1.9       joris     316:        req = cvs_remote_get_request_info(data);
                    317:        if (req == NULL)
                    318:                fatal("'%s' is an unknown request", data);
1.1       jfb       319:
1.9       joris     320:        if (req->supported != 1)
                    321:                fatal("remote cvs server does not support '%s'", data);
1.1       jfb       322:
1.9       joris     323:        if (s != NULL)
                    324:                *s = ' ';
1.1       jfb       325:
1.12      joris     326:        cvs_log(LP_TRACE, "%s", data);
1.16      xsa       327:
                    328:        if (cvs_client_inlog_fd != -1) {
                    329:                BUF *bp;
                    330:
                    331:                bp = cvs_buf_alloc(strlen(data), BUF_AUTOEXT);
                    332:
                    333:                if (cvs_buf_append(bp, data, strlen(data)) < 0)
                    334:                        fatal("cvs_client_send_request: cvs_buf_append");
                    335:
                    336:                cvs_buf_putc(bp, '\n');
                    337:
                    338:                if (cvs_buf_write_fd(bp, cvs_client_inlog_fd) < 0)
                    339:                        fatal("cvs_client_send_request: cvs_buf_write_fd");
                    340:
                    341:                cvs_buf_free(bp);
                    342:        }
                    343:
1.9       joris     344:        cvs_remote_output(data);
                    345:        xfree(data);
                    346: }
1.1       jfb       347:
1.9       joris     348: void
                    349: cvs_client_read_response(void)
1.1       jfb       350: {
1.9       joris     351:        char *cmd, *data;
                    352:        struct cvs_resp *resp;
1.1       jfb       353:
1.9       joris     354:        cmd = cvs_remote_input();
                    355:        if ((data = strchr(cmd, ' ')) != NULL)
                    356:                (*data++) = '\0';
1.1       jfb       357:
1.9       joris     358:        resp = cvs_remote_get_response_info(cmd);
                    359:        if (resp == NULL)
                    360:                fatal("response '%s' is not supported by our client", cmd);
1.1       jfb       361:
1.9       joris     362:        if (resp->hdlr == NULL)
                    363:                fatal("opencvs client does not support '%s'", cmd);
1.16      xsa       364:
1.9       joris     365:        (*resp->hdlr)(data);
1.16      xsa       366:
1.9       joris     367:        xfree(cmd);
1.1       jfb       368: }
                    369:
1.9       joris     370: void
                    371: cvs_client_get_responses(void)
                    372: {
                    373:        while (end_of_response != 1)
                    374:                cvs_client_read_response();
1.1       jfb       375:
1.9       joris     376:        end_of_response = 0;
                    377: }
1.1       jfb       378:
1.9       joris     379: void
                    380: cvs_client_senddir(const char *dir)
1.1       jfb       381: {
1.34      xsa       382:        struct stat st;
1.29      xsa       383:        int nb;
1.50      otto      384:        char *d, *date, fpath[MAXPATHLEN], repo[MAXPATHLEN], *tag;
1.29      xsa       385:
                    386:        d = NULL;
1.9       joris     387:
                    388:        if (lastdir != NULL && !strcmp(dir, lastdir))
                    389:                return;
1.1       jfb       390:
1.9       joris     391:        cvs_get_repository_path(dir, repo, MAXPATHLEN);
1.31      xsa       392:
1.70      xsa       393:        if (cvs_cmdop != CVS_OP_RLOG)
                    394:                cvs_client_send_request("Directory %s\n%s", dir, repo);
1.31      xsa       395:
1.58      xsa       396:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    397:            dir, CVS_PATH_STATICENTRIES);
1.34      xsa       398:
                    399:        if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
                    400:                cvs_client_send_request("Static-directory");
                    401:
1.29      xsa       402:        d = xstrdup(dir);
                    403:        cvs_parse_tagfile(d, &tag, &date, &nb);
                    404:
                    405:        if (tag != NULL || date != NULL) {
                    406:                char buf[128];
                    407:
                    408:                if (tag != NULL && nb != NULL) {
                    409:                        if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
                    410:                                fatal("cvs_client_senddir: truncation");
                    411:                } else if (tag != NULL) {
                    412:                        if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
                    413:                                fatal("cvs_client_senddir: truncation");
                    414:                } else {
                    415:                        if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
                    416:                                fatal("cvs_client_senddir: truncation");
                    417:                }
                    418:
                    419:                if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
                    420:                        fatal("cvs_client_senddir: truncation");
                    421:
                    422:                cvs_client_send_request("Sticky %s", buf);
                    423:
                    424:                if (tag != NULL)
                    425:                        xfree(tag);
                    426:                if (date != NULL)
                    427:                        xfree(date);
                    428:        }
                    429:        if (d != NULL)
                    430:                xfree(d);
1.1       jfb       431:
1.9       joris     432:        if (lastdir != NULL)
                    433:                xfree(lastdir);
                    434:        lastdir = xstrdup(dir);
                    435: }
1.1       jfb       436:
1.9       joris     437: void
                    438: cvs_client_sendfile(struct cvs_file *cf)
1.1       jfb       439: {
1.9       joris     440:        size_t len;
1.67      xsa       441:        char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[32];
1.1       jfb       442:
1.9       joris     443:        if (cf->file_type != CVS_FILE)
                    444:                return;
1.1       jfb       445:
1.9       joris     446:        cvs_client_senddir(cf->file_wd);
                    447:        cvs_remote_classify_file(cf);
                    448:
1.10      joris     449:        if (cf->file_type == CVS_DIR)
                    450:                return;
                    451:
1.9       joris     452:        if (cf->file_ent != NULL) {
                    453:                if (cf->file_status == FILE_ADDED) {
                    454:                        len = strlcpy(rev, "0", sizeof(rev));
                    455:                        if (len >= sizeof(rev))
                    456:                                fatal("cvs_client_sendfile: truncation");
                    457:
                    458:                        len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    459:                        if (len >= sizeof(timebuf))
                    460:                                fatal("cvs_client_sendfile: truncation");
                    461:
                    462:                        len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
                    463:                        if (len >= sizeof(timebuf))
                    464:                                fatal("cvs_client_sendfile: truncation");
                    465:                } else {
                    466:                        rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
                    467:                        ctime_r(&cf->file_ent->ce_mtime, timebuf);
                    468:                }
1.1       jfb       469:
1.9       joris     470:                if (cf->file_ent->ce_conflict == NULL) {
                    471:                        if (timebuf[strlen(timebuf) - 1] == '\n')
                    472:                                timebuf[strlen(timebuf) - 1] = '\0';
                    473:                } else {
                    474:                        len = strlcpy(timebuf, cf->file_ent->ce_conflict,
                    475:                            sizeof(timebuf));
                    476:                        if (len >= sizeof(timebuf))
                    477:                                fatal("cvs_client_sendfile: truncation");
1.56      joris     478:                        len = strlcat(timebuf, "+", sizeof(timebuf));
                    479:                        if (len >= sizeof(timebuf))
                    480:                                fatal("cvs_client_sendfile: truncation");
1.9       joris     481:                }
1.1       jfb       482:
1.9       joris     483:                sticky[0] = '\0';
                    484:                if (cf->file_ent->ce_tag != NULL) {
1.57      xsa       485:                        (void)xsnprintf(sticky, sizeof(sticky), "T%s",
1.9       joris     486:                            cf->file_ent->ce_tag);
1.1       jfb       487:                }
                    488:
1.55      otto      489:                cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
1.9       joris     490:                    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
1.53      xsa       491:                    rev, timebuf, cf->file_ent->ce_opts ?
                    492:                    cf->file_ent->ce_opts : "", sticky);
1.9       joris     493:        }
                    494:
                    495:        switch (cf->file_status) {
                    496:        case FILE_UNKNOWN:
                    497:                if (cf->fd != -1)
                    498:                        cvs_client_send_request("Questionable %s",
                    499:                            cf->file_name);
                    500:                break;
                    501:        case FILE_ADDED:
                    502:        case FILE_MODIFIED:
                    503:                cvs_client_send_request("Modified %s", cf->file_name);
                    504:                cvs_remote_send_file(cf->file_path);
                    505:                break;
                    506:        case FILE_UPTODATE:
                    507:                cvs_client_send_request("Unchanged %s", cf->file_name);
                    508:                break;
1.1       jfb       509:        }
1.9       joris     510: }
1.1       jfb       511:
1.9       joris     512: void
                    513: cvs_client_send_files(char **argv, int argc)
                    514: {
                    515:        int i;
1.1       jfb       516:
1.9       joris     517:        for (i = 0; i < argc; i++)
                    518:                cvs_client_send_request("Argument %s", argv[i]);
                    519: }
1.1       jfb       520:
1.9       joris     521: void
                    522: cvs_client_ok(char *data)
                    523: {
                    524:        end_of_response = 1;
1.37      joris     525:        server_response = SERVER_OK;
1.1       jfb       526: }
                    527:
1.9       joris     528: void
                    529: cvs_client_error(char *data)
                    530: {
                    531:        end_of_response = 1;
1.37      joris     532:        server_response = SERVER_ERROR;
1.9       joris     533: }
1.1       jfb       534:
1.9       joris     535: void
                    536: cvs_client_validreq(char *data)
1.1       jfb       537: {
1.9       joris     538:        int i;
                    539:        char *sp, *ep;
                    540:        struct cvs_req *req;
                    541:
1.62      ray       542:        if ((sp = data) == NULL)
                    543:                fatal("Missing argument for Valid-requests");
                    544:
1.9       joris     545:        do {
                    546:                if ((ep = strchr(sp, ' ')) != NULL)
                    547:                        *ep = '\0';
                    548:
                    549:                req = cvs_remote_get_request_info(sp);
                    550:                if (req != NULL)
                    551:                        req->supported = 1;
                    552:
                    553:                if (ep != NULL)
                    554:                        sp = ep + 1;
                    555:        } while (ep != NULL);
                    556:
                    557:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    558:                req = &cvs_requests[i];
                    559:                if ((req->flags & REQ_NEEDED) &&
                    560:                    req->supported != 1) {
                    561:                        fatal("server does not support required '%s'",
                    562:                            req->name);
                    563:                }
1.1       jfb       564:        }
1.9       joris     565: }
1.1       jfb       566:
1.9       joris     567: void
                    568: cvs_client_e(char *data)
                    569: {
1.62      ray       570:        if (data == NULL)
                    571:                fatal("Missing argument for E");
                    572:
1.9       joris     573:        cvs_printf("%s\n", data);
                    574: }
1.1       jfb       575:
1.9       joris     576: void
                    577: cvs_client_m(char *data)
                    578: {
1.62      ray       579:        if (data == NULL)
                    580:                fatal("Missing argument for M");
                    581:
1.9       joris     582:        cvs_printf("%s\n", data);
                    583: }
1.1       jfb       584:
1.9       joris     585: void
                    586: cvs_client_checkedin(char *data)
                    587: {
                    588:        CVSENTRIES *entlist;
                    589:        struct cvs_ent *ent, *newent;
1.67      xsa       590:        char *dir, *e, entry[CVS_ENT_MAXLINELEN], rev[CVS_REV_BUFSZ];
                    591:        char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
1.1       jfb       592:
1.62      ray       593:        if (data == NULL)
                    594:                fatal("Missing argument for Checked-in");
                    595:
1.9       joris     596:        dir = cvs_remote_input();
1.54      otto      597:        e = cvs_remote_input();
1.9       joris     598:        xfree(dir);
1.1       jfb       599:
1.9       joris     600:        entlist = cvs_ent_open(data);
1.54      otto      601:        newent = cvs_ent_parse(e);
1.9       joris     602:        ent = cvs_ent_get(entlist, newent->ce_name);
1.54      otto      603:        xfree(e);
1.1       jfb       604:
1.9       joris     605:        rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
                    606:        ctime_r(&ent->ce_mtime, timebuf);
                    607:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    608:                timebuf[strlen(timebuf) - 1] = '\0';
1.1       jfb       609:
1.9       joris     610:        sticky[0] = '\0';
1.57      xsa       611:        if (ent->ce_tag != NULL)
                    612:                (void)xsnprintf(sticky, sizeof(sticky), "T%s", ent->ce_tag);
1.1       jfb       613:
1.57      xsa       614:        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s%s/%s/%s/%s",
1.44      joris     615:            newent->ce_name, (newent->ce_status == CVS_ENT_REMOVED) ? "-" : "",
1.53      xsa       616:            rev, timebuf, ent->ce_opts ? ent->ce_opts : "", sticky);
1.9       joris     617:
                    618:        cvs_ent_free(ent);
                    619:        cvs_ent_free(newent);
                    620:        cvs_ent_add(entlist, entry);
                    621:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       622: }
                    623:
1.9       joris     624: void
                    625: cvs_client_updated(char *data)
                    626: {
1.57      xsa       627:        int fd;
1.9       joris     628:        time_t now;
1.51      otto      629:        mode_t fmode, mask;
1.9       joris     630:        size_t flen;
                    631:        CVSENTRIES *ent;
                    632:        struct cvs_ent *e;
                    633:        const char *errstr;
                    634:        struct timeval tv[2];
1.67      xsa       635:        char repo[MAXPATHLEN], entry[CVS_ENT_MAXLINELEN];
                    636:        char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
                    637:        char *en, *mode, *len, *fpath, *rpath, *wdir;
1.69      joris     638:        char sticky[CVS_ENT_MAXLINELEN];
1.9       joris     639:
1.62      ray       640:        if (data == NULL)
                    641:                fatal("Missing argument for Updated");
                    642:
1.10      joris     643:        client_check_directory(data);
1.9       joris     644:
                    645:        rpath = cvs_remote_input();
1.54      otto      646:        en = cvs_remote_input();
1.9       joris     647:        mode = cvs_remote_input();
                    648:        len = cvs_remote_input();
                    649:
                    650:        cvs_get_repository_path(".", repo, MAXPATHLEN);
1.48      joris     651:
                    652:        STRIP_SLASH(repo);
1.9       joris     653:
                    654:        if (strlen(repo) + 1 > strlen(rpath))
                    655:                fatal("received a repository path that is too short");
                    656:
                    657:        fpath = rpath + strlen(repo) + 1;
                    658:        if ((wdir = dirname(fpath)) == NULL)
                    659:                fatal("cvs_client_updated: dirname: %s", strerror(errno));
                    660:
                    661:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    662:        if (errstr != NULL)
                    663:                fatal("cvs_client_updated: %s: %s", len, errstr);
                    664:        xfree(len);
                    665:
                    666:        cvs_strtomode(mode, &fmode);
                    667:        xfree(mode);
1.51      otto      668:        mask = umask(0);
                    669:        umask(mask);
                    670:        fmode &= ~mask;
1.9       joris     671:
                    672:        time(&now);
1.52      joris     673:        asctime_r(gmtime(&now), timebuf);
1.9       joris     674:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    675:                timebuf[strlen(timebuf) - 1] = '\0';
                    676:
1.54      otto      677:        e = cvs_ent_parse(en);
                    678:        xfree(en);
1.69      joris     679:
                    680:        sticky[0] = '\0';
                    681:        if (e->ce_tag != NULL)
                    682:                (void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
                    683:
1.9       joris     684:        rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
1.69      joris     685:        (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
                    686:            e->ce_name, revbuf, timebuf,
                    687:            e->ce_opts ? e->ce_opts : "", sticky);
1.9       joris     688:
                    689:        cvs_ent_free(e);
                    690:        ent = cvs_ent_open(wdir);
                    691:        cvs_ent_add(ent, entry);
                    692:        cvs_ent_close(ent, ENT_SYNC);
                    693:
                    694:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    695:                fatal("cvs_client_updated: open: %s: %s",
                    696:                    fpath, strerror(errno));
1.1       jfb       697:
1.46      joris     698:        cvs_remote_receive_file(fd, flen);
1.1       jfb       699:
1.9       joris     700:        tv[0].tv_sec = now;
                    701:        tv[0].tv_usec = 0;
                    702:        tv[1] = tv[0];
1.1       jfb       703:
1.9       joris     704:        if (futimes(fd, tv) == -1)
                    705:                fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1       jfb       706:
1.9       joris     707:        if (fchmod(fd, fmode) == -1)
                    708:                fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1       jfb       709:
1.9       joris     710:        (void)close(fd);
1.1       jfb       711:
1.9       joris     712:        xfree(rpath);
1.1       jfb       713: }
                    714:
1.9       joris     715: void
                    716: cvs_client_merged(char *data)
                    717: {
1.56      joris     718:        int fd;
                    719:        time_t now;
                    720:        mode_t fmode;
                    721:        size_t flen;
                    722:        CVSENTRIES *ent;
                    723:        const char *errstr;
                    724:        struct timeval tv[2];
1.67      xsa       725:        char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
1.56      joris     726:        char *len, *fpath, *wdir;
                    727:
1.62      ray       728:        if (data == NULL)
                    729:                fatal("Missing argument for Merged");
                    730:
1.56      joris     731:        client_check_directory(data);
                    732:
                    733:        rpath = cvs_remote_input();
                    734:        entry = cvs_remote_input();
                    735:        mode = cvs_remote_input();
                    736:        len = cvs_remote_input();
                    737:
                    738:        repo = xmalloc(MAXPATHLEN);
                    739:        cvs_get_repository_path(".", repo, MAXPATHLEN);
                    740:
                    741:        STRIP_SLASH(repo);
                    742:
                    743:        if (strlen(repo) + 1 > strlen(rpath))
                    744:                fatal("received a repository path that is too short");
                    745:
                    746:        fpath = rpath + strlen(repo) + 1;
                    747:        if ((wdir = dirname(fpath)) == NULL)
                    748:                fatal("cvs_client_merged: dirname: %s", strerror(errno));
                    749:        xfree(repo);
                    750:
                    751:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    752:        if (errstr != NULL)
                    753:                fatal("cvs_client_merged: %s: %s", len, errstr);
                    754:        xfree(len);
                    755:
                    756:        cvs_strtomode(mode, &fmode);
                    757:        xfree(mode);
                    758:
                    759:        time(&now);
                    760:        asctime_r(gmtime(&now), timebuf);
                    761:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    762:                timebuf[strlen(timebuf) - 1] = '\0';
                    763:
                    764:        ent = cvs_ent_open(wdir);
                    765:        cvs_ent_add(ent, entry);
                    766:        cvs_ent_close(ent, ENT_SYNC);
                    767:
                    768:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    769:                fatal("cvs_client_merged: open: %s: %s",
                    770:                    fpath, strerror(errno));
                    771:
                    772:        cvs_remote_receive_file(fd, flen);
                    773:
                    774:        tv[0].tv_sec = now;
                    775:        tv[0].tv_usec = 0;
                    776:        tv[1] = tv[0];
                    777:
                    778:        if (futimes(fd, tv) == -1)
                    779:                fatal("cvs_client_merged: futimes: %s", strerror(errno));
                    780:
                    781:        if (fchmod(fd, fmode) == -1)
                    782:                fatal("cvs_client_merged: fchmod: %s", strerror(errno));
                    783:
                    784:        (void)close(fd);
                    785:
                    786:        xfree(rpath);
1.9       joris     787: }
1.1       jfb       788:
1.9       joris     789: void
                    790: cvs_client_removed(char *data)
                    791: {
1.65      joris     792:        CVSENTRIES *entlist;
                    793:        char *rpath, *filename, fpath[MAXPATHLEN];
                    794:
                    795:        rpath = cvs_remote_input();
                    796:        if ((filename = strrchr(rpath, '/')) == NULL)
                    797:                fatal("bad rpath in cvs_client_removed: %s", rpath);
                    798:        filename++;
                    799:
                    800:        entlist = cvs_ent_open(data);
                    801:        cvs_ent_remove(entlist, filename);
                    802:        cvs_ent_close(entlist, ENT_SYNC);
                    803:
                    804:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
                    805:        (void)unlink(fpath);
1.13      joris     806:
1.65      joris     807:        xfree(rpath);
1.9       joris     808: }
1.1       jfb       809:
1.9       joris     810: void
                    811: cvs_client_remove_entry(char *data)
1.1       jfb       812: {
1.38      xsa       813:        CVSENTRIES *entlist;
1.44      joris     814:        char *filename, *rpath;
1.41      joris     815:
1.62      ray       816:        if (data == NULL)
                    817:                fatal("Missing argument for Remove-entry");
                    818:
1.41      joris     819:        rpath = cvs_remote_input();
                    820:        if ((filename = strrchr(rpath, '/')) == NULL)
                    821:                fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
1.61      ray       822:        filename++;
1.1       jfb       823:
1.38      xsa       824:        entlist = cvs_ent_open(data);
1.44      joris     825:        cvs_ent_remove(entlist, filename);
1.38      xsa       826:        cvs_ent_close(entlist, ENT_SYNC);
                    827:
1.44      joris     828:        xfree(rpath);
1.36      xsa       829: }
                    830:
                    831: void
                    832: cvs_client_set_static_directory(char *data)
                    833: {
                    834:        FILE *fp;
1.50      otto      835:        char *dir, fpath[MAXPATHLEN];
1.36      xsa       836:
                    837:        if (cvs_cmdop == CVS_OP_EXPORT)
                    838:                return;
                    839:
1.62      ray       840:        if (data == NULL)
                    841:                fatal("Missing argument for Set-static-directory");
                    842:
1.36      xsa       843:        STRIP_SLASH(data);
                    844:
                    845:        dir = cvs_remote_input();
                    846:        xfree(dir);
                    847:
1.58      xsa       848:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    849:            data, CVS_PATH_STATICENTRIES);
1.36      xsa       850:
                    851:        if ((fp = fopen(fpath, "w+")) == NULL) {
                    852:                cvs_log(LP_ERRNO, "%s", fpath);
1.50      otto      853:                return;
1.36      xsa       854:        }
                    855:        (void)fclose(fp);
1.16      xsa       856: }
1.35      xsa       857:
                    858: void
                    859: cvs_client_clear_static_directory(char *data)
                    860: {
1.50      otto      861:        char *dir, fpath[MAXPATHLEN];
1.35      xsa       862:
                    863:        if (cvs_cmdop == CVS_OP_EXPORT)
                    864:                return;
                    865:
1.62      ray       866:        if (data == NULL)
                    867:                fatal("Missing argument for Clear-static-directory");
                    868:
1.35      xsa       869:        STRIP_SLASH(data);
                    870:
                    871:        dir = cvs_remote_input();
                    872:        xfree(dir);
                    873:
1.58      xsa       874:        (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
                    875:            data, CVS_PATH_STATICENTRIES);
1.35      xsa       876:
                    877:        (void)cvs_unlink(fpath);
                    878: }
                    879:
                    880: void
                    881: cvs_client_set_sticky(char *data)
                    882: {
                    883:        FILE *fp;
1.50      otto      884:        char *dir, *tag, tagpath[MAXPATHLEN];
1.35      xsa       885:
1.47      xsa       886:        if (cvs_cmdop == CVS_OP_EXPORT)
                    887:                return;
                    888:
1.62      ray       889:        if (data == NULL)
                    890:                fatal("Missing argument for Set-sticky");
                    891:
1.35      xsa       892:        STRIP_SLASH(data);
                    893:
                    894:        dir = cvs_remote_input();
                    895:        xfree(dir);
                    896:        tag = cvs_remote_input();
                    897:
1.69      joris     898:        client_check_directory(data);
                    899:
1.58      xsa       900:        (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.35      xsa       901:
                    902:        if ((fp = fopen(tagpath, "w+")) == NULL) {
                    903:                cvs_log(LP_ERRNO, "%s", tagpath);
                    904:                goto out;
                    905:        }
                    906:
                    907:        (void)fprintf(fp, "%s\n", tag);
                    908:        (void)fclose(fp);
                    909: out:
                    910:        xfree(tag);
                    911: }
                    912:
                    913: void
                    914: cvs_client_clear_sticky(char *data)
                    915: {
1.50      otto      916:        char *dir, tagpath[MAXPATHLEN];
1.47      xsa       917:
                    918:        if (cvs_cmdop == CVS_OP_EXPORT)
                    919:                return;
1.62      ray       920:
                    921:        if (data == NULL)
                    922:                fatal("Missing argument for Clear-sticky");
1.35      xsa       923:
                    924:        STRIP_SLASH(data);
                    925:
                    926:        dir = cvs_remote_input();
                    927:        xfree(dir);
                    928:
1.69      joris     929:        client_check_directory(data);
                    930:
1.58      xsa       931:        (void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1.69      joris     932:        (void)unlink(tagpath);
1.35      xsa       933: }
                    934:
                    935:
1.16      xsa       936: /*
                    937:  * cvs_client_initlog()
                    938:  *
                    939:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    940:  * set.  In this case, the variable's value is used as a path to which the
                    941:  * appropriate suffix is added (".in" for client input and ".out" for server
                    942:  * output).
                    943:  */
                    944: static void
                    945: cvs_client_initlog(void)
                    946: {
                    947:        u_int i;
                    948:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    949:        char rpath[MAXPATHLEN], *s;
                    950:        struct stat st;
                    951:        time_t now;
                    952:        struct passwd *pwd;
                    953:
                    954:        /* avoid doing it more than once */
                    955:        if (cvs_client_logon)
                    956:                return;
                    957:
                    958:        if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
                    959:                return;
                    960:
                    961:        envdup = xstrdup(env);
                    962:        if ((s = strchr(envdup, '%')) != NULL)
                    963:                *s = '\0';
                    964:
                    965:        if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
                    966:                fatal("cvs_client_initlog: truncation");
                    967:
                    968:        if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
                    969:                fatal("cvs_client_initlog: truncation");
                    970:
                    971:        xfree(envdup);
                    972:
                    973:        s = buf;
                    974:        while ((s = strchr(s, '%')) != NULL) {
                    975:                s++;
                    976:                switch (*s) {
                    977:                case 'c':
                    978:                        if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
                    979:                            sizeof(fpath))
                    980:                                fatal("cvs_client_initlog: truncation");
                    981:                        break;
                    982:                case 'd':
                    983:                        time(&now);
                    984:                        if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
                    985:                            sizeof(fpath))
                    986:                                fatal("cvs_client_initlog: truncation");
                    987:                        break;
                    988:                case 'p':
1.57      xsa       989:                        (void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1.16      xsa       990:                        break;
                    991:                case 'u':
                    992:                        if ((pwd = getpwuid(getuid())) != NULL) {
                    993:                                if (strlcpy(fpath, pwd->pw_name,
                    994:                                    sizeof(fpath)) >= sizeof(fpath))
                    995:                                        fatal("cvs_client_initlog: truncation");
                    996:                        } else {
                    997:                                fpath[0] = '\0';
                    998:                        }
                    999:                        endpwent();
                   1000:                        break;
                   1001:                default:
                   1002:                        fpath[0] = '\0';
                   1003:                        break;
                   1004:                }
                   1005:
                   1006:                if (fpath[0] != '\0') {
                   1007:                        if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
                   1008:                                fatal("cvs_client_initlog: truncation");
                   1009:
                   1010:                        if (strlcat(rpath, fpath, sizeof(rpath))
                   1011:                            >= sizeof(rpath))
                   1012:                                fatal("cvs_client_initlog: truncation");
                   1013:                }
                   1014:        }
                   1015:
                   1016:        for (i = 0; i < UINT_MAX; i++) {
1.57      xsa      1017:                (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.16      xsa      1018:
                   1019:                if (stat(fpath, &st) != -1)
                   1020:                        continue;
                   1021:
                   1022:                if (errno != ENOENT)
                   1023:                        fatal("cvs_client_initlog() stat failed '%s'",
                   1024:                            strerror(errno));
                   1025:
                   1026:                break;
                   1027:        }
                   1028:
                   1029:        if ((cvs_client_inlog_fd = open(fpath,
1.60      xsa      1030:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16      xsa      1031:                fatal("cvs_client_initlog: open `%s': %s",
                   1032:                    fpath, strerror(errno));
                   1033:        }
                   1034:
                   1035:        for (i = 0; i < UINT_MAX; i++) {
1.57      xsa      1036:                (void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.16      xsa      1037:
                   1038:                if (stat(fpath, &st) != -1)
                   1039:                        continue;
                   1040:
                   1041:                if (errno != ENOENT)
                   1042:                        fatal("cvs_client_initlog() stat failed '%s'",
                   1043:                            strerror(errno));
                   1044:
                   1045:                break;
                   1046:        }
                   1047:
                   1048:        if ((cvs_client_outlog_fd = open(fpath,
1.60      xsa      1049:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1.16      xsa      1050:                fatal("cvs_client_initlog: open `%s': %s",
                   1051:                    fpath, strerror(errno));
                   1052:        }
                   1053:
                   1054:        cvs_client_logon = 1;
1.1       jfb      1055: }