fix: 5053 old torrent files keep appearing (#5117)

This commit is contained in:
Charles Kerr 2023-03-01 20:12:19 -06:00 committed by GitHub
parent d21a3b622a
commit d3273504bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 34 deletions

View File

@ -621,22 +621,14 @@ auto loadProgress(tr_variant* dict, tr_torrent* tor)
// ---
auto loadFromFile(tr_torrent* tor, tr_resume::fields_t fields_to_load, bool* did_migrate_filename)
auto loadFromFile(tr_torrent* tor, tr_resume::fields_t fields_to_load)
{
auto fields_loaded = tr_resume::fields_t{};
TR_ASSERT(tr_isTorrent(tor));
auto const was_dirty = tor->isDirty;
auto const migrated = tr_torrent_metainfo::migrateFile(
tor->session->resumeDir(),
tor->name(),
tor->infoHashString(),
".resume"sv);
if (did_migrate_filename != nullptr)
{
*did_migrate_filename = migrated;
}
tr_torrent_metainfo::migrateFile(tor->session->resumeDir(), tor->name(), tor->infoHashString(), ".resume"sv);
auto const filename = tor->resumeFile();
if (!tr_sys_path_exists(filename))
@ -865,7 +857,7 @@ auto useFallbackFields(tr_torrent* tor, tr_resume::fields_t fields, tr_ctor cons
}
} // namespace
fields_t load(tr_torrent* tor, fields_t fields_to_load, tr_ctor const* ctor, bool* did_rename_to_hash_only_name)
fields_t load(tr_torrent* tor, fields_t fields_to_load, tr_ctor const* ctor)
{
TR_ASSERT(tr_isTorrent(tor));
@ -873,7 +865,7 @@ fields_t load(tr_torrent* tor, fields_t fields_to_load, tr_ctor const* ctor, boo
ret |= useMandatoryFields(tor, fields_to_load, ctor);
fields_to_load &= ~ret;
ret |= loadFromFile(tor, fields_to_load, did_rename_to_hash_only_name);
ret |= loadFromFile(tor, fields_to_load);
fields_to_load &= ~ret;
ret |= useFallbackFields(tor, fields_to_load, ctor);

View File

@ -46,7 +46,7 @@ auto inline constexpr Group = fields_t{ 1 << 23 };
auto inline constexpr All = ~fields_t{ 0 };
fields_t load(tr_torrent* tor, fields_t fields_to_load, tr_ctor const* ctor, bool* did_rename_to_hash_only_name);
fields_t load(tr_torrent* tor, fields_t fields_to_load, tr_ctor const* ctor);
void save(tr_torrent* tor);

View File

@ -679,22 +679,20 @@ bool tr_torrent_metainfo::migrateFile(
std::string_view suffix)
{
auto const old_filename = makeFilename(dirname, name, info_hash_string, BasenameFormat::NameAndPartialHash, suffix);
auto const old_filename_exists = tr_sys_path_exists(old_filename);
auto const new_filename = makeFilename(dirname, name, info_hash_string, BasenameFormat::Hash, suffix);
auto const new_filename_exists = tr_sys_path_exists(new_filename);
if (!tr_sys_path_exists(old_filename))
{
return false;
}
if (old_filename_exists && new_filename_exists)
auto const new_filename = makeFilename(dirname, name, info_hash_string, BasenameFormat::Hash, suffix);
if (tr_sys_path_exists(new_filename))
{
tr_sys_path_remove(old_filename);
return false;
}
if (new_filename_exists)
{
return false;
}
if (old_filename_exists && tr_sys_path_rename(old_filename, new_filename))
auto const renamed = tr_sys_path_rename(old_filename, new_filename);
if (!renamed)
{
tr_logAddError(
fmt::format(
@ -705,7 +703,7 @@ bool tr_torrent_metainfo::migrateFile(
return true;
}
return false; // neither file exists
return renamed;
}
void tr_torrent_metainfo::removeFile(

View File

@ -1105,15 +1105,9 @@ void torrentInit(tr_torrent* tor, tr_ctor const* ctor)
// the same ones that would be saved back again, so don't let them
// affect the 'is dirty' flag.
auto const was_dirty = tor->isDirty;
bool resume_file_was_migrated = false;
loaded = tr_resume::load(tor, tr_resume::All, ctor, &resume_file_was_migrated);
loaded = tr_resume::load(tor, tr_resume::All, ctor);
tor->isDirty = was_dirty;
if (resume_file_was_migrated)
{
tr_torrent_metainfo::migrateFile(session->torrentDir(), tor->name(), tor->infoHashString(), ".torrent"sv);
}
tr_torrent_metainfo::migrateFile(session->torrentDir(), tor->name(), tor->infoHashString(), ".torrent"sv);
}
tor->completeness = tor->completion.status();

View File

@ -192,7 +192,7 @@ TEST_F(RenameTest, singleFilenameTorrent)
// (while it's renamed: confirm that the .resume file remembers the changes)
tr_resume::save(tor);
sync();
auto const loaded = tr_resume::load(tor, tr_resume::All, ctor, nullptr);
auto const loaded = tr_resume::load(tor, tr_resume::All, ctor);
EXPECT_STREQ("foobar", tr_torrentName(tor));
EXPECT_NE(decltype(loaded){ 0 }, (loaded & tr_resume::Name));
@ -303,7 +303,7 @@ TEST_F(RenameTest, multifileTorrent)
tr_resume::save(tor);
// this is a bit dodgy code-wise, but let's make sure the .resume file got the name
tor->setFileSubpath(1, "gabba gabba hey"sv);
auto const loaded = tr_resume::load(tor, tr_resume::All, ctor, nullptr);
auto const loaded = tr_resume::load(tor, tr_resume::All, ctor);
EXPECT_NE(decltype(loaded){ 0 }, (loaded & tr_resume::Filenames));
EXPECT_EQ(ExpectedFiles[0], tr_torrentFile(tor, 0).name);
EXPECT_STREQ("Felidae/Felinae/Felis/placeholder/Kyphi", tr_torrentFile(tor, 1).name);