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