[BACK]Return to remote.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/remote.c, Revision 1.16

1.16    ! tobias      1: /*     $OpenBSD: remote.c,v 1.15 2007/05/16 19:40:45 xsa Exp $ */
1.1       joris       2: /*
                      3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
                      4:  *
                      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.
                     16:  */
                     17:
1.14      otto       18: #include <sys/stat.h>
                     19:
                     20: #include <errno.h>
                     21: #include <fcntl.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24: #include <unistd.h>
1.1       joris      25:
                     26: #include "cvs.h"
                     27: #include "remote.h"
                     28:
                     29: struct cvs_resp *
                     30: cvs_remote_get_response_info(const char *response)
                     31: {
                     32:        int i;
                     33:
                     34:        for (i = 0; cvs_responses[i].supported != -1; i++) {
                     35:                if (!strcmp(cvs_responses[i].name, response))
                     36:                        return (&(cvs_responses[i]));
                     37:        }
                     38:
                     39:        return (NULL);
                     40: }
                     41:
                     42: struct cvs_req *
                     43: cvs_remote_get_request_info(const char *request)
                     44: {
                     45:        int i;
                     46:
                     47:        for (i = 0; cvs_requests[i].supported != -1; i++) {
                     48:                if (!strcmp(cvs_requests[i].name, request))
                     49:                        return (&(cvs_requests[i]));
                     50:        }
                     51:
                     52:        return (NULL);
                     53: }
                     54:
                     55: void
                     56: cvs_remote_output(const char *data)
                     57: {
                     58:        FILE *out;
1.16    ! tobias     59:        char nl = '\n';
1.1       joris      60:
                     61:        if (cvs_server_active)
                     62:                out = stdout;
                     63:        else
                     64:                out = current_cvsroot->cr_srvin;
                     65:
                     66:        fputs(data, out);
                     67:        fputs("\n", out);
1.16    ! tobias     68:
        !            69:        if (cvs_server_active == 0 && cvs_client_inlog_fd != -1) {
        !            70:                (void)write(cvs_client_inlog_fd, data, strlen(data));
        !            71:                (void)write(cvs_client_inlog_fd, &nl, 1);
        !            72:        }
1.1       joris      73: }
                     74:
                     75: char *
                     76: cvs_remote_input(void)
                     77: {
                     78:        FILE *in;
                     79:        size_t len;
1.16    ! tobias     80:        char nl = '\n';
1.1       joris      81:        char *data, *ldata;
                     82:
                     83:        if (cvs_server_active)
                     84:                in = stdin;
                     85:        else
                     86:                in = current_cvsroot->cr_srvout;
                     87:
                     88:        data = fgetln(in, &len);
                     89:        if (data == NULL) {
                     90:                if (sig_received != 0)
                     91:                        fatal("received signal %d", sig_received);
                     92:
                     93:                if (cvs_server_active) {
                     94:                        cvs_cleanup();
                     95:                        exit(0);
                     96:                }
                     97:
                     98:                fatal("the connection has been closed by the server");
                     99:        }
                    100:
                    101:        if (data[len - 1] == '\n') {
                    102:                data[len - 1] = '\0';
1.9       otto      103:                ldata = xstrdup(data);
1.1       joris     104:        } else {
                    105:                ldata = xmalloc(len + 1);
1.9       otto      106:                memcpy(ldata, data, len);
                    107:                ldata[len] = '\0';
1.1       joris     108:        }
1.5       joris     109:
                    110:        if (cvs_server_active == 0 && cvs_client_outlog_fd != -1) {
1.16    ! tobias    111:                (void)write(cvs_client_outlog_fd, data, strlen(data));
        !           112:                (void)write(cvs_client_outlog_fd, &nl, 1);
1.5       joris     113:        }
                    114:
1.1       joris     115:        return (ldata);
                    116: }
                    117:
1.6       joris     118: void
                    119: cvs_remote_receive_file(int fd, size_t len)
