2015-03-21 01:12:40 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2014-2016, Arvid Norberg
|
2015-03-21 01:12:40 +01: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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2015-04-18 04:00:03 +02:00
|
|
|
#include "libtorrent/resolve_links.hpp"
|
2015-03-21 01:12:40 +01:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2016-04-02 18:27:42 +02:00
|
|
|
|
2015-03-21 01:12:40 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifndef TORRENT_DISABLE_MUTABLE_TORRENTS
|
2016-08-17 23:26:35 +02:00
|
|
|
resolve_links::resolve_links(std::shared_ptr<torrent_info> ti)
|
2015-03-21 01:12:40 +01:00
|
|
|
: m_torrent_file(ti)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(ti);
|
|
|
|
|
|
|
|
int piece_size = ti->piece_length();
|
|
|
|
|
|
|
|
file_storage const& fs = ti->files();
|
|
|
|
m_file_sizes.reserve(fs.num_files());
|
2016-12-22 16:42:33 +01:00
|
|
|
for (file_index_t i(0); i < fs.end_file(); ++i)
|
2015-03-21 01:12:40 +01:00
|
|
|
{
|
|
|
|
// don't match pad-files, and don't match files that aren't aligned to
|
2016-09-06 04:25:20 +02:00
|
|
|
// pieces. Files are matched by comparing piece hashes, so pieces must
|
2015-03-21 01:12:40 +01:00
|
|
|
// be aligned and the same size
|
|
|
|
if (fs.pad_file_at(i)) continue;
|
|
|
|
if ((fs.file_offset(i) % piece_size) != 0) continue;
|
|
|
|
|
|
|
|
m_file_sizes.insert(std::make_pair(fs.file_size(i), i));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_links.resize(m_torrent_file->num_files());
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:26:35 +02:00
|
|
|
void resolve_links::match(std::shared_ptr<const torrent_info> const& ti
|
2015-03-21 01:12:40 +01:00
|
|
|
, std::string const& save_path)
|
|
|
|
{
|
|
|
|
if (!ti) return;
|
|
|
|
|
2016-09-12 03:01:03 +02:00
|
|
|
// only torrents with the same piece size
|
2015-03-21 01:12:40 +01:00
|
|
|
if (ti->piece_length() != m_torrent_file->piece_length()) return;
|
|
|
|
|
|
|
|
int piece_size = ti->piece_length();
|
|
|
|
|
|
|
|
file_storage const& fs = ti->files();
|
|
|
|
m_file_sizes.reserve(fs.num_files());
|
2016-12-22 16:42:33 +01:00
|
|
|
for (file_index_t i(0); i < fs.end_file(); ++i)
|
2015-03-21 01:12:40 +01:00
|
|
|
{
|
|
|
|
// for every file in the other torrent, see if we have one that match
|
|
|
|
// it in m_torrent_file
|
|
|
|
|
|
|
|
// if the file base is not aligned to pieces, we're not going to match
|
|
|
|
// it anyway (we only compare piece hashes)
|
|
|
|
if ((fs.file_offset(i) % piece_size) != 0) continue;
|
|
|
|
if (fs.pad_file_at(i)) continue;
|
|
|
|
|
2016-09-12 03:01:03 +02:00
|
|
|
std::int64_t const file_size = fs.file_size(i);
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-06 19:04:28 +02:00
|
|
|
auto range = m_file_sizes.equal_range(file_size);
|
|
|
|
for (auto iter = range.first; iter != range.second; ++iter)
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT(iter->second >= file_index_t(0));
|
|
|
|
TORRENT_ASSERT(iter->second < m_torrent_file->files().end_file());
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-06 19:04:28 +02:00
|
|
|
// if we already have found a duplicate for this file, no need
|
|
|
|
// to keep looking
|
|
|
|
if (m_links[iter->second].ti) continue;
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-06 19:04:28 +02:00
|
|
|
// files are aligned and have the same size, now start comparing
|
|
|
|
// piece hashes, to see if the files are identical
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-06 19:04:28 +02:00
|
|
|
// the pieces of the incoming file
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t their_piece = fs.map_file(i, 0, 0).piece;
|
2016-09-06 19:04:28 +02:00
|
|
|
// the pieces of "this" file (from m_torrent_file)
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t our_piece = m_torrent_file->files().map_file(
|
2016-09-06 19:04:28 +02:00
|
|
|
iter->second, 0, 0).piece;
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-11-27 14:46:53 +01:00
|
|
|
int num_pieces = int((file_size + piece_size - 1) / piece_size);
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-06 19:04:28 +02:00
|
|
|
bool match = true;
|
|
|
|
for (int p = 0; p < num_pieces; ++p, ++their_piece, ++our_piece)
|
2015-03-21 01:12:40 +01:00
|
|
|
{
|
2016-09-06 19:04:28 +02:00
|
|
|
if (m_torrent_file->hash_for_piece(our_piece)
|
|
|
|
!= ti->hash_for_piece(their_piece))
|
|
|
|
{
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-21 01:12:40 +01:00
|
|
|
}
|
2016-09-06 19:04:28 +02:00
|
|
|
if (!match) continue;
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-06 19:04:28 +02:00
|
|
|
m_links[iter->second].ti = ti;
|
|
|
|
m_links[iter->second].save_path = save_path;
|
|
|
|
m_links[iter->second].file_idx = i;
|
2015-03-21 01:12:40 +01:00
|
|
|
|
2016-09-06 19:04:28 +02:00
|
|
|
// since we have a duplicate for this file, we may as well remove
|
|
|
|
// it from the file-size map, so we won't find it again.
|
|
|
|
m_file_sizes.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
2015-03-21 01:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // TORRENT_DISABLE_MUTABLE_TORRENTS
|
|
|
|
|
|
|
|
} // namespace libtorrent
|