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

1.25    ! xsa         1: /*     $OpenBSD: client.c,v 1.24 2006/11/14 09:59:54 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.1       jfb        39:
1.9       joris      40:        /*
                     41:         * used to tell the server what is going on in our
                     42:         * working copy, unsupported until we are told otherwise
                     43:         */
                     44:        { "Max-dotdot",                 0,      NULL, 0 },
                     45:        { "Static-directory",           0,      NULL, 0 },
                     46:        { "Sticky",                     0,      NULL, 0 },
                     47:        { "Checkin-prog",               0,      NULL, 0 },
                     48:        { "Update-prog",                0,      NULL, 0 },
                     49:        { "Kopt",                       0,      NULL, 0 },
                     50:        { "Checkin-time",               0,      NULL, 0 },
                     51:        { "Is-modified",                0,      NULL, 0 },
                     52:        { "Notify",                     0,      NULL, 0 },
                     53:        { "Case",                       0,      NULL, 0 },
                     54:        { "Gzip-stream",                0,      NULL, 0 },
1.25    ! xsa        55:        { "wrapper-sendme-rcsOptions",  0,      NULL, 0 },
1.9       joris      56:        { "Kerberos-encrypt",           0,      NULL, 0 },
                     57:        { "Gssapi-encrypt",             0,      NULL, 0 },
                     58:        { "Gssapi-authenticate",        0,      NULL, 0 },
                     59:        { "Set",                        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: {
                    173:        char *cmd, *argv[9], *resp;
                    174:        int ifd[2], ofd[2], argc;
1.1       jfb       175:
1.9       joris     176:        switch (current_cvsroot->cr_method) {
                    177:        case CVS_METHOD_PSERVER:
                    178:        case CVS_METHOD_KSERVER:
                    179:        case CVS_METHOD_GSERVER:
                    180:        case CVS_METHOD_FORK:
                    181:        case CVS_METHOD_EXT:
1.14      joris     182:                fatal("the specified connection method is not supported");
1.9       joris     183:        default:
                    184:                break;
                    185:        }
                    186:
                    187:        if (pipe(ifd) == -1)
                    188:                fatal("cvs_client_connect: %s", strerror(errno));
                    189:        if (pipe(ofd) == -1)
                    190:                fatal("cvs_client_connect: %s", strerror(errno));
                    191:
                    192:        switch (fork()) {
                    193:        case -1:
                    194:                fatal("cvs_client_connect: fork failed: %s", strerror(errno));
                    195:        case 0:
                    196:                if (dup2(ifd[0], STDIN_FILENO) == -1)
                    197:                        fatal("cvs_client_connect: %s", strerror(errno));
                    198:                if (dup2(ofd[1], STDOUT_FILENO) == -1)
                    199:                        fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       200:
1.9       joris     201:                close(ifd[1]);
                    202:                close(ofd[0]);
1.1       jfb       203:
1.9       joris     204:                if ((cmd = getenv("CVS_SERVER")) == NULL)
                    205:                        cmd = CVS_SERVER_DEFAULT;
1.1       jfb       206:
                    207:                argc = 0;
                    208:                argv[argc++] = cvs_rsh;
                    209:
1.9       joris     210:                if (current_cvsroot->cr_user != NULL) {
1.1       jfb       211:                        argv[argc++] = "-l";
1.9       joris     212:                        argv[argc++] = current_cvsroot->cr_user;
1.1       jfb       213:                }
                    214:
1.9       joris     215:                argv[argc++] = current_cvsroot->cr_host;
                    216:                argv[argc++] = cmd;
1.1       jfb       217:                argv[argc++] = "server";
                    218:                argv[argc] = NULL;
                    219:
1.9       joris     220:                cvs_log(LP_TRACE, "connecting to server %s",
                    221:                    current_cvsroot->cr_host);
                    222:
1.1       jfb       223:                execvp(argv[0], argv);
1.9       joris     224:                fatal("cvs_client_connect: failed to execute cvs server");
                    225:        default:
                    226:                break;
1.1       jfb       227:        }
                    228:
1.9       joris     229:        close(ifd[0]);
                    230:        close(ofd[1]);
                    231:
                    232:        if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
                    233:                fatal("cvs_client_connect: %s", strerror(errno));
                    234:        if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
                    235:                fatal("cvs_client_connect: %s", strerror(errno));
1.1       jfb       236:
1.9       joris     237:        setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
                    238:        setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
1.1       jfb       239:
1.16      xsa       240:        cvs_client_initlog();
                    241:
1.18      xsa       242:        if (cvs_cmdop != CVS_OP_INIT)
                    243:                cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
1.1       jfb       244:
1.9       joris     245:        resp = client_get_supported_responses();
                    246:        cvs_client_send_request("Valid-responses %s", resp);
                    247:        xfree(resp);
1.1       jfb       248:
1.9       joris     249:        cvs_client_send_request("valid-requests");
                    250:        cvs_client_get_responses();
1.1       jfb       251:
1.20      xsa       252:        if (cvs_nolog == 1)
                    253:                cvs_client_send_request("Global_option -l");
                    254:
1.19      xsa       255:        if (cvs_noexec == 1)
                    256:                cvs_client_send_request("Global_option -n");
                    257:
                    258:        if (verbosity == 0)
                    259:                cvs_client_send_request("Global_option -Q");
1.20      xsa       260:
                    261:        /* Be quiet. This is the default in OpenCVS. */
                    262:        cvs_client_send_request("Global_option -q");
1.19      xsa       263:
                    264:        if (cvs_readonly == 1)
                    265:                cvs_client_send_request("Global_option -r");
                    266:
                    267:        if (cvs_trace == 1)
1.9       joris     268:                cvs_client_send_request("Global_option -t");
1.1       jfb       269:
1.9       joris     270:        if (verbosity == 2)
                    271:                cvs_client_send_request("Global_option -V");
1.1       jfb       272:
1.9       joris     273:        cvs_client_send_request("UseUnchanged");
1.1       jfb       274: }
                    275:
1.9       joris     276: void
                    277: cvs_client_send_request(char *fmt, ...)
                    278: {
                    279:        va_list ap;
                    280:        char *data, *s;
                    281:        struct cvs_req *req;
                    282:
                    283:        va_start(ap, fmt);
                    284:        vasprintf(&data, fmt, ap);
                    285:        va_end(ap);
1.1       jfb       286:
1.9       joris     287:        if ((s = strchr(data, ' ')) != NULL)
                    288:                *s = '\0';
1.1       jfb       289:
1.9       joris     290:        req = cvs_remote_get_request_info(data);
                    291:        if (req == NULL)
                    292:                fatal("'%s' is an unknown request", data);
1.1       jfb       293:
1.9       joris     294:        if (req->supported != 1)
                    295:                fatal("remote cvs server does not support '%s'", data);
1.1       jfb       296:
1.9       joris     297:        if (s != NULL)
                    298:                *s = ' ';
1.1       jfb       299:
1.12      joris     300:        cvs_log(LP_TRACE, "%s", data);
1.16      xsa       301:
                    302:        if (cvs_client_inlog_fd != -1) {
                    303:                BUF *bp;
                    304:
                    305:                bp = cvs_buf_alloc(strlen(data), BUF_AUTOEXT);
                    306:
                    307:                if (cvs_buf_append(bp, data, strlen(data)) < 0)
                    308:                        fatal("cvs_client_send_request: cvs_buf_append");
                    309:
                    310:                cvs_buf_putc(bp, '\n');
                    311:
                    312:                if (cvs_buf_write_fd(bp, cvs_client_inlog_fd) < 0)
                    313:                        fatal("cvs_client_send_request: cvs_buf_write_fd");
                    314:
                    315:                cvs_buf_free(bp);
                    316:        }
                    317:
1.9       joris     318:        cvs_remote_output(data);
                    319:        xfree(data);
                    320: }
1.1       jfb       321:
1.9       joris     322: void
                    323: cvs_client_read_response(void)
1.1       jfb       324: {
1.9       joris     325:        char *cmd, *data;
                    326:        struct cvs_resp *resp;
1.1       jfb       327:
1.9       joris     328:        cmd = cvs_remote_input();
                    329:        if ((data = strchr(cmd, ' ')) != NULL)
                    330:                (*data++) = '\0';
1.1       jfb       331:
1.9       joris     332:        resp = cvs_remote_get_response_info(cmd);
                    333:        if (resp == NULL)
                    334:                fatal("response '%s' is not supported by our client", cmd);
1.1       jfb       335:
1.9       joris     336:        if (resp->hdlr == NULL)
                    337:                fatal("opencvs client does not support '%s'", cmd);
1.1       jfb       338:
1.16      xsa       339:        if (cvs_client_outlog_fd != -1) {
                    340:                BUF *bp;
                    341:
                    342:                bp = cvs_buf_alloc(strlen(cmd), BUF_AUTOEXT);
                    343:
                    344:                if (cvs_buf_append(bp, cmd, strlen(cmd)) < 0)
                    345:                        fatal("cvs_client_read_response: cvs_buf_append");
                    346:
                    347:                if (cvs_buf_append(bp,
                    348:                    (data == NULL) ? "" : " ",
                    349:                    (data == NULL) ? 0 : 1) < 0) {
                    350:                        fatal("cvs_client_read_response: cvs_buf_append");
                    351:                }
                    352:
                    353:                if (cvs_buf_append(bp,
                    354:                    (data == NULL) ? "" : data,
                    355:                    (data == NULL) ? 0 : strlen(data)) < 0) {
                    356:                        fatal("cvs_client_read_response: cvs_buf_append");
                    357:                }
                    358:
                    359:                cvs_buf_putc(bp, '\n');
                    360:
                    361:                if (cvs_buf_write_fd(bp, cvs_client_outlog_fd) < 0)
                    362:                        fatal("cvs_client_read_response: cvs_buf_write_fd");
                    363:
                    364:                cvs_buf_free(bp);
                    365:        }
                    366:
1.9       joris     367:        (*resp->hdlr)(data);
1.16      xsa       368:
1.9       joris     369:        xfree(cmd);
1.1       jfb       370: }
                    371:
1.9       joris     372: void
                    373: cvs_client_get_responses(void)
                    374: {
                    375:        while (end_of_response != 1)
                    376:                cvs_client_read_response();
1.1       jfb       377:
1.9       joris     378:        end_of_response = 0;
                    379: }
1.1       jfb       380:
1.9       joris     381: void
                    382: cvs_client_senddir(const char *dir)
1.1       jfb       383: {
1.9       joris     384:        char *repo;
                    385:
                    386:        if (lastdir != NULL && !strcmp(dir, lastdir))
                    387:                return;
1.1       jfb       388:
1.9       joris     389:        cvs_client_send_request("Directory %s", dir);
1.1       jfb       390:
1.9       joris     391:        repo = xmalloc(MAXPATHLEN);
                    392:        cvs_get_repository_path(dir, repo, MAXPATHLEN);
                    393:        cvs_remote_output(repo);
                    394:        xfree(repo);
1.1       jfb       395:
1.9       joris     396:        if (lastdir != NULL)
                    397:                xfree(lastdir);
                    398:        lastdir = xstrdup(dir);
                    399: }
1.1       jfb       400:
1.9       joris     401: void
                    402: cvs_client_sendfile(struct cvs_file *cf)
1.1       jfb       403: {
1.9       joris     404:        int l;
                    405:        size_t len;
                    406:        char rev[16], timebuf[64], sticky[32];
1.1       jfb       407:
1.9       joris     408:        if (cf->file_type != CVS_FILE)
                    409:                return;
1.1       jfb       410:
1.9       joris     411:        cvs_client_senddir(cf->file_wd);
                    412:        cvs_remote_classify_file(cf);
                    413:
1.10      joris     414:        if (cf->file_type == CVS_DIR)
                    415:                return;
                    416:
1.9       joris     417:        if (cf->file_ent != NULL) {
                    418:                if (cf->file_status == FILE_ADDED) {
                    419:                        len = strlcpy(rev, "0", sizeof(rev));
                    420:                        if (len >= sizeof(rev))
                    421:                                fatal("cvs_client_sendfile: truncation");
                    422:
                    423:                        len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
                    424:                        if (len >= sizeof(timebuf))
                    425:                                fatal("cvs_client_sendfile: truncation");
                    426:
                    427:                        len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
                    428:                        if (len >= sizeof(timebuf))
                    429:                                fatal("cvs_client_sendfile: truncation");
                    430:                } else {
                    431:                        rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
                    432:                        ctime_r(&cf->file_ent->ce_mtime, timebuf);
                    433:                }
1.1       jfb       434:
1.9       joris     435:                if (cf->file_ent->ce_conflict == NULL) {
                    436:                        if (timebuf[strlen(timebuf) - 1] == '\n')
                    437:                                timebuf[strlen(timebuf) - 1] = '\0';
                    438:                } else {
                    439:                        len = strlcpy(timebuf, cf->file_ent->ce_conflict,
                    440:                            sizeof(timebuf));
                    441:                        if (len >= sizeof(timebuf))
                    442:                                fatal("cvs_client_sendfile: truncation");
                    443:                }
1.1       jfb       444:
1.9       joris     445:                sticky[0] = '\0';
                    446:                if (cf->file_ent->ce_tag != NULL) {
                    447:                        l = snprintf(sticky, sizeof(sticky), "T%s",
                    448:                            cf->file_ent->ce_tag);
                    449:                        if (l == -1 || l >= (int)sizeof(sticky))
                    450:                                fatal("cvs_client_sendfile: overflow");
1.1       jfb       451:                }
                    452:
1.13      joris     453:                cvs_client_send_request("Entry /%s/%s%s/%s//%s",
1.9       joris     454:                    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
                    455:                   rev, timebuf, sticky);
                    456:        }
                    457:
                    458:        switch (cf->file_status) {
                    459:        case FILE_UNKNOWN:
                    460:                if (cf->fd != -1)
                    461:                        cvs_client_send_request("Questionable %s",
                    462:                            cf->file_name);
                    463:                break;
                    464:        case FILE_ADDED:
                    465:        case FILE_MODIFIED:
                    466:                cvs_client_send_request("Modified %s", cf->file_name);
                    467:                cvs_remote_send_file(cf->file_path);
                    468:                break;
                    469:        case FILE_UPTODATE:
                    470:                cvs_client_send_request("Unchanged %s", cf->file_name);
                    471:                break;
1.1       jfb       472:        }
1.9       joris     473: }
1.1       jfb       474:
1.9       joris     475: void
                    476: cvs_client_send_files(char **argv, int argc)
                    477: {
                    478:        int i;
1.1       jfb       479:
1.9       joris     480:        for (i = 0; i < argc; i++)
                    481:                cvs_client_send_request("Argument %s", argv[i]);
                    482: }
1.1       jfb       483:
1.9       joris     484: void
                    485: cvs_client_ok(char *data)
                    486: {
                    487:        end_of_response = 1;
1.1       jfb       488: }
                    489:
1.9       joris     490: void
                    491: cvs_client_error(char *data)
                    492: {
                    493:        end_of_response = 1;
                    494: }
1.1       jfb       495:
1.9       joris     496: void
                    497: cvs_client_validreq(char *data)
1.1       jfb       498: {
1.9       joris     499:        int i;
                    500:        char *sp, *ep;
                    501:        struct cvs_req *req;
                    502:
                    503:        sp = data;
                    504:        do {
                    505:                if ((ep = strchr(sp, ' ')) != NULL)
                    506:                        *ep = '\0';
                    507:
                    508:                req = cvs_remote_get_request_info(sp);
                    509:                if (req != NULL)
                    510:                        req->supported = 1;
                    511:
                    512:                if (ep != NULL)
                    513:                        sp = ep + 1;
                    514:        } while (ep != NULL);
                    515:
                    516:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                    517:                req = &cvs_requests[i];
                    518:                if ((req->flags & REQ_NEEDED) &&
                    519:                    req->supported != 1) {
                    520:                        fatal("server does not support required '%s'",
                    521:                            req->name);
                    522:                }
1.1       jfb       523:        }
1.9       joris     524: }
1.1       jfb       525:
1.9       joris     526: void
                    527: cvs_client_e(char *data)
                    528: {
                    529:        cvs_printf("%s\n", data);
                    530: }
