refactor: use std::ifstream in torrent-magnet (#3726)
This commit is contained in:
parent
185f1f96a5
commit
6c5fda2021
|
@ -7,6 +7,7 @@
|
||||||
#include <climits> /* INT_MAX */
|
#include <climits> /* INT_MAX */
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -17,7 +18,6 @@
|
||||||
|
|
||||||
#include "crypto-utils.h" /* tr_sha1() */
|
#include "crypto-utils.h" /* tr_sha1() */
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "file.h"
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "magnet-metainfo.h"
|
#include "magnet-metainfo.h"
|
||||||
#include "resume.h"
|
#include "resume.h"
|
||||||
|
@ -123,35 +123,31 @@ std::optional<std::vector<std::byte>> tr_torrentGetMetadataPiece(tr_torrent cons
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto const fd = tr_sys_file_open(tor->torrentFile(), TR_SYS_FILE_READ, 0);
|
auto in = std::ifstream{ tor->torrentFile(), std::ios_base::in };
|
||||||
if (fd == TR_BAD_SYS_FILE)
|
if (!in.is_open())
|
||||||
{
|
{
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto const info_dict_size = tor->infoDictSize();
|
auto const info_dict_size = tor->infoDictSize();
|
||||||
TR_ASSERT(info_dict_size > 0);
|
TR_ASSERT(info_dict_size > 0);
|
||||||
|
auto const offset_in_info_dict = static_cast<uint64_t>(piece) * METADATA_PIECE_SIZE;
|
||||||
if (size_t const o = piece * METADATA_PIECE_SIZE; tr_sys_file_seek(fd, tor->infoDictOffset() + o, TR_SEEK_SET, nullptr))
|
auto const offset_in_file = tor->infoDictOffset() + offset_in_info_dict;
|
||||||
|
if (!in.seekg(offset_in_file))
|
||||||
{
|
{
|
||||||
size_t const piece_len = o + METADATA_PIECE_SIZE <= info_dict_size ? METADATA_PIECE_SIZE : info_dict_size - o;
|
return {};
|
||||||
|
|
||||||
if (piece_len <= METADATA_PIECE_SIZE)
|
|
||||||
{
|
|
||||||
auto buf = std::vector<std::byte>{};
|
|
||||||
buf.resize(piece_len);
|
|
||||||
|
|
||||||
if (auto n_read = uint64_t{};
|
|
||||||
tr_sys_file_read(fd, std::data(buf), std::size(buf), &n_read) && n_read == std::size(buf))
|
|
||||||
{
|
|
||||||
tr_sys_file_close(fd);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tr_sys_file_close(fd);
|
auto buf = std::vector<std::byte>{};
|
||||||
return {};
|
auto const piece_len = offset_in_info_dict + METADATA_PIECE_SIZE <= info_dict_size ? METADATA_PIECE_SIZE :
|
||||||
|
info_dict_size - offset_in_info_dict;
|
||||||
|
buf.resize(piece_len);
|
||||||
|
if (!in.read(reinterpret_cast<char*>(std::data(buf)), std::size(buf)))
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getPieceLength(struct tr_incomplete_metadata const* m, int piece)
|
static int getPieceLength(struct tr_incomplete_metadata const* m, int piece)
|
||||||
|
|
|
@ -36,6 +36,7 @@ add_executable(libtransmission-test
|
||||||
subprocess-test.cc
|
subprocess-test.cc
|
||||||
test-fixtures.h
|
test-fixtures.h
|
||||||
torrent-files-test.cc
|
torrent-files-test.cc
|
||||||
|
torrent-magnet-test.cc
|
||||||
torrent-metainfo-test.cc
|
torrent-metainfo-test.cc
|
||||||
torrents-test.cc
|
torrents-test.cc
|
||||||
utils-test.cc
|
utils-test.cc
|
||||||
|
|
Loading…
Reference in New Issue