mirror of
https://github.com/transmission/transmission
synced 2025-02-21 13:46:52 +00:00
fix: return error when renaming into existing file (#5563)
This commit is contained in:
parent
b8ff35c4ce
commit
ddac05954b
2 changed files with 33 additions and 4 deletions
|
@ -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)
|
||||
|
@ -2567,7 +2593,7 @@ void torrentRenamePath(
|
|||
|
||||
int error = 0;
|
||||
|
||||
if (!renameArgsAreValid(oldpath, newname))
|
||||
if (!renameArgsAreValid(tor, oldpath, newname))
|
||||
{
|
||||
error = EINVAL;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
|
Loading…
Reference in a new issue