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

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