refactor: tr_loadFile() tr_saveFile() take a std::string_view filename arg (#2822)

* refactor: tr_saveFile takes a string_view

* refactor: tr_saveFile, tr_loadFile take string_view filename
This commit is contained in:
Charles Kerr
2022-03-25 19:31:27 -05:00
committed by GitHub
parent 1051160b99
commit 977b2a1bab
15 changed files with 102 additions and 86 deletions

View File

@@ -49,6 +49,7 @@
#include "net.h" // ntohl()
#include "platform-quota.h" /* tr_device_info_create(), tr_device_info_get_disk_space(), tr_device_info_free() */
#include "tr-assert.h"
#include "tr-strbuf.h"
#include "utils.h"
#include "variant.h"
@@ -226,16 +227,18 @@ void tr_timerAddMsec(struct event* timer, int msec)
***
**/
uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
uint8_t* tr_loadFile(std::string_view path_in, size_t* size, tr_error** error)
{
auto const path = tr_pathbuf{ path_in };
/* 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.c_str(), 0, &info, &my_error))
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path),
fmt::arg("path", path.sv()),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -244,7 +247,7 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
if (info.type != TR_SYS_PATH_IS_FILE)
{
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path)));
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path.sv())));
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
return nullptr;
}
@@ -256,12 +259,12 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
}
/* Load the torrent file into our buffer */
auto const fd = tr_sys_file_open(path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
auto const fd = tr_sys_file_open(path.c_str(), TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
if (fd == TR_BAD_SYS_FILE)
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path),
fmt::arg("path", path.sv()),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -273,7 +276,7 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path),
fmt::arg("path", path.sv()),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_sys_file_close(fd, nullptr);
@@ -288,18 +291,18 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
return buf;
}
bool tr_loadFile(std::vector<char>& setme, std::string const& path, tr_error** error)
bool tr_loadFile(std::string_view path_in, std::vector<char>& setme, tr_error** error)
{
auto const* const path_sz = path.c_str();
auto const path = tr_pathbuf{ path_in };
/* try to stat the file */
auto info = tr_sys_path_info{};
tr_error* my_error = nullptr;
if (!tr_sys_path_get_info(path_sz, 0, &info, &my_error))
if (!tr_sys_path_get_info(path.c_str(), 0, &info, &my_error))
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path),
fmt::arg("path", path.sv()),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -308,18 +311,18 @@ bool tr_loadFile(std::vector<char>& setme, std::string const& path, tr_error** e
if (info.type != TR_SYS_PATH_IS_FILE)
{
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path)));
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path.sv())));
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
return false;
}
/* Load the torrent file into our buffer */
auto const fd = tr_sys_file_open(path_sz, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
auto const fd = tr_sys_file_open(path.c_str(), TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
if (fd == TR_BAD_SYS_FILE)
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path),
fmt::arg("path", path.sv()),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_error_propagate(error, &my_error);
@@ -331,7 +334,7 @@ bool tr_loadFile(std::vector<char>& setme, std::string const& path, tr_error** e
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", path),
fmt::arg("path", path.sv()),
fmt::arg("error", my_error->message),
fmt::arg("error_code", my_error->code)));
tr_sys_file_close(fd, nullptr);
@@ -343,24 +346,26 @@ bool tr_loadFile(std::vector<char>& setme, std::string const& path, tr_error** e
return true;
}
bool tr_saveFile(std::string const& filename, std::string_view contents, tr_error** error)
bool tr_saveFile(std::string_view filename_in, std::string_view contents, tr_error** error)
{
auto const filename = tr_pathbuf{ filename_in };
// follow symlinks to find the "real" file, to make sure the temporary
// we build with tr_sys_file_open_temp() is created on the right partition
if (char* const real_filename_c_str = tr_sys_path_resolve(filename.c_str(), nullptr); real_filename_c_str != nullptr)
if (char* const real_filename = tr_sys_path_resolve(filename.c_str()); real_filename != nullptr)
{
auto const real_filename = std::string{ real_filename_c_str };
tr_free(real_filename_c_str);
if (real_filename != filename)
if (filename_in != real_filename)
{
return tr_saveFile(real_filename, contents, error);
auto const saved = tr_saveFile(real_filename, contents, error);
tr_free(real_filename);
return saved;
}
tr_free(real_filename);
}
// Write it to a temp file first.
// This is a safeguard against edge cases, e.g. disk full, crash while writing, etc.
auto tmp = tr_strvJoin(filename, ".tmp.XXXXXX"sv);
auto tmp = tr_pathbuf{ filename.sv(), ".tmp.XXXXXX"sv };
auto const fd = tr_sys_file_open_temp(std::data(tmp), error);
if (fd == TR_BAD_SYS_FILE)
{
@@ -386,7 +391,7 @@ bool tr_saveFile(std::string const& filename, std::string_view contents, tr_erro
return false;
}
tr_logAddTrace(fmt::format("Saved '{}'", filename));
tr_logAddTrace(fmt::format("Saved '{}'", filename.sv()));
return true;
}