1.1       jfb       531:
1.9       joris     532: void
                    533: cvs_client_m(char *data)
                    534: {
                    535:        cvs_printf("%s\n", data);
                    536: }
1.1       jfb       537:
1.9       joris     538: void
                    539: cvs_client_checkedin(char *data)
                    540: {
                    541:        int l;
                    542:        CVSENTRIES *entlist;
                    543:        struct cvs_ent *ent, *newent;
                    544:        char *dir, *entry, rev[16], timebuf[64], sticky[16];
1.1       jfb       545:
1.9       joris     546:        dir = cvs_remote_input();
                    547:        entry = cvs_remote_input();
                    548:        xfree(dir);
1.1       jfb       549:
1.9       joris     550:        entlist = cvs_ent_open(data);
                    551:        newent = cvs_ent_parse(entry);
                    552:        ent = cvs_ent_get(entlist, newent->ce_name);
                    553:        xfree(entry);
1.1       jfb       554:
1.9       joris     555:        entry = xmalloc(CVS_ENT_MAXLINELEN);
1.1       jfb       556:
1.9       joris     557:        rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
                    558:        ctime_r(&ent->ce_mtime, timebuf);
                    559:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    560:                timebuf[strlen(timebuf) - 1] = '\0';
1.1       jfb       561:
1.9       joris     562:        sticky[0] = '\0';
                    563:        if (ent->ce_tag != NULL) {
                    564:                l = snprintf(sticky, sizeof(sticky), "T%s", ent->ce_tag);
                    565:                if (l == -1 || l >= (int)sizeof(sticky))
                    566:                        fatal("cvs_client_checkedin: overflow");
1.2       jfb       567:        }
1.1       jfb       568:
1.9       joris     569:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//%s/",
                    570:            newent->ce_name, rev, timebuf, sticky);
                    571:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    572:                fatal("cvs_client_checkedin: overflow");
                    573:
                    574:        cvs_ent_free(ent);
                    575:        cvs_ent_free(newent);
                    576:        cvs_ent_add(entlist, entry);
                    577:        cvs_ent_close(entlist, ENT_SYNC);
