refactor: remove tr_pathbuf::popdir() (#8221)

This commit is contained in:
Charles Kerr
2026-01-25 20:02:59 -06:00
committed by GitHub
parent daf15c01c3
commit a7b2a72cca
7 changed files with 11 additions and 47 deletions

View File

@@ -141,7 +141,7 @@ std::optional<tr_sys_file_t> tr_open_files::get(
tr_torrent_id_t tor_id, tr_torrent_id_t tor_id,
tr_file_index_t file_num, tr_file_index_t file_num,
bool writable, bool writable,
std::string_view filename_in, std::string_view const filename,
Preallocation allocation, Preallocation allocation,
uint64_t file_size) uint64_t file_size)
{ {
@@ -158,13 +158,10 @@ std::optional<tr_sys_file_t> tr_open_files::get(
} }
// create subfolders, if any // create subfolders, if any
auto const filename = tr_pathbuf{ filename_in };
auto error = tr_error{}; auto error = tr_error{};
if (writable) if (writable)
{ {
auto dir = tr_pathbuf{ filename.sv() }; if (auto const dir = tr_sys_path_dirname(filename); !tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777, &error))
dir.popdir();
if (!tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777, &error))
{ {
tr_logAddError( tr_logAddError(
fmt::format( fmt::format(

View File

@@ -1799,9 +1799,7 @@ void tr_torrent::create_empty_files() const
filename.assign(base, '/', subpath); filename.assign(base, '/', subpath);
// create subfolders, if any // create subfolders, if any
auto dir = tr_pathbuf{ filename.sv() }; tr_sys_dir_create(tr_sys_path_dirname(filename), TR_SYS_DIR_CREATE_PARENTS, 0777);
dir.popdir();
tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777);
// create the file // 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); if (auto const fd = tr_sys_file_open(filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_SEQUENTIAL, 0666);

View File

@@ -263,28 +263,6 @@ public:
return c_str(); 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: private:
/** /**
* Ensure that the buffer's string is zero-terminated, e.g. for * Ensure that the buffer's string is zero-terminated, e.g. for

View File

@@ -588,9 +588,7 @@ bool tr_file_move(std::string_view oldpath, std::string_view newpath, bool allow
} }
// ensure the target directory exists // ensure the target directory exists
auto newdir = tr_pathbuf{ newpath }; if (!tr_sys_dir_create(tr_sys_path_dirname(newpath), TR_SYS_DIR_CREATE_PARENTS, 0777, error))
newdir.popdir();
if (!tr_sys_dir_create(newdir, TR_SYS_DIR_CREATE_PARENTS, 0777, error))
{ {
error->prefix_message("Unable to create directory for new file: "); error->prefix_message("Unable to create directory for new file: ");
return false; return false;

View File

@@ -845,10 +845,6 @@ TEST_F(FileTest, pathDirname)
{ {
EXPECT_EQ(expected, tr_sys_path_dirname(input)) EXPECT_EQ(expected, tr_sys_path_dirname(input))
<< "input[" << input << "] expected [" << expected << "] actual [" << tr_sys_path_dirname(input) << "]\n"; << "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) */ /* TODO: is_same(dirname(x) + '/' + basename(x), x) */

View File

@@ -178,13 +178,14 @@ protected:
for (tr_file_index_t i = 0, n = files.file_count(); i < n; ++i) for (tr_file_index_t i = 0, n = files.file_count(); i < n; ++i)
{ {
auto walk = tr_pathbuf{ parent, '/', files.path(i) }; auto const filename = tr_pathbuf{ parent, '/', files.path(i) };
createFileWithContents(walk, std::data(Content), std::size(Content)); createFileWithContents(filename, std::data(Content), std::size(Content));
paths.emplace(walk); paths.emplace(filename);
auto walk = filename.sv();
while (!tr_sys_path_is_same(parent, walk)) while (!tr_sys_path_is_same(parent, walk))
{ {
walk.popdir(); walk = tr_sys_path_dirname(walk);
paths.emplace(walk); paths.emplace(walk);
} }
} }

View File

@@ -212,9 +212,7 @@ protected:
{ {
auto const tmperr = errno; auto const tmperr = errno;
auto dir = tr_pathbuf{ path }; if (auto const dir = tr_sys_path_dirname(path); !tr_sys_path_exists(dir))
dir.popdir();
if (auto const info = tr_sys_path_get_info(path); !info)
{ {
auto error = tr_error{}; auto error = tr_error{};
tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0700, &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 suffix = std::string_view{ partial ? ".part" : "" };
auto const filename = tr_pathbuf{ base, '/', subpath, suffix }; auto const filename = tr_pathbuf{ base, '/', subpath, suffix };
auto dirname = tr_pathbuf{ filename.sv() }; tr_sys_dir_create(tr_sys_path_dirname(filename), TR_SYS_DIR_CREATE_PARENTS, 0700);
dirname.popdir();
tr_sys_dir_create(dirname, 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 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); auto const file_size = metainfo->file_size(i);