]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef CEPH_RGW_TORRENT_H |
2 | #define CEPH_RGW_TORRENT_H | |
3 | ||
4 | #include <string> | |
5 | #include <list> | |
6 | #include <map> | |
7 | #include <set> | |
8 | ||
9 | #include "common/ceph_time.h" | |
10 | ||
11 | #include "rgw_rados.h" | |
12 | #include "rgw_common.h" | |
13 | ||
14 | using namespace std; | |
15 | using ceph::crypto::SHA1; | |
16 | ||
17 | struct req_state; | |
18 | ||
19 | #define RGW_OBJ_TORRENT "rgw.torrent" | |
20 | ||
21 | #define ANNOUNCE "announce" | |
22 | #define ANNOUNCE_LIST "announce-list" | |
23 | #define COMMENT "comment" | |
24 | #define CREATED_BY "created by" | |
25 | #define CREATION_DATE "creation date" | |
26 | #define ENCODING "encoding" | |
27 | #define LENGTH "length" | |
28 | #define NAME "name" | |
29 | #define PIECE_LENGTH "piece length" | |
30 | #define PIECES "pieces" | |
31 | #define INFO_PIECES "info" | |
32 | #define GET_TORRENT "torrent" | |
33 | ||
34 | class TorrentBencode | |
35 | { | |
36 | public: | |
37 | TorrentBencode() {} | |
38 | ~TorrentBencode() {} | |
39 | ||
40 | //control characters | |
41 | void bencode_dict(bufferlist& bl) { bl.append('d'); } | |
42 | void bencode_list(bufferlist& bl) { bl.append('l'); } | |
43 | void bencode_end(bufferlist& bl) { bl.append('e'); } | |
44 | ||
45 | //single values | |
46 | void bencode(int value, bufferlist& bl) | |
47 | { | |
48 | bl.append('i'); | |
49 | char info[100] = { 0 }; | |
50 | sprintf(info, "%d", value); | |
51 | bl.append(info, strlen(info)); | |
52 | bencode_end(bl); | |
53 | } | |
54 | ||
55 | //single values | |
56 | void bencode(const std::string& str, bufferlist& bl) | |
57 | { | |
58 | bencode_key(str, bl); | |
59 | } | |
60 | ||
61 | //dictionary elements | |
62 | void bencode(const std::string& key, int value, bufferlist& bl) | |
63 | { | |
64 | bencode_key(key, bl); | |
65 | bencode(value, bl); | |
66 | } | |
67 | ||
68 | //dictionary elements | |
69 | void bencode(const std::string& key, const std::string& value, bufferlist& bl) | |
70 | { | |
71 | bencode_key(key, bl); | |
72 | bencode(value, bl); | |
73 | } | |
74 | ||
75 | //key len | |
76 | void bencode_key(const std::string& key, bufferlist& bl) | |
77 | { | |
78 | int len = key.length(); | |
79 | char info[100] = { 0 }; | |
80 | sprintf(info, "%d:", len); | |
81 | bl.append(info, strlen(info)); | |
82 | bl.append(key.c_str(), len); | |
83 | } | |
84 | }; | |
85 | ||
86 | /* torrent file struct */ | |
87 | class seed | |
88 | { | |
89 | private: | |
90 | struct | |
91 | { | |
92 | int piece_length; // each piece length | |
93 | bufferlist sha1_bl; // save sha1 | |
94 | string name; // file name | |
95 | off_t len; // file total bytes | |
96 | }info; | |
97 | ||
98 | string announce; // tracker | |
99 | string origin; // origin | |
100 | time_t create_date; // time of the file created | |
101 | string comment; // comment | |
102 | string create_by; // app name and version | |
103 | string encoding; // if encode use gbk rather than gtf-8 use this field | |
104 | uint64_t sha_len; // sha1 length | |
105 | bool is_torrent; // flag | |
106 | bufferlist bl; // bufflist ready to send | |
107 | list<bufferlist> torrent_bl; // meate data | |
108 | ||
109 | struct req_state *s; | |
110 | RGWRados *store; | |
111 | ||
112 | TorrentBencode dencode; | |
113 | public: | |
114 | seed(); | |
115 | ~seed(); | |
116 | ||
117 | int get_params(); | |
118 | void init(struct req_state *p_req, RGWRados *p_store); | |
119 | void get_torrent_file(int &op_ret, RGWRados::Object::Read &read_op, | |
120 | uint64_t &total_len, bufferlist &bl_data, rgw_obj &obj); | |
121 | ||
122 | off_t get_data_len(); | |
123 | bool get_flag(); | |
124 | ||
125 | int handle_data(); | |
126 | void save_data(bufferlist &bl); | |
127 | void set_create_date(ceph::real_time& value); | |
128 | void set_info_name(const string& value); | |
129 | ||
130 | private: | |
131 | void do_encode (); | |
132 | void set_announce(); | |
133 | void set_exist(bool exist); | |
134 | void set_info_pieces(char *buff); | |
135 | int sha1_process(); | |
136 | void sha1(SHA1 *h, bufferlist &bl, off_t bl_len); | |
137 | int save_torrent_file(); | |
138 | }; | |
139 | #endif /* CEPH_RGW_TORRENT_H */ |