1.1       jfb       578:
1.9       joris     579:        xfree(entry);
1.1       jfb       580: }
                    581:
1.9       joris     582: void
                    583: cvs_client_updated(char *data)
                    584: {
                    585:        BUF *bp;
                    586:        int l, fd;
                    587:        time_t now;
                    588:        mode_t fmode;
                    589:        size_t flen;
                    590:        CVSENTRIES *ent;
                    591:        struct cvs_ent *e;
                    592:        const char *errstr;
                    593:        struct timeval tv[2];
                    594:        char timebuf[32], *repo, *rpath, *entry, *mode;
                    595:        char revbuf[32], *len, *fpath, *wdir;
                    596:
1.10      joris     597:        client_check_directory(data);
1.9       joris     598:
                    599:        rpath = cvs_remote_input();
                    600:        entry = cvs_remote_input();
                    601:        mode = cvs_remote_input();
                    602:        len = cvs_remote_input();
                    603:
                    604:        repo = xmalloc(MAXPATHLEN);
                    605:        cvs_get_repository_path(".", repo, MAXPATHLEN);
                    606:
                    607:        if (strlen(repo) + 1 > strlen(rpath))
                    608:                fatal("received a repository path that is too short");
                    609:
                    610:        fpath = rpath + strlen(repo) + 1;
                    611:        if ((wdir = dirname(fpath)) == NULL)
                    612:                fatal("cvs_client_updated: dirname: %s", strerror(errno));
                    613:        xfree(repo);
                    614:
                    615:        flen = strtonum(len, 0, INT_MAX, &errstr);
                    616:        if (errstr != NULL)
                    617:                fatal("cvs_client_updated: %s: %s", len, errstr);
                    618:        xfree(len);
                    619:
                    620:        cvs_strtomode(mode, &fmode);
                    621:        xfree(mode);
                    622:
                    623:        time(&now);
                    624:        now = cvs_hack_time(now, 0);
                    625:        ctime_r(&now, timebuf);
                    626:        if (timebuf[strlen(timebuf) - 1] == '\n')
                    627:                timebuf[strlen(timebuf) - 1] = '\0';
                    628:
                    629:        e = cvs_ent_parse(entry);
                    630:        xfree(entry);
                    631:        rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
                    632:        entry = xmalloc(CVS_ENT_MAXLINELEN);
                    633:        l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//", e->ce_name,
                    634:            revbuf, timebuf);
                    635:        if (l == -1 || l >= CVS_ENT_MAXLINELEN)
                    636:                fatal("cvs_client_updated: overflow");
                    637:
                    638:        cvs_ent_free(e);
                    639:        ent = cvs_ent_open(wdir);
                    640:        cvs_ent_add(ent, entry);
                    641:        cvs_ent_close(ent, ENT_SYNC);
                    642:        xfree(entry);
                    643:
                    644:        bp = cvs_remote_receive_file(flen);
                    645:        if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
                    646:                fatal("cvs_client_updated: open: %s: %s",
                    647:                    fpath, strerror(errno));
