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

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