-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3block.c
230 lines (214 loc) · 4.98 KB
/
s3block.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Copyright (c) 2008 Richard Bilson */
#include "stdinc.h"
#include "dat.h"
#include "fns.h"
#include "whack.h"
static char*
doS3write(S3Con *c, VtReq *r)
{
S3Vhdr hdr;
S3Req req;
S3Resp resp;
char *err = nil;
uchar *block;
int wlen;
hdr.blocktype = r->tx.blocktype;
hdr.size = packetsize(r->tx.data);
packetsha1(r->tx.data, r->rx.score);
memset(&req, 0, sizeof(req));
req.method = "PUT";
req.resource = smprint("/%U/%V", config.bucket, r->rx.score);
req.content = vtmalloc(HdrSize + AESbsize + hdr.size);
/* compress */
block = vtmalloc(hdr.size);
packetcopy(r->tx.data, block, 0, hdr.size);
wlen = whackblock(req.content+HdrSize+AESbsize, block, hdr.size);
if(wlen > 0 && wlen < hdr.size) {
//fprint(2, "dos3write compress %V\n", r->rx.score);
hdr.codec = BlockECompress;
hdr.csize = wlen;
} else {
if(wlen > hdr.size) {
fprint(2, "whack error: dsize=%d size=%d\n", wlen, hdr.size);
abort();
}
hdr.codec = BlockENone;
hdr.csize = hdr.size;
memmove(req.content+HdrSize+AESbsize, block, hdr.size);
}
req.clen = HdrSize + AESbsize + hdr.csize;
/* encrypt */
if(config.key){
encryptblock(req.content+HdrSize, hdr.csize, config.key);
}
/* write */
vhdrpack(&hdr, req.content);
//sha1(req.content+HdrSize+AESbsize, hdr.csize, r->rx.score, nil);
//req.resource = smprint("/rcbilson-ventitest/%V", r->rx.score);
if(err = S3request(c, &req, &resp)) {
err = vtstrdup(err);
goto cleanup;
}
if(resp.result[0] != '2'){
err = vtstrdup(resp.result);
S3responsediscard(&resp);
goto cleanup;
}
cleanup:
vtfree(req.content);
vtfree(block);
free(req.resource);
return err;
}
static char*
doS3read(S3Con *c, VtReq *r)
{
S3Vhdr hdr;
S3Req req;
S3Resp resp;
char *err = nil;
uchar rawhdr[HdrSize], sha1[VtScoreSize], *buf, *ubuf;
long len;
Unwhack uw;
int nunc;
memset(&req, 0, sizeof(req));
req.method = "GET";
req.resource = smprint("/%U/%V", config.bucket, r->tx.score);
if(err = S3request(c, &req, &resp)) {
err = vtstrdup(err);
goto cleanup;
}
if(memcmp(resp.result, "404", 3) == 0) {
r->rx.error = vtstrdup("no such block");
r->rx.msgtype = VtRerror;
goto cleanup2;
}
if(resp.result[0] != '2'){
err = vtstrdup(resp.result);
goto cleanup2;
}
len = 0;
do {
long n = S3response(&resp, rawhdr, HdrSize - len);
if(n < 0) {
err = vtstrdup("EOF on response");
goto cleanup;
}
len += n;
} while( len < HdrSize );
vhdrunpack(&hdr, rawhdr);
if(hdr.blocktype != r->tx.blocktype) {
r->rx.error = vtstrdup("type mismatch");
r->rx.msgtype = VtRerror;
goto cleanup;
}
if(hdr.size > r->tx.count) {
r->rx.error = vtstrdup("too big");
r->rx.msgtype = VtRerror;
goto cleanup;
}
buf = vtmalloc(AESbsize + hdr.csize);
len = 0;
do {
long n = S3response(&resp, buf, AESbsize + hdr.csize - len);
if(n <= 0) {
err = vtstrdup("EOF on response");
vtfree(buf);
goto cleanup;
}
len += n;
} while(len < AESbsize + hdr.csize);
/* decrypt */
if(config.key)
decryptblock(buf, hdr.csize, config.key);
/* decompress */
ubuf = vtmalloc(hdr.size);
switch(hdr.codec){
case BlockECompress:
//fprint(2, "dos3read decompress %V\n", r->tx.score);
unwhackinit(&uw);
nunc = unwhack(&uw, ubuf, hdr.size, buf+AESbsize, hdr.csize);
if(nunc != hdr.size){
if(nunc < 0) {
char *msg = smprint("decompression failed: %s", uw.err);
r->rx.error = vtstrdup(msg);
r->rx.msgtype = VtRerror;
free(msg);
} else {
char *msg = smprint("decompression gave partial block: %d/%d\n", nunc, hdr.size);
r->rx.error = vtstrdup(msg);
r->rx.msgtype = VtRerror;
free(msg);
}
vtfree(buf);
vtfree(ubuf);
goto cleanup;
}
break;
case BlockENone:
if(hdr.csize != hdr.size){
char *msg = smprint("loading clump: bad uncompressed size for uncompressed block %V", r->tx.score);
r->rx.error = vtstrdup(msg);
r->rx.msgtype = VtRerror;
free(msg);
vtfree(buf);
vtfree(ubuf);
goto cleanup;
}
memmove(ubuf, buf+AESbsize, hdr.size);
break;
default:
r->rx.error = vtstrdup("unknown encoding in loadlump");
r->rx.msgtype = VtRerror;
vtfree(buf);
vtfree(ubuf);
goto cleanup;
}
vtfree(buf);
r->rx.data = packetalloc();
packetappend(r->rx.data, ubuf, hdr.size);
packetsha1(r->rx.data, sha1);
if(memcmp(sha1, r->tx.score, VtScoreSize) != 0) {
r->rx.error = vtstrdup("score mismatch");
r->rx.msgtype = VtRerror;
packetfree(r->rx.data);
vtfree(ubuf);
goto cleanup2;
}
vtfree(ubuf);
goto cleanup;
cleanup2:
S3responsediscard(&resp);
cleanup:
free(req.resource);
return err;
}
int nretries = 2;
static void
S3retry(S3Con *c, VtReq *r, char *(*op)(S3Con *c, VtReq *r), int nretries)
{
char *err;
int ntries = 0;
while(1) {
err = op(c, r);
if(!err || ntries++ >= nretries ) break;
vtfree(err);
//fprint(2, "RETRY\n");
S3reopen(c);
}
if(err) {
//fprint(2, "RETRIES FAILED\n");
r->rx.error = err;
r->rx.msgtype = VtRerror;
}
}
void
S3writeblock(S3Con *c, VtReq *r)
{
S3retry(c, r, doS3write, nretries);
}
void
S3readblock(S3Con *c, VtReq *r)
{
S3retry(c, r, doS3read, nretries);
}