1.1       joris     120: {
                    121:        FILE *in;
1.10      otto      122:        char data[MAXBSIZE];
1.6       joris     123:        size_t nread, nwrite, nleft, toread;
1.1       joris     124:
                    125:        if (cvs_server_active)
                    126:                in = stdin;
                    127:        else
                    128:                in = current_cvsroot->cr_srvout;
                    129:
1.6       joris     130:        nleft = len;
                    131:
                    132:        while (nleft > 0) {
                    133:                toread = MIN(nleft, MAXBSIZE);
                    134:
                    135:                nread = fread(data, sizeof(char), toread, in);
                    136:                if (nread == 0)
                    137:                        fatal("error receiving file");
1.4       joris     138:
1.6       joris     139:                nwrite = write(fd, data, nread);
                    140:                if (nwrite != nread)
1.8       otto      141:                        fatal("failed to write %zu bytes", nread);
1.6       joris     142:
                    143:                if (cvs_server_active == 0 &&
                    144:                    cvs_client_outlog_fd != -1)
                    145:                        (void)write(cvs_client_outlog_fd, data, nread);
1.1       joris     146:
1.6       joris     147:                nleft -= nread;
1.5       joris     148:        }
1.1       joris     149: }
                    150:
                    151: void
                    152: cvs_remote_send_file(const char *path)
                    153: {
1.13      xsa       154:        int fd;
1.6       joris     155:        FILE *out, *in;
1.8       otto      156:        size_t ret, rw;
                    157:        off_t total;
1.1       joris     158:        struct stat st;
1.11      otto      159:        char buf[18], data[MAXBSIZE];
1.1       joris     160:
                    161:        if (cvs_server_active)
                    162:                out = stdout;
                    163:        else
                    164:                out = current_cvsroot->cr_srvin;
                    165:
                    166:        if ((fd = open(path, O_RDONLY)) == -1)
                    167:                fatal("cvs_remote_send_file: %s: %s", path, strerror(errno));
                    168:
                    169:        if (fstat(fd, &st) == -1)
                    170:                fatal("cvs_remote_send_file: %s: %s", path, strerror(errno));
                    171:
                    172:        cvs_modetostr(st.st_mode, buf, sizeof(buf));
                    173:        cvs_remote_output(buf);
                    174:
1.13      xsa       175:        (void)xsnprintf(buf, sizeof(buf), "%lld", st.st_size);
1.1       joris     176:        cvs_remote_output(buf);
                    177:
1.6       joris     178:        if ((in = fdopen(fd, "r")) == NULL)
                    179:                fatal("cvs_remote_send_file: fdopen %s", strerror(errno));
                    180:
                    181:        total = 0;
                    182:        while ((ret = fread(data, sizeof(char), MAXBSIZE, in)) != 0) {
                    183:                rw = fwrite(data, sizeof(char), ret, out);
                    184:                if (rw != ret)
1.8       otto      185:                        fatal("failed to write %zu bytes", ret);
1.5       joris     186:
1.15      xsa       187:                if (cvs_server_active == 0 && cvs_client_inlog_fd != -1)
                    188:                        (void)write(cvs_client_inlog_fd, data, ret);
1.6       joris     189:
                    190:                total += ret;
1.5       joris     191:        }
1.1       joris     192:
1.6       joris     193:        if (total != st.st_size)
1.8       otto      194:                fatal("length mismatch, %lld vs %lld", total, st.st_size);
1.4       joris     195:
1.6       joris     196:        (void)fclose(in);
1.1       joris     197: }
                    198:
                    199: void
                    200: cvs_remote_classify_file(struct cvs_file *cf)
                    201: {
                    202:        struct stat st;
                    203:        CVSENTRIES *entlist;
                    204:
                    205:        entlist = cvs_ent_open(cf->file_wd);
                    206:        cf->file_ent = cvs_ent_get(entlist, cf->file_name);
                    207:        cvs_ent_close(entlist, ENT_NOSYNC);
                    208:
                    209:        if (cf->file_ent != NULL && cf->file_ent->ce_status != CVS_ENT_REG) {
                    210:                if (cf->file_ent->ce_status == CVS_ENT_ADDED)
                    211:                        cf->file_status = FILE_ADDED;
                    212:                else
                    213:                        cf->file_status = FILE_REMOVED;
                    214:                return;
1.2       joris     215:        }
                    216:
                    217:        if (cf->file_ent != NULL) {
                    218:                if (cf->file_ent->ce_type == CVS_ENT_DIR)
                    219:                        cf->file_type = CVS_DIR;
                    220:                else
                    221:                        cf->file_type = CVS_FILE;
1.1       joris     222:        }
                    223:
                    224:        if (cf->fd != -1 && cf->file_ent != NULL) {
                    225:                if (fstat(cf->fd, &st) == -1)
                    226:                        fatal("cvs_remote_classify_file(%s): %s", cf->file_path,
                    227:                            strerror(errno));
                    228:
1.12      joris     229:                if (st.st_mtime != cf->file_ent->ce_mtime)
1.1       joris     230:                        cf->file_status = FILE_MODIFIED;
                    231:                else
                    232:                        cf->file_status = FILE_UPTODATE;
                    233:        } else if (cf->fd == -1) {
                    234:                cf->file_status = FILE_UNKNOWN;
                    235:        }
1.7       joris     236:
                    237:        if (cvs_cmdop == CVS_OP_IMPORT && cf->file_type == CVS_FILE)
                    238:                cf->file_status = FILE_MODIFIED;
1.1       joris     239: }
                    240: