1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-31 03:12:44 +00:00

fix: empty torrent filename (#2435)

fix torrents readded due to unremoved cfg files.
This commit is contained in:
Charles Kerr 2022-01-17 23:14:00 -06:00 committed by GitHub
parent aa2183c9c5
commit 8d75736ad1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 40 additions and 48 deletions

View file

@ -682,7 +682,7 @@ void tr_torrentSaveResume(tr_torrent* tor)
saveName(&top, tor);
saveLabels(&top, tor);
auto const err = tr_variantToFile(&top, TR_VARIANT_FMT_BENC, tor->resumeFilename());
auto const err = tr_variantToFile(&top, TR_VARIANT_FMT_BENC, tor->resumeFile());
if (err != 0)
{
tor->setLocalError(tr_strvJoin("Unable to save resume file: ", tr_strerror(err)));
@ -702,8 +702,7 @@ static uint64_t loadFromFile(tr_torrent* tor, uint64_t fieldsToLoad, bool* did_m
*did_migrate_filename = migrated;
}
std::string const filename = tor->resumeFilename();
auto const filename = tor->resumeFile();
auto buf = std::vector<char>{};
tr_error* error = nullptr;
auto top = tr_variant{};

View file

@ -80,6 +80,7 @@ bool tr_ctorSetMetainfoFromFile(tr_ctor* ctor, std::string const& filename, tr_e
return false;
}
ctor->torrent_filename = filename;
auto const contents_sv = std::string_view{ std::data(ctor->contents), std::size(ctor->contents) };
return ctor->metainfo.parseBenc(contents_sv, error);
}
@ -91,6 +92,7 @@ bool tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename, tr_error**
bool tr_ctorSetMetainfo(tr_ctor* ctor, char const* metainfo, size_t len, tr_error** error)
{
ctor->torrent_filename.clear();
ctor->contents.assign(metainfo, metainfo + len);
auto const contents_sv = std::string_view{ std::data(ctor->contents), std::size(ctor->contents) };
return ctor->metainfo.parseBenc(contents_sv, error);
@ -98,6 +100,7 @@ bool tr_ctorSetMetainfo(tr_ctor* ctor, char const* metainfo, size_t len, tr_erro
bool tr_ctorSetMetainfoFromMagnetLink(tr_ctor* ctor, char const* magnet_link, tr_error** error)
{
ctor->torrent_filename.clear();
return ctor->metainfo.parseMagnet(magnet_link ? magnet_link : "", error);
}
@ -108,7 +111,7 @@ std::string_view tr_ctorGetContents(tr_ctor const* ctor)
char const* tr_ctorGetSourceFile(tr_ctor const* ctor)
{
return ctor->metainfo.torrentFile().c_str();
return ctor->torrent_filename.c_str();
}
bool tr_ctorSaveContents(tr_ctor const* ctor, std::string const& filename, tr_error** error)

View file

@ -277,14 +277,13 @@ static bool useNewMetainfo(tr_torrent* tor, tr_incomplete_metadata const* m, tr_
}
// save it
auto const filename = tor->torrentFilename();
auto const filename = tor->torrentFile();
if (!tr_saveFile(filename, benc, error))
{
return false;
}
// tor should keep this metainfo
metainfo.setTorrentFile(filename);
tor->setMetainfo(metainfo);
return true;

View file

@ -126,16 +126,6 @@ public:
return is_private_;
}
[[nodiscard]] auto const& torrentFile() const
{
return torrent_file_;
}
void setTorrentFile(std::string_view filename)
{
torrent_file_ = filename;
}
[[nodiscard]] tr_sha1_digest_t const& pieceHash(tr_piece_index_t piece) const;
[[nodiscard]] auto const& dateCreated() const
@ -155,12 +145,12 @@ public:
return info_dict_offset_;
}
std::string torrentFilename(std::string_view torrent_dir) const
std::string torrentFile(std::string_view torrent_dir) const
{
return makeFilename(torrent_dir, name(), infoHashString(), BasenameFormat::Hash, ".torrent");
}
std::string resumeFilename(std::string_view resume_dir) const
std::string resumeFile(std::string_view resume_dir) const
{
return makeFilename(resume_dir, name(), infoHashString(), BasenameFormat::Hash, ".resume");
}
@ -241,9 +231,6 @@ private:
std::string creator_;
std::string source_;
// empty unless `parseTorrentFile()` was used
std::string torrent_file_;
time_t date_created_ = 0;
// Offset + size of the bencoded info dict subset of the bencoded data.

View file

@ -723,16 +723,12 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor)
tr_sessionAddTorrent(session, tor);
// if we don't have a local .torrent file already, assume the torrent is new
auto filename = tor->torrentFilename();
auto const filename = tor->torrentFile();
bool const is_new_torrent = !tr_sys_path_exists(filename.c_str(), nullptr);
if (is_new_torrent)
{
tr_error* error = nullptr;
if (tr_ctorSaveContents(ctor, filename, &error))
{
tor->setTorrentFile(filename);
}
else
if (!tr_ctorSaveContents(ctor, filename, &error))
{
tor->setLocalError(
tr_strvJoin("Unable to save torrent file: ", error->message, " ("sv, std::to_string(error->code), ")"sv));
@ -1154,7 +1150,6 @@ tr_torrent_view tr_torrentView(tr_torrent const* tor)
auto ret = tr_torrent_view{};
ret.name = tr_torrentName(tor);
ret.hash_string = tor->infoHashString().c_str();
ret.torrent_filename = tor->torrentFile().c_str();
ret.comment = tor->comment().c_str();
ret.creator = tor->creator().c_str();
ret.source = tor->source().c_str();
@ -1168,6 +1163,11 @@ tr_torrent_view tr_torrentView(tr_torrent const* tor)
return ret;
}
char* tr_torrentFilename(tr_torrent const* tor)
{
return tr_strvDup(tor->torrentFile());
}
/***
****
***/

View file

@ -476,14 +476,14 @@ public:
return metainfo_.dateCreated();
}
[[nodiscard]] auto const& torrentFile() const
[[nodiscard]] auto torrentFile() const
{
return metainfo_.torrentFile();
return metainfo_.torrentFile(this->session->torrent_dir);
}
void setTorrentFile(std::string_view filename)
[[nodiscard]] auto resumeFile() const
{
metainfo_.setTorrentFile(filename);
return metainfo_.resumeFile(this->session->resume_dir);
}
[[nodiscard]] auto const& comment() const
@ -516,16 +516,6 @@ public:
return metainfo_.infoDictOffset();
}
[[nodiscard]] auto torrentFilename() const
{
return metainfo_.torrentFilename(this->session->torrent_dir);
}
[[nodiscard]] auto resumeFilename() const
{
return metainfo_.resumeFilename(this->session->resume_dir);
}
/// METAINFO - CHECKSUMS
[[nodiscard]] bool ensurePieceIsChecked(tr_piece_index_t piece);

