2015-04-19 18:17:47 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2018-04-14 22:53:45 +03:00
|
|
|
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
|
2015-04-19 18:17:47 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-04-28 14:02:38 +03:00
|
|
|
#include "torrentcreatorthread.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <libtorrent/bencode.hpp>
|
2016-04-28 14:02:38 +03:00
|
|
|
#include <libtorrent/create_torrent.hpp>
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <libtorrent/storage.hpp>
|
2017-11-21 13:23:39 +03:00
|
|
|
#include <libtorrent/torrent_info.hpp>
|
2018-04-30 22:56:24 +03:00
|
|
|
#include <libtorrent/version.hpp>
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2018-04-14 09:50:28 +03:00
|
|
|
#include <QDirIterator>
|
2016-04-28 14:02:38 +03:00
|
|
|
#include <QFile>
|
2018-04-14 09:50:28 +03:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QHash>
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2018-04-14 09:50:28 +03:00
|
|
|
#include "base/global.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/utils/fs.h"
|
|
|
|
#include "base/utils/misc.h"
|
|
|
|
#include "base/utils/string.h"
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2017-11-21 13:23:39 +03:00
|
|
|
namespace
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2017-11-21 13:23:39 +03:00
|
|
|
// do not include files and folders whose
|
|
|
|
// name starts with a .
|
|
|
|
bool fileFilter(const std::string &f)
|
|
|
|
{
|
|
|
|
return !Utils::Fs::fileName(QString::fromStdString(f)).startsWith('.');
|
|
|
|
}
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:23:39 +03:00
|
|
|
namespace libt = libtorrent;
|
|
|
|
using namespace BitTorrent;
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
TorrentCreatorThread::TorrentCreatorThread(QObject *parent)
|
|
|
|
: QThread(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentCreatorThread::~TorrentCreatorThread()
|
|
|
|
{
|
2017-05-15 10:53:58 +03:00
|
|
|
requestInterruption();
|
2017-11-21 13:29:22 +03:00
|
|
|
wait();
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 18:17:29 +03:00
|
|
|
void TorrentCreatorThread::create(const TorrentCreatorParams ¶ms)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2017-12-05 18:17:29 +03:00
|
|
|
m_params = params;
|
2015-04-19 18:17:47 +03:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2017-12-05 18:17:29 +03:00
|
|
|
void TorrentCreatorThread::sendProgressSignal(int currentPieceIdx, int totalPieces)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
2017-12-05 18:17:29 +03:00
|
|
|
emit updateProgress(static_cast<int>((currentPieceIdx * 100.) / totalPieces));
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorThread::run()
|
|
|
|
{
|
2017-12-05 18:17:29 +03:00
|
|
|
const QString creatorStr("qBittorrent " QBT_VERSION);
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
emit updateProgress(0);
|
|
|
|
|
|
|
|
try {
|
2018-07-21 08:28:13 +03:00
|
|
|
const QString parentPath = Utils::Fs::branchPath(m_params.inputPath) + '/';
|
2018-04-14 09:50:28 +03:00
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
// Adding files to the torrent
|
2018-04-14 09:50:28 +03:00
|
|
|
libt::file_storage fs;
|
|
|
|
if (QFileInfo(m_params.inputPath).isFile()) {
|
|
|
|
libt::add_files(fs, Utils::Fs::toNativePath(m_params.inputPath).toStdString(), fileFilter);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// need to sort the file names by natural sort order
|
|
|
|
QStringList dirs = {m_params.inputPath};
|
|
|
|
|
|
|
|
QDirIterator dirIter(m_params.inputPath, (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories);
|
|
|
|
while (dirIter.hasNext()) {
|
|
|
|
dirIter.next();
|
|
|
|
dirs += dirIter.filePath();
|
|
|
|
}
|
|
|
|
std::sort(dirs.begin(), dirs.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>);
|
|
|
|
|
|
|
|
QStringList fileNames;
|
|
|
|
QHash<QString, boost::int64_t> fileSizeMap;
|
|
|
|
|
2018-11-27 23:15:04 +03:00
|
|
|
for (const auto &dir : asConst(dirs)) {
|
2018-04-14 09:50:28 +03:00
|
|
|
QStringList tmpNames; // natural sort files within each dir
|
|
|
|
|
|
|
|
QDirIterator fileIter(dir, QDir::Files);
|
|
|
|
while (fileIter.hasNext()) {
|
|
|
|
fileIter.next();
|
|
|
|
|
|
|
|
const QString relFilePath = fileIter.filePath().mid(parentPath.length());
|
|
|
|
tmpNames += relFilePath;
|
|
|
|
fileSizeMap[relFilePath] = fileIter.fileInfo().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(tmpNames.begin(), tmpNames.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>);
|
|
|
|
fileNames += tmpNames;
|
|
|
|
}
|
|
|
|
|
2018-11-27 23:15:04 +03:00
|
|
|
for (const auto &fileName : asConst(fileNames))
|
2018-04-14 09:50:28 +03:00
|
|
|
fs.add_file(fileName.toStdString(), fileSizeMap[fileName]);
|
|
|
|
}
|
2017-05-15 10:53:58 +03:00
|
|
|
|
|
|
|
if (isInterruptionRequested()) return;
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2018-04-30 22:56:24 +03:00
|
|
|
#if LIBTORRENT_VERSION_NUM < 10100
|
|
|
|
libt::create_torrent newTorrent(fs, m_params.pieceSize, -1
|
|
|
|
, (m_params.isAlignmentOptimized ? libt::create_torrent::optimize : 0));
|
|
|
|
#else
|
2018-04-14 03:24:08 +03:00
|
|
|
libt::create_torrent newTorrent(fs, m_params.pieceSize, -1
|
|
|
|
, (m_params.isAlignmentOptimized ? libt::create_torrent::optimize_alignment : 0));
|
2018-04-30 22:56:24 +03:00
|
|
|
#endif
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
// Add url seeds
|
2018-11-27 23:15:04 +03:00
|
|
|
for (QString seed : asConst(m_params.urlSeeds)) {
|
2017-12-05 18:17:29 +03:00
|
|
|
seed = seed.trimmed();
|
|
|
|
if (!seed.isEmpty())
|
|
|
|
newTorrent.add_url_seed(seed.toStdString());
|
|
|
|
}
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
int tier = 0;
|
2018-11-27 23:15:04 +03:00
|
|
|
for (const QString &tracker : asConst(m_params.trackers)) {
|
2017-12-05 18:17:29 +03:00
|
|
|
if (tracker.isEmpty())
|
2015-04-19 18:17:47 +03:00
|
|
|
++tier;
|
2017-12-05 18:17:29 +03:00
|
|
|
else
|
|
|
|
newTorrent.add_tracker(tracker.trimmed().toStdString(), tier);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
2017-05-15 10:53:58 +03:00
|
|
|
|
|
|
|
if (isInterruptionRequested()) return;
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2017-12-05 18:17:29 +03:00
|
|
|
libt::set_piece_hashes(newTorrent, Utils::Fs::toNativePath(parentPath).toStdString()
|
|
|
|
, [this, &newTorrent](const int n) { sendProgressSignal(n, newTorrent.num_pieces()); });
|
2015-04-19 18:17:47 +03:00
|
|
|
// Set qBittorrent as creator and add user comment to
|
|
|
|
// torrent_info structure
|
2017-12-05 18:17:29 +03:00
|
|
|
newTorrent.set_creator(creatorStr.toUtf8().constData());
|
|
|
|
newTorrent.set_comment(m_params.comment.toUtf8().constData());
|
2015-04-19 18:17:47 +03:00
|
|
|
// Is private ?
|
2017-12-05 18:17:29 +03:00
|
|
|
newTorrent.set_priv(m_params.isPrivate);
|
2017-05-15 10:53:58 +03:00
|
|
|
|
|
|
|
if (isInterruptionRequested()) return;
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2017-12-05 16:18:18 +03:00
|
|
|
libt::entry entry = newTorrent.generate();
|
|
|
|
|
|
|
|
// add source field
|
|
|
|
if (!m_params.source.isEmpty())
|
|
|
|
entry["info"]["source"] = m_params.source.toStdString();
|
|
|
|
|
|
|
|
if (isInterruptionRequested()) return;
|
|
|
|
|
2017-12-05 18:17:29 +03:00
|
|
|
// create the torrent
|
|
|
|
std::ofstream outfile(
|
2015-04-19 18:17:47 +03:00
|
|
|
#ifdef _MSC_VER
|
2017-12-05 18:17:29 +03:00
|
|
|
Utils::Fs::toNativePath(m_params.savePath).toStdWString().c_str()
|
2015-04-19 18:17:47 +03:00
|
|
|
#else
|
2017-12-05 18:17:29 +03:00
|
|
|
Utils::Fs::toNativePath(m_params.savePath).toUtf8().constData()
|
2015-04-19 18:17:47 +03:00
|
|
|
#endif
|
2017-12-05 18:17:29 +03:00
|
|
|
, (std::ios_base::out | std::ios_base::binary | std::ios_base::trunc));
|
2015-04-19 18:17:47 +03:00
|
|
|
if (outfile.fail())
|
2017-12-05 18:17:29 +03:00
|
|
|
throw std::runtime_error(tr("create new torrent file failed").toStdString());
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2017-05-15 10:53:58 +03:00
|
|
|
if (isInterruptionRequested()) return;
|
|
|
|
|
2017-12-05 16:18:18 +03:00
|
|
|
libt::bencode(std::ostream_iterator<char>(outfile), entry);
|
2015-04-19 18:17:47 +03:00
|
|
|
outfile.close();
|
|
|
|
|
|
|
|
emit updateProgress(100);
|
2017-12-05 18:17:29 +03:00
|
|
|
emit creationSuccess(m_params.savePath, parentPath);
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
2017-12-05 18:17:29 +03:00
|
|
|
catch (const std::exception &e) {
|
|
|
|
emit creationFailure(e.what());
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 17:48:10 +03:00
|
|
|
|
2018-04-14 20:00:08 +03:00
|
|
|
int TorrentCreatorThread::calculateTotalPieces(const QString &inputPath, const int pieceSize, const bool isAlignmentOptimized)
|
2017-05-25 17:48:10 +03:00
|
|
|
{
|
|
|
|
if (inputPath.isEmpty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
libt::file_storage fs;
|
|
|
|
libt::add_files(fs, Utils::Fs::toNativePath(inputPath).toStdString(), fileFilter);
|
2018-04-30 22:56:24 +03:00
|
|
|
|
|
|
|
#if LIBTORRENT_VERSION_NUM < 10100
|
|
|
|
return libt::create_torrent(fs, pieceSize, -1
|
|
|
|
, (isAlignmentOptimized ? libt::create_torrent::optimize : 0)).num_pieces();
|
|
|
|
#else
|
2018-04-14 20:00:08 +03:00
|
|
|
return libt::create_torrent(fs, pieceSize, -1
|
|
|
|
, (isAlignmentOptimized ? libt::create_torrent::optimize_alignment : 0)).num_pieces();
|
2018-04-30 22:56:24 +03:00
|
|
|
#endif
|
2017-05-25 17:48:10 +03:00
|
|
|
}
|