MCPcopy Create free account
hub / github.com/arvidn/libtorrent / generate_torrent

Function generate_torrent

tools/checking_benchmark.cpp:68–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

66}
67
68std::vector<char> generate_torrent(int num_pieces, std::string save_path
69 , lt::create_flags_t const flags)
70{
71 lt::file_storage fs;
72 // 1 MiB piece size
73 const int piece_size = 1024 * 1024;
74 const std::int64_t total_size = std::int64_t(piece_size) * num_pieces + 2356;
75
76 std::string const filename = "test_checking_file";
77
78 fs.add_file(filename, total_size);
79
80 std::string const filepath = save_path + "/" + filename;
81
82 lt::file_status st;
83 lt::error_code ec;
84 lt::stat_file(filepath, &st, ec);
85 if (ec && ec != boost::system::errc::no_such_file_or_directory)
86 {
87 std::cerr << "stat() failed: " << ec.message()
88 << " for file: " << filepath << '\n';
89 std::exit(1);
90 }
91 if (st.file_size != total_size)
92 {
93 std::cout << "writing test file\n";
94 std::uint64_t state = 0;
95 std::ofstream output(filepath, std::ios_base::binary);
96 std::int64_t bytes_left = total_size;
97 std::array<char, 100000> buffer;
98 while (bytes_left > 0)
99 {
100 generate_block_fill(buffer, state);
101 output.write(buffer.data(), std::min(buffer.size(), std::size_t(bytes_left)));
102 bytes_left -= buffer.size();
103
104 std::cout << "\rleft: " << bytes_left << " B ";
105 std::cout.flush();
106 }
107 std::cout << '\n';
108 }
109
110 lt::create_torrent t(fs, piece_size, flags);
111
112 std::cout << "hashing torrent\n";
113 lt::set_piece_hashes(t, save_path);
114
115 std::vector<char> ret;
116 bencode(std::back_inserter(ret), t.generate());
117 return ret;
118}
119
120void run_test(std::string const& save_path, lt::create_flags_t const flags
121 , lt::disk_io_constructor_type disk)

Callers 1

run_testFunction · 0.70

Calls 12

stat_fileFunction · 0.85
minFunction · 0.85
add_fileMethod · 0.80
flushMethod · 0.80
generateMethod · 0.80
generate_block_fillFunction · 0.70
bencodeFunction · 0.70
set_piece_hashesFunction · 0.50
messageMethod · 0.45
writeMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected