add_file_borrow: use string_view instead of pointer, size parameters (#2302)

This commit is contained in:
Steven Siloti 2017-09-02 14:58:36 -07:00 committed by Arvid Norberg
parent 7087a16b36
commit 5361ffc1c2
4 changed files with 28 additions and 14 deletions

View File

@ -232,16 +232,16 @@ namespace libtorrent {
void reserve(int num_files);
// Adds a file to the file storage. The ``add_file_borrow`` version
// expects that ``filename`` points to a string of ``filename_len``
// bytes that is the file name (without a path) of the file that's
// being added. This memory is *borrowed*, i.e. it is the caller's
// expects that ``filename`` is the file name (without a path) of
// the file that's being added.
// This memory is *borrowed*, i.e. it is the caller's
// responsibility to make sure it stays valid throughout the lifetime
// of this file_storage object or any copy of it. The same thing applies
// to ``filehash``, which is an optional pointer to a 20 byte binary
// SHA-1 hash of the file.
//
// if ``filename`` is nullptr, the filename from ``path`` is used and not
// borrowed. In this case ``filename_len`` is ignored.
// if ``filename`` is empty, the filename from ``path`` is used and not
// borrowed.
//
// The ``path`` argument is the full path (in the torrent file) to
// the file to add. Note that this is not supposed to be an absolute
@ -268,7 +268,7 @@ namespace libtorrent {
// That is, the first path element of all files must be the same.
// This shared path element is also set to the name of the torrent. It
// can be changed by calling ``set_name``.
void add_file_borrow(char const* filename, int filename_len
void add_file_borrow(string_view filename
, std::string const& path, std::int64_t file_size
, file_flags_t file_flags = {}, char const* filehash = 0
, std::int64_t mtime = 0, string_view symlink_path = string_view());
@ -282,6 +282,11 @@ namespace libtorrent {
#ifndef TORRENT_NO_DEPRECATE
TORRENT_DEPRECATED
void add_file_borrow(char const* filename, int filename_len
, std::string const& path, std::int64_t file_size
, file_flags_t file_flags = {}, char const* filehash = 0
, std::int64_t mtime = 0, string_view symlink_path = string_view());
TORRENT_DEPRECATED
void add_file(file_entry const& fe, char const* filehash = nullptr);
// all wstring APIs are deprecated since 0.16.11

View File

@ -344,6 +344,15 @@ namespace {
#ifndef TORRENT_NO_DEPRECATE
void file_storage::add_file_borrow(char const* filename, int filename_len
, std::string const& path, std::int64_t file_size, file_flags_t file_flags
, char const* filehash, std::int64_t mtime, string_view symlink_path)
{
TORRENT_ASSERT(filename_len >= 0);
add_file_borrow({filename, std::size_t(filename_len)}, path, file_size
, file_flags, filehash, mtime, symlink_path);
}
void file_storage::add_file(file_entry const& fe, char const* filehash)
{
file_flags_t flags = {};
@ -352,7 +361,7 @@ namespace {
if (fe.executable_attribute) flags |= file_storage::flag_executable;
if (fe.symlink_attribute) flags |= file_storage::flag_symlink;
add_file_borrow(nullptr, 0, fe.path, fe.size, flags, filehash, fe.mtime
add_file_borrow({}, fe.path, fe.size, flags, filehash, fe.mtime
, fe.symlink_path);
}
@ -547,11 +556,11 @@ namespace {
void file_storage::add_file(std::string const& path, std::int64_t file_size
, file_flags_t const file_flags, std::time_t mtime, string_view symlink_path)
{
add_file_borrow(nullptr, 0, path, file_size, file_flags, nullptr, mtime
add_file_borrow({}, path, file_size, file_flags, nullptr, mtime
, symlink_path);
}
void file_storage::add_file_borrow(char const* filename, int const filename_len
void file_storage::add_file_borrow(string_view filename
, std::string const& path, std::int64_t const file_size
, file_flags_t const file_flags, char const* filehash
, std::int64_t const mtime, string_view symlink_path)
@ -581,11 +590,11 @@ namespace {
// if filename is nullptr, we should copy it. If it isn't, we're borrowing
// it and we can save the copy by setting it after this call to
// update_path_index().
update_path_index(e, path, filename == nullptr);
update_path_index(e, path, filename.empty());
// filename is allowed to be nullptr, in which case we just use path
if (filename)
e.set_name(filename, true, filename_len);
if (!filename.empty())
e.set_name(filename.data(), true, int(filename.size()));
e.size = aux::numeric_cast<std::uint64_t>(file_size);
e.offset = aux::numeric_cast<std::uint64_t>(m_total_size);

View File

@ -484,7 +484,7 @@ namespace {
filename_len = 0;
}
files.add_file_borrow(filename, filename_len, path, file_size, file_flags, filehash
files.add_file_borrow({filename, std::size_t(filename_len)}, path, file_size, file_flags, filehash
, mtime, symlink_path);
return true;
}

View File

@ -177,7 +177,7 @@ TORRENT_TEST(pointer_offset)
file_storage st;
char const filename[] = "test1fooba";
st.add_file_borrow(filename, 5, combine_path("test-torrent-1", "test1")
st.add_file_borrow({filename, 5}, combine_path("test-torrent-1", "test1")
, 10);
// test filename_ptr and filename_len