1.1       jfb       648:
1.9       joris     649:        if (cvs_buf_write_fd(bp, fd) == -1)
                    650:                fatal("cvs_client_updated: cvs_buf_write_fd failed for %s",
                    651:                    fpath);
1.1       jfb       652:
1.9       joris     653:        cvs_buf_free(bp);
1.1       jfb       654:
1.9       joris     655:        now = cvs_hack_time(now, 0);
                    656:        tv[0].tv_sec = now;
                    657:        tv[0].tv_usec = 0;
                    658:        tv[1] = tv[0];
1.1       jfb       659:
1.9       joris     660:        if (futimes(fd, tv) == -1)
                    661:                fatal("cvs_client_updated: futimes: %s", strerror(errno));
1.1       jfb       662:
1.9       joris     663:        if (fchmod(fd, fmode) == -1)
                    664:                fatal("cvs_client_updated: fchmod: %s", strerror(errno));
1.1       jfb       665:
1.9       joris     666:        (void)close(fd);
1.1       jfb       667:
1.9       joris     668:        xfree(rpath);
1.1       jfb       669: }
                    670:
1.9       joris     671: void
                    672: cvs_client_merged(char *data)
                    673: {
                    674: }
1.1       jfb       675:
1.9       joris     676: void
                    677: cvs_client_removed(char *data)
                    678: {
1.13      joris     679:        char *dir;
                    680:
                    681:        dir = cvs_remote_input();
                    682:        xfree(dir);
1.9       joris     683: }
1.1       jfb       684:
1.9       joris     685: void
                    686: cvs_client_remove_entry(char *data)
