qBittorrent/src/base/bittorrent/torrentinfo.cpp

425 lines
12 KiB
C++
Raw Normal View History

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.
*/
#include "torrentinfo.h"
#include <boost/optional.hpp>
#include <libtorrent/error_code.hpp>
#include <QByteArray>
2018-04-14 22:53:45 +03:00
#include <QDateTime>
#include <QDebug>
#include <QDir>
2015-04-19 18:17:47 +03:00
#include <QString>
#include <QStringList>
2015-04-19 18:17:47 +03:00
#include <QUrl>
#include "base/global.h"
2015-09-25 11:10:05 +03:00
#include "base/utils/fs.h"
2015-04-19 18:17:47 +03:00
#include "infohash.h"
#include "trackerentry.h"
namespace
{
#if (LIBTORRENT_VERSION_NUM < 10200)
using LTPieceIndex = int;
using LTFileIndex = int;
#else
using LTPieceIndex = lt::piece_index_t;
using LTFileIndex = lt::file_index_t;
#endif
}
2015-04-19 18:17:47 +03:00
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
{
#if (LIBTORRENT_VERSION_NUM < 10200)
m_nativeInfo = boost::const_pointer_cast<libt::torrent_info>(nativeInfo);
#else
m_nativeInfo = std::const_pointer_cast<libt::torrent_info>(nativeInfo);
#endif
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;
}
TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept
2015-04-19 18:17:47 +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;
libt::bdecode_node node;
bdecode(data.constData(), (data.constData() + data.size()), node, ec
, nullptr, depthLimit, tokenLimit);
if (ec) {
if (error)
*error = QString::fromStdString(ec.message());
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;
}
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexcept
2015-04-19 18:17:47 +03:00
{
if (error)
error->clear();
QFile file {path};
if (!file.open(QIODevice::ReadOnly)) {
if (error)
*error = file.errorString();
return TorrentInfo();
}
if (file.size() > MAX_TORRENT_SIZE) {
if (error)
*error = tr("File size exceeds max limit %1").arg(MAX_TORRENT_SIZE);
return TorrentInfo();
}
QByteArray data;
try {
data = file.readAll();
}
catch (const std::bad_alloc &e) {
if (error)
*error = tr("Torrent file read error: %1").arg(e.what());
return TorrentInfo();
}
if (data.size() != file.size()) {
if (error)
*error = tr("Torrent file read error: size mismatch");
return TorrentInfo();
}
file.close();
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
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
2015-04-19 18:17:47 +03:00
return m_nativeInfo->info_hash();
}
QString TorrentInfo::name() const
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
return QString::fromStdString(m_nativeInfo->name());
2015-04-19 18:17:47 +03:00
}
QDateTime TorrentInfo::creationDate() const
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
const boost::optional<time_t> t = m_nativeInfo->creation_date();
2015-04-19 18:17:47 +03:00
return t ? QDateTime::fromTime_t(*t) : QDateTime();
}
QString TorrentInfo::creator() const
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
return QString::fromStdString(m_nativeInfo->creator());
2015-04-19 18:17:47 +03:00
}
QString TorrentInfo::comment() const
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
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();
}
int TorrentInfo::pieceLength(const int index) const
{
if (!isValid()) return -1;
return m_nativeInfo->piece_size(LTPieceIndex {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(const int index) const
2015-04-19 18:17:47 +03:00
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
return Utils::Fs::fromNativePath(
QString::fromStdString(m_nativeInfo->files().file_path(LTFileIndex {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(const int index) const
2015-04-19 18:17:47 +03:00
{
return Utils::Fs::fileName(filePath(index));
2015-04-19 18:17:47 +03:00
}
QString TorrentInfo::origFilePath(const int index) const
2015-04-19 18:17:47 +03:00
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
return Utils::Fs::fromNativePath(
QString::fromStdString(m_nativeInfo->orig_files().file_path(LTFileIndex {index})));
2015-04-19 18:17:47 +03:00
}
qlonglong TorrentInfo::fileSize(const int index) const
2015-04-19 18:17:47 +03:00
{
if (!isValid()) return -1;
return m_nativeInfo->files().file_size(LTFileIndex {index});
2015-04-19 18:17:47 +03:00
}
qlonglong TorrentInfo::fileOffset(const int index) const
2015-04-19 18:17:47 +03:00
{
if (!isValid()) return -1;
return m_nativeInfo->files().file_offset(LTFileIndex {index});
2015-04-19 18:17:47 +03:00
}
QList<TrackerEntry> TorrentInfo::trackers() const
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
2015-04-19 18:17:47 +03:00
QList<TrackerEntry> trackers;
for (const libt::announce_entry &tracker : m_nativeInfo->trackers())
2015-04-19 18:17:47 +03:00
trackers.append(tracker);
return trackers;
}
QList<QUrl> TorrentInfo::urlSeeds() const
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
2015-04-19 18:17:47 +03:00
QList<QUrl> urlSeeds;
for (const libt::web_seed_entry &webSeed : m_nativeInfo->web_seeds())
2015-04-19 18:17:47 +03:00
if (webSeed.type == libt::web_seed_entry::url_seed)
urlSeeds.append(QUrl(webSeed.url.c_str()));
return urlSeeds;
}
QByteArray TorrentInfo::metadata() const
{
2019-02-14 20:16:42 +03:00
if (!isValid()) return {};
return {m_nativeInfo->metadata().get(), m_nativeInfo->metadata_size()};
2015-04-19 18:17:47 +03:00
}
QStringList TorrentInfo::filesForPiece(const int pieceIndex) const
{
// no checks here because fileIndicesForPiece() will return an empty list
const QVector<int> fileIndices = fileIndicesForPiece(pieceIndex);
QStringList res;
res.reserve(fileIndices.size());
std::transform(fileIndices.begin(), fileIndices.end(), std::back_inserter(res),
[this](int i) { return filePath(i); });
return res;
}
QVector<int> TorrentInfo::fileIndicesForPiece(const int pieceIndex) const
{
if (!isValid() || (pieceIndex < 0) || (pieceIndex >= piecesCount()))
2019-02-14 20:16:42 +03:00
return {};
const std::vector<libt::file_slice> files(
nativeInfo()->map_block(LTPieceIndex {pieceIndex}, 0
, nativeInfo()->piece_size(LTPieceIndex {pieceIndex})));
QVector<int> res;
res.reserve(int(files.size()));
std::transform(files.begin(), files.end(), std::back_inserter(res),
[](const libt::file_slice &s) { return static_cast<int>(s.file_index); });
return res;
}
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(LTPieceIndex {i}), InfoHash::length()};
return hashes;
}
2018-04-14 22:53:45 +03:00
TorrentInfo::PieceRange TorrentInfo::filePieces(const QString &file) const
{
if (!isValid()) // if we do not check here the debug message will be printed, which would be not correct
return {};
const int index = fileIndex(file);
if (index == -1) {
qDebug() << "Filename" << file << "was not found in torrent" << name();
return {};
}
return filePieces(index);
}
TorrentInfo::PieceRange TorrentInfo::filePieces(const 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(LTFileIndex {fileIndex});
const auto fileOffset = files.file_offset(LTFileIndex {fileIndex});
2018-09-11 20:56:55 +03:00
return makeInterval(static_cast<int>(fileOffset / pieceLength()),
static_cast<int>((fileOffset + fileSize - 1) / pieceLength()));
}
2018-09-07 14:12:38 +03:00
void TorrentInfo::renameFile(const int index, const QString &newPath)
2015-04-19 18:17:47 +03:00
{
if (!isValid()) return;
nativeInfo()->rename_file(index, Utils::Fs::toNativePath(newPath).toStdString());
2015-04-19 18:17:47 +03:00
}
2018-11-06 18:49:17 +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
for (int i = 0; i < filesCount(); ++i)
if (fileName == filePath(i))
return i;
return -1;
}
QString TorrentInfo::rootFolder() const
{
QString rootFolder;
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
if (filePathElements.count() <= 1) return "";
if (rootFolder.isEmpty())
rootFolder = filePathElements.at(0).toString();
else if (rootFolder != filePathElements.at(0))
return "";
}
return rootFolder;
}
bool TorrentInfo::hasRootFolder() const
{
return !rootFolder().isEmpty();
}
void TorrentInfo::stripRootFolder()
{
if (!hasRootFolder()) return;
libtorrent::file_storage files = m_nativeInfo->files();
// Solution for case of renamed root folder
const 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);
}
2016-01-20 09:57:02 +03:00
TorrentInfo::NativePtr TorrentInfo::nativeInfo() const
2015-04-19 18:17:47 +03:00
{
return m_nativeInfo;
2015-04-19 18:17:47 +03:00
}