diff --git a/cli/cli.cc b/cli/cli.cc index 8c5e5fcda..accde7e0b 100644 --- a/cli/cli.cc +++ b/cli/cli.cc @@ -245,7 +245,6 @@ int tr_main(int argc, char* argv[]) char const* configDir; uint8_t* fileContents; size_t fileLength; - char const* str; tr_formatter_mem_init(MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR); tr_formatter_size_init(DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR); @@ -283,15 +282,22 @@ int tr_main(int argc, char* argv[]) return EXIT_FAILURE; } - if (tr_variantDictFindStr(&settings, TR_KEY_download_dir, &str, nullptr) && !tr_sys_path_exists(str, nullptr)) + auto sv = std::string_view{}; + if (tr_variantDictFindStrView(&settings, TR_KEY_download_dir, &sv)) { - tr_error* error = nullptr; + // tr_sys_path_exists and tr_sys_dir_create need zero-terminated strs + auto const sz_download_dir = std::string{ sv }; - if (!tr_sys_dir_create(str, TR_SYS_DIR_CREATE_PARENTS, 0700, &error)) + if (!tr_sys_path_exists(sz_download_dir.c_str(), nullptr)) { - fprintf(stderr, "Unable to create download directory \"%s\": %s\n", str, error->message); - tr_error_free(error); - return EXIT_FAILURE; + tr_error* error = nullptr; + + if (!tr_sys_dir_create(sz_download_dir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0700, &error)) + { + fprintf(stderr, "Unable to create download directory \"%s\": %s\n", sz_download_dir.c_str(), error->message); + tr_error_free(error); + return EXIT_FAILURE; + } } } diff --git a/daemon/daemon.cc b/daemon/daemon.cc index a38712bd7..3f1b5a18a 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -649,7 +649,6 @@ static void daemon_stop(void* /*arg*/) static int daemon_start(void* varg, [[maybe_unused]] bool foreground) { bool boolVal; - char const* pid_filename; bool pidfile_created = false; tr_session* session = nullptr; struct event* status_ev = nullptr; @@ -684,13 +683,14 @@ static int daemon_start(void* varg, [[maybe_unused]] bool foreground) tr_logAddNamedInfo(nullptr, "Using settings from \"%s\"", configDir); tr_sessionSaveSettings(session, configDir, settings); - pid_filename = nullptr; - (void)tr_variantDictFindStr(settings, key_pidfile, &pid_filename, nullptr); - if (!tr_str_is_empty(pid_filename)) + auto sv = std::string_view{}; + (void)tr_variantDictFindStrView(settings, key_pidfile, &sv); + auto const sz_pid_filename = std::string{ sv }; + if (!std::empty(sz_pid_filename)) { tr_error* error = nullptr; tr_sys_file_t fp = tr_sys_file_open( - pid_filename, + sz_pid_filename.c_str(), TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, &error); @@ -699,12 +699,12 @@ static int daemon_start(void* varg, [[maybe_unused]] bool foreground) { tr_sys_file_write_fmt(fp, "%d", nullptr, (int)getpid()); tr_sys_file_close(fp, nullptr); - tr_logAddInfo("Saved pidfile \"%s\"", pid_filename); + tr_logAddInfo("Saved pidfile \"%s\"", sz_pid_filename.c_str()); pidfile_created = true; } else { - tr_logAddError("Unable to save pidfile \"%s\": %s", pid_filename, error->message); + tr_logAddError("Unable to save pidfile \"%s\": %s", sz_pid_filename.c_str(), error->message); tr_error_free(error); } } @@ -826,7 +826,7 @@ CLEANUP: /* cleanup */ if (pidfile_created) { - tr_sys_path_remove(pid_filename, nullptr); + tr_sys_path_remove(sz_pid_filename.c_str(), nullptr); } sd_notify(0, "STATUS=\n"); diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index d308fedaa..ad329261e 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -1735,9 +1735,8 @@ static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_varia return "no filename or metainfo specified"; } - char const* download_dir = nullptr; - - if (tr_variantDictFindStr(args_in, TR_KEY_download_dir, &download_dir, nullptr) && tr_sys_path_is_relative(download_dir)) + auto download_dir = std::string_view{}; + if (tr_variantDictFindStrView(args_in, TR_KEY_download_dir, &download_dir) && tr_sys_path_is_relative(download_dir)) { return "download directory path is not absolute"; } @@ -1752,9 +1751,10 @@ static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_varia auto cookies = std::string_view{}; (void)tr_variantDictFindStrView(args_in, TR_KEY_cookies, &cookies); - if (download_dir != nullptr) + if (!std::empty(download_dir)) { - tr_ctorSetDownloadDir(ctor, TR_FORCE, download_dir); + auto const sz_download_dir = std::string{ download_dir }; + tr_ctorSetDownloadDir(ctor, TR_FORCE, sz_download_dir.c_str()); } if (tr_variantDictFindBool(args_in, TR_KEY_paused, &boolVal)) diff --git a/libtransmission/variant.cc b/libtransmission/variant.cc index 6ca5b9ee8..e08240c3a 100644 --- a/libtransmission/variant.cc +++ b/libtransmission/variant.cc @@ -326,27 +326,6 @@ bool tr_variantGetStrView(tr_variant const* v, std::string_view* setme) return true; } -static bool tr_variantGetStr(tr_variant const* v, char const** setme, size_t* len) -{ - auto sv = std::string_view{}; - if (!tr_variantGetStrView(v, &sv)) - { - return false; - } - - if (setme != nullptr) - { - *setme = std::data(sv); - } - - if (len != nullptr) - { - *len = std::size(sv); - } - - return true; -} - bool tr_variantGetRaw(tr_variant const* v, uint8_t const** setme_raw, size_t* setme_len) { bool const success = tr_variantIsString(v); @@ -452,12 +431,6 @@ bool tr_variantDictFindStrView(tr_variant* dict, tr_quark const key, std::string return tr_variantGetStrView(child, setme); } -bool tr_variantDictFindStr(tr_variant* dict, tr_quark const key, char const** setme, size_t* len) -{ - tr_variant const* const child = tr_variantDictFind(dict, key); - return tr_variantGetStr(child, setme, len); -} - bool tr_variantDictFindList(tr_variant* dict, tr_quark const key, tr_variant** setme) { return tr_variantDictFindType(dict, key, TR_VARIANT_TYPE_LIST, setme); diff --git a/libtransmission/variant.h b/libtransmission/variant.h index d10c0a226..47dd9e500 100644 --- a/libtransmission/variant.h +++ b/libtransmission/variant.h @@ -239,7 +239,6 @@ bool tr_variantDictFindDict(tr_variant* dict, tr_quark const key, tr_variant** s bool tr_variantDictFindInt(tr_variant* dict, tr_quark const key, int64_t* setme); bool tr_variantDictFindReal(tr_variant* dict, tr_quark const key, double* setme); bool tr_variantDictFindBool(tr_variant* dict, tr_quark const key, bool* setme); -bool tr_variantDictFindStr(tr_variant* dict, tr_quark const key, char const** setme, size_t* len); bool tr_variantDictFindStrView(tr_variant* dict, tr_quark const key, std::string_view* setme); bool tr_variantDictFindRaw(tr_variant* dict, tr_quark const key, uint8_t const** setme_raw, size_t* setme_len);