refactor: tr_sys_dir_get_current() returns a std::string (#3581)

This commit is contained in:
Charles Kerr
2022-08-03 23:59:41 -05:00
committed by GitHub
parent 8a77f571ec
commit 4f6909a27a
7 changed files with 27 additions and 53 deletions

View File

@@ -1207,42 +1207,26 @@ bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] in
return ret;
}
char* tr_sys_dir_get_current(tr_error** error)
std::string tr_sys_dir_get_current(tr_error** error)
{
char* ret = getcwd(nullptr, 0);
auto buf = std::vector<char>{};
buf.resize(PATH_MAX);
if (ret == nullptr && (errno == EINVAL || errno == ERANGE))
for (;;)
{
size_t size = PATH_MAX;
char* tmp = nullptr;
do
if (char* ret = getcwd(std::data(buf), std::size(buf)); ret != nullptr)
{
tmp = tr_renew(char, tmp, size);
if (tmp == nullptr)
{
break;
}
ret = getcwd(tmp, size);
size += 2048;
} while (ret == nullptr && errno == ERANGE);
if (ret == nullptr)
{
int const err = errno;
tr_free(tmp);
errno = err;
return ret;
}
}
if (ret == nullptr)
{
set_system_error(error, errno);
}
if (errno != ERANGE)
{
set_system_error(error, errno);
return {};
}
return ret;
buf.resize(std::size(buf) * 2);
}
}
bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** error)

View File

@@ -1339,26 +1339,20 @@ bool tr_sys_file_lock(tr_sys_file_t handle, int operation, tr_error** error)
return ret;
}
char* tr_sys_dir_get_current(tr_error** error)
std::string tr_sys_dir_get_current(tr_error** error)
{
char* ret = nullptr;
if (auto const size = GetCurrentDirectoryW(0, nullptr); size != 0)
{
auto wide_ret = std::wstring{};
wide_ret.resize(size);
if (GetCurrentDirectoryW(std::size(wide_ret), std::data(wide_ret)) != 0)
{
ret = tr_win32_native_to_utf8(std::data(wide_ret), std::size(wide_ret));
return tr_win32_native_to_utf8(wide_ret);
}
}
if (ret == nullptr)
{
set_system_error(error, GetLastError());
}
return ret;
set_system_error(error, GetLastError());
return {};
}
bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** error)

View File

@@ -644,7 +644,7 @@ bool tr_sys_file_write_line(tr_sys_file_t handle, std::string_view buffer, struc
* directory (use @ref tr_free to free it when no longer needed) on
* success, `nullptr` otherwise (with `error` set accordingly).
*/
char* tr_sys_dir_get_current(struct tr_error** error = nullptr);
std::string tr_sys_dir_get_current(struct tr_error** error = nullptr);
/**
* @brief Like `mkdir()`, but makes parent directories if needed.

View File

@@ -49,9 +49,8 @@ int main(int argc, char** argv)
}
else if (test_action == "--dump-cwd")
{
char* const value = tr_sys_dir_get_current(nullptr);
tr_sys_file_write_line(fd, value != nullptr ? value : "<null>");
tr_free(value);
auto const value = tr_sys_dir_get_current(nullptr);
tr_sys_file_write_line(fd, !std::empty(value) ? value : "<null>");
}
else
{

View File

@@ -56,8 +56,8 @@ protected:
[[nodiscard]] static std::string nativeCwd()
{
auto path = makeString(tr_sys_dir_get_current(nullptr));
tr_sys_path_native_separators(&path.front());
auto path = tr_sys_dir_get_current();
tr_sys_path_native_separators(path.data());
return path;
}

View File

@@ -128,11 +128,9 @@ protected:
tr_error* error = nullptr;
if (auto* path = tr_sys_dir_get_current(&error); path != nullptr)
if (auto path = tr_sys_dir_get_current(&error); !std::empty(path))
{
auto ret = std::string{ path };
tr_free(path);
return ret;
return path;
}
std::cerr << "tr_sys_dir_get_current error: '" << error->message << "'" << std::endl;

View File

@@ -129,12 +129,11 @@ int parseCommandLine(app_options& options, int argc, char const* const* argv)
std::string tr_getcwd()
{
tr_error* error = nullptr;
if (char* const cur = tr_sys_dir_get_current(&error); cur != nullptr)
if (auto cur = tr_sys_dir_get_current(&error); !std::empty(cur))
{
auto path = std::string{ cur };
tr_free(cur);
return path;
return cur;
}
fprintf(stderr, "getcwd error: \"%s\"", error->message);
tr_error_free(error);
return "";