From 4f6909a27a3d96186e25bf0fbe874e2e1e20c939 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 3 Aug 2022 23:59:41 -0500 Subject: [PATCH] refactor: tr_sys_dir_get_current() returns a std::string (#3581) --- libtransmission/file-posix.cc | 42 ++++++------------- libtransmission/file-win32.cc | 14 ++----- libtransmission/file.h | 2 +- .../subprocess-test-program.cc | 5 +-- tests/libtransmission/subprocess-test.cc | 4 +- tests/libtransmission/test-fixtures.h | 6 +-- utils/create.cc | 7 ++-- 7 files changed, 27 insertions(+), 53 deletions(-) diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index 902975028..175cc1285 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -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{}; + 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) diff --git a/libtransmission/file-win32.cc b/libtransmission/file-win32.cc index 5f23f200c..544cb4029 100644 --- a/libtransmission/file-win32.cc +++ b/libtransmission/file-win32.cc @@ -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) diff --git a/libtransmission/file.h b/libtransmission/file.h index 83d619fda..a4a4b4e38 100644 --- a/libtransmission/file.h +++ b/libtransmission/file.h @@ -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. diff --git a/tests/libtransmission/subprocess-test-program.cc b/tests/libtransmission/subprocess-test-program.cc index d8abfb9a5..f822070c0 100644 --- a/tests/libtransmission/subprocess-test-program.cc +++ b/tests/libtransmission/subprocess-test-program.cc @@ -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 : ""); - tr_free(value); + auto const value = tr_sys_dir_get_current(nullptr); + tr_sys_file_write_line(fd, !std::empty(value) ? value : ""); } else { diff --git a/tests/libtransmission/subprocess-test.cc b/tests/libtransmission/subprocess-test.cc index 403ca24d2..1162fea3a 100644 --- a/tests/libtransmission/subprocess-test.cc +++ b/tests/libtransmission/subprocess-test.cc @@ -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; } diff --git a/tests/libtransmission/test-fixtures.h b/tests/libtransmission/test-fixtures.h index e486a76ed..7c349e21b 100644 --- a/tests/libtransmission/test-fixtures.h +++ b/tests/libtransmission/test-fixtures.h @@ -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; diff --git a/utils/create.cc b/utils/create.cc index d6e7b5dbe..f398d73f8 100644 --- a/utils/create.cc +++ b/utils/create.cc @@ -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 "";