View file

@ -1442,7 +1442,6 @@ struct tr_torrent_view
{
char const* name;
char const* hash_string;
char const* torrent_filename;
char const* comment; // optional; may be nullptr
char const* creator; // optional; may be nullptr
@ -1461,6 +1460,12 @@ struct tr_torrent_view
struct tr_torrent_view tr_torrentView(tr_torrent const* tor);
/*
* Get the filename of Transmission's internal copy of the .torrent file.
* This is a duplicate that must be freed with tr_free() when done.
*/
char* tr_torrentFilename(tr_torrent const* tor);
/***********************************************************************
* tr_torrentAvailability
***********************************************************************

View file

@ -360,9 +360,12 @@ bool tr_saveFile(std::string const& filename, std::string_view contents, tr_erro
{
// follow symlinks to find the "real" file, to make sure the temporary
// we build with tr_sys_file_open_temp() is created on the right partition
if (char* real_filename = tr_sys_path_resolve(filename.c_str(), nullptr); real_filename != nullptr)
if (char* const real_filename_c_str = tr_sys_path_resolve(filename.c_str(), nullptr); real_filename_c_str != nullptr)
{
if (auto const rfsv = std::string_view{ real_filename }; rfsv != filename)
auto const real_filename = std::string{ real_filename_c_str };
tr_free(real_filename_c_str);
if (real_filename != filename)
{
return tr_saveFile(real_filename, contents, error);
}

View file

@ -844,8 +844,10 @@ bool trashDataFile(char const* filename, tr_error** error)
- (NSString*)torrentLocation
{
auto const* filename = tr_torrentView(fHandle).torrent_filename;
return filename ? @(filename) : @"";
auto* const filename = tr_torrentFilename(fHandle);
NSString* ret = @(filename ? filename : "");
tr_free(filename);
return ret;
}
- (NSString*)dataLocation

View file

@ -7,11 +7,13 @@
*/
#include "transmission.h"
#include "crypto-utils.h"
#include "file.h"
#include "resume.h"
#include "torrent.h" // tr_isTorrent()
#include "tr-assert.h"
#include "utils.h"
#include "variant.h"
#include "test-fixtures.h"
@ -200,7 +202,9 @@ TEST_F(RenameTest, singleFilenameTorrent)
EXPECT_FALSE(tr_sys_path_exists(tmpstr.c_str(), nullptr)); // confirm the old filename can't be found
EXPECT_STREQ("foobar", tr_torrentName(tor)); // confirm the torrent's name is now 'foobar'
EXPECT_STREQ("foobar", tr_torrentFile(tor, 0).name); // confirm the file's name is now 'foobar'
EXPECT_STREQ(nullptr, strstr(tr_torrentView(tor).torrent_filename, "foobar")); // confirm .torrent file hasn't changed
char* const torrent_filename = tr_torrentFilename(tor);
EXPECT_STREQ(nullptr, strstr(torrent_filename, "foobar")); // confirm .torrent file hasn't changed
tr_free(torrent_filename);
tmpstr = tr_strvPath(tor->currentDir().sv(), "foobar");
EXPECT_TRUE(tr_sys_path_exists(tmpstr.c_str(), nullptr)); // confirm the file's name is now 'foobar' on the disk
EXPECT_TRUE(testFileExistsAndConsistsOfThisString(tor, 0, "hello, world!\n")); // confirm the contents are right