File indexing completed on 2024-05-19 05:01:59

0001 /*
0002     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "dummytorrentcreator.h"
0008 #include <QFile>
0009 #include <torrent/torrentcreator.h>
0010 #include <util/error.h>
0011 #include <util/fileops.h>
0012 #include <util/functions.h>
0013 #include <util/log.h>
0014 
0015 using namespace bt;
0016 
0017 DummyTorrentCreator::DummyTorrentCreator()
0018 {
0019     chunk_size = 256;
0020     trackers.append(QStringLiteral("http://localhost:5000/announce"));
0021     tmpdir.setAutoRemove(true);
0022 }
0023 
0024 DummyTorrentCreator::~DummyTorrentCreator()
0025 {
0026 }
0027 
0028 bool DummyTorrentCreator::createMultiFileTorrent(const QMap<QString, bt::Uint64> &files, const QString &name)
0029 {
0030     if (!tmpdir.isValid())
0031         return false;
0032 
0033     try {
0034         dpath = tmpdir.path() + QLatin1String("data") + bt::DirSeparator() + name + bt::DirSeparator();
0035         bt::MakePath(dpath);
0036 
0037         QMap<QString, bt::Uint64>::const_iterator i = files.begin();
0038         while (i != files.end()) {
0039             MakeFilePath(dpath + i.key());
0040             if (!createRandomFile(dpath + i.key(), i.value()))
0041                 return false;
0042             ++i;
0043         }
0044 
0045         bt::TorrentCreator creator(dpath, trackers, QList<QUrl>(), chunk_size, name, "", false, false);
0046         // Start the hashing thread and wait until it is done
0047         creator.start();
0048         creator.wait();
0049         creator.saveTorrent(torrentPath());
0050         return true;
0051     } catch (bt::Error &err) {
0052         Out(SYS_GEN | LOG_NOTICE) << "Error creating torrent: " << err.toString() << endl;
0053         return false;
0054     }
0055 }
0056 
0057 bool DummyTorrentCreator::createSingleFileTorrent(bt::Uint64 size, const QString &filename)
0058 {
0059     if (!tmpdir.isValid())
0060         return false;
0061 
0062     try {
0063         bt::MakePath(tmpdir.path() + QLatin1String("data") + bt::DirSeparator());
0064         dpath = tmpdir.path() + QLatin1String("data") + bt::DirSeparator() + filename;
0065         if (!createRandomFile(dpath, size))
0066             return false;
0067 
0068         bt::TorrentCreator creator(dpath, trackers, QList<QUrl>(), chunk_size, filename, "", false, false);
0069         // Start the hashing thread and wait until it is done
0070         creator.start();
0071         creator.wait();
0072         creator.saveTorrent(torrentPath());
0073         return true;
0074     } catch (bt::Error &err) {
0075         Out(SYS_GEN | LOG_NOTICE) << "Error creating torrent: " << err.toString() << endl;
0076         return false;
0077     }
0078 }
0079 
0080 bool DummyTorrentCreator::createRandomFile(const QString &path, bt::Uint64 size)
0081 {
0082     QFile file(path);
0083     if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
0084         Out(SYS_GEN | LOG_NOTICE) << "Error opening " << path << ": " << file.errorString() << endl;
0085         return false;
0086     }
0087 
0088     bt::Uint64 written = 0;
0089     while (written < size) {
0090         char tmp[4096];
0091         for (int i = 0; i < 4096; i++)
0092             tmp[i] = rand() % 0xFF;
0093 
0094         bt::Uint64 to_write = 4096;
0095         if (to_write + written > size)
0096             to_write = size - written;
0097         written += file.write(tmp, to_write);
0098     }
0099 
0100     return true;
0101 }
0102 
0103 QString DummyTorrentCreator::dataPath() const
0104 {
0105     return dpath;
0106 }
0107 
0108 QString DummyTorrentCreator::torrentPath() const
0109 {
0110     return tmpdir.path() + QLatin1String("torrent");
0111 }