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

1.26    ! xsa         1: /*     $OpenBSD: client.c,v 1.25 2006/11/14 10:10:19 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 },
                     31:        { "Entry",              0,      cvs_server_entry, REQ_NEEDED },
                     32:        { "Modified",           0,      cvs_server_modified, REQ_NEEDED },
                     33:        { "UseUnchanged",       0,      cvs_server_useunchanged, REQ_NEEDED },
                     34:        { "Unchanged",          0,      cvs_server_unchanged, REQ_NEEDED },
                     35:        { "Questionable",       0,      cvs_server_questionable, REQ_NEEDED },
                     36:        { "Argument",           0,      cvs_server_argument, REQ_NEEDED },
1.24      xsa        37:        { "Argumentx",          0,      cvs_server_argumentx, REQ_NEEDED },
                     38:        { "Global_option",      0,      cvs_server_globalopt, REQ_NEEDED },
1.26    ! xsa        39:        { "Set",                0,      cvs_server_set, REQ_NEEDED },
1.1       jfb        40:
1.9       joris      41:        /*
                     42:         * used to tell the server what is going on in our
                     43:         * working copy, unsupported until we are told otherwise
                     44:         */
                     45:        { "Max-dotdot",                 0,      NULL, 0 },
                     46:        { "Static-directory",           0,      NULL, 0 },
                     47:        { "Sticky",                     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 },
                     71:        { "update-patches",             0,      NULL, 0 },
                     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 },
                     89:        { "annotate",                   0,      NULL, 0 },
                     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.20      xsa       253:        if (cvs_nolog == 1)
                    254:                cvs_client_send_request("Global_option -l");
                    255:
1.19      xsa       256:        if (cvs_noexec == 1)
                    257:                cvs_client_send_request("Global_option -n");
                    258:
                    259:        if (verbosity == 0)
                    260:                cvs_client_send_request("Global_option -Q");
1.20      xsa       261:
                    262:        /* Be quiet. This is the default in OpenCVS. */
                    263:        cvs_client_send_request("Global_option -q");
1.19      xsa       264:
                    265:        if (cvs_readonly == 1)
                    266:                cvs_client_send_request("Global_option -r");
                    267:
                    268:        if (cvs_trace == 1)
1.9       joris     269:                cvs_client_send_request("Global_option -t");
1.1       jfb       270:
1.9       joris     271:        if (verbosity == 2)
                    272:                cvs_client_send_request("Global_option -V");
1.1       jfb       273:
1.9       joris     274:        cvs_client_send_request("UseUnchanged");
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.9       joris     389:        char *repo;
                    390:
                    391:        if (lastdir != NULL && !strcmp(dir, lastdir))
                    392:                return;
1.1       jfb       393:
1.9       joris     394:        cvs_client_send_request("Directory %s", dir);
1.1       jfb       395:
1.9       joris     396:        repo = xmalloc(MAXPATHLEN);
                    397:        cvs_get_repository_path(dir, repo, MAXPATHLEN);
                    398:        cvs_remote_output(repo);
                    399:        xfree(repo);
1.1       jfb       400:
1.9       joris     401:        if (lastdir != NULL)
                    402:                xfree(lastdir);
                    403:        lastdir = xstrdup(dir);
                    404: }
1.1       jfb       405:
1.9       joris     406: void
                    407: cvs_client_sendfile(struct cvs_file *cf)
