Skip to content

Commit

Permalink
fix: return error when renaming into existing file (#5563)
Browse files Browse the repository at this point in the history
  • Loading branch information
basilefff committed Jun 19, 2023
1 parent b8ff35c commit ddac059
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
34 changes: 30 additions & 4 deletions libtransmission/torrent.cc
Expand Up @@ -2447,10 +2447,36 @@ namespace
{
namespace rename_helpers
{
bool renameArgsAreValid(std::string_view oldpath, std::string_view newname)
bool renameArgsAreValid(tr_torrent const* tor, std::string_view oldpath, std::string_view newname)
{
return !std::empty(oldpath) && !std::empty(newname) && newname != "."sv && newname != ".."sv &&
!tr_strvContains(newname, TR_PATH_DELIMITER);
if (std::empty(oldpath) || std::empty(newname) || newname == "."sv || newname == ".."sv ||
tr_strvContains(newname, TR_PATH_DELIMITER))
{
return false;
}

auto const newpath = tr_strvContains(oldpath, TR_PATH_DELIMITER) ?
tr_pathbuf{ tr_sys_path_dirname(oldpath), '/', newname } :
tr_pathbuf{ newname };

if (newpath == oldpath)
{
return true;
}

auto const newpath_as_dir = tr_pathbuf{ newpath, '/' };
auto const n_files = tor->file_count();

for (tr_file_index_t i = 0; i < n_files; ++i)
{
auto const& name = tor->file_subpath(i);
if (newpath == name || tr_strvStartsWith(name, newpath_as_dir))
{
return false;
}
}

return true;
}

auto renameFindAffectedFiles(tr_torrent const* tor, std::string_view oldpath)
Expand Down Expand Up @@ -2567,7 +2593,7 @@ void torrentRenamePath(

int error = 0;

if (!renameArgsAreValid(oldpath, newname))
if (!renameArgsAreValid(tor, oldpath, newname))
{
error = EINVAL;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/libtransmission/rename-test.cc
Expand Up @@ -414,6 +414,9 @@ TEST_F(RenameTest, multifileTorrent)
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "Felidae/FelinaeX", "Genus Felinae"));
EXPECT_STREQ("Felidae", tr_torrentName(tor));

// rename filename collision
EXPECT_EQ(EINVAL, torrentRenameAndWait(tor, "Felidae/Felinae/Felis/catus/Kyphi", "Saffron"));
EXPECT_STREQ("Felidae", tr_torrentName(tor));
/***
****
***/
Expand Down

0 comments on commit ddac059

Please sign in to comment.