1.1       jfb       687: {
1.9       joris     688:        char *dir;
1.1       jfb       689:
1.9       joris     690:        dir = cvs_remote_input();
                    691:        xfree(dir);
1.16      xsa       692: }
                    693: /*
                    694:  * cvs_client_initlog()
                    695:  *
                    696:  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
                    697:  * set.  In this case, the variable's value is used as a path to which the
                    698:  * appropriate suffix is added (".in" for client input and ".out" for server
                    699:  * output).
                    700:  */
                    701: static void
                    702: cvs_client_initlog(void)
                    703: {
                    704:        int l;
                    705:        u_int i;
                    706:        char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
                    707:        char rpath[MAXPATHLEN], *s;
                    708:        struct stat st;
                    709:        time_t now;
                    710:        struct passwd *pwd;
                    711:
                    712:        /* avoid doing it more than once */
                    713:        if (cvs_client_logon)
                    714:                return;
                    715:
                    716:        if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
                    717:                return;
                    718:
                    719:        envdup = xstrdup(env);
                    720:        if ((s = strchr(envdup, '%')) != NULL)
                    721:                *s = '\0';
                    722:
                    723:        if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
                    724:                fatal("cvs_client_initlog: truncation");
                    725:
                    726:        if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
                    727:                fatal("cvs_client_initlog: truncation");
                    728:
                    729:        xfree(envdup);
                    730:
                    731:        s = buf;
                    732:        while ((s = strchr(s, '%')) != NULL) {
                    733:                s++;
                    734:                switch (*s) {
                    735:                case 'c':
                    736:                        if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
                    737:                            sizeof(fpath))
                    738:                                fatal("cvs_client_initlog: truncation");
                    739:                        break;
                    740:                case 'd':
                    741:                        time(&now);
                    742:                        if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
                    743:                            sizeof(fpath))
                    744:                                fatal("cvs_client_initlog: truncation");
                    745:                        break;
                    746:                case 'p':
                    747:                        snprintf(fpath, sizeof(fpath), "%d", getpid());
                    748:                        break;
                    749:                case 'u':
                    750:                        if ((pwd = getpwuid(getuid())) != NULL) {
                    751:                                if (strlcpy(fpath, pwd->pw_name,
                    752:                                    sizeof(fpath)) >= sizeof(fpath))
                    753:                                        fatal("cvs_client_initlog: truncation");
                    754:                        } else {
                    755:                                fpath[0] = '\0';
                    756:                        }
                    757:                        endpwent();
                    758:                        break;
                    759:                default:
                    760:                        fpath[0] = '\0';
                    761:                        break;
                    762:                }
                    763:
                    764:                if (fpath[0] != '\0') {
                    765:                        if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
                    766:                                fatal("cvs_client_initlog: truncation");
                    767:
                    768:                        if (strlcat(rpath, fpath, sizeof(rpath))
                    769:                            >= sizeof(rpath))
                    770:                                fatal("cvs_client_initlog: truncation");
                    771:                }
                    772:        }
                    773:
                    774:        for (i = 0; i < UINT_MAX; i++) {
                    775:                l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
                    776:                if (l == -1 || l >= (int)sizeof(fpath))
                    777:                        fatal("cvs_client_initlog: overflow");
                    778:
                    779:                if (stat(fpath, &st) != -1)
                    780:                        continue;
                    781:
                    782:                if (errno != ENOENT)
                    783:                        fatal("cvs_client_initlog() stat failed '%s'",
                    784:                            strerror(errno));
                    785:
                    786:                break;
                    787:        }
                    788:
                    789:        if ((cvs_client_inlog_fd = open(fpath,
                    790:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    791:                fatal("cvs_client_initlog: open `%s': %s",
                    792:                    fpath, strerror(errno));
                    793:        }
                    794:
                    795:        for (i = 0; i < UINT_MAX; i++) {
                    796:                l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
                    797:                if (l == -1 || l >= (int)sizeof(fpath))
                    798:                        fatal("cvs_client_initlog: overflow");
                    799:
                    800:                if (stat(fpath, &st) != -1)
                    801:                        continue;
                    802:
                    803:                if (errno != ENOENT)
                    804:                        fatal("cvs_client_initlog() stat failed '%s'",
                    805:                            strerror(errno));
                    806:
                    807:                break;
                    808:        }
                    809:
                    810:        if ((cvs_client_outlog_fd = open(fpath,
                    811:            O_RDWR | O_CREAT | O_TRUNC, 0644)) == NULL) {
                    812:                fatal("cvs_client_initlog: open `%s': %s",
                    813:                    fpath, strerror(errno));
                    814:        }
                    815:
                    816:        cvs_client_logon = 1;
1.1       jfb       817: }