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