mirror of
https://github.com/transmission/transmission.git
synced 2025-12-24 12:28:52 +00:00
refactor: tr_sys_dir_get_current() returns a std::string (#3581)
This commit is contained in:
@@ -1207,42 +1207,26 @@ bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] in
|
|||||||
return ret;
|
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;
|
if (char* ret = getcwd(std::data(buf), std::size(buf)); ret != nullptr)
|
||||||
char* tmp = nullptr;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
{
|
||||||
tmp = tr_renew(char, tmp, size);
|
return ret;
|
||||||
|
|
||||||
if (tmp == nullptr)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = getcwd(tmp, size);
|
if (errno != ERANGE)
|
||||||
size += 2048;
|
|
||||||
} while (ret == nullptr && errno == ERANGE);
|
|
||||||
|
|
||||||
if (ret == nullptr)
|
|
||||||
{
|
|
||||||
int const err = errno;
|
|
||||||
tr_free(tmp);
|
|
||||||
errno = err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret == nullptr)
|
|
||||||
{
|
{
|
||||||
set_system_error(error, errno);
|
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)
|
bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** error)
|
||||||
|
|||||||
@@ -1339,26 +1339,20 @@ bool tr_sys_file_lock(tr_sys_file_t handle, int operation, tr_error** error)
|
|||||||
return ret;
|
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)
|
if (auto const size = GetCurrentDirectoryW(0, nullptr); size != 0)
|
||||||
{
|
{
|
||||||
auto wide_ret = std::wstring{};
|
auto wide_ret = std::wstring{};
|
||||||
wide_ret.resize(size);
|
wide_ret.resize(size);
|
||||||
if (GetCurrentDirectoryW(std::size(wide_ret), std::data(wide_ret)) != 0)
|
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());
|
set_system_error(error, GetLastError());
|
||||||
}
|
return {};
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** error)
|
bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** error)
|
||||||
|
|||||||
@@ -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
|
* directory (use @ref tr_free to free it when no longer needed) on
|
||||||
* success, `nullptr` otherwise (with `error` set accordingly).
|
* 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.
|
* @brief Like `mkdir()`, but makes parent directories if needed.
|
||||||
|
|||||||
@@ -49,9 +49,8 @@ int main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
else if (test_action == "--dump-cwd")
|
else if (test_action == "--dump-cwd")
|
||||||
{
|
{
|
||||||
char* const value = tr_sys_dir_get_current(nullptr);
|
auto const value = tr_sys_dir_get_current(nullptr);
|
||||||
tr_sys_file_write_line(fd, value != nullptr ? value : "<null>");
|
tr_sys_file_write_line(fd, !std::empty(value) ? value : "<null>");
|
||||||
tr_free(value);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ protected:
|
|||||||
|
|
||||||
[[nodiscard]] static std::string nativeCwd()
|
[[nodiscard]] static std::string nativeCwd()
|
||||||
{
|
{
|
||||||
auto path = makeString(tr_sys_dir_get_current(nullptr));
|
auto path = tr_sys_dir_get_current();
|
||||||
tr_sys_path_native_separators(&path.front());
|
tr_sys_path_native_separators(path.data());
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -128,11 +128,9 @@ protected:
|
|||||||
|
|
||||||
tr_error* error = nullptr;
|
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 };
|
return path;
|
||||||
tr_free(path);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "tr_sys_dir_get_current error: '" << error->message << "'" << std::endl;
|
std::cerr << "tr_sys_dir_get_current error: '" << error->message << "'" << std::endl;
|
||||||
|
|||||||
@@ -129,12 +129,11 @@ int parseCommandLine(app_options& options, int argc, char const* const* argv)
|
|||||||
std::string tr_getcwd()
|
std::string tr_getcwd()
|
||||||
{
|
{
|
||||||
tr_error* error = nullptr;
|
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 };
|
return cur;
|
||||||
tr_free(cur);
|
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "getcwd error: \"%s\"", error->message);
|
fprintf(stderr, "getcwd error: \"%s\"", error->message);
|
||||||
tr_error_free(error);
|
tr_error_free(error);
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
Reference in New Issue
Block a user