1.1       jfb       408: {
1.9       joris     409:        int l;
                    410:        size_t len;
                    411:        char rev[16], timebuf[64], sticky[32];
1.1       jfb       412:
1.9       joris     413:        if (cf->file_type != CVS_FILE)
                    414:                return;
1.1       jfb       415:
1.9       joris     416:        cvs_client_senddir(cf->file_wd);
                    417:        cvs_remote_classify_file(cf);
                    418:
1.10      joris     419:        if (cf->file_type == CVS_DIR)
                    420:                return;
                    421:
1.9       joris     422:        if (cf->file_ent != NULL) {
                    423:                if (cf->file_status == FILE_ADDED) {
                    424:                        len = strlcpy(rev, "0", sizeof(rev));
                    425:                        if (len >= sizeof(rev))
                    426:                                fatal("cvs_client_sendfile: truncation");
                    427:
                    428:                        len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    429:                        if (len >= sizeof(timebuf))
                    430:                                fatal("cvs_client_sendfile: truncation");
                    431:
                    432:                        len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
                    433:                        if (len >= sizeof(timebuf))
                    434:                                fatal("cvs_client_sendfile: truncation");
                    435:                } else {
                    436:                        rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
                    437:                        ctime_r(&cf->file_ent->ce_mtime, timebuf);
                    438:                }
1.1       jfb       439:
1.9       joris     440:                if (cf->file_ent->ce_conflict == NULL) {
                    441:                        if (timebuf[strlen(timebuf) - 1] == '\n')
                    442:                                timebuf[strlen(timebuf) - 1] = '\0';
                    443:                } else {
                    444:                        len = strlcpy(timebuf, cf->file_ent->ce_conflict,
                    445:                            sizeof(timebuf));
                    446:                        if (len >= sizeof(timebuf))
                    447:                                fatal("cvs_client_sendfile: truncation");
                    448:                }
1.1       jfb       449:
1.9       joris     450:                sticky[0] = '\0';
                    451:                if (cf->file_ent->ce_tag != NULL) {
                    452:                        l = snprintf(sticky, sizeof(sticky), "T%s",
                    453:                            cf->file_ent->ce_tag);
                    454:                        if (l == -1 || l >= (int)sizeof(sticky))
                    455:                                fatal("cvs_client_sendfile: overflow");
1.1       jfb       456:                }
                    457:
1.13      joris     458:                cvs_client_send_request("Entry /%s/%s%s/%s//%s",
1.9       joris     459:                    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
                    460:                   rev, timebuf, sticky);
                    461:        }
                    462:
                    463:        switch (cf->file_status) {
                    464:        case FILE_UNKNOWN:
                    465:                if (cf->fd != -1)
                    466:                        cvs_client_send_request("Questionable %s",
                    467:                            cf->file_name);
                    468:                break;
                    469:        case FILE_ADDED:
                    470:        case FILE_MODIFIED:
                    471:                cvs_client_send_request("Modified %s", cf->file_name);
                    472:                cvs_remote_send_file(cf->file_path);
                    473:                break;
                    474:        case FILE_UPTODATE:
                    475:                cvs_client_send_request("Unchanged %s", cf->file_name);
                    476:                break;
1.1       jfb       477:        }
1.9       joris     478: }
1.1       jfb       479:
1.9       joris     480: void
                    481: cvs_client_send_files(char **argv, int argc)
                    482: {
                    483:        int i;
1.1       jfb       484:
1.9       joris     485:        for (i = 0; i < argc; i++)
                    486:                cvs_client_send_request("Argument %s", argv[i]);
                    487: }
1.1       jfb       488:
1.9       joris     489: void
                    490: cvs_client_ok(char *data)
                    491: {
                    492:        end_of_response = 1;
1.1       jfb       493: }
                    494:
1.9       joris     495: void
                    496: cvs_client_error(char *data)
                    497: {
                    498:        end_of_response = 1;
                    499: }
1.1       jfb       500:
1.9       joris     501: void
                    502: cvs_client_validreq(char *data)
1.1       jfb       503: {
1.9       joris     504:        int i;
                    505:        char *sp, *ep;
                    506:        struct cvs_req *req;
                    507:
                    508:        sp = data;
                    509:        do {
                    510:                if ((ep = strchr(sp, ' ')) != NULL)
                    511:                        *ep = '\0';
                    512:
                    513:                req = cvs_remote_get_request_info(sp);
                    514:                if (req != NULL)
                    515:                        req->supported = 1;
                    516:
                    517:                if (ep != NULL)
                    518:                        sp = ep + 1;
                    519:        } while (ep != NULL);
                    520:
                    521:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    522:                req = &cvs_requests[i];
                    523:                if ((req->flags & REQ_NEEDED) &&
                    524:                    req->supported != 1) {
                    525:                        fatal("server does not support required '%s'",
                    526:                            req->name);
                    527:                }
1.1       jfb       528:        }
1.9       joris     529: }
1.1       jfb       530:
1.9       joris     531: void
                    532: cvs_client_e(char *data)
                    533: {
                    534:        cvs_printf("%s\n", data);
                    535: }
1.1       jfb       536:
1.9       joris     537: void
                    538: cvs_client_m(char *data)
                    539: {
                    540:        cvs_printf("%s\n", data);
                    541: }
1.1       jfb       542:
1.9       joris     543: void
                    544: cvs_client_checkedin(char *data)
                    545: {
                    546:        int l;
                    547:        CVSENTRIES *entlist;
                    548:        struct cvs_ent *ent, *newent;
                    549:        char *dir, *entry, rev[16], timebuf[64], sticky[16];
1.1       jfb       550:
1.9       joris     551:        dir = cvs_remote_input();
                    552:        entry = cvs_remote_input();
                    553:        xfree(dir);
1.1       jfb       554:
1.9       joris     555:        entlist = cvs_ent_open(data);
                    556:        newent = cvs_ent_parse(entry);
                    557:        ent = cvs_ent_get(entlist, newent->ce_name);
                    558:        xfree(entry);
1.1       jfb       559:
1.9       joris     560:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.1       jfb       561:
1.9       joris     562:        rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
                    563:        ctime_r(&ent->ce_mtime, timebuf);
                    564:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    565:                timebuf[strlen(timebuf) - 1] = '\0';
1.1       jfb       566:
1.9       joris     567:        sticky[0] = '\0';
                    568:        if (ent->ce_tag != NULL) {
                    569:                l = snprintf(sticky, sizeof(sticky), "T%s", ent->ce_tag);
                    570:                if (l == -1 || l >= (int)sizeof(sticky))
                    571:                        fatal("cvs_client_checkedin: overflow");
1.2       jfb       572:        }
1.1       jfb       573:
1.9       joris     574:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s/",
                    575:            newent->ce_name, rev, timebuf, sticky);
                    576:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    577:                fatal("cvs_client_checkedin: overflow");
                    578:
                    579:        cvs_ent_free(ent);
                    580:        cvs_ent_free(newent);
                    581:        cvs_ent_add(entlist, entry);
                    582:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       583:
1.9       joris     584:        xfree(entry);
1.1       jfb       585: }
                    586:
1.9       joris     587: void
                    588: cvs_client_updated(char *data)
                    589: {
                    590:        BUF *bp;
                    591:        int l, fd;
                    592:        time_t now;
                    593:        mode_t fmode;
                    594:        size_t flen;
                    595:        CVSENTRIES *ent;
                    596:        struct cvs_ent *e;
                    597:        const char *errstr;
                    598:        struct timeval tv[2];
                    599:        char timebuf[32], *repo, *rpath, *entry, *mode;
                    600:        char revbuf[32], *len, *fpath, *wdir;
                    601:
1.10      joris     602:        client_check_directory(data);
1.9       joris     603:
                    604:        rpath = cvs_remote_input();
                    605:        entry = cvs_remote_input();
                    606:        mode = cvs_remote_input();
                    607:        len = cvs_remote_input();
                    608:
                    609:        repo = xmalloc(MAXPATHLEN);
                    610:        cvs_get_repository_path(".", repo, MAXPATHLEN);
                    611:
                    612:        if (strlen(repo) + 1 > strlen(rpath))
                    613:                fatal("received a repository path that is too short");
                    614:
                    615:        fpath = rpath + strlen(repo) + 1;
                    616:        if ((wdir = dirname(fpath)) == NULL)
                    617:                fatal("cvs_client_updated: dirname: %s", strerror(errno));
                    618:        xfree(repo);
                    619:
                    620:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    621:        if (errstr != NULL)
                    622:                fatal("cvs_client_updated: %s: %s", len, errstr);
                    623:        xfree(len);
                    624:
                    625:        cvs_strtomode(mode, &fmode);
                    626:        xfree(mode);
                    627:
                    628:        time(&now);
                    629:        now = cvs_hack_time(now, 0);
                    630:        ctime_r(&now, timebuf);
                    631:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    632:                timebuf[strlen(timebuf) - 1] = '\0';
                    633:
                    634:        e = cvs_ent_parse(entry);
                    635:        xfree(entry);
                    636:        rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
                    637:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    638:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//", e->ce_name,
                    639:            revbuf, timebuf);
                    640:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    641:                fatal("cvs_client_updated: overflow");
                    642:
                    643:        cvs_ent_free(e);
                    644:        ent = cvs_ent_open(wdir);
                    645:        cvs_ent_add(ent, entry);
                    646:        cvs_ent_close(ent, ENT_SYNC);
                    647:        xfree(entry);
                    648:
                    649:        bp = cvs_remote_receive_file(flen);
                    650:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    651:                fatal("cvs_client_updated: open: %s: %s",
                    652:                    fpath, strerror(errno));
1.1       jfb       653:
1.9       joris     654:        if (cvs_buf_write_fd(bp, fd) == -1)
                    655:                fatal("cvs_client_updated: cvs_buf_write_fd failed for %s",
                    656:                    fpath);
1.1       jfb       657:
1.9       joris     658:        cvs_buf_free(bp);
1.1       jfb       659:
1.9       joris     660:        now = cvs_hack_time(now, 0);
                    661:        tv[0].tv_sec = now;
                    662:        tv[0].tv_usec = 0;
                    663:        tv[1] = tv[0];
1.1       jfb       664:
1.9       joris     665:        if (futimes(fd, tv) == -1)
                    666:                fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1       jfb       667:
1.9       joris     668:        if (fchmod(fd, fmode) == -1)
                    669:                fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1       jfb       670:
1.9       joris     671:        (void)close(fd);
1.1       jfb       672:
1.9       joris     673:        xfree(rpath);
1.1       jfb       674: }
                    675:
1.9       joris     676: void
                    677: cvs_client_merged(char *data)
                    678: {
                    679: }
