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

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