File: dummytorrentcreator.cpp

package info (click to toggle)
libktorrent 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,948 kB
  • sloc: cpp: 40,776; makefile: 7; sh: 3
file content (111 lines) | stat: -rw-r--r-- 3,229 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
    SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "dummytorrentcreator.h"
#include <QFile>
#include <torrent/torrentcreator.h>
#include <util/error.h>
#include <util/fileops.h>
#include <util/functions.h>
#include <util/log.h>

using namespace bt;

DummyTorrentCreator::DummyTorrentCreator()
    : chunk_size(256)
{
    trackers.append(QStringLiteral("http://localhost:5000/announce"));
    tmpdir.setAutoRemove(true);
}

DummyTorrentCreator::~DummyTorrentCreator()
{
}

bool DummyTorrentCreator::createMultiFileTorrent(const QMap<QString, bt::Uint64> &files, const QString &name)
{
    if (!tmpdir.isValid())
        return false;

    try {
        dpath = tmpdir.path() + QLatin1String("data") + bt::DirSeparator() + name + bt::DirSeparator();
        bt::MakePath(dpath);

        QMap<QString, bt::Uint64>::const_iterator i = files.begin();
        while (i != files.end()) {
            MakeFilePath(dpath + i.key());
            if (!createRandomFile(dpath + i.key(), i.value()))
                return false;
            ++i;
        }

        bt::TorrentCreator creator(dpath, trackers, QList<QUrl>(), chunk_size, name, QString(), false, false);
        // Start the hashing thread and wait until it is done
        creator.start();
        creator.wait();
        creator.saveTorrent(torrentPath());
        return true;
    } catch (bt::Error &err) {
        Out(SYS_GEN | LOG_NOTICE) << "Error creating torrent: " << err.toString() << endl;
        return false;
    }
}

bool DummyTorrentCreator::createSingleFileTorrent(bt::Uint64 size, const QString &filename)
{
    if (!tmpdir.isValid())
        return false;

    try {
        bt::MakePath(tmpdir.path() + QLatin1String("data") + bt::DirSeparator());
        dpath = tmpdir.path() + QLatin1String("data") + bt::DirSeparator() + filename;
        if (!createRandomFile(dpath, size))
            return false;

        bt::TorrentCreator creator(dpath, trackers, QList<QUrl>(), chunk_size, filename, QString(), false, false);
        // Start the hashing thread and wait until it is done
        creator.start();
        creator.wait();
        creator.saveTorrent(torrentPath());
        return true;
    } catch (bt::Error &err) {
        Out(SYS_GEN | LOG_NOTICE) << "Error creating torrent: " << err.toString() << endl;
        return false;
    }
}

bool DummyTorrentCreator::createRandomFile(const QString &path, bt::Uint64 size)
{
    QFile file(path);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        Out(SYS_GEN | LOG_NOTICE) << "Error opening " << path << ": " << file.errorString() << endl;
        return false;
    }

    bt::Uint64 written = 0;
    while (written < size) {
        char tmp[4096];
        for (int i = 0; i < 4096; i++)
            tmp[i] = rand() % 0xFF;

        bt::Uint64 to_write = 4096;
        if (to_write + written > size)
            to_write = size - written;
        written += file.write(tmp, to_write);
    }

    return true;
}

QString DummyTorrentCreator::dataPath() const
{
    return dpath;
}

QString DummyTorrentCreator::torrentPath() const
{
    return tmpdir.path() + QLatin1String("torrent");
}