2015-04-19 18:17:47 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2018-05-08 07:11:00 +03:00
|
|
|
#include "torrentinfo.h"
|
|
|
|
|
|
|
|
#include <libtorrent/error_code.hpp>
|
|
|
|
|
2018-04-14 22:53:45 +03:00
|
|
|
#include <QDateTime>
|
2016-02-29 02:44:08 +03:00
|
|
|
#include <QDebug>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/utils/fs.h"
|
2018-04-14 22:53:45 +03:00
|
|
|
#include "base/utils/misc.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/utils/string.h"
|
2015-04-19 18:17:47 +03:00
|
|
|
#include "infohash.h"
|
|
|
|
#include "trackerentry.h"
|
|
|
|
|
|
|
|
namespace libt = libtorrent;
|
|
|
|
using namespace BitTorrent;
|
|
|
|
|
2016-01-20 09:57:02 +03:00
|
|
|
TorrentInfo::TorrentInfo(NativeConstPtr nativeInfo)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
m_nativeInfo = boost::const_pointer_cast<libt::torrent_info>(nativeInfo);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TorrentInfo::TorrentInfo(const TorrentInfo &other)
|
|
|
|
: m_nativeInfo(other.m_nativeInfo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentInfo &TorrentInfo::operator=(const TorrentInfo &other)
|
|
|
|
{
|
|
|
|
m_nativeInfo = other.m_nativeInfo;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:20:55 +03:00
|
|
|
TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2018-06-25 20:42:07 +03:00
|
|
|
// 2-step construction to overcome default limits of `depth_limit` & `token_limit` which are
|
|
|
|
// used in `torrent_info()` constructor
|
|
|
|
const int depthLimit = 100;
|
|
|
|
const int tokenLimit = 10000000;
|
2015-04-19 18:17:47 +03:00
|
|
|
libt::error_code ec;
|
2018-06-25 20:42:07 +03:00
|
|
|
|
|
|
|
#if LIBTORRENT_VERSION_NUM < 10100
|
|
|
|
libt::lazy_entry node;
|
|
|
|
libt::lazy_bdecode(data.constData(), (data.constData() + data.size()), node, ec
|
|
|
|
, nullptr, depthLimit, tokenLimit);
|
|
|
|
#else
|
|
|
|
libt::bdecode_node node;
|
|
|
|
bdecode(data.constData(), (data.constData() + data.size()), node, ec
|
|
|
|
, nullptr, depthLimit, tokenLimit);
|
|
|
|
#endif
|
|
|
|
if (ec) {
|
|
|
|
if (error)
|
2017-12-13 17:20:55 +03:00
|
|
|
*error = QString::fromStdString(ec.message());
|
2018-06-25 20:42:07 +03:00
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentInfo info {NativePtr(new libt::torrent_info(node, ec))};
|
|
|
|
if (ec) {
|
|
|
|
if (error)
|
|
|
|
*error = QString::fromStdString(ec.message());
|
|
|
|
return TorrentInfo();
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:20:55 +03:00
|
|
|
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexcept
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2018-02-19 20:02:57 +03:00
|
|
|
if (error)
|
|
|
|
error->clear();
|
|
|
|
|
|
|
|
QFile file {path};
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
if (error)
|
|
|
|
*error = file.errorString();
|
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
const qint64 fileSizeLimit = 100 * 1024 * 1024; // 100 MB
|
|
|
|
if (file.size() > fileSizeLimit) {
|
|
|
|
if (error)
|
|
|
|
*error = tr("File size exceeds max limit %1").arg(fileSizeLimit);
|
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray data = file.read(fileSizeLimit);
|
|
|
|
if (data.size() != file.size()) {
|
|
|
|
if (error)
|
|
|
|
*error = tr("Torrent file read error");
|
|
|
|
return TorrentInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
2018-06-25 20:42:07 +03:00
|
|
|
return load(data, error);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TorrentInfo::isValid() const
|
|
|
|
{
|
|
|
|
return (m_nativeInfo && m_nativeInfo->is_valid() && (m_nativeInfo->num_files() > 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
InfoHash TorrentInfo::hash() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return InfoHash();
|
|
|
|
return m_nativeInfo->info_hash();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::name() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QString();
|
2017-03-07 14:41:38 +03:00
|
|
|
return QString::fromStdString(m_nativeInfo->name());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime TorrentInfo::creationDate() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QDateTime();
|
|
|
|
boost::optional<time_t> t = m_nativeInfo->creation_date();
|
|
|
|
return t ? QDateTime::fromTime_t(*t) : QDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::creator() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QString();
|
2017-03-07 14:41:38 +03:00
|
|
|
return QString::fromStdString(m_nativeInfo->creator());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::comment() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QString();
|
2017-03-07 14:41:38 +03:00
|
|
|
return QString::fromStdString(m_nativeInfo->comment());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TorrentInfo::isPrivate() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return false;
|
|
|
|
return m_nativeInfo->priv();
|
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong TorrentInfo::totalSize() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->total_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TorrentInfo::filesCount() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->num_files();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TorrentInfo::pieceLength() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->piece_length();
|
|
|
|
}
|
|
|
|
|
2016-02-29 02:44:08 +03:00
|
|
|
int TorrentInfo::pieceLength(int index) const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->piece_size(index);
|
|
|
|
}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
int TorrentInfo::piecesCount() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->num_pieces();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::filePath(int index) const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QString();
|
2017-03-07 14:41:38 +03:00
|
|
|
return Utils::Fs::fromNativePath(QString::fromStdString(m_nativeInfo->files().file_path(index)));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList TorrentInfo::filePaths() const
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
for (int i = 0; i < filesCount(); ++i)
|
|
|
|
list << filePath(i);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::fileName(int index) const
|
|
|
|
{
|
2015-05-06 14:53:27 +03:00
|
|
|
return Utils::Fs::fileName(filePath(index));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString TorrentInfo::origFilePath(int index) const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QString();
|
2017-03-07 14:41:38 +03:00
|
|
|
return Utils::Fs::fromNativePath(QString::fromStdString(m_nativeInfo->orig_files().file_path(index)));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong TorrentInfo::fileSize(int index) const
|
|
|
|
{
|
|
|
|
if (!isValid()) return -1;
|
|
|
|
return m_nativeInfo->files().file_size(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong TorrentInfo::fileOffset(int index) const
|
|
|
|
{
|
2016-02-29 02:44:08 +03:00
|
|
|
if (!isValid()) return -1;
|
2017-05-04 13:05:12 +03:00
|
|
|
return m_nativeInfo->files().file_offset(index);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<TrackerEntry> TorrentInfo::trackers() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QList<TrackerEntry>();
|
|
|
|
|
|
|
|
QList<TrackerEntry> trackers;
|
|
|
|
foreach (const libt::announce_entry &tracker, m_nativeInfo->trackers())
|
|
|
|
trackers.append(tracker);
|
|
|
|
|
|
|
|
return trackers;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QUrl> TorrentInfo::urlSeeds() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QList<QUrl>();
|
|
|
|
|
|
|
|
QList<QUrl> urlSeeds;
|
|
|
|
foreach (const libt::web_seed_entry &webSeed, m_nativeInfo->web_seeds())
|
|
|
|
if (webSeed.type == libt::web_seed_entry::url_seed)
|
|
|
|
urlSeeds.append(QUrl(webSeed.url.c_str()));
|
|
|
|
|
|
|
|
return urlSeeds;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray TorrentInfo::metadata() const
|
|
|
|
{
|
|
|
|
if (!isValid()) return QByteArray();
|
|
|
|
return QByteArray(m_nativeInfo->metadata().get(), m_nativeInfo->metadata_size());
|
|
|
|
}
|
|
|
|
|
2016-02-27 22:51:39 +03:00
|
|
|
QStringList TorrentInfo::filesForPiece(int pieceIndex) const
|
|
|
|
{
|
2016-02-29 02:44:08 +03:00
|
|
|
// no checks here because fileIndicesForPiece() will return an empty list
|
|
|
|
QVector<int> fileIndices = fileIndicesForPiece(pieceIndex);
|
2016-02-27 22:51:39 +03:00
|
|
|
|
|
|
|
QStringList res;
|
2016-02-29 02:44:08 +03:00
|
|
|
res.reserve(fileIndices.size());
|
|
|
|
std::transform(fileIndices.begin(), fileIndices.end(), std::back_inserter(res),
|
|
|
|
[this](int i) { return filePath(i); });
|
|
|
|
|
2016-02-27 22:51:39 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-02-29 02:44:08 +03:00
|
|
|
QVector<int> TorrentInfo::fileIndicesForPiece(int pieceIndex) const
|
|
|
|
{
|
|
|
|
if (!isValid() || (pieceIndex < 0) || (pieceIndex >= piecesCount()))
|
|
|
|
return QVector<int>();
|
|
|
|
|
|
|
|
std::vector<libt::file_slice> files(
|
|
|
|
nativeInfo()->map_block(pieceIndex, 0, nativeInfo()->piece_size(pieceIndex)));
|
|
|
|
QVector<int> res;
|
2016-12-01 10:51:54 +03:00
|
|
|
res.reserve(int(files.size()));
|
2016-02-29 02:44:08 +03:00
|
|
|
std::transform(files.begin(), files.end(), std::back_inserter(res),
|
|
|
|
[](const libt::file_slice &s) { return s.file_index; });
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-04-05 19:36:00 +03:00
|
|
|
QVector<QByteArray> TorrentInfo::pieceHashes() const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const int count = piecesCount();
|
|
|
|
QVector<QByteArray> hashes;
|
|
|
|
hashes.reserve(count);
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
hashes += { m_nativeInfo->hash_for_piece_ptr(i), libtorrent::sha1_hash::size };
|
|
|
|
|
|
|
|
return hashes;
|
|
|
|
}
|
|
|
|
|
2018-04-14 22:53:45 +03:00
|
|
|
TorrentInfo::PieceRange TorrentInfo::filePieces(const QString &file) const
|
2016-02-29 02:44:08 +03:00
|
|
|
{
|
|
|
|
if (!isValid()) // if we do not check here the debug message will be printed, which would be not correct
|
|
|
|
return {};
|
|
|
|
|
|
|
|
int index = fileIndex(file);
|
|
|
|
if (index == -1) {
|
|
|
|
qDebug() << "Filename" << file << "was not found in torrent" << name();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return filePieces(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentInfo::PieceRange TorrentInfo::filePieces(int fileIndex) const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if ((fileIndex < 0) || (fileIndex >= filesCount())) {
|
|
|
|
qDebug() << "File index (" << fileIndex << ") is out of range for torrent" << name();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const libt::file_storage &files = nativeInfo()->files();
|
|
|
|
const auto fileSize = files.file_size(fileIndex);
|
|
|
|
const auto firstOffset = files.file_offset(fileIndex);
|
|
|
|
return makeInterval(static_cast<int>(firstOffset / pieceLength()),
|
|
|
|
static_cast<int>((firstOffset + fileSize - 1) / pieceLength()));
|
|
|
|
}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
void TorrentInfo::renameFile(uint index, const QString &newPath)
|
|
|
|
{
|
|
|
|
if (!isValid()) return;
|
2016-01-04 16:00:50 +03:00
|
|
|
nativeInfo()->rename_file(index, Utils::Fs::toNativePath(newPath).toStdString());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2016-02-29 02:44:08 +03:00
|
|
|
int BitTorrent::TorrentInfo::fileIndex(const QString& fileName) const
|
|
|
|
{
|
2018-04-14 22:53:45 +03:00
|
|
|
// the check whether the object is valid is not needed here
|
|
|
|
// because if filesCount() returns -1 the loop exits immediately
|
2016-02-29 02:44:08 +03:00
|
|
|
for (int i = 0; i < filesCount(); ++i)
|
|
|
|
if (fileName == filePath(i))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-19 09:41:33 +03:00
|
|
|
QString TorrentInfo::rootFolder() const
|
2017-07-28 12:13:57 +03:00
|
|
|
{
|
2018-05-19 09:41:33 +03:00
|
|
|
QString rootFolder;
|
2017-07-28 12:13:57 +03:00
|
|
|
for (int i = 0; i < filesCount(); ++i) {
|
|
|
|
const QString filePath = this->filePath(i);
|
|
|
|
if (QDir::isAbsolutePath(filePath)) continue;
|
|
|
|
|
|
|
|
const auto filePathElements = filePath.splitRef('/');
|
|
|
|
// if at least one file has no root folder, no common root folder exists
|
2018-05-19 09:41:33 +03:00
|
|
|
if (filePathElements.count() <= 1) return "";
|
2017-07-28 12:13:57 +03:00
|
|
|
|
2018-05-19 09:41:33 +03:00
|
|
|
if (rootFolder.isEmpty())
|
|
|
|
rootFolder = filePathElements.at(0).toString();
|
|
|
|
else if (rootFolder != filePathElements.at(0))
|
|
|
|
return "";
|
2017-07-28 12:13:57 +03:00
|
|
|
}
|
|
|
|
|
2018-05-19 09:41:33 +03:00
|
|
|
return rootFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TorrentInfo::hasRootFolder() const
|
|
|
|
{
|
|
|
|
return !rootFolder().isEmpty();
|
2017-07-28 12:13:57 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 16:00:50 +03:00
|
|
|
void TorrentInfo::stripRootFolder()
|
2015-11-07 21:44:53 +03:00
|
|
|
{
|
2017-07-28 12:13:57 +03:00
|
|
|
if (!hasRootFolder()) return;
|
2016-01-04 16:00:50 +03:00
|
|
|
|
|
|
|
libtorrent::file_storage files = m_nativeInfo->files();
|
|
|
|
|
|
|
|
// Solution for case of renamed root folder
|
|
|
|
std::string testName = filePath(0).split('/').value(0).toStdString();
|
|
|
|
if (files.name() != testName) {
|
|
|
|
files.set_name(testName);
|
|
|
|
for (int i = 0; i < files.num_files(); ++i)
|
|
|
|
files.rename_file(i, files.file_path(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
files.set_name("");
|
|
|
|
m_nativeInfo->remap_files(files);
|
2015-11-07 21:44:53 +03:00
|
|
|
}
|
|
|
|
|
2016-01-20 09:57:02 +03:00
|
|
|
TorrentInfo::NativePtr TorrentInfo::nativeInfo() const
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
return m_nativeInfo;
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|