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