1 /*
2 SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dummytorrentcreator.h"
8 #include <QFile>
9 #include <torrent/torrentcreator.h>
10 #include <util/error.h>
11 #include <util/fileops.h>
12 #include <util/functions.h>
13 #include <util/log.h>
14
15 using namespace bt;
16
DummyTorrentCreator()17 DummyTorrentCreator::DummyTorrentCreator()
18 {
19 chunk_size = 256;
20 trackers.append(QStringLiteral("http://localhost:5000/announce"));
21 tmpdir.setAutoRemove(true);
22 }
23
~DummyTorrentCreator()24 DummyTorrentCreator::~DummyTorrentCreator()
25 {
26 }
27
createMultiFileTorrent(const QMap<QString,bt::Uint64> & files,const QString & name)28 bool DummyTorrentCreator::createMultiFileTorrent(const QMap<QString, bt::Uint64> &files, const QString &name)
29 {
30 if (!tmpdir.isValid())
31 return false;
32
33 try {
34 dpath = tmpdir.path() + QLatin1String("data") + bt::DirSeparator() + name + bt::DirSeparator();
35 bt::MakePath(dpath);
36
37 QMap<QString, bt::Uint64>::const_iterator i = files.begin();
38 while (i != files.end()) {
39 MakeFilePath(dpath + i.key());
40 if (!createRandomFile(dpath + i.key(), i.value()))
41 return false;
42 ++i;
43 }
44
45 bt::TorrentCreator creator(dpath, trackers, QList<QUrl>(), chunk_size, name, "", false, false);
46 // Start the hashing thread and wait until it is done
47 creator.start();
48 creator.wait();
49 creator.saveTorrent(torrentPath());
50 return true;
51 } catch (bt::Error &err) {
52 Out(SYS_GEN | LOG_NOTICE) << "Error creating torrent: " << err.toString() << endl;
53 return false;
54 }
55 }
56
createSingleFileTorrent(bt::Uint64 size,const QString & filename)57 bool DummyTorrentCreator::createSingleFileTorrent(bt::Uint64 size, const QString &filename)
58 {
59 if (!tmpdir.isValid())
60 return false;
61
62 try {
63 bt::MakePath(tmpdir.path() + QLatin1String("data") + bt::DirSeparator());
64 dpath = tmpdir.path() + QLatin1String("data") + bt::DirSeparator() + filename;
65 if (!createRandomFile(dpath, size))
66 return false;
67
68 bt::TorrentCreator creator(dpath, trackers, QList<QUrl>(), chunk_size, filename, "", false, false);
69 // Start the hashing thread and wait until it is done
70 creator.start();
71 creator.wait();
72 creator.saveTorrent(torrentPath());
73 return true;
74 } catch (bt::Error &err) {
75 Out(SYS_GEN | LOG_NOTICE) << "Error creating torrent: " << err.toString() << endl;
76 return false;
77 }
78 }
79
createRandomFile(const QString & path,bt::Uint64 size)80 bool DummyTorrentCreator::createRandomFile(const QString &path, bt::Uint64 size)
81 {
82 QFile file(path);
83 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
84 Out(SYS_GEN | LOG_NOTICE) << "Error opening " << path << ": " << file.errorString() << endl;
85 return false;
86 }
87
88 bt::Uint64 written = 0;
89 while (written < size) {
90 char tmp[4096];
91 for (int i = 0; i < 4096; i++)
92 tmp[i] = rand() % 0xFF;
93
94 bt::Uint64 to_write = 4096;
95 if (to_write + written > size)
96 to_write = size - written;
97 written += file.write(tmp, to_write);
98 }
99
100 return true;
101 }
102
dataPath() const103 QString DummyTorrentCreator::dataPath() const
104 {
105 return dpath;
106 }
107
torrentPath() const108 QString DummyTorrentCreator::torrentPath() const
109 {
110 return tmpdir.path() + QLatin1String("torrent");
111 }
112