2008-05-28 10:44:40 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2008-05-28 10:44:40 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/file_storage.hpp"
|
2012-08-26 17:26:17 +02:00
|
|
|
#include "libtorrent/string_util.hpp" // for allocate_string_copy
|
2008-11-30 09:12:26 +01:00
|
|
|
#include "libtorrent/utf8.hpp"
|
2018-06-02 19:29:40 +02:00
|
|
|
#include "libtorrent/index_range.hpp"
|
2017-04-11 06:52:46 +02:00
|
|
|
#include "libtorrent/aux_/path.hpp"
|
2017-02-07 06:22:30 +01:00
|
|
|
#include "libtorrent/aux_/numeric_cast.hpp"
|
2015-08-02 05:57:11 +02:00
|
|
|
|
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2015-02-17 03:08:47 +01:00
|
|
|
#include <boost/crc.hpp>
|
2015-08-02 05:57:11 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2009-06-22 18:13:16 +02:00
|
|
|
#include <cstdio>
|
2009-11-26 19:31:27 +01:00
|
|
|
#include <algorithm>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <functional>
|
2019-07-22 00:50:57 +02:00
|
|
|
#include <set>
|
2019-08-12 16:25:00 +02:00
|
|
|
#include <atomic>
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2015-02-17 03:08:47 +01:00
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
#define TORRENT_SEPARATOR '\\'
|
|
|
|
#else
|
|
|
|
#define TORRENT_SEPARATOR '/'
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2017-08-03 11:31:03 +02:00
|
|
|
constexpr file_flags_t file_storage::flag_pad_file;
|
|
|
|
constexpr file_flags_t file_storage::flag_hidden;
|
|
|
|
constexpr file_flags_t file_storage::flag_executable;
|
|
|
|
constexpr file_flags_t file_storage::flag_symlink;
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2017-08-03 11:31:03 +02:00
|
|
|
constexpr file_flags_t file_storage::pad_file;
|
|
|
|
constexpr file_flags_t file_storage::attribute_hidden;
|
|
|
|
constexpr file_flags_t file_storage::attribute_executable;
|
|
|
|
constexpr file_flags_t file_storage::attribute_symlink;
|
|
|
|
#endif
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage::file_storage()
|
2014-07-06 21:18:00 +02:00
|
|
|
: m_piece_length(0)
|
2008-05-28 10:44:40 +02:00
|
|
|
, m_num_pieces(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_total_size(0)
|
2008-05-28 10:44:40 +02:00
|
|
|
{}
|
|
|
|
|
2016-07-10 13:34:45 +02:00
|
|
|
file_storage::~file_storage() = default;
|
2015-01-01 21:25:39 +01:00
|
|
|
|
2015-04-11 16:17:33 +02:00
|
|
|
// even though this copy constructor and the copy assignment
|
|
|
|
// operator are identical to what the compiler would have
|
|
|
|
// generated, they are put here to explicitly make them part
|
|
|
|
// of libtorrent and properly exported by the .dll.
|
2016-07-10 13:34:45 +02:00
|
|
|
file_storage::file_storage(file_storage const&) = default;
|
|
|
|
file_storage& file_storage::operator=(file_storage const&) = default;
|
2017-05-28 13:14:58 +02:00
|
|
|
file_storage::file_storage(file_storage&&) noexcept = default;
|
2015-04-11 16:17:33 +02:00
|
|
|
|
2009-11-26 07:54:52 +01:00
|
|
|
void file_storage::reserve(int num_files)
|
|
|
|
{
|
|
|
|
m_files.reserve(num_files);
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
int file_storage::piece_size(piece_index_t const index) const
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= piece_index_t(0) && index < end_piece());
|
|
|
|
if (index == last_piece())
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2016-09-21 07:06:07 +02:00
|
|
|
std::int64_t const size_except_last
|
|
|
|
= (num_pieces() - 1) * std::int64_t(piece_length());
|
|
|
|
std::int64_t const size = total_size() - size_except_last;
|
2008-05-28 10:44:40 +02:00
|
|
|
TORRENT_ASSERT(size > 0);
|
|
|
|
TORRENT_ASSERT(size <= piece_length());
|
|
|
|
return int(size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return piece_length();
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace {
|
|
|
|
|
2017-08-03 10:38:58 +02:00
|
|
|
bool compare_file_offset(internal_file_entry const& lhs
|
|
|
|
, internal_file_entry const& rhs)
|
|
|
|
{
|
|
|
|
return lhs.offset < rhs.offset;
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
|
|
|
|
2017-08-03 10:38:58 +02:00
|
|
|
}
|
|
|
|
|
2019-03-20 09:36:37 +01:00
|
|
|
// path is supposed to include the name of the torrent itself.
|
|
|
|
// or an absolute path, to move a file outside of the download directory
|
2015-02-17 03:08:47 +01:00
|
|
|
void file_storage::update_path_index(internal_file_entry& e
|
2016-09-21 07:06:07 +02:00
|
|
|
, std::string const& path, bool const set_name)
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2015-02-17 03:08:47 +01:00
|
|
|
if (is_complete(path))
|
2013-06-10 00:30:02 +02:00
|
|
|
{
|
2015-02-17 03:08:47 +01:00
|
|
|
TORRENT_ASSERT(set_name);
|
2019-03-21 13:15:23 +01:00
|
|
|
e.set_name(path);
|
2013-06-10 00:30:02 +02:00
|
|
|
e.path_index = -2;
|
|
|
|
return;
|
|
|
|
}
|
2015-02-17 03:08:47 +01:00
|
|
|
|
|
|
|
TORRENT_ASSERT(path[0] != '/');
|
|
|
|
|
2019-03-20 09:36:37 +01:00
|
|
|
// split the string into the leaf filename
|
|
|
|
// and the branch path
|
|
|
|
string_view leaf;
|
2017-10-01 03:17:10 +02:00
|
|
|
string_view branch_path;
|
2019-03-20 09:36:37 +01:00
|
|
|
std::tie(branch_path, leaf) = rsplit_path(path);
|
|
|
|
|
2017-10-01 03:17:10 +02:00
|
|
|
if (branch_path.empty())
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2019-03-21 13:15:23 +01:00
|
|
|
if (set_name) e.set_name(leaf);
|
2010-11-25 00:49:22 +01:00
|
|
|
e.path_index = -1;
|
2013-11-17 09:02:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-20 09:36:37 +01:00
|
|
|
// if the path *does* contain the name of the torrent (as we expect)
|
|
|
|
// strip it before adding it to m_paths
|
|
|
|
if (lsplit_path(branch_path).first == m_name)
|
2013-11-17 09:02:16 +01:00
|
|
|
{
|
2019-03-20 09:36:37 +01:00
|
|
|
branch_path = lsplit_path(branch_path).second;
|
|
|
|
// strip duplicate separators
|
|
|
|
while (!branch_path.empty() && (branch_path.front() == TORRENT_SEPARATOR
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
|| branch_path.front() == '/'
|
|
|
|
#endif
|
|
|
|
))
|
2017-10-01 03:17:10 +02:00
|
|
|
branch_path.remove_prefix(1);
|
2013-11-17 09:02:16 +01:00
|
|
|
e.no_root_dir = false;
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-17 09:02:16 +01:00
|
|
|
e.no_root_dir = true;
|
|
|
|
}
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2017-10-01 03:17:10 +02:00
|
|
|
e.path_index = get_or_add_path(branch_path);
|
2019-03-21 13:15:23 +01:00
|
|
|
if (set_name) e.set_name(leaf);
|
2017-08-03 10:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int file_storage::get_or_add_path(string_view const path)
|
|
|
|
{
|
2013-11-17 09:02:16 +01:00
|
|
|
// do we already have this path in the path list?
|
2017-08-16 23:21:11 +02:00
|
|
|
auto const p = std::find(m_paths.rbegin(), m_paths.rend(), path);
|
2013-11-17 09:02:16 +01:00
|
|
|
|
|
|
|
if (p == m_paths.rend())
|
|
|
|
{
|
|
|
|
// no, we don't. add it
|
2017-08-03 10:38:58 +02:00
|
|
|
int const ret = int(m_paths.size());
|
|
|
|
TORRENT_ASSERT(path.size() == 0 || path[0] != '/');
|
|
|
|
m_paths.emplace_back(path.data(), path.size());
|
|
|
|
return ret;
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
2013-11-17 09:02:16 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// yes we do. use it
|
2017-08-03 10:38:58 +02:00
|
|
|
return int(p.base() - m_paths.begin() - 1);
|
2013-11-17 09:02:16 +01:00
|
|
|
}
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2017-05-04 15:22:17 +02:00
|
|
|
file_entry::file_entry(): offset(0), size(0)
|
2012-03-19 07:06:52 +01:00
|
|
|
, mtime(0), pad_file(false), hidden_attribute(false)
|
|
|
|
, executable_attribute(false)
|
|
|
|
, symlink_attribute(false)
|
|
|
|
{}
|
|
|
|
|
2016-07-10 13:34:45 +02:00
|
|
|
file_entry::~file_entry() = default;
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2012-03-19 07:06:52 +01:00
|
|
|
|
2016-08-13 13:04:53 +02:00
|
|
|
internal_file_entry::internal_file_entry()
|
|
|
|
: offset(0)
|
|
|
|
, symlink_index(not_a_symlink)
|
|
|
|
, no_root_dir(false)
|
|
|
|
, size(0)
|
|
|
|
, name_len(name_is_owned)
|
|
|
|
, pad_file(false)
|
|
|
|
, hidden_attribute(false)
|
|
|
|
, executable_attribute(false)
|
|
|
|
, symlink_attribute(false)
|
|
|
|
, name(nullptr)
|
|
|
|
, path_index(-1)
|
|
|
|
{}
|
|
|
|
|
2013-11-17 09:02:16 +01:00
|
|
|
internal_file_entry::~internal_file_entry()
|
|
|
|
{
|
2017-09-17 12:50:09 +02:00
|
|
|
if (name_len == name_is_owned) delete[] name;
|
2013-11-17 09:02:16 +01:00
|
|
|
}
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2010-11-29 06:44:29 +01:00
|
|
|
internal_file_entry::internal_file_entry(internal_file_entry const& fe)
|
2013-11-26 08:47:48 +01:00
|
|
|
: offset(fe.offset)
|
2013-11-17 23:38:37 +01:00
|
|
|
, symlink_index(fe.symlink_index)
|
2013-11-28 09:48:17 +01:00
|
|
|
, no_root_dir(fe.no_root_dir)
|
|
|
|
, size(fe.size)
|
2010-11-25 00:49:22 +01:00
|
|
|
, name_len(fe.name_len)
|
|
|
|
, pad_file(fe.pad_file)
|
|
|
|
, hidden_attribute(fe.hidden_attribute)
|
|
|
|
, executable_attribute(fe.executable_attribute)
|
|
|
|
, symlink_attribute(fe.symlink_attribute)
|
2016-07-09 22:26:26 +02:00
|
|
|
, name(nullptr)
|
2010-11-25 00:49:22 +01:00
|
|
|
, path_index(fe.path_index)
|
|
|
|
{
|
2019-03-21 13:15:23 +01:00
|
|
|
bool const borrow = fe.name_len != name_is_owned;
|
|
|
|
set_name(fe.filename(), borrow);
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
|
2018-06-30 18:31:45 +02:00
|
|
|
internal_file_entry& internal_file_entry::operator=(internal_file_entry const& fe) &
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2017-09-12 23:10:11 +02:00
|
|
|
if (&fe == this) return *this;
|
2010-11-25 00:49:22 +01:00
|
|
|
offset = fe.offset;
|
|
|
|
size = fe.size;
|
|
|
|
path_index = fe.path_index;
|
|
|
|
symlink_index = fe.symlink_index;
|
|
|
|
pad_file = fe.pad_file;
|
|
|
|
hidden_attribute = fe.hidden_attribute;
|
|
|
|
executable_attribute = fe.executable_attribute;
|
|
|
|
symlink_attribute = fe.symlink_attribute;
|
2013-11-17 09:02:16 +01:00
|
|
|
no_root_dir = fe.no_root_dir;
|
2019-03-21 13:15:23 +01:00
|
|
|
// if the name is not owned, don't allocate memory, we can point into the
|
|
|
|
// same metadata buffer
|
|
|
|
bool const borrow = fe.name_len != name_is_owned;
|
|
|
|
set_name(fe.filename(), borrow);
|
2016-08-13 13:04:53 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-05-28 13:14:58 +02:00
|
|
|
internal_file_entry::internal_file_entry(internal_file_entry&& fe) noexcept
|
2016-08-13 13:04:53 +02:00
|
|
|
: offset(fe.offset)
|
|
|
|
, symlink_index(fe.symlink_index)
|
|
|
|
, no_root_dir(fe.no_root_dir)
|
|
|
|
, size(fe.size)
|
|
|
|
, name_len(fe.name_len)
|
|
|
|
, pad_file(fe.pad_file)
|
|
|
|
, hidden_attribute(fe.hidden_attribute)
|
|
|
|
, executable_attribute(fe.executable_attribute)
|
|
|
|
, symlink_attribute(fe.symlink_attribute)
|
|
|
|
, name(fe.name)
|
|
|
|
, path_index(fe.path_index)
|
|
|
|
{
|
2019-03-21 13:15:23 +01:00
|
|
|
fe.name_len = 0;
|
2016-08-13 13:04:53 +02:00
|
|
|
fe.name = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-30 18:31:45 +02:00
|
|
|
internal_file_entry& internal_file_entry::operator=(internal_file_entry&& fe) & noexcept
|
2016-08-13 13:04:53 +02:00
|
|
|
{
|
2017-09-12 23:10:11 +02:00
|
|
|
if (&fe == this) return *this;
|
2016-08-13 13:04:53 +02:00
|
|
|
offset = fe.offset;
|
|
|
|
size = fe.size;
|
|
|
|
path_index = fe.path_index;
|
|
|
|
symlink_index = fe.symlink_index;
|
|
|
|
pad_file = fe.pad_file;
|
|
|
|
hidden_attribute = fe.hidden_attribute;
|
|
|
|
executable_attribute = fe.executable_attribute;
|
|
|
|
symlink_attribute = fe.symlink_attribute;
|
|
|
|
no_root_dir = fe.no_root_dir;
|
|
|
|
name = fe.name;
|
|
|
|
name_len = fe.name_len;
|
|
|
|
|
2019-03-21 13:15:23 +01:00
|
|
|
fe.name_len = 0;
|
2016-08-13 13:04:53 +02:00
|
|
|
fe.name = nullptr;
|
2010-11-25 00:49:22 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-21 13:15:23 +01:00
|
|
|
// if borrow_string is true, don't take ownership over n, just
|
|
|
|
// point to it.
|
|
|
|
// if borrow_string is false, n will be copied and owned by the
|
|
|
|
// internal_file_entry.
|
|
|
|
void internal_file_entry::set_name(string_view n, bool const borrow_string)
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2013-10-05 06:18:24 +02:00
|
|
|
// free the current string, before assigning the new one
|
2017-09-17 12:50:09 +02:00
|
|
|
if (name_len == name_is_owned) delete[] name;
|
2019-03-21 13:15:23 +01:00
|
|
|
if (n.empty())
|
2013-10-05 06:18:24 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(borrow_string == false);
|
2016-06-20 17:32:06 +02:00
|
|
|
name = nullptr;
|
2013-10-05 06:18:24 +02:00
|
|
|
}
|
|
|
|
else if (borrow_string)
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2019-03-21 13:15:23 +01:00
|
|
|
// we have limited space in the length field. truncate string
|
|
|
|
// if it's too long
|
|
|
|
if (n.size() >= name_is_owned) n = n.substr(name_is_owned - 1);
|
|
|
|
|
|
|
|
name = n.data();
|
|
|
|
name_len = aux::numeric_cast<std::uint64_t>(n.size());
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-05 06:18:24 +02:00
|
|
|
name = allocate_string_copy(n);
|
|
|
|
name_len = name_is_owned;
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 13:04:53 +02:00
|
|
|
string_view internal_file_entry::filename() const
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2016-12-11 02:51:55 +01:00
|
|
|
if (name_len != name_is_owned) return {name, std::size_t(name_len)};
|
2016-08-13 13:04:53 +02:00
|
|
|
return name ? string_view(name) : string_view();
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 20:03:17 +01:00
|
|
|
void file_storage::apply_pointer_offset(std::ptrdiff_t const off)
|
2014-12-17 06:33:16 +01:00
|
|
|
{
|
2016-11-27 14:46:53 +01:00
|
|
|
for (auto& f : m_files)
|
2014-12-17 06:33:16 +01:00
|
|
|
{
|
2016-11-27 14:46:53 +01:00
|
|
|
if (f.name_len == internal_file_entry::name_is_owned) continue;
|
|
|
|
f.name += off;
|
2014-12-17 06:33:16 +01:00
|
|
|
}
|
|
|
|
|
2016-11-27 14:46:53 +01:00
|
|
|
for (auto& h : m_file_hashes)
|
2014-12-17 06:33:16 +01:00
|
|
|
{
|
2016-11-27 14:46:53 +01:00
|
|
|
if (h == nullptr) continue;
|
|
|
|
h += off;
|
2014-12-17 06:33:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2015-01-21 16:46:12 +01:00
|
|
|
|
2017-09-02 23:58:36 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-21 06:30:34 +02:00
|
|
|
void file_storage::add_file(file_entry const& fe, char const* filehash)
|
2015-01-21 16:46:12 +01:00
|
|
|
{
|
2017-08-03 11:31:03 +02:00
|
|
|
file_flags_t flags = {};
|
2015-01-21 16:46:12 +01:00
|
|
|
if (fe.pad_file) flags |= file_storage::flag_pad_file;
|
|
|
|
if (fe.hidden_attribute) flags |= file_storage::flag_hidden;
|
|
|
|
if (fe.executable_attribute) flags |= file_storage::flag_executable;
|
|
|
|
if (fe.symlink_attribute) flags |= file_storage::flag_symlink;
|
|
|
|
|
2017-09-02 23:58:36 +02:00
|
|
|
add_file_borrow({}, fe.path, fe.size, flags, filehash, fe.mtime
|
2015-01-21 16:46:12 +01:00
|
|
|
, fe.symlink_path);
|
|
|
|
}
|
|
|
|
|
2008-11-30 09:12:26 +01:00
|
|
|
void file_storage::set_name(std::wstring const& n)
|
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
m_name = wchar_utf8(n);
|
2008-11-30 09:12:26 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void file_storage::rename_file_deprecated(file_index_t index, std::wstring const& new_filename)
|
2008-11-30 09:12:26 +01:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2016-09-25 04:16:10 +02:00
|
|
|
update_path_index(m_files[index], wchar_utf8(new_filename));
|
2008-11-30 09:12:26 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
void file_storage::add_file(std::wstring const& file, std::int64_t file_size
|
2017-08-03 11:31:03 +02:00
|
|
|
, file_flags_t const file_flags, std::time_t mtime, string_view symlink_path)
|
2009-03-31 10:12:35 +02:00
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
add_file(wchar_utf8(file), file_size, file_flags, mtime, symlink_path);
|
2009-03-31 10:12:35 +02:00
|
|
|
}
|
2014-07-05 16:10:25 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void file_storage::rename_file(file_index_t index, std::wstring const& new_filename)
|
2014-07-05 16:10:25 +02:00
|
|
|
{
|
|
|
|
rename_file_deprecated(index, new_filename);
|
|
|
|
}
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2009-03-31 10:12:35 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void file_storage::rename_file(file_index_t const index
|
|
|
|
, std::string const& new_filename)
|
2009-03-31 10:12:35 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2015-02-17 03:08:47 +01:00
|
|
|
update_path_index(m_files[index], new_filename);
|
2009-03-31 10:12:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-06-18 20:01:38 +02:00
|
|
|
file_storage::iterator file_storage::file_at_offset_deprecated(std::int64_t offset) const
|
2009-07-04 06:58:24 +02:00
|
|
|
{
|
|
|
|
// find the file iterator and file offset
|
2010-11-29 06:44:29 +01:00
|
|
|
internal_file_entry target;
|
2017-02-07 06:22:30 +01:00
|
|
|
target.offset = aux::numeric_cast<std::uint64_t>(offset);
|
2009-07-04 06:58:24 +02:00
|
|
|
TORRENT_ASSERT(!compare_file_offset(target, m_files.front()));
|
|
|
|
|
2016-09-18 06:24:20 +02:00
|
|
|
auto file_iter = std::upper_bound(
|
2013-10-14 04:25:21 +02:00
|
|
|
begin_deprecated(), end_deprecated(), target, compare_file_offset);
|
2009-07-04 06:58:24 +02:00
|
|
|
|
2013-10-14 04:25:21 +02:00
|
|
|
TORRENT_ASSERT(file_iter != begin_deprecated());
|
2009-07-04 06:58:24 +02:00
|
|
|
--file_iter;
|
|
|
|
return file_iter;
|
|
|
|
}
|
2013-10-14 04:25:21 +02:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
file_storage::iterator file_storage::file_at_offset(std::int64_t offset) const
|
2013-10-14 04:25:21 +02:00
|
|
|
{
|
|
|
|
return file_at_offset_deprecated(offset);
|
|
|
|
}
|
2013-08-12 09:30:57 +02:00
|
|
|
#endif
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
file_index_t file_storage::file_index_at_offset(std::int64_t const offset) const
|
2013-08-12 09:30:57 +02:00
|
|
|
{
|
2017-08-03 10:38:58 +02:00
|
|
|
TORRENT_ASSERT_PRECOND(offset >= 0);
|
|
|
|
TORRENT_ASSERT_PRECOND(offset < m_total_size);
|
2013-08-12 09:30:57 +02:00
|
|
|
// find the file iterator and file offset
|
|
|
|
internal_file_entry target;
|
2017-02-07 06:22:30 +01:00
|
|
|
target.offset = aux::numeric_cast<std::uint64_t>(offset);
|
2013-08-12 09:30:57 +02:00
|
|
|
TORRENT_ASSERT(!compare_file_offset(target, m_files.front()));
|
|
|
|
|
2016-09-18 06:24:20 +02:00
|
|
|
auto file_iter = std::upper_bound(
|
2013-08-12 09:30:57 +02:00
|
|
|
m_files.begin(), m_files.end(), target, compare_file_offset);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(file_iter != m_files.begin());
|
|
|
|
--file_iter;
|
2016-12-22 16:42:33 +01:00
|
|
|
return file_index_t(int(file_iter - m_files.begin()));
|
2013-08-12 09:30:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
char const* file_storage::file_name_ptr(file_index_t const index) const
|
2013-08-12 09:30:57 +02:00
|
|
|
{
|
|
|
|
return m_files[index].name;
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
int file_storage::file_name_len(file_index_t const index) const
|
2013-08-12 09:30:57 +02:00
|
|
|
{
|
2013-10-05 06:18:24 +02:00
|
|
|
if (m_files[index].name_len == internal_file_entry::name_is_owned)
|
|
|
|
return -1;
|
2013-08-12 09:30:57 +02:00
|
|
|
return m_files[index].name_len;
|
|
|
|
}
|
2009-07-04 06:58:24 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::vector<file_slice> file_storage::map_block(piece_index_t const piece
|
2016-09-21 07:06:07 +02:00
|
|
|
, std::int64_t const offset, int size) const
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2017-08-03 10:38:58 +02:00
|
|
|
TORRENT_ASSERT_PRECOND(piece >= piece_index_t{0});
|
|
|
|
TORRENT_ASSERT_PRECOND(piece < end_piece());
|
2014-03-23 08:40:43 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(num_files() > 0);
|
2008-05-28 10:44:40 +02:00
|
|
|
std::vector<file_slice> ret;
|
|
|
|
|
2009-01-14 07:55:28 +01:00
|
|
|
if (m_files.empty()) return ret;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
// find the file iterator and file offset
|
2010-11-29 06:44:29 +01:00
|
|
|
internal_file_entry target;
|
2017-02-07 06:22:30 +01:00
|
|
|
target.offset = aux::numeric_cast<std::uint64_t>(static_cast<int>(piece) * std::int64_t(m_piece_length) + offset);
|
|
|
|
TORRENT_ASSERT_PRECOND(std::int64_t(target.offset) + size <= m_total_size);
|
2009-01-14 07:55:28 +01:00
|
|
|
TORRENT_ASSERT(!compare_file_offset(target, m_files.front()));
|
2009-01-14 02:12:36 +01:00
|
|
|
|
2016-01-21 23:46:03 +01:00
|
|
|
// in case the size is past the end, fix it up
|
2017-02-07 06:22:30 +01:00
|
|
|
if (std::int64_t(target.offset) + size > m_total_size)
|
|
|
|
size = aux::numeric_cast<int>(m_total_size - std::int64_t(target.offset));
|
2016-01-21 23:46:03 +01:00
|
|
|
|
2016-09-18 06:24:20 +02:00
|
|
|
auto file_iter = std::upper_bound(
|
2013-08-12 09:30:57 +02:00
|
|
|
m_files.begin(), m_files.end(), target, compare_file_offset);
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2013-08-12 09:30:57 +02:00
|
|
|
TORRENT_ASSERT(file_iter != m_files.begin());
|
2009-01-14 07:55:28 +01:00
|
|
|
--file_iter;
|
2009-01-14 02:12:36 +01:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_offset = target.offset - file_iter->offset;
|
2009-01-14 02:12:36 +01:00
|
|
|
for (; size > 0; file_offset -= file_iter->size, ++file_iter)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2013-08-12 09:30:57 +02:00
|
|
|
TORRENT_ASSERT(file_iter != m_files.end());
|
2016-06-18 20:01:38 +02:00
|
|
|
if (file_offset < std::int64_t(file_iter->size))
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2018-01-11 01:35:15 +01:00
|
|
|
file_slice f{};
|
2016-12-22 16:42:33 +01:00
|
|
|
f.file_index = file_index_t(int(file_iter - m_files.begin()));
|
2017-05-04 15:22:17 +02:00
|
|
|
f.offset = file_offset;
|
2017-02-07 06:22:30 +01:00
|
|
|
f.size = std::min(std::int64_t(file_iter->size) - file_offset, std::int64_t(size));
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(f.size <= size);
|
|
|
|
size -= int(f.size);
|
2008-05-28 10:44:40 +02:00
|
|
|
file_offset += f.size;
|
|
|
|
ret.push_back(f);
|
|
|
|
}
|
2015-08-08 08:33:54 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
TORRENT_ASSERT(size >= 0);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2010-11-29 06:44:29 +01:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2010-11-29 06:44:29 +01:00
|
|
|
file_entry file_storage::at(int index) const
|
2015-04-25 06:22:51 +02:00
|
|
|
{
|
|
|
|
return at_deprecated(index);
|
|
|
|
}
|
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
internal_file_entry const& file_storage::internal_at(int const index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < int(m_files.size()));
|
|
|
|
return m_files[file_index_t(index)];
|
|
|
|
}
|
|
|
|
|
2015-04-25 06:22:51 +02:00
|
|
|
file_entry file_storage::at_deprecated(int index) const
|
2010-11-29 06:44:29 +01:00
|
|
|
{
|
2014-03-23 08:40:43 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= 0 && index < int(m_files.size()));
|
2010-11-29 06:44:29 +01:00
|
|
|
file_entry ret;
|
|
|
|
internal_file_entry const& ife = m_files[index];
|
2013-08-12 09:30:57 +02:00
|
|
|
ret.path = file_path(index);
|
2010-11-29 06:44:29 +01:00
|
|
|
ret.offset = ife.offset;
|
|
|
|
ret.size = ife.size;
|
2013-08-12 09:30:57 +02:00
|
|
|
ret.mtime = mtime(index);
|
2010-11-29 06:44:29 +01:00
|
|
|
ret.pad_file = ife.pad_file;
|
|
|
|
ret.hidden_attribute = ife.hidden_attribute;
|
|
|
|
ret.executable_attribute = ife.executable_attribute;
|
|
|
|
ret.symlink_attribute = ife.symlink_attribute;
|
2013-11-17 09:02:16 +01:00
|
|
|
if (ife.symlink_index != internal_file_entry::not_a_symlink)
|
|
|
|
ret.symlink_path = symlink(index);
|
2013-08-12 09:30:57 +02:00
|
|
|
ret.filehash = hash(index);
|
2010-11-29 06:44:29 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2010-11-29 06:44:29 +01:00
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
int file_storage::num_files() const noexcept
|
|
|
|
{ return int(m_files.size()); }
|
|
|
|
|
|
|
|
// returns the index of the one-past-end file in the file storage
|
|
|
|
file_index_t file_storage::end_file() const noexcept
|
|
|
|
{ return m_files.end_index(); }
|
|
|
|
|
|
|
|
file_index_t file_storage::last_file() const noexcept
|
|
|
|
{ return --m_files.end_index(); }
|
|
|
|
|
|
|
|
index_range<file_index_t> file_storage::file_range() const noexcept
|
2018-07-07 20:47:50 +02:00
|
|
|
{ return m_files.range(); }
|
2018-06-02 19:29:40 +02:00
|
|
|
|
|
|
|
index_range<piece_index_t> file_storage::piece_range() const noexcept
|
2018-07-07 20:47:50 +02:00
|
|
|
{ return {piece_index_t{0}, end_piece()}; }
|
2018-06-02 19:29:40 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
peer_request file_storage::map_file(file_index_t const file_index
|
2017-09-25 00:40:52 +02:00
|
|
|
, std::int64_t const file_offset, int const size) const
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(file_index < end_file());
|
2014-04-16 09:12:30 +02:00
|
|
|
TORRENT_ASSERT(m_num_pieces >= 0);
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2018-01-11 01:35:15 +01:00
|
|
|
peer_request ret{};
|
2016-12-22 16:42:33 +01:00
|
|
|
if (file_index >= end_file())
|
2013-02-28 04:29:33 +01:00
|
|
|
{
|
2018-07-07 20:47:50 +02:00
|
|
|
ret.piece = end_piece();
|
2013-02-28 04:29:33 +01:00
|
|
|
ret.start = 0;
|
|
|
|
ret.length = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-21 07:06:07 +02:00
|
|
|
std::int64_t const offset = file_offset + this->file_offset(file_index);
|
2013-02-28 04:29:33 +01:00
|
|
|
|
|
|
|
if (offset >= total_size())
|
|
|
|
{
|
2018-07-07 20:47:50 +02:00
|
|
|
ret.piece = end_piece();
|
2013-02-28 04:29:33 +01:00
|
|
|
ret.start = 0;
|
|
|
|
ret.length = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
ret.piece = piece_index_t(int(offset / piece_length()));
|
2013-02-28 04:29:33 +01:00
|
|
|
ret.start = int(offset % piece_length());
|
|
|
|
ret.length = size;
|
|
|
|
if (offset + size > total_size())
|
2014-05-10 05:23:05 +02:00
|
|
|
ret.length = int(total_size() - offset);
|
2013-02-28 04:29:33 +01:00
|
|
|
}
|
2008-05-28 10:44:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
void file_storage::add_file(std::string const& path, std::int64_t file_size
|
2017-08-03 11:31:03 +02:00
|
|
|
, file_flags_t const file_flags, std::time_t mtime, string_view symlink_path)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2017-09-02 23:58:36 +02:00
|
|
|
add_file_borrow({}, path, file_size, file_flags, nullptr, mtime
|
2015-01-21 16:46:12 +01:00
|
|
|
, symlink_path);
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
|
2017-09-02 23:58:36 +02:00
|
|
|
void file_storage::add_file_borrow(string_view filename
|
2016-08-21 22:28:49 +02:00
|
|
|
, std::string const& path, std::int64_t const file_size
|
2017-08-03 11:31:03 +02:00
|
|
|
, file_flags_t const file_flags, char const* filehash
|
2016-12-22 05:23:45 +01:00
|
|
|
, std::int64_t const mtime, string_view symlink_path)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2015-01-21 16:46:12 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(file_size >= 0);
|
2017-10-01 04:30:32 +02:00
|
|
|
TORRENT_ASSERT_PRECOND(!is_complete(filename));
|
2015-01-21 16:46:12 +01:00
|
|
|
if (!has_parent_path(path))
|
2009-01-11 23:27:43 +01:00
|
|
|
{
|
|
|
|
// you have already added at least one file with a
|
|
|
|
// path to the file (branch_path), which means that
|
|
|
|
// all the other files need to be in the same top
|
|
|
|
// directory as the first file.
|
2014-03-23 08:40:43 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(m_files.empty());
|
2015-01-21 16:46:12 +01:00
|
|
|
m_name = path;
|
2009-01-11 23:27:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_files.empty())
|
2019-03-20 09:36:37 +01:00
|
|
|
m_name = lsplit_path(path).first.to_string();
|
2009-01-11 23:27:43 +01:00
|
|
|
}
|
2015-02-17 03:08:47 +01:00
|
|
|
|
|
|
|
// this is poor-man's emplace_back()
|
|
|
|
m_files.resize(m_files.size() + 1);
|
2010-11-29 06:44:29 +01:00
|
|
|
internal_file_entry& e = m_files.back();
|
2015-01-21 16:46:12 +01:00
|
|
|
|
2015-02-17 03:08:47 +01:00
|
|
|
// the last argument specified whether the function should also set
|
|
|
|
// the filename. If it does, it will copy the leaf filename from path.
|
2019-03-20 09:36:37 +01:00
|
|
|
// if filename is empty, we should copy it. If it isn't, we're borrowing
|
2015-02-17 03:08:47 +01:00
|
|
|
// it and we can save the copy by setting it after this call to
|
|
|
|
// update_path_index().
|
2017-09-02 23:58:36 +02:00
|
|
|
update_path_index(e, path, filename.empty());
|
2015-01-21 16:46:12 +01:00
|
|
|
|
2019-03-20 09:36:37 +01:00
|
|
|
// filename is allowed to be empty, in which case we just use path
|
2017-09-02 23:58:36 +02:00
|
|
|
if (!filename.empty())
|
2019-03-21 13:15:23 +01:00
|
|
|
e.set_name(filename, true);
|
2015-01-21 16:46:12 +01:00
|
|
|
|
2017-02-07 06:22:30 +01:00
|
|
|
e.size = aux::numeric_cast<std::uint64_t>(file_size);
|
|
|
|
e.offset = aux::numeric_cast<std::uint64_t>(m_total_size);
|
2017-08-03 11:31:03 +02:00
|
|
|
e.pad_file = bool(file_flags & file_storage::flag_pad_file);
|
|
|
|
e.hidden_attribute = bool(file_flags & file_storage::flag_hidden);
|
|
|
|
e.executable_attribute = bool(file_flags & file_storage::flag_executable);
|
|
|
|
e.symlink_attribute = bool(file_flags & file_storage::flag_symlink);
|
2015-01-21 16:46:12 +01:00
|
|
|
|
2010-11-15 06:10:36 +01:00
|
|
|
if (filehash)
|
|
|
|
{
|
2010-11-25 00:49:22 +01:00
|
|
|
if (m_file_hashes.size() < m_files.size()) m_file_hashes.resize(m_files.size());
|
2016-12-22 16:42:33 +01:00
|
|
|
m_file_hashes[last_file()] = filehash;
|
2010-11-15 06:10:36 +01:00
|
|
|
}
|
2015-01-21 16:46:12 +01:00
|
|
|
if (!symlink_path.empty()
|
|
|
|
&& m_symlinks.size() < internal_file_entry::not_a_symlink - 1)
|
2010-11-15 06:10:36 +01:00
|
|
|
{
|
|
|
|
e.symlink_index = m_symlinks.size();
|
2016-08-13 13:04:53 +02:00
|
|
|
m_symlinks.emplace_back(symlink_path.to_string());
|
2010-11-15 06:10:36 +01:00
|
|
|
}
|
2015-01-21 16:46:12 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
e.symlink_attribute = false;
|
|
|
|
}
|
|
|
|
if (mtime)
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
|
|
|
if (m_mtime.size() < m_files.size()) m_mtime.resize(m_files.size());
|
2016-12-23 01:41:13 +01:00
|
|
|
m_mtime[last_file()] = std::time_t(mtime);
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
2015-01-21 16:46:12 +01:00
|
|
|
|
|
|
|
m_total_size += e.size;
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
sha1_hash file_storage::hash(file_index_t const index) const
|
2012-03-26 18:07:55 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
if (index >= m_file_hashes.end_index()) return sha1_hash();
|
2012-03-26 18:07:55 +02:00
|
|
|
return sha1_hash(m_file_hashes[index]);
|
|
|
|
}
|
2016-08-13 13:04:53 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::string const& file_storage::symlink(file_index_t const index) const
|
2012-03-26 18:07:55 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2012-03-26 18:07:55 +02:00
|
|
|
internal_file_entry const& fe = m_files[index];
|
|
|
|
TORRENT_ASSERT(fe.symlink_index < int(m_symlinks.size()));
|
2019-07-22 00:50:57 +02:00
|
|
|
|
|
|
|
auto const& link = m_symlinks[fe.symlink_index];
|
|
|
|
|
|
|
|
// TODO: 3 this is a hack to retain ABI compatibility with 1.2.1
|
|
|
|
// in next major release, make this return by value
|
2019-08-12 16:25:00 +02:00
|
|
|
static std::string storage[4];
|
|
|
|
static std::atomic<size_t> counter{0};
|
|
|
|
std::string& ret = storage[(counter++) % 4];
|
2019-07-22 00:50:57 +02:00
|
|
|
ret.reserve(m_name.size() + link.size() + 1);
|
|
|
|
ret.assign(m_name);
|
|
|
|
append_path(ret, link);
|
|
|
|
return ret;
|
2012-03-26 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::time_t file_storage::mtime(file_index_t const index) const
|
2012-03-26 18:07:55 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
if (index >= m_mtime.end_index()) return 0;
|
2012-03-26 18:07:55 +02:00
|
|
|
return m_mtime[index];
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace {
|
|
|
|
|
2015-02-17 03:08:47 +01:00
|
|
|
template <class CRC>
|
2016-08-13 13:04:53 +02:00
|
|
|
void process_string_lowercase(CRC& crc, string_view str)
|
2015-02-17 03:08:47 +01:00
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
for (char const c : str)
|
2017-02-07 06:22:30 +01:00
|
|
|
crc.process_byte(to_lower(c) & 0xff);
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
2015-03-21 17:55:23 +01:00
|
|
|
|
|
|
|
template <class CRC>
|
|
|
|
void process_path_lowercase(
|
2016-06-18 20:01:38 +02:00
|
|
|
std::unordered_set<std::uint32_t>& table
|
2019-03-21 13:15:23 +01:00
|
|
|
, CRC crc, string_view str)
|
2015-03-21 17:55:23 +01:00
|
|
|
{
|
2019-03-21 13:15:23 +01:00
|
|
|
if (str.empty()) return;
|
|
|
|
for (char const c : str)
|
2015-03-21 17:55:23 +01:00
|
|
|
{
|
2019-03-21 13:15:23 +01:00
|
|
|
if (c == TORRENT_SEPARATOR)
|
2015-03-21 17:55:23 +01:00
|
|
|
table.insert(crc.checksum());
|
2019-03-21 13:15:23 +01:00
|
|
|
crc.process_byte(to_lower(c) & 0xff);
|
2015-03-21 17:55:23 +01:00
|
|
|
}
|
|
|
|
table.insert(crc.checksum());
|
|
|
|
}
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
|
|
|
|
2015-03-21 17:55:23 +01:00
|
|
|
void file_storage::all_path_hashes(
|
2016-06-18 20:01:38 +02:00
|
|
|
std::unordered_set<std::uint32_t>& table) const
|
2015-02-17 03:08:47 +01:00
|
|
|
{
|
|
|
|
boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc;
|
|
|
|
|
2015-03-21 17:55:23 +01:00
|
|
|
if (!m_name.empty())
|
2015-02-17 03:08:47 +01:00
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, m_name);
|
2016-12-11 02:51:55 +01:00
|
|
|
TORRENT_ASSERT(m_name[m_name.size() - 1] != TORRENT_SEPARATOR);
|
2015-02-17 03:08:47 +01:00
|
|
|
crc.process_byte(TORRENT_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2017-01-24 20:03:17 +01:00
|
|
|
for (auto const& p : m_paths)
|
2019-03-21 13:15:23 +01:00
|
|
|
process_path_lowercase(table, crc, p);
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::uint32_t file_storage::file_path_hash(file_index_t const index
|
2015-02-17 03:08:47 +01:00
|
|
|
, std::string const& save_path) const
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2015-02-17 03:08:47 +01:00
|
|
|
internal_file_entry const& fe = m_files[index];
|
|
|
|
|
|
|
|
boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc;
|
|
|
|
|
|
|
|
if (fe.path_index == -2)
|
|
|
|
{
|
|
|
|
// -2 means this is an absolute path filename
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, fe.filename());
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
|
|
|
else if (fe.path_index == -1)
|
|
|
|
{
|
|
|
|
// -1 means no path
|
|
|
|
if (!save_path.empty())
|
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, save_path);
|
2016-12-11 02:51:55 +01:00
|
|
|
TORRENT_ASSERT(save_path[save_path.size() - 1] != TORRENT_SEPARATOR);
|
2015-02-17 03:08:47 +01:00
|
|
|
crc.process_byte(TORRENT_SEPARATOR);
|
|
|
|
}
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, fe.filename());
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
|
|
|
else if (fe.no_root_dir)
|
|
|
|
{
|
|
|
|
if (!save_path.empty())
|
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, save_path);
|
2016-12-11 02:51:55 +01:00
|
|
|
TORRENT_ASSERT(save_path[save_path.size() - 1] != TORRENT_SEPARATOR);
|
2015-02-17 03:08:47 +01:00
|
|
|
crc.process_byte(TORRENT_SEPARATOR);
|
|
|
|
}
|
|
|
|
std::string const& p = m_paths[fe.path_index];
|
|
|
|
if (!p.empty())
|
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, p);
|
2016-12-11 02:51:55 +01:00
|
|
|
TORRENT_ASSERT(p[p.size() - 1] != TORRENT_SEPARATOR);
|
2015-02-17 03:08:47 +01:00
|
|
|
crc.process_byte(TORRENT_SEPARATOR);
|
|
|
|
}
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, fe.filename());
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!save_path.empty())
|
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, save_path);
|
2016-12-11 02:51:55 +01:00
|
|
|
TORRENT_ASSERT(save_path[save_path.size() - 1] != TORRENT_SEPARATOR);
|
2015-02-17 03:08:47 +01:00
|
|
|
crc.process_byte(TORRENT_SEPARATOR);
|
|
|
|
}
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, m_name);
|
2015-02-17 03:08:47 +01:00
|
|
|
TORRENT_ASSERT(m_name.size() > 0);
|
2016-12-11 02:51:55 +01:00
|
|
|
TORRENT_ASSERT(m_name[m_name.size() - 1] != TORRENT_SEPARATOR);
|
2015-02-17 03:08:47 +01:00
|
|
|
crc.process_byte(TORRENT_SEPARATOR);
|
|
|
|
|
|
|
|
std::string const& p = m_paths[fe.path_index];
|
|
|
|
if (!p.empty())
|
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, p);
|
2015-02-17 03:08:47 +01:00
|
|
|
TORRENT_ASSERT(p.size() > 0);
|
2016-12-11 02:51:55 +01:00
|
|
|
TORRENT_ASSERT(p[p.size() - 1] != TORRENT_SEPARATOR);
|
2015-02-17 03:08:47 +01:00
|
|
|
crc.process_byte(TORRENT_SEPARATOR);
|
|
|
|
}
|
2016-08-13 13:04:53 +02:00
|
|
|
process_string_lowercase(crc, fe.filename());
|
2015-02-17 03:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return crc.checksum();
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::string file_storage::file_path(file_index_t const index, std::string const& save_path) const
|
2012-03-26 18:07:55 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2012-03-26 18:07:55 +02:00
|
|
|
internal_file_entry const& fe = m_files[index];
|
2013-11-17 23:38:37 +01:00
|
|
|
|
2015-02-18 04:32:49 +01:00
|
|
|
std::string ret;
|
|
|
|
|
2013-11-17 23:38:37 +01:00
|
|
|
// -2 means this is an absolute path filename
|
2015-02-18 04:32:49 +01:00
|
|
|
if (fe.path_index == -2)
|
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
ret = fe.filename().to_string();
|
2015-02-18 04:32:49 +01:00
|
|
|
}
|
|
|
|
else if (fe.path_index == -1)
|
|
|
|
{
|
|
|
|
// -1 means no path
|
2016-08-13 13:04:53 +02:00
|
|
|
ret.reserve(save_path.size() + fe.filename().size() + 1);
|
2015-02-18 04:32:49 +01:00
|
|
|
ret.assign(save_path);
|
2016-08-13 13:04:53 +02:00
|
|
|
append_path(ret, fe.filename());
|
2015-02-18 04:32:49 +01:00
|
|
|
}
|
|
|
|
else if (fe.no_root_dir)
|
|
|
|
{
|
|
|
|
std::string const& p = m_paths[fe.path_index];
|
2013-11-17 23:38:37 +01:00
|
|
|
|
2016-08-13 13:04:53 +02:00
|
|
|
ret.reserve(save_path.size() + p.size() + fe.filename().size() + 2);
|
2015-02-18 04:32:49 +01:00
|
|
|
ret.assign(save_path);
|
|
|
|
append_path(ret, p);
|
2016-08-13 13:04:53 +02:00
|
|
|
append_path(ret, fe.filename());
|
2015-02-18 04:32:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string const& p = m_paths[fe.path_index];
|
2013-11-17 23:38:37 +01:00
|
|
|
|
2016-08-13 13:04:53 +02:00
|
|
|
ret.reserve(save_path.size() + m_name.size() + p.size() + fe.filename().size() + 3);
|
2015-02-18 04:32:49 +01:00
|
|
|
ret.assign(save_path);
|
|
|
|
append_path(ret, m_name);
|
|
|
|
append_path(ret, p);
|
2016-08-13 13:04:53 +02:00
|
|
|
append_path(ret, fe.filename());
|
2015-02-18 04:32:49 +01:00
|
|
|
}
|
2013-11-17 23:38:37 +01:00
|
|
|
|
2015-02-18 04:32:49 +01:00
|
|
|
// a single return statement, just to make NRVO more likely to kick in
|
|
|
|
return ret;
|
2012-03-26 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
std::string file_storage::internal_file_path(file_index_t const index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
|
|
|
internal_file_entry const& fe = m_files[index];
|
|
|
|
|
|
|
|
if (fe.path_index >= 0)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::string const& p = m_paths[fe.path_index];
|
|
|
|
ret.reserve(p.size() + fe.filename().size() + 2);
|
|
|
|
append_path(ret, p);
|
|
|
|
append_path(ret, fe.filename());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return fe.filename().to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
string_view file_storage::file_name(file_index_t const index) const
|
2012-08-31 04:31:37 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2012-08-31 04:31:37 +02:00
|
|
|
internal_file_entry const& fe = m_files[index];
|
|
|
|
return fe.filename();
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::int64_t file_storage::file_size(file_index_t const index) const
|
2012-03-26 18:07:55 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2012-03-26 18:07:55 +02:00
|
|
|
return m_files[index].size;
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
bool file_storage::pad_file_at(file_index_t const index) const
|
2012-09-30 22:55:12 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2012-09-30 22:55:12 +02:00
|
|
|
return m_files[index].pad_file;
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::int64_t file_storage::file_offset(file_index_t const index) const
|
2012-09-29 19:46:41 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2012-09-29 19:46:41 +02:00
|
|
|
return m_files[index].offset;
|
|
|
|
}
|
|
|
|
|
2017-08-03 11:31:03 +02:00
|
|
|
file_flags_t file_storage::file_flags(file_index_t const index) const
|
2013-08-12 09:30:57 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2013-08-12 09:30:57 +02:00
|
|
|
internal_file_entry const& fe = m_files[index];
|
2017-08-03 11:31:03 +02:00
|
|
|
return (fe.pad_file ? file_storage::flag_pad_file : file_flags_t{})
|
|
|
|
| (fe.hidden_attribute ? file_storage::flag_hidden : file_flags_t{})
|
|
|
|
| (fe.executable_attribute ? file_storage::flag_executable : file_flags_t{})
|
|
|
|
| (fe.symlink_attribute ? file_storage::flag_symlink : file_flags_t{});
|
2013-08-12 09:30:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
bool file_storage::file_absolute_path(file_index_t const index) const
|
2016-05-01 18:23:02 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= file_index_t(0) && index < end_file());
|
2016-05-01 18:23:02 +02:00
|
|
|
internal_file_entry const& fe = m_files[index];
|
|
|
|
return fe.path_index == -2;
|
|
|
|
}
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2010-11-29 06:44:29 +01:00
|
|
|
sha1_hash file_storage::hash(internal_file_entry const& fe) const
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2016-12-09 14:23:54 +01:00
|
|
|
int index = int(&fe - &m_files[0]);
|
2016-07-09 22:26:26 +02:00
|
|
|
if (index >= int(m_file_hashes.size())) return sha1_hash(nullptr);
|
2010-11-25 00:49:22 +01:00
|
|
|
return sha1_hash(m_file_hashes[index]);
|
|
|
|
}
|
2016-05-13 06:16:51 +02:00
|
|
|
|
2010-11-29 06:44:29 +01:00
|
|
|
std::string const& file_storage::symlink(internal_file_entry const& fe) const
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2014-03-23 08:40:43 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(fe.symlink_index < int(m_symlinks.size()));
|
2019-03-14 10:02:41 +01:00
|
|
|
return m_symlinks[fe.symlink_index];
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
|
2016-12-11 02:51:55 +01:00
|
|
|
std::time_t file_storage::mtime(internal_file_entry const& fe) const
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2016-12-09 14:23:54 +01:00
|
|
|
int index = int(&fe - &m_files[0]);
|
2011-02-14 02:59:01 +01:00
|
|
|
if (index >= int(m_mtime.size())) return 0;
|
2010-11-25 00:49:22 +01:00
|
|
|
return m_mtime[index];
|
|
|
|
}
|
|
|
|
|
2010-11-29 06:44:29 +01:00
|
|
|
int file_storage::file_index(internal_file_entry const& fe) const
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2016-12-09 14:23:54 +01:00
|
|
|
int index = int(&fe - &m_files[0]);
|
2014-03-23 08:40:43 +01:00
|
|
|
TORRENT_ASSERT_PRECOND(index >= 0 && index < int(m_files.size()));
|
2010-11-25 00:49:22 +01:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2013-11-17 09:02:16 +01:00
|
|
|
std::string file_storage::file_path(internal_file_entry const& fe
|
|
|
|
, std::string const& save_path) const
|
2012-03-26 18:07:55 +02:00
|
|
|
{
|
2016-12-09 14:23:54 +01:00
|
|
|
int const index = int(&fe - &m_files[0]);
|
2015-04-21 06:30:34 +02:00
|
|
|
return file_path(index, save_path);
|
2012-03-26 18:07:55 +02:00
|
|
|
}
|
|
|
|
|
2012-08-31 04:31:37 +02:00
|
|
|
std::string file_storage::file_name(internal_file_entry const& fe) const
|
|
|
|
{
|
2016-08-13 13:04:53 +02:00
|
|
|
return fe.filename().to_string();
|
2012-08-31 04:31:37 +02:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_storage::file_size(internal_file_entry const& fe) const
|
2012-03-26 18:07:55 +02:00
|
|
|
{
|
|
|
|
return fe.size;
|
|
|
|
}
|
|
|
|
|
2012-09-30 22:55:12 +02:00
|
|
|
bool file_storage::pad_file_at(internal_file_entry const& fe) const
|
|
|
|
{
|
|
|
|
return fe.pad_file;
|
|
|
|
}
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_storage::file_offset(internal_file_entry const& fe) const
|
2012-09-29 19:46:41 +02:00
|
|
|
{
|
|
|
|
return fe.offset;
|
|
|
|
}
|
2013-08-12 09:30:57 +02:00
|
|
|
|
|
|
|
file_entry file_storage::at(file_storage::iterator i) const
|
2016-12-09 14:23:54 +01:00
|
|
|
{ return at_deprecated(int(i - m_files.begin())); }
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2012-09-29 19:46:41 +02:00
|
|
|
|
2016-09-21 07:06:07 +02:00
|
|
|
void file_storage::reorder_file(int const index, int const dst)
|
2010-11-25 00:49:22 +01:00
|
|
|
{
|
2012-08-12 21:16:20 +02:00
|
|
|
TORRENT_ASSERT(index < int(m_files.size()));
|
|
|
|
TORRENT_ASSERT(dst < int(m_files.size()));
|
|
|
|
TORRENT_ASSERT(dst < index);
|
|
|
|
|
2012-08-12 21:46:24 +02:00
|
|
|
std::iter_swap(m_files.begin() + index, m_files.begin() + dst);
|
2010-11-25 00:49:22 +01:00
|
|
|
if (!m_mtime.empty())
|
|
|
|
{
|
2012-08-12 21:16:20 +02:00
|
|
|
TORRENT_ASSERT(m_mtime.size() == m_files.size());
|
2017-01-24 20:03:17 +01:00
|
|
|
if (int(m_mtime.size()) < index) m_mtime.resize(index + 1, 0);
|
2012-08-12 21:46:24 +02:00
|
|
|
std::iter_swap(m_mtime.begin() + dst, m_mtime.begin() + index);
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
|
|
|
if (!m_file_hashes.empty())
|
|
|
|
{
|
2012-08-12 21:16:20 +02:00
|
|
|
TORRENT_ASSERT(m_file_hashes.size() == m_files.size());
|
2016-06-20 17:32:06 +02:00
|
|
|
if (int(m_file_hashes.size()) < index) m_file_hashes.resize(index + 1, nullptr);
|
2012-08-12 21:46:24 +02:00
|
|
|
std::iter_swap(m_file_hashes.begin() + dst, m_file_hashes.begin() + index);
|
2010-11-25 00:49:22 +01:00
|
|
|
}
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
2009-01-11 11:32:57 +01:00
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
void file_storage::swap(file_storage& ti) noexcept
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap(ti.m_files, m_files);
|
|
|
|
swap(ti.m_file_hashes, m_file_hashes);
|
|
|
|
swap(ti.m_symlinks, m_symlinks);
|
|
|
|
swap(ti.m_mtime, m_mtime);
|
|
|
|
swap(ti.m_paths, m_paths);
|
|
|
|
swap(ti.m_name, m_name);
|
|
|
|
swap(ti.m_total_size, m_total_size);
|
|
|
|
swap(ti.m_num_pieces, m_num_pieces);
|
|
|
|
swap(ti.m_piece_length, m_piece_length);
|
|
|
|
}
|
|
|
|
|
2016-09-18 06:24:20 +02:00
|
|
|
void file_storage::optimize(int const pad_file_limit, int alignment
|
|
|
|
, bool const tail_padding)
|
2009-01-11 11:32:57 +01:00
|
|
|
{
|
2014-06-18 08:22:01 +02:00
|
|
|
if (alignment == -1)
|
|
|
|
alignment = m_piece_length;
|
2009-01-11 11:32:57 +01:00
|
|
|
|
2016-09-18 06:24:20 +02:00
|
|
|
// TODO: padfiles should be removed
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t off = 0;
|
2009-01-11 11:32:57 +01:00
|
|
|
int padding_file = 0;
|
2016-09-21 07:06:07 +02:00
|
|
|
for (auto i = m_files.begin(); i != m_files.end(); ++i)
|
2009-01-11 11:32:57 +01:00
|
|
|
{
|
2014-06-18 08:22:01 +02:00
|
|
|
if ((off % alignment) == 0)
|
2011-03-22 03:27:54 +01:00
|
|
|
{
|
|
|
|
// this file position is aligned, pick the largest
|
2016-09-18 06:24:20 +02:00
|
|
|
// available file to put here. If we encounter a file whose size is
|
|
|
|
// divisible by `alignment`, we pick that immediately, since that
|
|
|
|
// will not affect whether we're at an aligned position and will
|
|
|
|
// improve packing of files
|
2016-09-21 07:06:07 +02:00
|
|
|
auto best_match = i;
|
2016-09-18 06:24:20 +02:00
|
|
|
for (auto k = i; k != m_files.end(); ++k)
|
|
|
|
{
|
|
|
|
// a file whose size fits the alignment always takes priority,
|
|
|
|
// since it will let us keep placing aligned files
|
2017-02-07 06:22:30 +01:00
|
|
|
if ((k->size % aux::numeric_cast<std::uint64_t>(alignment)) == 0)
|
2016-09-18 06:24:20 +02:00
|
|
|
{
|
|
|
|
best_match = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// otherwise, pick the largest file, to have as many bytes be
|
|
|
|
// aligned.
|
|
|
|
if (best_match->size < k->size) best_match = k;
|
|
|
|
}
|
2011-03-22 03:27:54 +01:00
|
|
|
|
|
|
|
if (best_match != i)
|
|
|
|
{
|
2016-12-09 14:23:54 +01:00
|
|
|
int const index = int(best_match - m_files.begin());
|
|
|
|
int const cur_index = int(i - m_files.begin());
|
2011-11-16 03:27:28 +01:00
|
|
|
reorder_file(index, cur_index);
|
|
|
|
i = m_files.begin() + cur_index;
|
2011-03-22 03:27:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (pad_file_limit >= 0
|
2016-06-18 20:01:38 +02:00
|
|
|
&& i->size > std::uint32_t(pad_file_limit)
|
2009-01-11 23:27:43 +01:00
|
|
|
&& i->pad_file == false)
|
2009-01-11 11:32:57 +01:00
|
|
|
{
|
|
|
|
// if we have pad files enabled, and this file is
|
|
|
|
// not piece-aligned and the file size exceeds the
|
2009-01-11 23:27:43 +01:00
|
|
|
// limit, and it's not a padding file itself.
|
|
|
|
// so add a padding file in front of it
|
2016-08-08 04:37:10 +02:00
|
|
|
int const pad_size = alignment - (off % alignment);
|
|
|
|
|
2009-01-11 11:32:57 +01:00
|
|
|
// find the largest file that fits in pad_size
|
2016-09-21 07:06:07 +02:00
|
|
|
auto best_match = m_files.end();
|
2009-01-11 11:32:57 +01:00
|
|
|
|
2014-06-18 08:22:01 +02:00
|
|
|
// if pad_file_limit is 0, it means all files are padded, there's
|
|
|
|
// no point in trying to find smaller files to use as filling
|
|
|
|
if (pad_file_limit > 0)
|
2009-01-11 11:32:57 +01:00
|
|
|
{
|
2016-09-21 07:06:07 +02:00
|
|
|
for (auto j = i + 1; j < m_files.end(); ++j)
|
2014-06-18 08:22:01 +02:00
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
if (j->size > std::uint32_t(pad_size)) continue;
|
2014-06-18 08:22:01 +02:00
|
|
|
if (best_match == m_files.end() || j->size > best_match->size)
|
|
|
|
best_match = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best_match != m_files.end())
|
|
|
|
{
|
|
|
|
// we found one
|
|
|
|
// We cannot have found i, because i->size > pad_file_limit
|
|
|
|
// which is forced to be no less than alignment. We only
|
|
|
|
// look for files <= pad_size, which never is greater than
|
|
|
|
// alignment
|
|
|
|
TORRENT_ASSERT(best_match != i);
|
2016-12-09 14:23:54 +01:00
|
|
|
int index = int(best_match - m_files.begin());
|
|
|
|
int cur_index = int(i - m_files.begin());
|
2014-06-18 08:22:01 +02:00
|
|
|
reorder_file(index, cur_index);
|
|
|
|
i = m_files.begin() + cur_index;
|
2017-02-07 06:22:30 +01:00
|
|
|
i->offset = aux::numeric_cast<std::uint64_t>(off);
|
2014-06-18 08:22:01 +02:00
|
|
|
off += i->size;
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-11 11:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// we could not find a file that fits in pad_size
|
|
|
|
// add a padding file
|
2011-02-21 07:57:03 +01:00
|
|
|
// note that i will be set to point to the
|
|
|
|
// new pad file. Once we're done adding it, we need
|
|
|
|
// to increment i to point to the current file again
|
2012-08-12 21:46:24 +02:00
|
|
|
// first add the pad file to the end of the file list
|
|
|
|
// then swap it in place. This minimizes the amount
|
|
|
|
// of copying of internal_file_entry, which is somewhat
|
|
|
|
// expensive (until we have move semantics)
|
2015-03-21 01:12:40 +01:00
|
|
|
add_pad_file(pad_size, i, off, padding_file);
|
2012-08-12 21:16:20 +02:00
|
|
|
|
2014-06-18 08:22:01 +02:00
|
|
|
TORRENT_ASSERT((off % alignment) == 0);
|
2012-08-12 23:18:38 +02:00
|
|
|
continue;
|
2009-01-11 11:32:57 +01:00
|
|
|
}
|
2017-02-07 06:22:30 +01:00
|
|
|
i->offset = aux::numeric_cast<std::uint64_t>(off);
|
2009-01-11 11:32:57 +01:00
|
|
|
off += i->size;
|
2015-03-21 01:12:40 +01:00
|
|
|
|
|
|
|
if (tail_padding
|
2016-06-18 20:01:38 +02:00
|
|
|
&& i->size > std::uint32_t(pad_file_limit)
|
2015-03-21 01:12:40 +01:00
|
|
|
&& (off % alignment) != 0)
|
|
|
|
{
|
|
|
|
// skip the file we just put in place, so we put the pad
|
|
|
|
// file after it
|
|
|
|
++i;
|
|
|
|
|
|
|
|
// tail-padding is enabled, and the offset after this file is not
|
2016-09-18 06:24:20 +02:00
|
|
|
// aligned. The last file must be padded too, in order to match an
|
|
|
|
// equivalent tail-padded file.
|
2015-03-21 01:12:40 +01:00
|
|
|
add_pad_file(alignment - (off % alignment), i, off, padding_file);
|
|
|
|
|
|
|
|
TORRENT_ASSERT((off % alignment) == 0);
|
2016-09-18 06:24:20 +02:00
|
|
|
|
|
|
|
if (i == m_files.end()) break;
|
2015-03-21 01:12:40 +01:00
|
|
|
}
|
2009-01-11 11:32:57 +01:00
|
|
|
}
|
|
|
|
m_total_size = off;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-09-21 07:06:07 +02:00
|
|
|
void file_storage::add_pad_file(int const size
|
2015-03-21 01:12:40 +01:00
|
|
|
, std::vector<internal_file_entry>::iterator& i
|
2016-06-18 20:01:38 +02:00
|
|
|
, std::int64_t& offset
|
2015-03-21 01:12:40 +01:00
|
|
|
, int& pad_file_counter)
|
|
|
|
{
|
2016-12-09 14:23:54 +01:00
|
|
|
int const cur_index = int(i - m_files.begin());
|
2016-09-18 06:24:20 +02:00
|
|
|
int const index = int(m_files.size());
|
2015-03-21 01:12:40 +01:00
|
|
|
m_files.push_back(internal_file_entry());
|
|
|
|
internal_file_entry& e = m_files.back();
|
|
|
|
// i may have been invalidated, refresh it
|
|
|
|
i = m_files.begin() + cur_index;
|
2017-02-07 06:22:30 +01:00
|
|
|
e.size = aux::numeric_cast<std::uint64_t>(size);
|
|
|
|
e.offset = aux::numeric_cast<std::uint64_t>(offset);
|
2017-08-03 10:38:58 +02:00
|
|
|
e.path_index = get_or_add_path(".pad");
|
|
|
|
char name[15];
|
|
|
|
std::snprintf(name, sizeof(name), "%d", pad_file_counter);
|
|
|
|
e.set_name(name);
|
2015-03-21 01:12:40 +01:00
|
|
|
e.pad_file = true;
|
|
|
|
offset += size;
|
|
|
|
++pad_file_counter;
|
|
|
|
|
|
|
|
if (!m_mtime.empty()) m_mtime.resize(index + 1, 0);
|
2016-06-20 17:32:06 +02:00
|
|
|
if (!m_file_hashes.empty()) m_file_hashes.resize(index + 1, nullptr);
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-18 06:24:20 +02:00
|
|
|
if (index != cur_index) reorder_file(index, cur_index);
|
2015-03-21 01:12:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 10:02:41 +01:00
|
|
|
void file_storage::sanitize_symlinks()
|
|
|
|
{
|
|
|
|
// symlinks are unusual, this function is optimized assuming there are no
|
|
|
|
// symbolic links in the torrent. If we find one symbolic link, we'll
|
|
|
|
// build the hash table of files it's allowed to refer to, but don't pay
|
|
|
|
// that price up-front.
|
|
|
|
std::unordered_map<std::string, file_index_t> file_map;
|
|
|
|
bool file_map_initialized = false;
|
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
// lazily instantiated set of all valid directories a symlink may point to
|
|
|
|
// TODO: in C++17 this could be string_view
|
|
|
|
std::unordered_set<std::string> dir_map;
|
|
|
|
bool dir_map_initialized = false;
|
|
|
|
|
|
|
|
// symbolic links that points to directories
|
|
|
|
std::unordered_map<std::string, std::string> dir_links;
|
|
|
|
|
|
|
|
// we validate symlinks in (potentially) 2 passes over the files.
|
|
|
|
// remaining symlinks to validate after the first pass
|
|
|
|
std::vector<file_index_t> symlinks_to_validate;
|
|
|
|
|
2019-03-14 10:02:41 +01:00
|
|
|
for (auto const i : file_range())
|
|
|
|
{
|
|
|
|
if (!(file_flags(i) & file_storage::flag_symlink)) continue;
|
|
|
|
|
|
|
|
if (!file_map_initialized)
|
|
|
|
{
|
2019-07-22 00:50:57 +02:00
|
|
|
for (auto const j : file_range())
|
|
|
|
file_map.insert({internal_file_path(j), j});
|
2019-03-14 10:02:41 +01:00
|
|
|
file_map_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_file_entry const& fe = m_files[i];
|
|
|
|
TORRENT_ASSERT(fe.symlink_index < int(m_symlinks.size()));
|
|
|
|
|
|
|
|
// symlink targets are only allowed to point to files or directories in
|
|
|
|
// this torrent.
|
|
|
|
{
|
2019-07-22 00:50:57 +02:00
|
|
|
std::string target = m_symlinks[fe.symlink_index];
|
2019-03-14 10:02:41 +01:00
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
if (is_complete(target))
|
2019-03-14 10:02:41 +01:00
|
|
|
{
|
2019-07-22 00:50:57 +02:00
|
|
|
// a symlink target is not allowed to be an absolute path, ever
|
|
|
|
// this symlink is invalid, make it point to itself
|
|
|
|
m_symlinks[fe.symlink_index] = internal_file_path(i);
|
2019-03-14 10:02:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
auto const iter = file_map.find(target);
|
|
|
|
if (iter != file_map.end())
|
|
|
|
{
|
|
|
|
m_symlinks[fe.symlink_index] = target;
|
|
|
|
if (file_flags(iter->second) & file_storage::flag_symlink)
|
|
|
|
{
|
|
|
|
// we don't know whether this symlink is a file or a
|
|
|
|
// directory, so make the conservative assumption that it's a
|
|
|
|
// directory
|
|
|
|
dir_links[internal_file_path(i)] = target;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-14 10:02:41 +01:00
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
// it may point to a directory that doesn't have any files (but only
|
|
|
|
// other directories), in which case it won't show up in m_paths
|
|
|
|
if (!dir_map_initialized)
|
2019-03-14 10:02:41 +01:00
|
|
|
{
|
2019-07-22 00:50:57 +02:00
|
|
|
for (auto const& p : m_paths)
|
|
|
|
for (string_view pv = p; !pv.empty(); pv = rsplit_path(pv).first)
|
|
|
|
dir_map.insert(pv.to_string());
|
|
|
|
dir_map_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir_map.count(target))
|
|
|
|
{
|
|
|
|
// it points to a sub directory within the torrent, that's OK
|
2019-03-14 10:02:41 +01:00
|
|
|
m_symlinks[fe.symlink_index] = target;
|
2019-07-22 00:50:57 +02:00
|
|
|
dir_links[internal_file_path(i)] = target;
|
2019-03-14 10:02:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
}
|
2019-03-14 10:02:41 +01:00
|
|
|
|
|
|
|
// for backwards compatibility, allow paths relative to the link as
|
|
|
|
// well
|
|
|
|
if (fe.path_index >= 0)
|
|
|
|
{
|
|
|
|
std::string target = m_paths[fe.path_index];
|
2019-07-22 00:50:57 +02:00
|
|
|
append_path(target, m_symlinks[fe.symlink_index]);
|
2019-03-14 10:02:41 +01:00
|
|
|
// if it points to a directory, that's OK
|
2019-07-22 00:50:57 +02:00
|
|
|
auto const it = std::find(m_paths.begin(), m_paths.end(), target);
|
2019-03-14 10:02:41 +01:00
|
|
|
if (it != m_paths.end())
|
|
|
|
{
|
2019-07-22 00:50:57 +02:00
|
|
|
m_symlinks[fe.symlink_index] = *it;
|
|
|
|
dir_links[internal_file_path(i)] = *it;
|
2019-03-14 10:02:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
if (dir_map.count(target))
|
2019-03-14 10:02:41 +01:00
|
|
|
{
|
2019-07-22 00:50:57 +02:00
|
|
|
// it points to a sub directory within the torrent, that's OK
|
2019-03-14 10:02:41 +01:00
|
|
|
m_symlinks[fe.symlink_index] = target;
|
2019-07-22 00:50:57 +02:00
|
|
|
dir_links[internal_file_path(i)] = target;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const iter = file_map.find(target);
|
|
|
|
if (iter != file_map.end())
|
|
|
|
{
|
|
|
|
m_symlinks[fe.symlink_index] = target;
|
|
|
|
if (file_flags(iter->second) & file_storage::flag_symlink)
|
|
|
|
{
|
|
|
|
// we don't know whether this symlink is a file or a
|
|
|
|
// directory, so make the conservative assumption that it's a
|
|
|
|
// directory
|
|
|
|
dir_links[internal_file_path(i)] = target;
|
|
|
|
}
|
2019-03-14 10:02:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 00:50:57 +02:00
|
|
|
// we don't know whether this symlink is a file or a
|
|
|
|
// directory, so make the conservative assumption that it's a
|
|
|
|
// directory
|
|
|
|
dir_links[internal_file_path(i)] = m_symlinks[fe.symlink_index];
|
|
|
|
symlinks_to_validate.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case there were some "complex" symlinks, we nee a second pass to
|
|
|
|
// validate those. For example, symlinks whose target rely on other
|
|
|
|
// symlinks
|
|
|
|
for (auto const i : symlinks_to_validate)
|
|
|
|
{
|
|
|
|
internal_file_entry const& fe = m_files[i];
|
|
|
|
TORRENT_ASSERT(fe.symlink_index < int(m_symlinks.size()));
|
|
|
|
|
|
|
|
std::string target = m_symlinks[fe.symlink_index];
|
|
|
|
|
|
|
|
// to avoid getting stuck in an infinite loop, we only allow traversing
|
|
|
|
// a symlink once
|
|
|
|
std::set<std::string> traversed;
|
|
|
|
|
|
|
|
// this is where we check every path element for existence. If it's not
|
|
|
|
// among the concrete paths, it may be a symlink, which is also OK
|
|
|
|
// note that we won't iterate through this for the last step, where the
|
|
|
|
// filename is included. The filename is validated after the loop
|
|
|
|
for (string_view branch = lsplit_path(target).first;
|
|
|
|
branch.size() < target.size();
|
|
|
|
branch = lsplit_path(target, branch.size() + 1).first)
|
|
|
|
{
|
|
|
|
// this is a concrete directory
|
|
|
|
if (dir_map.count(branch.to_string())) continue;
|
|
|
|
|
|
|
|
auto const iter = dir_links.find(branch.to_string());
|
|
|
|
if (iter == dir_links.end()) goto failed;
|
|
|
|
if (traversed.count(branch.to_string())) goto failed;
|
|
|
|
traversed.insert(branch.to_string());
|
|
|
|
|
|
|
|
// this path element is a symlink. substitute the branch so far by
|
|
|
|
// the link target
|
|
|
|
target = combine_path(iter->second, target.substr(branch.size() + 1));
|
|
|
|
|
|
|
|
// start over with the new (concrete) path
|
|
|
|
branch = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// the final (resolved) target must be a valid file
|
|
|
|
// or directory
|
|
|
|
if (file_map.count(target) == 0
|
|
|
|
&& dir_map.count(target) == 0) goto failed;
|
|
|
|
|
|
|
|
// this is OK
|
|
|
|
continue;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
2019-03-14 10:02:41 +01:00
|
|
|
// this symlink is invalid, make it point to itself
|
2019-07-22 00:50:57 +02:00
|
|
|
m_symlinks[fe.symlink_index] = internal_file_path(i);
|
2019-03-14 10:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace aux {
|
2016-10-03 08:08:03 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::tuple<piece_index_t, piece_index_t>
|
|
|
|
file_piece_range_exclusive(file_storage const& fs, file_index_t const file)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
peer_request const range = fs.map_file(file, 0, 1);
|
|
|
|
std::int64_t const file_size = fs.file_size(file);
|
|
|
|
std::int64_t const piece_size = fs.piece_length();
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const begin_piece = range.start == 0 ? range.piece : piece_index_t(static_cast<int>(range.piece) + 1);
|
2016-10-03 08:08:03 +02:00
|
|
|
// the last piece is potentially smaller than the other pieces, so the
|
|
|
|
// generic logic doesn't really work. If this file is the last file, the
|
|
|
|
// last piece doesn't overlap with any other file and it's entirely
|
|
|
|
// contained within the last file.
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const end_piece = (file == file_index_t(fs.num_files() - 1))
|
|
|
|
? piece_index_t(fs.num_pieces())
|
|
|
|
: piece_index_t(int((static_cast<int>(range.piece) * piece_size + range.start + file_size + 1) / piece_size));
|
2016-10-03 08:08:03 +02:00
|
|
|
return std::make_tuple(begin_piece, end_piece);
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::tuple<piece_index_t, piece_index_t>
|
|
|
|
file_piece_range_inclusive(file_storage const& fs, file_index_t const file)
|
2016-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
peer_request const range = fs.map_file(file, 0, 1);
|
|
|
|
std::int64_t const file_size = fs.file_size(file);
|
|
|
|
std::int64_t const piece_size = fs.piece_length();
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const end_piece = piece_index_t(int((static_cast<int>(range.piece)
|
|
|
|
* piece_size + range.start + file_size - 1) / piece_size + 1));
|
2016-10-03 08:08:03 +02:00
|
|
|
return std::make_tuple(range.piece, end_piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aux
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|