1.1       jfb       680:
1.9       joris     681: void
                    682: cvs_client_removed(char *data)
                    683: {
1.13      joris     684:        char *dir;
                    685:
                    686:        dir = cvs_remote_input();
                    687:        xfree(dir);
1.9       joris     688: }
1.1       jfb       689:
1.9       joris     690: void
                    691: cvs_client_remove_entry(char *data)
1.1       jfb       692: {
1.9       joris     693:        char *dir;
1.1       jfb       694:
1.9       joris     695:        dir = cvs_remote_input();
                    696:        xfree(dir);
1.16      xsa       697: }
                    698: /*
                    699:  * cvs_client_initlog()
                    700:  *
                    701:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    702:  * set.  In this case, the variable's value is used as a path to which the
                    703:  * appropriate suffix is added (".in" for client input and ".out" for server
                    704:  * output).
                    705:  */
                    706: static void
                    707: cvs_client_initlog(void)
                    708: {
                    709:        int l;
                    710:        u_int i;
                    711:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    712:        char rpath[MAXPATHLEN], *s;
                    713:        struct stat st;
                    714:        time_t now;
                    715:        struct passwd *pwd;
                    716:
                    717:        /* avoid doing it more than once */
                    718:        if (cvs_client_logon)
                    719:                return;
                    720:
                    721:        if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
                    722:                return;
                    723:
                    724:        envdup = xstrdup(env);
                    725:        if ((s = strchr(envdup, '%')) != NULL)
                    726:                *s = '\0';
                    727:
                    728:        if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
                    729:                fatal("cvs_client_initlog: truncation");
                    730:
                    731:        if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
                    732:                fatal("cvs_client_initlog: truncation");
                    733:
                    734:        xfree(envdup);
                    735:
                    736:        s = buf;
                    737:        while ((s = strchr(s, '%')) != NULL) {
                    738:                s++;
                    739:                switch (*s) {
                    740:                case 'c':
                    741:                        if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
                    742:                            sizeof(fpath))
                    743:                                fatal("cvs_client_initlog: truncation");
                    744:                        break;
                    745:                case 'd':
                    746:                        time(&now);
                    747:                        if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
                    748:                            sizeof(fpath))
                    749:                                fatal("cvs_client_initlog: truncation");
                    750:                        break;
                    751:                case 'p':
                    752:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                    753:                        break;
                    754:                case 'u':
                    755:                        if ((pwd = getpwuid(getuid())) != NULL) {
                    756:                                if (strlcpy(fpath, pwd->pw_name,
                    757:                                    sizeof(fpath)) >= sizeof(fpath))
                    758:                                        fatal("cvs_client_initlog: truncation");
                    759:                        } else {
                    760:                                fpath[0] = '\0';
                    761:                        }
                    762:                        endpwent();
                    763:                        break;
                    764:                default:
                    765:                        fpath[0] = '\0';
                    766:                        break;
                    767:                }
                    768:
                    769:                if (fpath[0] != '\0') {
                    770:                        if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
                    771:                                fatal("cvs_client_initlog: truncation");
                    772:
                    773:                        if (strlcat(rpath, fpath, sizeof(rpath))
                    774:                            >= sizeof(rpath))
                    775:                                fatal("cvs_client_initlog: truncation");
                    776:                }
                    777:        }
                    778:
                    779:        for (i = 0; i < UINT_MAX; i++) {
                    780:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                    781:                if (l == -1 || l >= (int)sizeof(fpath))
                    782:                        fatal("cvs_client_initlog: overflow");
                    783:
                    784:                if (stat(fpath, &st) != -1)
                    785:                        continue;
                    786:
                    787:                if (errno != ENOENT)
                    788:                        fatal("cvs_client_initlog() stat failed '%s'",
                    789:                            strerror(errno));
                    790:
                    791:                break;
                    792:        }
                    793:
                    794:        if ((cvs_client_inlog_fd = open(fpath,
                    795:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    796:                fatal("cvs_client_initlog: open `%s': %s",
                    797:                    fpath, strerror(errno));
                    798:        }
                    799:
                    800:        for (i = 0; i < UINT_MAX; i++) {
                    801:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                    802:                if (l == -1 || l >= (int)sizeof(fpath))
                    803:                        fatal("cvs_client_initlog: overflow");
                    804:
                    805:                if (stat(fpath, &st) != -1)
                    806:                        continue;
                    807:
                    808:                if (errno != ENOENT)
                    809:                        fatal("cvs_client_initlog() stat failed '%s'",
                    810:                            strerror(errno));
                    811:
                    812:                break;
                    813:        }
                    814:
                    815:        if ((cvs_client_outlog_fd = open(fpath,
                    816:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    817:                fatal("cvs_client_initlog: open `%s': %s",
                    818:                    fpath, strerror(errno));
                    819:        }
                    820:
                    821:        cvs_client_logon = 1;
1.1       jfb       822: }