1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 01:27:28 +00:00

refactor: take string_view in tr_loadFile(), tr_saveFile() (#2335)

This commit is contained in:
Charles Kerr 2021-12-24 13:37:34 -06:00 committed by GitHub
parent 33553c5331
commit deee2b043e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 28 additions and 22 deletions

View file

@ -205,7 +205,7 @@ bool tr_announce_list::canAdd(tr_url_parsed_t const& announce)
return std::none_of(std::begin(trackers_), std::end(trackers_), is_same);
}
bool tr_announce_list::save(char const* torrent_file, tr_error** error) const
bool tr_announce_list::save(std::string_view torrent_file, tr_error** error) const
{
// load the .torrent file
auto metainfo = tr_variant{};

View file

@ -105,7 +105,7 @@ public:
return trackers_.clear();
}
bool save(char const* torrent_file, tr_error** error = nullptr) const;
bool save(std::string_view torrent_file, tr_error** error = nullptr) const;
static std::optional<std::string> announceToScrape(std::string_view announce);
static tr_quark announceToScrape(tr_quark announce);

View file

@ -312,7 +312,7 @@ static std::string getUserDirsFilename()
static std::string getXdgEntryFromUserDirs(std::string_view key)
{
auto content = std::vector<char>{};
if (!tr_loadFile(content, getUserDirsFilename().c_str()) && std::empty(content))
if (!tr_loadFile(content, getUserDirsFilename()) && std::empty(content))
{
return {};
}

View file

@ -736,7 +736,7 @@ static uint64_t loadFromFile(tr_torrent* tor, uint64_t fieldsToLoad, bool* didRe
std::string const filename = getResumeFilename(tor, TR_METAINFO_BASENAME_HASH);
auto buf = std::vector<char>{};
if (!tr_loadFile(buf, filename.c_str(), &error) ||
if (!tr_loadFile(buf, filename, &error) ||
!tr_variantFromBuf(
&top,
TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE,

View file

@ -166,10 +166,10 @@ int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename)
return 0;
}
bool tr_ctorSaveContents(tr_ctor const* ctor, char const* filename, tr_error** error)
bool tr_ctorSaveContents(tr_ctor const* ctor, std::string_view filename, tr_error** error)
{
TR_ASSERT(ctor != nullptr);
TR_ASSERT(filename != nullptr);
TR_ASSERT(!std::empty(filename));
if (std::empty(ctor->contents))
{

View file

@ -57,7 +57,7 @@ void tr_ctorInitTorrentPriorities(tr_ctor const* ctor, tr_torrent* tor);
void tr_ctorInitTorrentWanted(tr_ctor const* ctor, tr_torrent* tor);
bool tr_ctorSaveContents(tr_ctor const* ctor, char const* filename, tr_error** error);
bool tr_ctorSaveContents(tr_ctor const* ctor, std::string_view filename, tr_error** error);
bool tr_ctorGetMetainfo(tr_ctor const* ctor, tr_variant const** setme);

View file

@ -318,32 +318,34 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
return buf;
}
bool tr_loadFile(std::vector<char>& setme, char const* path, tr_error** error)
bool tr_loadFile(std::vector<char>& setme, std::string_view path_sv, tr_error** error)
{
char const* const err_fmt = _("Couldn't read \"%1$s\": %2$s");
auto const path = std::string{ path_sv };
auto const* const path_sz = path.c_str();
/* try to stat the file */
auto info = tr_sys_path_info{};
tr_error* my_error = nullptr;
if (!tr_sys_path_get_info(path, 0, &info, &my_error))
if (!tr_sys_path_get_info(path_sz, 0, &info, &my_error))
{
tr_logAddDebug(err_fmt, path, my_error->message);
tr_logAddDebug(err_fmt, path_sz, my_error->message);
tr_error_propagate(error, &my_error);
return false;
}
if (info.type != TR_SYS_PATH_IS_FILE)
{
tr_logAddError(err_fmt, path, _("Not a regular file"));
tr_logAddError(err_fmt, path_sz, _("Not a regular file"));
tr_error_set_literal(error, TR_ERROR_EISDIR, _("Not a regular file"));
return false;
}
/* Load the torrent file into our buffer */
tr_sys_file_t const fd = tr_sys_file_open(path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
tr_sys_file_t const fd = tr_sys_file_open(path_sz, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
if (fd == TR_BAD_SYS_FILE)
{
tr_logAddError(err_fmt, path, my_error->message);
tr_logAddError(err_fmt, path_sz, my_error->message);
tr_error_propagate(error, &my_error);
return false;
}
@ -351,7 +353,7 @@ bool tr_loadFile(std::vector<char>& setme, char const* path, tr_error** error)
setme.resize(info.size);
if (!tr_sys_file_read(fd, std::data(setme), info.size, nullptr, &my_error))
{
tr_logAddError(err_fmt, path, my_error->message);
tr_logAddError(err_fmt, path_sz, my_error->message);
tr_sys_file_close(fd, nullptr);
tr_error_propagate(error, &my_error);
return false;
@ -361,7 +363,7 @@ bool tr_loadFile(std::vector<char>& setme, char const* path, tr_error** error)
return true;
}
bool tr_saveFile(char const* filename_in, std::string_view contents, tr_error** error)
bool tr_saveFile(std::string_view filename_in, std::string_view contents, tr_error** error)
{
auto filename = std::string{ filename_in };

View file

@ -79,9 +79,9 @@ bool tr_wildmat(char const* text, char const* pattern) TR_GNUC_NONNULL(1, 2);
*/
uint8_t* tr_loadFile(char const* filename, size_t* size, struct tr_error** error) TR_GNUC_MALLOC TR_GNUC_NONNULL(1);
bool tr_loadFile(std::vector<char>& setme, char const* filename, tr_error** error = nullptr);
bool tr_loadFile(std::vector<char>& setme, std::string_view filename, tr_error** error = nullptr);
bool tr_saveFile(char const* filename_in, std::string_view contents, tr_error** error = nullptr);
bool tr_saveFile(std::string_view filename, std::string_view contents, tr_error** error = nullptr);
/** @brief build a filename from a series of elements using the
platform's correct directory separator. */

View file

@ -1206,7 +1206,7 @@ char* tr_variantToStr(tr_variant const* v, tr_variant_fmt fmt, size_t* len)
return evbuffer_free_to_str(buf, len);
}
int tr_variantToFile(tr_variant const* v, tr_variant_fmt fmt, char const* filename)
int tr_variantToFile(tr_variant const* v, tr_variant_fmt fmt, std::string_view filename)
{
auto error_code = int{ 0 };
auto contents_len = size_t{};
@ -1216,7 +1216,7 @@ int tr_variantToFile(tr_variant const* v, tr_variant_fmt fmt, char const* filena
tr_saveFile(filename, { contents, contents_len }, &error);
if (error != nullptr)
{
tr_logAddError(_("Error saving \"%s\": %s (%d)"), filename, error->message, error->code);
tr_logAddError(_("Error saving \"%" TR_PRIsv "\": %s (%d)"), TR_PRIsv_ARG(filename), error->message, error->code);
error_code = error->code;
tr_error_clear(&error);
}
@ -1253,7 +1253,7 @@ bool tr_variantFromBuf(tr_variant* setme, int opts, std::string_view buf, char c
return true;
}
bool tr_variantFromFile(tr_variant* setme, tr_variant_parse_opts opts, char const* filename, tr_error** error)
bool tr_variantFromFile(tr_variant* setme, tr_variant_parse_opts opts, std::string_view filename, tr_error** error)
{
// can't do inplace when this function is allocating & freeing the memory...
TR_ASSERT((opts & TR_VARIANT_PARSE_INPLACE) == 0);

View file

@ -106,7 +106,7 @@ enum tr_variant_fmt
TR_VARIANT_FMT_JSON_LEAN /* saves bandwidth by omitting all whitespace. */
};
int tr_variantToFile(tr_variant const* variant, tr_variant_fmt fmt, char const* filename);
int tr_variantToFile(tr_variant const* variant, tr_variant_fmt fmt, std::string_view filename);
char* tr_variantToStr(tr_variant const* variant, tr_variant_fmt fmt, size_t* len);
@ -119,7 +119,11 @@ enum tr_variant_parse_opts
TR_VARIANT_PARSE_INPLACE = (1 << 2)
};
bool tr_variantFromFile(tr_variant* setme, tr_variant_parse_opts opts, char const* filename, struct tr_error** error = nullptr);
bool tr_variantFromFile(
tr_variant* setme,
tr_variant_parse_opts opts,
std::string_view filename,
struct tr_error** error = nullptr);
bool tr_variantFromBuf(
tr_variant* setme,