diff --git a/libtransmission/open-files.cc b/libtransmission/open-files.cc index 163a167fb..dbd8612d9 100644 --- a/libtransmission/open-files.cc +++ b/libtransmission/open-files.cc @@ -141,7 +141,7 @@ std::optional tr_open_files::get( tr_torrent_id_t tor_id, tr_file_index_t file_num, bool writable, - std::string_view filename_in, + std::string_view const filename, Preallocation allocation, uint64_t file_size) { @@ -158,13 +158,10 @@ std::optional tr_open_files::get( } // create subfolders, if any - auto const filename = tr_pathbuf{ filename_in }; auto error = tr_error{}; if (writable) { - auto dir = tr_pathbuf{ filename.sv() }; - dir.popdir(); - if (!tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777, &error)) + if (auto const dir = tr_sys_path_dirname(filename); !tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777, &error)) { tr_logAddError( fmt::format( diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 93ad75592..d4f831642 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -1799,9 +1799,7 @@ void tr_torrent::create_empty_files() const filename.assign(base, '/', subpath); // create subfolders, if any - auto dir = tr_pathbuf{ filename.sv() }; - dir.popdir(); - tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777); + tr_sys_dir_create(tr_sys_path_dirname(filename), TR_SYS_DIR_CREATE_PARENTS, 0777); // create the file if (auto const fd = tr_sys_file_open(filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_SEQUENTIAL, 0666); diff --git a/libtransmission/tr-strbuf.h b/libtransmission/tr-strbuf.h index 824badb29..2819a57e4 100644 --- a/libtransmission/tr-strbuf.h +++ b/libtransmission/tr-strbuf.h @@ -263,28 +263,6 @@ public: return c_str(); } - bool popdir() noexcept - { - // NOLINTNEXTLINE(readability-redundant-declaration): P.S. This looks like some dark magic - std::string_view tr_sys_path_dirname(std::string_view path); - auto const parent = tr_sys_path_dirname(sv()); - auto const changed = parent != sv(); - - if (changed) - { - if (std::data(parent) == std::data(*this)) - { - resize(std::size(parent)); - } - else - { - assign(parent); - } - } - - return changed; - } - private: /** * Ensure that the buffer's string is zero-terminated, e.g. for diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index b496f2fa4..f2567d058 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -588,9 +588,7 @@ bool tr_file_move(std::string_view oldpath, std::string_view newpath, bool allow } // ensure the target directory exists - auto newdir = tr_pathbuf{ newpath }; - newdir.popdir(); - if (!tr_sys_dir_create(newdir, TR_SYS_DIR_CREATE_PARENTS, 0777, error)) + if (!tr_sys_dir_create(tr_sys_path_dirname(newpath), TR_SYS_DIR_CREATE_PARENTS, 0777, error)) { error->prefix_message("Unable to create directory for new file: "); return false; diff --git a/tests/libtransmission/file-test.cc b/tests/libtransmission/file-test.cc index e903ed454..b4d1d99f7 100644 --- a/tests/libtransmission/file-test.cc +++ b/tests/libtransmission/file-test.cc @@ -845,10 +845,6 @@ TEST_F(FileTest, pathDirname) { EXPECT_EQ(expected, tr_sys_path_dirname(input)) << "input[" << input << "] expected [" << expected << "] actual [" << tr_sys_path_dirname(input) << "]\n"; - - auto path = tr_pathbuf{ input }; - path.popdir(); - EXPECT_EQ(expected, path) << "input[" << input << "] expected [" << expected << "] actual [" << path << "]\n"; } /* TODO: is_same(dirname(x) + '/' + basename(x), x) */ diff --git a/tests/libtransmission/remove-test.cc b/tests/libtransmission/remove-test.cc index 7aee2f793..7dd21ff42 100644 --- a/tests/libtransmission/remove-test.cc +++ b/tests/libtransmission/remove-test.cc @@ -178,13 +178,14 @@ protected: for (tr_file_index_t i = 0, n = files.file_count(); i < n; ++i) { - auto walk = tr_pathbuf{ parent, '/', files.path(i) }; - createFileWithContents(walk, std::data(Content), std::size(Content)); - paths.emplace(walk); + auto const filename = tr_pathbuf{ parent, '/', files.path(i) }; + createFileWithContents(filename, std::data(Content), std::size(Content)); + paths.emplace(filename); + auto walk = filename.sv(); while (!tr_sys_path_is_same(parent, walk)) { - walk.popdir(); + walk = tr_sys_path_dirname(walk); paths.emplace(walk); } } diff --git a/tests/libtransmission/test-fixtures.h b/tests/libtransmission/test-fixtures.h index 3ba9b0185..a8d253da1 100644 --- a/tests/libtransmission/test-fixtures.h +++ b/tests/libtransmission/test-fixtures.h @@ -212,9 +212,7 @@ protected: { auto const tmperr = errno; - auto dir = tr_pathbuf{ path }; - dir.popdir(); - if (auto const info = tr_sys_path_get_info(path); !info) + if (auto const dir = tr_sys_path_dirname(path); !tr_sys_path_exists(dir)) { auto error = tr_error{}; tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0700, &error); @@ -433,9 +431,7 @@ protected: auto const suffix = std::string_view{ partial ? ".part" : "" }; auto const filename = tr_pathbuf{ base, '/', subpath, suffix }; - auto dirname = tr_pathbuf{ filename.sv() }; - dirname.popdir(); - tr_sys_dir_create(dirname, TR_SYS_DIR_CREATE_PARENTS, 0700); + tr_sys_dir_create(tr_sys_path_dirname(filename), TR_SYS_DIR_CREATE_PARENTS, 0700); auto fd = tr_sys_file_open(filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600); auto const file_size = metainfo->file_size(i);