From 03fdb6f48e39b3dbf6e1dfa65768bde280014348 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 25 Jan 2026 18:39:50 -0600 Subject: [PATCH] refactor: migrate C strings to `std::string_view` in `libtransmission/file.h` (#8220) * refactor: tr_sys_file_open() now takes a std::string_view arg * refactor: tr_sys_path_copy() now takes a std::string_view arg * refactor: tr_blocklistSetContent() now takes a std::string_view arg * refactor: tr_sys_path_remove() now takes a std::string_view arg * refactor: tr_sys_path_exists() now takes a std::string_view arg * refactor: tr_sys_dir_create() now takes a std::string_view arg * refactor: add private stat_sv(), lstat_sv() helpers in file-posix.cc * refactor: tr_sys_path_is_same() now takes a std::string_view arg * refactor: tr_sys_path_rename() now takes a std::string_view arg --- daemon/daemon.cc | 12 +-- libtransmission/blocklist.cc | 10 +-- libtransmission/file-posix.cc | 76 ++++++++++--------- libtransmission/file-win32.cc | 48 ++++++------ libtransmission/file.h | 44 ++--------- libtransmission/rpcimpl.cc | 2 +- libtransmission/session.cc | 25 +++--- libtransmission/session.h | 2 +- libtransmission/torrent-magnet.cc | 2 +- libtransmission/tr-dht.cc | 4 +- libtransmission/transmission.h | 4 +- libtransmission/utils.cc | 5 +- libtransmission/verify.cc | 2 +- tests/libtransmission/dht-test.cc | 12 +-- tests/libtransmission/move-test.cc | 2 +- .../subprocess-test-program.cc | 2 +- tests/libtransmission/test-fixtures.h | 2 +- utils/remote.cc | 2 +- 18 files changed, 113 insertions(+), 143 deletions(-) diff --git a/daemon/daemon.cc b/daemon/daemon.cc index 71145b937..ad957d7e2 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -864,13 +864,13 @@ int tr_daemon::start([[maybe_unused]] bool foreground) return 1; } - auto const sz_pid_filename = std::string{ settings_map->value_if(TR_KEY_pidfile).value_or(""sv) }; auto pidfile_created = false; - if (!std::empty(sz_pid_filename)) + auto const pid_filename = settings_map->value_if(TR_KEY_pidfile).value_or(""sv); + if (!std::empty(pid_filename)) { auto error = tr_error{}; tr_sys_file_t fp = tr_sys_file_open( - sz_pid_filename.c_str(), + pid_filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, &error); @@ -880,7 +880,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground) auto const out = std::to_string(getpid()); tr_sys_file_write(fp, std::data(out), std::size(out), nullptr); tr_sys_file_close(fp); - tr_logAddInfo(fmt::format(fmt::runtime(_("Saved pidfile '{path}'")), fmt::arg("path", sz_pid_filename))); + tr_logAddInfo(fmt::format(fmt::runtime(_("Saved pidfile '{path}'")), fmt::arg("path", pid_filename))); pidfile_created = true; } else @@ -888,7 +888,7 @@ int tr_daemon::start([[maybe_unused]] bool foreground) tr_logAddError( fmt::format( fmt::runtime(_("Couldn't save '{path}': {error} ({error_code})")), - fmt::arg("path", sz_pid_filename), + fmt::arg("path", pid_filename), fmt::arg("error", error.message()), fmt::arg("error_code", error.code()))); } @@ -1028,7 +1028,7 @@ CLEANUP: /* cleanup */ if (pidfile_created) { - tr_sys_path_remove(sz_pid_filename); + tr_sys_path_remove(pid_filename); } sd_notify(0, "STATUS=\n"); diff --git a/libtransmission/blocklist.cc b/libtransmission/blocklist.cc index 13c4beb8e..b0b705e3d 100644 --- a/libtransmission/blocklist.cc +++ b/libtransmission/blocklist.cc @@ -467,8 +467,8 @@ bool Blocklists::Blocklist::contains(tr_address const& addr) const } std::optional Blocklists::Blocklist::saveNew( - std::string_view external_file, - std::string_view bin_file, + std::string_view const external_file, + std::string_view const bin_file, bool is_enabled) { // if we can't parse the file, do nothing @@ -480,9 +480,9 @@ std::optional Blocklists::Blocklist::saveNew( // make a copy of `external_file` for our own safekeeping auto const src_file = std::string{ std::data(bin_file), std::size(bin_file) - std::size(BinFileSuffix) }; - tr_sys_path_remove(src_file.c_str()); + tr_sys_path_remove(src_file); auto error = tr_error{}; - auto const copied = tr_sys_path_copy(tr_pathbuf{ external_file }, src_file.c_str(), &error); + auto const copied = tr_sys_path_copy(external_file, src_file, &error); if (error) { tr_logAddWarn( @@ -561,7 +561,7 @@ std::vector Blocklists::Blocklists::load_folder(std::stri return ret; } -size_t Blocklists::update_primary_blocklist(std::string_view external_file, bool is_enabled) +size_t Blocklists::update_primary_blocklist(std::string_view const external_file, bool const is_enabled) { // These rules will replace the default blocklist. // Build the path of the default blocklist .bin file where we'll save these rules. diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index 45e00b4fd..6b99615ef 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -129,11 +129,10 @@ void set_file_for_single_pass(tr_sys_file_t handle) } } // namespace -bool tr_sys_path_exists(char const* path, tr_error* error) +bool tr_sys_path_exists(std::string_view const path, tr_error* error) { - TR_ASSERT(path != nullptr); - - bool const ret = access(path, F_OK) != -1; + auto const sz_path = tr_pathbuf{ path }; + bool const ret = access(sz_path.c_str(), F_OK) != -1; if (error != nullptr && !ret && errno != ENOENT) { @@ -143,20 +142,34 @@ bool tr_sys_path_exists(char const* path, tr_error* error) return ret; } -std::optional tr_sys_path_get_info(std::string_view path, int flags, tr_error* error) +namespace +{ +[[nodiscard]] auto stat_sv(std::string_view const path, struct stat* sb) +{ + auto const sz_path = tr_pathbuf{ path }; + return stat(sz_path.c_str(), sb); +} + +[[nodiscard]] auto lstat_sv(std::string_view const path, struct stat* sb) +{ + auto const sz_path = tr_pathbuf{ path }; + return lstat(sz_path.c_str(), sb); +} +} // namespace + +std::optional tr_sys_path_get_info(std::string_view const path, int const flags, tr_error* error) { struct stat sb = {}; bool ok = false; - auto const szpath = tr_pathbuf{ path }; if ((flags & TR_SYS_PATH_NO_FOLLOW) == 0) { - ok = stat(szpath, &sb) != -1; + ok = stat_sv(path, &sb) != -1; } else { - ok = lstat(szpath, &sb) != -1; + ok = lstat_sv(path, &sb) != -1; } if (!ok) @@ -195,16 +208,13 @@ bool tr_sys_path_is_relative(std::string_view path) return std::empty(path) || path.front() != '/'; } -bool tr_sys_path_is_same(char const* path1, char const* path2, tr_error* error) +bool tr_sys_path_is_same(std::string_view const path1, std::string_view const path2, tr_error* error) { - TR_ASSERT(path1 != nullptr); - TR_ASSERT(path2 != nullptr); - bool ret = false; struct stat sb1 = {}; struct stat sb2 = {}; - if (stat(path1, &sb1) != -1 && stat(path2, &sb2) != -1) + if (stat_sv(path1, &sb1) != -1 && stat_sv(path2, &sb2) != -1) { ret = sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino; } @@ -308,12 +318,11 @@ std::string_view tr_sys_path_dirname(std::string_view path) return path.substr(0, end); } -bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error* error) +bool tr_sys_path_rename(std::string_view const src_path, std::string_view const dst_path, tr_error* error) { - TR_ASSERT(src_path != nullptr); - TR_ASSERT(dst_path != nullptr); - - bool const ret = rename(src_path, dst_path) != -1; + auto const sz_src_path = tr_pathbuf{ src_path }; + auto const sz_dst_path = tr_pathbuf{ dst_path }; + bool const ret = rename(sz_src_path.c_str(), sz_dst_path.c_str()) != -1; if (error != nullptr && !ret) { @@ -326,11 +335,8 @@ bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error* er /* We try to do a fast (in-kernel) copy using a variety of non-portable system * calls. If the current implementation does not support in-kernel copying, we * use a user-space fallback instead. */ -bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error* error) +bool tr_sys_path_copy(std::string_view const src_path, std::string_view const dst_path, tr_error* error) { - TR_ASSERT(src_path != nullptr); - TR_ASSERT(dst_path != nullptr); - auto local_error = tr_error{}; if (error == nullptr) { @@ -338,7 +344,9 @@ bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error* erro } #if defined(USE_COPYFILE) - if (copyfile(src_path, dst_path, nullptr, COPYFILE_CLONE | COPYFILE_ALL) < 0) + auto const sz_src_path = tr_pathbuf{ src_path }; + auto const sz_dst_path = tr_pathbuf{ dst_path }; + if (copyfile(sz_src_path.c_str(), sz_dst_path.c_str(), nullptr, COPYFILE_CLONE | COPYFILE_ALL) < 0) { error->set_from_errno(errno); return false; @@ -529,11 +537,11 @@ bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error* erro #endif /* USE_COPYFILE */ } -bool tr_sys_path_remove(char const* path, tr_error* error) +bool tr_sys_path_remove(std::string_view const path, tr_error* error) { - TR_ASSERT(path != nullptr); + auto const sz_path = tr_pathbuf{ path }; - bool const ret = remove(path) != -1; + bool const ret = remove(sz_path.c_str()) != -1; if (error != nullptr && !ret) { @@ -548,9 +556,8 @@ char* tr_sys_path_native_separators(char* path) return path; } -tr_sys_file_t tr_sys_file_open(char const* path, int flags, int permissions, tr_error* error) +tr_sys_file_t tr_sys_file_open(std::string_view path, int const flags, int const permissions, tr_error* error) { - TR_ASSERT(path != nullptr); TR_ASSERT((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) != 0); struct native_map_item @@ -603,7 +610,8 @@ tr_sys_file_t tr_sys_file_open(char const* path, int flags, int permissions, tr_ } } - tr_sys_file_t const ret = open(path, native_flags, permissions); + auto const sz_path = tr_pathbuf{ path }; + tr_sys_file_t const ret = open(sz_path.c_str(), native_flags, permissions); if (ret != TR_BAD_SYS_FILE) { @@ -1087,24 +1095,24 @@ namespace #endif } // namespace -bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error* error) +bool tr_sys_dir_create(std::string_view const path, int const flags, int const permissions, tr_error* error) { - TR_ASSERT(path != nullptr); - auto ret = false; auto local_error = tr_error{}; + auto const sz_path = tr_pathbuf{ path }; + if ((flags & TR_SYS_DIR_CREATE_PARENTS) != 0) { #ifdef HAVE_MKDIRP - ret = mkdirp(path, permissions) != -1; + ret = mkdirp(sz_path.c_str(), permissions) != -1; #else ret = tr_mkdirp_(path, permissions, &local_error); #endif } else { - ret = mkdir(path, permissions) != -1; + ret = mkdir(sz_path.c_str(), permissions) != -1; } if (!ret && errno == EEXIST) diff --git a/libtransmission/file-win32.cc b/libtransmission/file-win32.cc index dc812846f..3a49e1405 100644 --- a/libtransmission/file-win32.cc +++ b/libtransmission/file-win32.cc @@ -136,7 +136,7 @@ bool is_valid_path(std::string_view path) return path.find_first_of(R"(<>:"|?*)"sv) == std::string_view::npos; } -auto path_to_fixed_native_path(std::string_view path) +[[nodiscard]] auto path_to_fixed_native_path(std::string_view const path) { auto wide_path = tr_win32_utf8_to_native(path); @@ -161,7 +161,7 @@ auto path_to_fixed_native_path(std::string_view path) /* Extending maximum path length limit up to ~32K. See "Naming Files, Paths, and Namespaces" https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx for more info */ -auto path_to_native_path(std::string_view path) +[[nodiscard]] auto path_to_native_path(std::string_view path) { if (is_unc_path(path)) { @@ -207,7 +207,12 @@ std::string native_path_to_path(std::wstring_view wide_path) return tr_win32_native_to_utf8(wide_path); } -tr_sys_file_t open_file(std::string_view path, DWORD access, DWORD disposition, DWORD flags, tr_error* error) +[[nodiscard]] tr_sys_file_t open_file( + std::string_view const path, + DWORD const access, + DWORD const disposition, + DWORD const flags, + tr_error* error) { tr_sys_file_t ret = TR_BAD_SYS_FILE; @@ -231,7 +236,12 @@ tr_sys_file_t open_file(std::string_view path, DWORD access, DWORD disposition, return ret; } -bool create_dir(std::string_view path, int flags, int /*permissions*/, bool okay_if_exists, tr_error* error) +[[nodiscard]] bool create_dir( + std::string_view const path, + int const flags, + int /*permissions*/, + bool const okay_if_exists, + tr_error* error) { bool ret = false; DWORD error_code = ERROR_SUCCESS; @@ -339,7 +349,7 @@ std::optional tr_sys_file_get_info_(tr_sys_file_t handle, tr_e return {}; } -std::optional get_file_info(char const* path, tr_error* error) +[[nodiscard]] std::optional get_file_info(std::string_view const path, tr_error* error) { auto const wpath = path_to_native_path(path); if (std::empty(wpath)) @@ -370,10 +380,8 @@ std::optional get_file_info(char const* path, tr_err } // namespace -bool tr_sys_path_exists(char const* path, tr_error* error) +bool tr_sys_path_exists(std::string_view const path, tr_error* error) { - TR_ASSERT(path != nullptr); - bool ret = false; HANDLE handle = INVALID_HANDLE_VALUE; @@ -462,11 +470,8 @@ bool tr_sys_path_is_relative(std::string_view path) return true; } -bool tr_sys_path_is_same(char const* path1, char const* path2, tr_error* error) +bool tr_sys_path_is_same(std::string_view const path1, std::string_view const path2, tr_error* error) { - TR_ASSERT(path1 != nullptr); - TR_ASSERT(path2 != nullptr); - auto const fi1 = get_file_info(path1, error); if (!fi1) { @@ -684,11 +689,8 @@ std::string_view tr_sys_path_dirname(std::string_view path) return path.substr(0, end); } -bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error* error) +bool tr_sys_path_rename(std::string_view const src_path, std::string_view const dst_path, tr_error* error) { - TR_ASSERT(src_path != nullptr); - TR_ASSERT(dst_path != nullptr); - bool ret = false; auto const wide_src_path = path_to_native_path(src_path); auto const wide_dst_path = path_to_native_path(dst_path); @@ -719,11 +721,8 @@ bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error* er return ret; } -bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error* error) +bool tr_sys_path_copy(std::string_view const src_path, std::string_view const dst_path, tr_error* error) { - TR_ASSERT(src_path != nullptr); - TR_ASSERT(dst_path != nullptr); - auto const wide_src_path = path_to_native_path(src_path); auto const wide_dst_path = path_to_native_path(dst_path); if (std::empty(wide_src_path) || std::empty(wide_dst_path)) @@ -743,10 +742,8 @@ bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error* erro return true; } -bool tr_sys_path_remove(char const* path, tr_error* error) +bool tr_sys_path_remove(std::string_view const path, tr_error* error) { - TR_ASSERT(path != nullptr); - bool ret = false; if (auto const wide_path = path_to_native_path(path); !std::empty(wide_path)) @@ -789,9 +786,8 @@ char* tr_sys_path_native_separators(char* path) return path; } -tr_sys_file_t tr_sys_file_open(char const* path, int flags, int /*permissions*/, tr_error* error) +tr_sys_file_t tr_sys_file_open(std::string_view const path, int const flags, int /*permissions*/, tr_error* error) { - TR_ASSERT(path != nullptr); TR_ASSERT((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) != 0); DWORD native_access = 0; @@ -1094,7 +1090,7 @@ std::string tr_sys_dir_get_current(tr_error* error) return {}; } -bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error* error) +bool tr_sys_dir_create(std::string_view const path, int const flags, int const permissions, tr_error* error) { return create_dir(path, flags, permissions, true, error); } diff --git a/libtransmission/file.h b/libtransmission/file.h index 1137b52e2..8a3a9f2b6 100644 --- a/libtransmission/file.h +++ b/libtransmission/file.h @@ -131,7 +131,7 @@ struct tr_sys_path_capacity * * @return `True` on success, `false` otherwise (with `error` set accordingly). */ -bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error* error = nullptr); +bool tr_sys_path_copy(std::string_view src_path, std::string_view dst_path, tr_error* error = nullptr); /** * @brief Portability wrapper for `stat()`. @@ -168,13 +168,7 @@ bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error* erro * be returned in case of error; if you need to distinguish the two, * check if `error` is `nullptr` afterwards. */ -bool tr_sys_path_exists(char const* path, tr_error* error = nullptr); - -template -bool tr_sys_path_exists(T const& path, tr_error* error = nullptr) -{ - return tr_sys_path_exists(path.c_str(), error); -} +bool tr_sys_path_exists(std::string_view path, tr_error* error = nullptr); /** * @brief Check whether path is relative. @@ -200,13 +194,7 @@ bool tr_sys_path_is_relative(std::string_view path); * if you need to distinguish the two, check if `error` is `nullptr` * afterwards. */ -bool tr_sys_path_is_same(char const* path1, char const* path2, tr_error* error = nullptr); - -template -bool tr_sys_path_is_same(T const& path1, U const& path2, tr_error* error = nullptr) -{ - return tr_sys_path_is_same(path1.c_str(), path2.c_str(), error); -} +bool tr_sys_path_is_same(std::string_view path1, std::string_view path2, tr_error* error = nullptr); /** * @brief Portability wrapper for `realpath()`. @@ -254,13 +242,7 @@ std::string_view tr_sys_path_dirname(std::string_view path); * Rename will generally only succeed if both source and destination are * on the same partition. */ -bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error* error = nullptr); - -template -bool tr_sys_path_rename(T const& src_path, U const& dst_path, tr_error* error = nullptr) -{ - return tr_sys_path_rename(src_path.c_str(), dst_path.c_str(), error); -} +bool tr_sys_path_rename(std::string_view src_path, std::string_view dst_path, tr_error* error = nullptr); /** * @brief Portability wrapper for `remove()`. @@ -273,13 +255,7 @@ bool tr_sys_path_rename(T const& src_path, U const& dst_path, tr_error* error = * Directory removal will only succeed if it is empty (contains no other * files and directories). */ -bool tr_sys_path_remove(char const* path, tr_error* error = nullptr); - -template -bool tr_sys_path_remove(T const& path, tr_error* error = nullptr) -{ - return tr_sys_path_remove(path.c_str(), error); -} +bool tr_sys_path_remove(std::string_view path, tr_error* error = nullptr); /** * @brief Transform path separators to native ones, in-place. @@ -305,7 +281,7 @@ char* tr_sys_path_native_separators(char* path); * @return Opened file descriptor on success, `TR_BAD_SYS_FILE` otherwise (with * `error` set accordingly). */ -tr_sys_file_t tr_sys_file_open(char const* path, int flags, int permissions, tr_error* error = nullptr); +tr_sys_file_t tr_sys_file_open(std::string_view path, int flags, int permissions, tr_error* error = nullptr); /** * @brief Portability wrapper for `mkstemp()`. @@ -480,13 +456,7 @@ std::string tr_sys_dir_get_current(tr_error* error = nullptr); * * @return `True` on success, `false` otherwise (with `error` set accordingly). */ -bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error* error = nullptr); - -template -bool tr_sys_dir_create(T const& path, int flags, int permissions, tr_error* error = nullptr) -{ - return tr_sys_dir_create(path.c_str(), flags, permissions, error); -} +bool tr_sys_dir_create(std::string_view path, int flags, int permissions, tr_error* error = nullptr); /** * @brief Portability wrapper for `mkdtemp()`. diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index 6974e6796..ac1a39d27 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -1855,7 +1855,7 @@ void torrentAdd(tr_session* session, tr_variant::Map const& args_in, tr_rpc_idle { ok = ctor.set_metainfo(tr_base64_decode(metainfo_base64)); } - else if (tr_sys_path_exists(tr_pathbuf{ filename })) + else if (tr_sys_path_exists(filename)) { ok = ctor.set_metainfo_from_file(filename); } diff --git a/libtransmission/session.cc b/libtransmission/session.cc index 2e558fa10..b196c93f2 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -1826,7 +1826,7 @@ bool tr_blocklistExists(tr_session const* session) return session->blocklists_.num_lists() > 0U; } -size_t tr_blocklistSetContent(tr_session* session, char const* content_filename) +size_t tr_blocklistSetContent(tr_session* session, std::string_view const content_filename) { auto const lock = session->unique_lock(); return session->blocklists_.update_primary_blocklist(content_filename, session->blocklist_enabled()); @@ -2164,33 +2164,34 @@ namespace auto constexpr QueueInterval = 1s; auto constexpr SaveInterval = 360s; +[[nodiscard]] auto makeSubdir(std::string_view const parent, std::string_view const child) +{ + auto dir = fmt::format("{:s}/{:s}"sv, parent, child); + tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777); + return dir; +} + auto makeResumeDir(std::string_view config_dir) { #if defined(__APPLE__) || defined(_WIN32) - auto dir = fmt::format("{:s}/Resume"sv, config_dir); + return makeSubdir(config_dir, "Resume"sv); #else - auto dir = fmt::format("{:s}/resume"sv, config_dir); + return makeSubdir(config_dir, "resume"sv); #endif - tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777); - return dir; } auto makeTorrentDir(std::string_view config_dir) { #if defined(__APPLE__) || defined(_WIN32) - auto dir = fmt::format("{:s}/Torrents"sv, config_dir); + return makeSubdir(config_dir, "Torrents"sv); #else - auto dir = fmt::format("{:s}/torrents"sv, config_dir); + return makeSubdir(config_dir, "torrents"sv); #endif - tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777); - return dir; } auto makeBlocklistDir(std::string_view config_dir) { - auto dir = fmt::format("{:s}/blocklists"sv, config_dir); - tr_sys_dir_create(dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777); - return dir; + return makeSubdir(config_dir, "blocklists"sv); } } // namespace diff --git a/libtransmission/session.h b/libtransmission/session.h index 728b12d83..132524f18 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -1267,7 +1267,7 @@ private: friend std::string tr_sessionGetRPCUsername(tr_session const* session); friend std::string tr_sessionGetRPCWhitelist(tr_session const* session); friend size_t tr_blocklistGetRuleCount(tr_session const* session); - friend size_t tr_blocklistSetContent(tr_session* session, char const* content_filename); + friend size_t tr_blocklistSetContent(tr_session* session, std::string_view content_filename); friend size_t tr_sessionGetAltSpeedBegin(tr_session const* session); friend size_t tr_sessionGetAltSpeedEnd(tr_session const* session); friend size_t tr_sessionGetCacheLimit_MB(tr_session const* session); diff --git a/libtransmission/torrent-magnet.cc b/libtransmission/torrent-magnet.cc index b026c5655..588de30a8 100644 --- a/libtransmission/torrent-magnet.cc +++ b/libtransmission/torrent-magnet.cc @@ -132,7 +132,7 @@ void tr_torrent::maybe_start_metadata_transfer(int64_t const size) noexcept bool tr_torrent::use_metainfo_from_file(tr_torrent_metainfo const* metainfo, char const* filename_in, tr_error* error) { // add .torrent file - if (!tr_sys_path_copy(filename_in, torrent_file().c_str(), error)) + if (!tr_sys_path_copy(filename_in, torrent_file(), error)) { return false; } diff --git a/libtransmission/tr-dht.cc b/libtransmission/tr-dht.cc index a13cb2780..c377dfbb2 100644 --- a/libtransmission/tr-dht.cc +++ b/libtransmission/tr-dht.cc @@ -463,14 +463,14 @@ private: tr_variant_serde::benc().to_file(benc, state_filename_); } - void init_state(std::string_view filename) + void init_state(std::string_view const filename) { // Note that DHT ids need to be distributed uniformly, // so it should be something truly random id_ = tr_rand_obj(); id_timestamp_ = tr_time(); - if (!tr_sys_path_exists(std::data(filename))) + if (!tr_sys_path_exists(filename)) { return; } diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 456ef817b..142636f8d 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -688,10 +688,8 @@ void tr_sessionSetScriptEnabled(tr_session* session, TrScript type, bool enabled * * The caller only needs to invoke this when the blocklist * has changed. - * - * Passing nullptr for a filename will clear the blocklist. */ -size_t tr_blocklistSetContent(tr_session* session, char const* content_filename); +size_t tr_blocklistSetContent(tr_session* session, std::string_view content_filename); size_t tr_blocklistGetRuleCount(tr_session const* session); diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc index 32bd63ca4..b496f2fa4 100644 --- a/libtransmission/utils.cc +++ b/libtransmission/utils.cc @@ -566,11 +566,8 @@ std::string tr_strratio(double ratio, std::string_view const none, std::string_v // --- -bool tr_file_move(std::string_view oldpath_in, std::string_view newpath_in, bool allow_copy, tr_error* error) +bool tr_file_move(std::string_view oldpath, std::string_view newpath, bool allow_copy, tr_error* error) { - auto const oldpath = tr_pathbuf{ oldpath_in }; - auto const newpath = tr_pathbuf{ newpath_in }; - auto local_error = tr_error{}; if (error == nullptr) { diff --git a/libtransmission/verify.cc b/libtransmission/verify.cc index 839fefa2e..02fadddaf 100644 --- a/libtransmission/verify.cc +++ b/libtransmission/verify.cc @@ -57,7 +57,7 @@ void tr_verify_worker::verify_torrent( if (file_pos == 0U && fd == TR_BAD_SYS_FILE && file_index != prev_file_index) { auto const found = verify_mediator.find_file(file_index); - fd = !found ? TR_BAD_SYS_FILE : tr_sys_file_open(found->c_str(), TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0); + fd = !found ? TR_BAD_SYS_FILE : tr_sys_file_open(*found, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0); prev_file_index = file_index; } diff --git a/tests/libtransmission/dht-test.cc b/tests/libtransmission/dht-test.cc index 5ff2e0988..16bf31536 100644 --- a/tests/libtransmission/dht-test.cc +++ b/tests/libtransmission/dht-test.cc @@ -537,7 +537,7 @@ TEST_F(DhtTest, savesStateIfSwarmIsGood) { auto const state_file = MockStateFile{}; auto const dat_file = MockStateFile::filename(sandboxDir()); - EXPECT_FALSE(tr_sys_path_exists(dat_file.c_str())); + EXPECT_FALSE(tr_sys_path_exists(dat_file)); { auto mediator = MockMediator{ event_base_ }; @@ -548,17 +548,17 @@ TEST_F(DhtTest, savesStateIfSwarmIsGood) // as dht goes out of scope, // it should save its state if the swarm is healthy - EXPECT_FALSE(tr_sys_path_exists(dat_file.c_str())); + EXPECT_FALSE(tr_sys_path_exists(dat_file)); } - EXPECT_TRUE(tr_sys_path_exists(dat_file.c_str())); + EXPECT_TRUE(tr_sys_path_exists(dat_file)); } TEST_F(DhtTest, doesNotSaveStateIfSwarmIsBad) { auto const state_file = MockStateFile{}; auto const dat_file = MockStateFile::filename(sandboxDir()); - EXPECT_FALSE(tr_sys_path_exists(dat_file.c_str())); + EXPECT_FALSE(tr_sys_path_exists(dat_file)); { auto mediator = MockMediator{ event_base_ }; @@ -569,10 +569,10 @@ TEST_F(DhtTest, doesNotSaveStateIfSwarmIsBad) // as dht goes out of scope, // it should save its state if the swarm is healthy - EXPECT_FALSE(tr_sys_path_exists(dat_file.c_str())); + EXPECT_FALSE(tr_sys_path_exists(dat_file)); } - EXPECT_FALSE(tr_sys_path_exists(dat_file.c_str())); + EXPECT_FALSE(tr_sys_path_exists(dat_file)); } TEST_F(DhtTest, usesBootstrapFile) diff --git a/tests/libtransmission/move-test.cc b/tests/libtransmission/move-test.cc index 866b43dff..25390dc8d 100644 --- a/tests/libtransmission/move-test.cc +++ b/tests/libtransmission/move-test.cc @@ -159,7 +159,7 @@ using MoveTest = SessionTest; TEST_F(MoveTest, setLocation) { auto const target_dir = tr_pathbuf{ session_->configDir(), "/target"sv }; - tr_sys_dir_create(target_dir.data(), TR_SYS_DIR_CREATE_PARENTS, 0777, nullptr); + tr_sys_dir_create(target_dir, TR_SYS_DIR_CREATE_PARENTS, 0777, nullptr); // init a torrent. auto* const tor = zeroTorrentInit(ZeroTorrentState::Complete); diff --git a/tests/libtransmission/subprocess-test-program.cc b/tests/libtransmission/subprocess-test-program.cc index 8d73a266d..1c32c7081 100644 --- a/tests/libtransmission/subprocess-test-program.cc +++ b/tests/libtransmission/subprocess-test-program.cc @@ -62,6 +62,6 @@ int main(int argc, char** argv) } out.close(); - tr_sys_path_rename(tmp_result_path.c_str(), result_path.c_str()); + tr_sys_path_rename(tmp_result_path, result_path); return 0; } diff --git a/tests/libtransmission/test-fixtures.h b/tests/libtransmission/test-fixtures.h index e0e230aa6..3ba9b0185 100644 --- a/tests/libtransmission/test-fixtures.h +++ b/tests/libtransmission/test-fixtures.h @@ -208,7 +208,7 @@ protected: return child; } - static void buildParentDir(std::string_view path) + static void buildParentDir(std::string_view const path) { auto const tmperr = errno; diff --git a/utils/remote.cc b/utils/remote.cc index 8aa326843..43a9c93af 100644 --- a/utils/remote.cc +++ b/utils/remote.cc @@ -557,7 +557,7 @@ enum } } -[[nodiscard]] std::string get_encoded_metainfo(char const* filename) +[[nodiscard]] std::string get_encoded_metainfo(std::string_view const filename) { if (auto contents = std::vector{}; tr_sys_path_exists(filename) && tr_file_read(filename, contents)) {