]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #include <errno.h> |
2 | #include <stdlib.h> | |
3 | ||
4 | #include <sstream> | |
5 | ||
6 | #include "rgw_torrent.h" | |
7 | #include "include/str_list.h" | |
8 | #include "include/rados/librados.hpp" | |
9 | ||
10 | #define dout_subsys ceph_subsys_rgw | |
11 | ||
12 | using namespace std; | |
13 | using ceph::crypto::MD5; | |
14 | using namespace librados; | |
15 | using namespace boost; | |
16 | using ceph::crypto::SHA1; | |
17 | ||
18 | seed::seed() | |
19 | { | |
20 | seed::info.piece_length = 0; | |
21 | seed::info.len = 0; | |
22 | sha_len = 0; | |
23 | is_torrent = false; | |
24 | } | |
25 | ||
26 | seed::~seed() | |
27 | { | |
28 | seed::info.sha1_bl.clear(); | |
29 | bl.clear(); | |
7c673cae FG |
30 | s = NULL; |
31 | store = NULL; | |
32 | } | |
33 | ||
34 | void seed::init(struct req_state *p_req, RGWRados *p_store) | |
35 | { | |
36 | s = p_req; | |
37 | store = p_store; | |
38 | } | |
39 | ||
3efd9988 FG |
40 | int seed::get_torrent_file(RGWRados::Object::Read &read_op, |
41 | uint64_t &total_len, | |
42 | ceph::bufferlist &bl_data, | |
43 | rgw_obj &obj) | |
7c673cae FG |
44 | { |
45 | /* add other field if config is set */ | |
46 | dencode.bencode_dict(bl); | |
47 | set_announce(); | |
48 | if (!comment.empty()) | |
49 | { | |
50 | dencode.bencode(COMMENT, comment, bl); | |
51 | } | |
52 | if (!create_by.empty()) | |
53 | { | |
54 | dencode.bencode(CREATED_BY, create_by, bl); | |
55 | } | |
56 | if (!encoding.empty()) | |
57 | { | |
58 | dencode.bencode(ENCODING, encoding, bl); | |
59 | } | |
60 | ||
61 | string oid, key; | |
7c673cae | 62 | get_obj_bucket_and_oid_loc(obj, oid, key); |
28e407b8 | 63 | ldout(s->cct, 20) << "NOTICE: head obj oid= " << oid << dendl; |
7c673cae | 64 | |
28e407b8 AA |
65 | const set<string> obj_key{RGW_OBJ_TORRENT}; |
66 | map<string, bufferlist> m; | |
67 | const int r = read_op.state.io_ctx.omap_get_vals_by_keys(oid, obj_key, &m); | |
68 | if (r < 0) { | |
69 | ldout(s->cct, 0) << "ERROR: omap_get_vals_by_keys failed: " << r << dendl; | |
70 | return r; | |
7c673cae | 71 | } |
28e407b8 AA |
72 | if (m.size() != 1) { |
73 | ldout(s->cct, 0) << "ERROR: omap key " RGW_OBJ_TORRENT " not found" << dendl; | |
74 | return -EINVAL; | |
7c673cae | 75 | } |
28e407b8 | 76 | bl.append(std::move(m.begin()->second)); |
7c673cae FG |
77 | dencode.bencode_end(bl); |
78 | ||
79 | bl_data = bl; | |
80 | total_len = bl.length(); | |
3efd9988 | 81 | return 0; |
7c673cae FG |
82 | } |
83 | ||
84 | bool seed::get_flag() | |
85 | { | |
86 | return is_torrent; | |
87 | } | |
88 | ||
31f18b77 | 89 | void seed::update(bufferlist &bl) |
7c673cae FG |
90 | { |
91 | if (!is_torrent) | |
92 | { | |
93 | return; | |
94 | } | |
7c673cae | 95 | info.len += bl.length(); |
31f18b77 FG |
96 | sha1(&h, bl, bl.length()); |
97 | } | |
98 | ||
99 | int seed::complete() | |
100 | { | |
101 | uint64_t remain = info.len%info.piece_length; | |
102 | uint8_t remain_len = ((remain > 0)? 1 : 0); | |
103 | sha_len = (info.len/info.piece_length + remain_len)*CEPH_CRYPTO_SHA1_DIGESTSIZE; | |
104 | ||
105 | int ret = 0; | |
106 | /* produce torrent data */ | |
107 | do_encode(); | |
108 | ||
109 | /* save torrent data into OMAP */ | |
110 | ret = save_torrent_file(); | |
111 | if (0 != ret) | |
112 | { | |
113 | ldout(s->cct, 0) << "ERROR: failed to save_torrent_file() ret= "<< ret << dendl; | |
114 | return ret; | |
115 | } | |
116 | ||
117 | return 0; | |
7c673cae FG |
118 | } |
119 | ||
120 | off_t seed::get_data_len() | |
121 | { | |
122 | return info.len; | |
123 | } | |
124 | ||
125 | void seed::set_create_date(ceph::real_time& value) | |
126 | { | |
127 | utime_t date = ceph::real_clock::to_timespec(value); | |
128 | create_date = date.sec(); | |
129 | } | |
130 | ||
131 | void seed::set_info_pieces(char *buff) | |
132 | { | |
133 | info.sha1_bl.append(buff, CEPH_CRYPTO_SHA1_DIGESTSIZE); | |
134 | } | |
135 | ||
136 | void seed::set_info_name(const string& value) | |
137 | { | |
138 | info.name = value; | |
139 | } | |
140 | ||
141 | void seed::sha1(SHA1 *h, bufferlist &bl, off_t bl_len) | |
142 | { | |
143 | off_t num = bl_len/info.piece_length; | |
144 | off_t remain = 0; | |
145 | remain = bl_len%info.piece_length; | |
146 | ||
147 | char *pstr = bl.c_str(); | |
148 | char sha[25]; | |
149 | ||
150 | /* get sha1 */ | |
151 | for (off_t i = 0; i < num; i++) | |
152 | { | |
153 | memset(sha, 0x00, sizeof(sha)); | |
154 | h->Update((byte *)pstr, info.piece_length); | |
155 | h->Final((byte *)sha); | |
156 | set_info_pieces(sha); | |
157 | pstr += info.piece_length; | |
158 | } | |
159 | ||
160 | /* process remain */ | |
161 | if (0 != remain) | |
162 | { | |
163 | memset(sha, 0x00, sizeof(sha)); | |
164 | h->Update((byte *)pstr, remain); | |
165 | h->Final((byte *)sha); | |
166 | set_info_pieces(sha); | |
167 | } | |
168 | } | |
169 | ||
7c673cae FG |
170 | int seed::get_params() |
171 | { | |
172 | is_torrent = true; | |
173 | info.piece_length = g_conf->rgw_torrent_sha_unit; | |
174 | create_by = g_conf->rgw_torrent_createby; | |
175 | encoding = g_conf->rgw_torrent_encoding; | |
176 | origin = g_conf->rgw_torrent_origin; | |
177 | comment = g_conf->rgw_torrent_comment; | |
178 | announce = g_conf->rgw_torrent_tracker; | |
179 | ||
180 | /* tracker and tracker list is empty, set announce to origin */ | |
181 | if (announce.empty() && !origin.empty()) | |
182 | { | |
183 | announce = origin; | |
184 | } | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
189 | void seed::set_announce() | |
190 | { | |
191 | list<string> announce_list; // used to get announce list from conf | |
192 | get_str_list(announce, ",", announce_list); | |
193 | ||
194 | if (announce_list.empty()) | |
195 | { | |
196 | ldout(s->cct, 5) << "NOTICE: announce_list is empty " << dendl; | |
197 | return; | |
198 | } | |
199 | ||
200 | list<string>::iterator iter = announce_list.begin(); | |
201 | dencode.bencode_key(ANNOUNCE, bl); | |
202 | dencode.bencode_key((*iter), bl); | |
203 | ||
204 | dencode.bencode_key(ANNOUNCE_LIST, bl); | |
205 | dencode.bencode_list(bl); | |
206 | for (; iter != announce_list.end(); ++iter) | |
207 | { | |
208 | dencode.bencode_list(bl); | |
209 | dencode.bencode_key((*iter), bl); | |
210 | dencode.bencode_end(bl); | |
211 | } | |
212 | dencode.bencode_end(bl); | |
213 | } | |
214 | ||
215 | void seed::do_encode() | |
216 | { | |
217 | /*Only encode create_date and sha1 info*/ | |
218 | /*Other field will be added if confi is set when run get torrent*/ | |
219 | dencode.bencode(CREATION_DATE, create_date, bl); | |
220 | ||
221 | dencode.bencode_key(INFO_PIECES, bl); | |
222 | dencode.bencode_dict(bl); | |
223 | dencode.bencode(LENGTH, info.len, bl); | |
224 | dencode.bencode(NAME, info.name, bl); | |
225 | dencode.bencode(PIECE_LENGTH, info.piece_length, bl); | |
226 | ||
227 | char info_sha[100] = { 0 }; | |
228 | sprintf(info_sha, "%" PRIu64, sha_len); | |
229 | string sha_len_str = info_sha; | |
230 | dencode.bencode_key(PIECES, bl); | |
231 | bl.append(sha_len_str.c_str(), sha_len_str.length()); | |
232 | bl.append(':'); | |
233 | bl.append(info.sha1_bl.c_str(), sha_len); | |
234 | dencode.bencode_end(bl); | |
235 | } | |
236 | ||
237 | int seed::save_torrent_file() | |
238 | { | |
239 | int op_ret = 0; | |
240 | string key = RGW_OBJ_TORRENT; | |
241 | rgw_obj obj(s->bucket, s->object.name); | |
242 | ||
243 | rgw_raw_obj raw_obj; | |
244 | store->obj_to_raw(s->bucket_info.placement_rule, obj, &raw_obj); | |
245 | ||
246 | op_ret = store->omap_set(raw_obj, key, bl); | |
247 | if (op_ret < 0) | |
248 | { | |
249 | ldout(s->cct, 0) << "ERROR: failed to omap_set() op_ret = " << op_ret << dendl; | |
250 | return op_ret; | |
251 | } | |
252 | ||
253 | return op_ret; | |
254 | } |