Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Sep 30, 2024
1 parent fa4576c commit 46e45f3
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 6 deletions.
3 changes: 2 additions & 1 deletion include/libtorrent/aux_/posix_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ namespace aux {
, storage_error& error);

bool has_any_file(storage_error& error);
void set_file_priority(aux::vector<download_priority_t, file_index_t>& prio
void set_file_priority(settings_interface const&
, aux::vector<download_priority_t, file_index_t>& prio
, storage_error& ec);
bool verify_resume_data(add_torrent_params const& rd
, aux::vector<std::string, file_index_t> const& links
Expand Down
1 change: 0 additions & 1 deletion src/part_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ namespace libtorrent {
span<char> v = {buf.get(), block_to_copy};
auto bytes_read = aux::pread_all(file.fd(), v, slot_offset(slot) + piece_offset, ec);
v = v.first(static_cast<std::ptrdiff_t>(bytes_read));
TORRENT_ASSERT(!ec);
if (ec || v.empty()) return;

f(file_offset, {buf.get(), block_to_copy});
Expand Down
2 changes: 1 addition & 1 deletion src/posix_disk_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ namespace {
{
posix_storage* st = m_torrents[storage].get();
storage_error error;
st->set_file_priority(prio, error);
st->set_file_priority(m_settings, prio, error);
post(m_ios, [p = std::move(prio), h = std::move(handler), error] () mutable
{ h(error, std::move(p)); });
}
Expand Down
1 change: 0 additions & 1 deletion src/posix_part_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ namespace aux {
if (int(bytes_read) != block_to_copy)
ec.assign(errno, generic_category());

TORRENT_ASSERT(!ec);
if (ec) return;

f(file_offset, {buf.get(), block_to_copy});
Expand Down
3 changes: 2 additions & 1 deletion src/posix_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ namespace aux {
, files().num_pieces(), files().piece_length());
}

void posix_storage::set_file_priority(aux::vector<download_priority_t, file_index_t>& prio
void posix_storage::set_file_priority(settings_interface const&
, aux::vector<download_priority_t, file_index_t>& prio
, storage_error& ec)
{
// extend our file priorities in case it's truncated
Expand Down
126 changes: 125 additions & 1 deletion test/test_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,118 @@ void test_rename(std::string const& test_path)
TEST_EQUAL(s->files().file_path(0_file), "new_filename");
}

template <typename StorageType>
void test_pre_allocate()
{
std::string const test_path = complete("pre_allocate_test_path");
delete_dirs(combine_path(test_path, "temp_storage"));

file_storage fs;
std::vector<char> buf;
typename file_pool_type<StorageType>::type fp;
io_context ios;

aux::session_settings set;
std::shared_ptr<torrent_info> info = setup_torrent_info(fs, buf);

aux::vector<download_priority_t, file_index_t> priorities{
lt::dont_download,
lt::default_priority,
lt::default_priority,
lt::default_priority,
lt::default_priority,
};
sha1_hash info_hash;
storage_params p{
fs,
nullptr,
test_path,
storage_mode_allocate,
priorities,
info_hash
};
auto s = make_storage<StorageType>(p, fp);

// allocate the files and create the directories
storage_error se;
s->initialize(set, se);
if (se)
{
TEST_ERROR(se.ec.message().c_str());
std::printf("storage::initialize %s: %d\n"
, se.ec.message().c_str(), static_cast<int>(se.file()));
throw system_error(se.ec);
}

std::vector<char> piece1 = new_piece(0x4000);
span<char> iov = span<char>(piece1);

// ensure all files, except the first one, have been allocated
for (auto i : fs.file_range())
{
if (fs.file_size(i) > 0)
{
int ret = write(s, set, iov, fs.piece_index_at_file(i), 0, aux::open_mode::write, se);
TEST_EQUAL(ret, int(iov.size()));
TEST_CHECK(!se.ec);
}

error_code ec;
file_status st;
std::string const path = fs.file_path(i, test_path);
stat_file(path, &st, ec);
if (i == file_index_t{0})
{
// the first file has priority 0, and so should not be created
TEST_EQUAL(ec, boost::system::errc::no_such_file_or_directory);
}
else
{
TEST_CHECK(!ec);
std::cerr << "error: " << ec.message() << std::endl;
TEST_EQUAL(st.file_size, fs.file_size(i));

#ifdef TORRENT_WINDOWS
native_path_string path2 = convert_to_native_path_string(path);
FILE_STANDARD_INFO Standard;
TEST_CHECK(GetFileInformationByName(path2.c_str(), FILE_INFO_BY_HANDLE_CLASS::FileStandardInfo, &Standard, sizeof(FILE_STANDARD_INFO)));
TEST_EQUAL(Standard.AllocationSize, fs.file_size(i));
#else
struct ::stat st2{};
TEST_EQUAL(::stat(path.c_str(), &st2), 0);
TEST_CHECK(st2.st_blocks * 512 >= fs.file_size(i));
#endif
}
}

std::cerr << "set file priority" << std::endl;
// set priority of file 0 to non-zero, and make sure we create the file now
priorities[0_file] = lt::default_priority;
s->set_file_priority(set, priorities, se);
TEST_CHECK(!se.ec);

for (auto i : fs.file_range())
{
error_code ec;
file_status st;
std::string const path = fs.file_path(i, test_path);
stat_file(path, &st, ec);
std::cerr<< "error: " << ec.message() << std::endl;
TEST_CHECK(!ec);

#ifdef TORRENT_WINDOWS
native_path_string path2 = convert_to_native_path_string(path);
FILE_STANDARD_INFO Standard;
TEST_CHECK(GetFileInformationByName(path2.c_str(), FILE_INFO_BY_HANDLE_CLASS::FileStandardInfo, &Standard, sizeof(FILE_STANDARD_INFO)));
TEST_EQUAL(Standard.AllocationSize, fs.file_size(i));
#else
struct ::stat st2{};
TEST_EQUAL(::stat(path.c_str(), &st2), 0);
TEST_CHECK(st2.st_blocks * 512 >= fs.file_size(i));
#endif
}
}

using lt::operator""_bit;
using check_files_flag_t = lt::flags::bitfield_flag<std::uint64_t, struct check_files_flag_type_tag>;

Expand Down Expand Up @@ -709,11 +821,15 @@ TORRENT_TEST(check_files_oversized_mmap)
test_check_files(sparse | test_oversized, lt::mmap_disk_io_constructor);
}


TORRENT_TEST(check_files_allocate_mmap)
{
test_check_files(zero_prio, lt::mmap_disk_io_constructor);
}

TORRENT_TEST(test_pre_allocate_mmap)
{
test_pre_allocate<mmap_storage>();
}
#endif
TORRENT_TEST(check_files_sparse_posix)
{
Expand All @@ -736,6 +852,14 @@ TORRENT_TEST(check_files_allocate_posix)
test_check_files(zero_prio, lt::posix_disk_io_constructor);
}

// posix_storage doesn't support pre-allocating files on non-windows
/*
TORRENT_TEST(test_pre_allocate_posix)
{
test_pre_allocate<posix_storage>();
}
*/

Check notice

Code scanning / CodeQL

Commented-out code Note test

This comment appears to contain commented-out code.

#if TORRENT_HAVE_MMAP || TORRENT_HAVE_MAP_VIEW_OF_FILE
TORRENT_TEST(rename_mmap_disk_io)
{
Expand Down

0 comments on commit 46e45f3

Please sign in to comment.