Annotation of src/usr.bin/cvs/remote.c, Revision 1.3
1.3 ! joris 1: /* $OpenBSD: remote.c,v 1.2 2006/07/08 00:34:20 joris 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:
18: #include "includes.h"
19:
20: #include "cvs.h"
21: #include "log.h"
22: #include "diff.h"
23: #include "remote.h"
24:
25: struct cvs_resp *
26: cvs_remote_get_response_info(const char *response)
27: {
28: int i;
29:
30: for (i = 0; cvs_responses[i].supported != -1; i++) {
31: if (!strcmp(cvs_responses[i].name, response))
32: return (&(cvs_responses[i]));
33: }
34:
35: return (NULL);
36: }
37:
38: struct cvs_req *
39: cvs_remote_get_request_info(const char *request)
40: {
41: int i;
42:
43: for (i = 0; cvs_requests[i].supported != -1; i++) {
44: if (!strcmp(cvs_requests[i].name, request))
45: return (&(cvs_requests[i]));
46: }
47:
48: return (NULL);
49: }
50:
51: void
52: cvs_remote_output(const char *data)
53: {
54: FILE *out;
55:
56: if (cvs_server_active)
57: out = stdout;
58: else
59: out = current_cvsroot->cr_srvin;
60:
61: fputs(data, out);
62: fputs("\n", out);
63: }
64:
65: char *
66: cvs_remote_input(void)
67: {
68: FILE *in;
69: size_t len;
70: char *data, *ldata;
71:
72: if (cvs_server_active)
73: in = stdin;
74: else
75: in = current_cvsroot->cr_srvout;
76:
77: data = fgetln(in, &len);
78: if (data == NULL) {
79: if (sig_received != 0)
80: fatal("received signal %d", sig_received);
81:
82: if (cvs_server_active) {
83: cvs_cleanup();
84: exit(0);
85: }
86:
87: fatal("the connection has been closed by the server");
88: }
89:
90: if (data[len - 1] == '\n') {
91: data[len - 1] = '\0';
92: } else {
93: ldata = xmalloc(len + 1);
94: if (strlcpy(ldata, data, len) >= len)
95: fatal("cvs_remote_input: truncation");
96: data = ldata;
97: }
98:
99: ldata = xstrdup(data);
100: return (ldata);
101: }
102:
103: BUF *
104: cvs_remote_receive_file(size_t len)
105: {
106: BUF *bp;
107: FILE *in;
108: size_t ret;
109: char *data;
110:
111: if (cvs_server_active)
112: in = stdin;
113: else
114: in = current_cvsroot->cr_srvout;
115:
116: data = xmalloc(len);
117: ret = fread(data, sizeof(char), len, in);
118: if (ret != len)
119: fatal("length mismatch, expected %ld, got %ld", len, ret);
120:
121: bp = cvs_buf_alloc(len, BUF_AUTOEXT);
122: cvs_buf_set(bp, data, len, 0);
123: xfree(data);
124: return (bp);
125: }
126:
127: void
128: cvs_remote_send_file(const char *path)
129: {
130: BUF *bp;
131: int l, fd;
132: FILE *out;
133: size_t ret;
134: struct stat st;
135: char buf[16], *fcont;
136:
137: if (cvs_server_active)
138: out = stdout;
139: else
140: out = current_cvsroot->cr_srvin;
141:
142: if ((fd = open(path, O_RDONLY)) == -1)
143: fatal("cvs_remote_send_file: %s: %s", path, strerror(errno));
144:
145: if (fstat(fd, &st) == -1)
146: fatal("cvs_remote_send_file: %s: %s", path, strerror(errno));
147:
148: cvs_modetostr(st.st_mode, buf, sizeof(buf));
149: cvs_remote_output(buf);
150:
151: l = snprintf(buf, sizeof(buf), "%lld", st.st_size);
152: if (l == -1 || l >= (int)sizeof(buf))
153: fatal("cvs_remote_send_file: overflow");
154: cvs_remote_output(buf);
155:
156: bp = cvs_buf_load_fd(fd, BUF_AUTOEXT);
157: fcont = cvs_buf_release(bp);
158: ret = fwrite(fcont, sizeof(char), st.st_size, out);
159: if (ret != st.st_size)
160: fatal("length mismatch, tried to write %lld only wrote %ld",
161: st.st_size, ret);
162:
163: xfree(fcont);
164: (void)close(fd);
165: }
166:
167: void
168: cvs_remote_classify_file(struct cvs_file *cf)
169: {
170: time_t mtime;
171: struct stat st;
172: CVSENTRIES *entlist;
173:
174: entlist = cvs_ent_open(cf->file_wd);
175: cf->file_ent = cvs_ent_get(entlist, cf->file_name);
176: cvs_ent_close(entlist, ENT_NOSYNC);
177:
178: if (cf->file_ent != NULL && cf->file_ent->ce_status != CVS_ENT_REG) {
179: if (cf->file_ent->ce_status == CVS_ENT_ADDED)
180: cf->file_status = FILE_ADDED;
181: else
182: cf->file_status = FILE_REMOVED;
183: return;
1.2 joris 184: }
185:
186: if (cf->file_ent != NULL) {
187: if (cf->file_ent->ce_type == CVS_ENT_DIR)
188: cf->file_type = CVS_DIR;
189: else
190: cf->file_type = CVS_FILE;
1.1 joris 191: }
192:
193: if (cf->fd != -1 && cf->file_ent != NULL) {
194: if (fstat(cf->fd, &st) == -1)
195: fatal("cvs_remote_classify_file(%s): %s", cf->file_path,
196: strerror(errno));
197:
198: mtime = cvs_hack_time(st.st_mtime, 1);
199: if (mtime != cf->file_ent->ce_mtime)
200: cf->file_status = FILE_MODIFIED;
201: else
202: cf->file_status = FILE_UPTODATE;
203: } else if (cf->fd == -1) {
204: cf->file_status = FILE_UNKNOWN;
205: }
206: }
207: