2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2012-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <array>
|
2022-01-02 17:51:59 +00:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2022-02-12 18:50:47 +00:00
|
|
|
#include <iostream>
|
2022-01-02 17:51:59 +00:00
|
|
|
#include <string>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2022-01-02 17:51:59 +00:00
|
|
|
|
2014-09-21 17:55:39 +00:00
|
|
|
#include <libtransmission/error.h>
|
2014-07-08 00:08:43 +00:00
|
|
|
#include <libtransmission/file.h>
|
2022-03-17 22:39:06 +00:00
|
|
|
#include <libtransmission/log.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/makemeta.h>
|
|
|
|
#include <libtransmission/tr-getopt.h>
|
2022-04-07 22:26:59 +00:00
|
|
|
#include <libtransmission/tr-strbuf.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2010-08-08 22:53:24 +00:00
|
|
|
#include <libtransmission/version.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2014-06-10 00:43:21 +00:00
|
|
|
#include "units.h"
|
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
char constexpr MyName[] = "transmission-create";
|
|
|
|
char constexpr Usage[] = "Usage: transmission-create [options] <file|directory>";
|
2021-12-17 20:48:02 +00:00
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
uint32_t constexpr KiB = 1024;
|
2021-12-17 20:48:02 +00:00
|
|
|
|
2022-02-12 18:50:47 +00:00
|
|
|
auto constexpr Options = std::array<tr_option, 9>{
|
2021-12-17 20:48:02 +00:00
|
|
|
{ { 'p', "private", "Allow this torrent to only be used with the specified tracker(s)", "p", false, nullptr },
|
|
|
|
{ 'r', "source", "Set the source for private trackers", "r", true, "<source>" },
|
|
|
|
{ 'o', "outfile", "Save the generated .torrent to this filename", "o", true, "<file>" },
|
|
|
|
{ 's', "piecesize", "Set the piece size in KiB, overriding the preferred default", "s", true, "<KiB>" },
|
|
|
|
{ 'c', "comment", "Add a comment", "c", true, "<comment>" },
|
|
|
|
{ 't', "tracker", "Add a tracker's announce URL", "t", true, "<url>" },
|
2022-02-12 18:50:47 +00:00
|
|
|
{ 'w', "webseed", "Add a webseed URL", "w", true, "<url>" },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr } }
|
2010-06-16 14:27:24 +00:00
|
|
|
};
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
struct app_options
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
std::vector<tr_tracker_info> trackers;
|
2022-02-12 18:50:47 +00:00
|
|
|
std::vector<char const*> webseeds;
|
2022-01-02 17:51:59 +00:00
|
|
|
std::string outfile;
|
2021-12-17 20:48:02 +00:00
|
|
|
char const* comment = nullptr;
|
|
|
|
char const* infile = nullptr;
|
|
|
|
char const* source = nullptr;
|
2022-01-02 17:51:59 +00:00
|
|
|
uint32_t piecesize_kib = 0;
|
|
|
|
bool is_private = false;
|
|
|
|
bool show_version = false;
|
2021-12-17 20:48:02 +00:00
|
|
|
};
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
int parseCommandLine(app_options& options, int argc, char const* const* argv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &optarg)) != TR_OPT_DONE)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (c)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'V':
|
2021-12-17 20:48:02 +00:00
|
|
|
options.show_version = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'p':
|
2021-12-17 20:48:02 +00:00
|
|
|
options.is_private = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'o':
|
2021-12-17 20:48:02 +00:00
|
|
|
options.outfile = optarg;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'c':
|
2021-12-17 20:48:02 +00:00
|
|
|
options.comment = optarg;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 't':
|
2022-02-12 18:50:47 +00:00
|
|
|
options.trackers.push_back(tr_tracker_info{ 0, const_cast<char*>(optarg) });
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
options.webseeds.push_back(optarg);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 's':
|
2021-10-06 16:32:17 +00:00
|
|
|
if (optarg != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
char* endptr = nullptr;
|
2021-12-17 20:48:02 +00:00
|
|
|
options.piecesize_kib = strtoul(optarg, &endptr, 10);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (endptr != nullptr && *endptr == 'M')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
options.piecesize_kib *= KiB;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-17 18:55:51 +00:00
|
|
|
break;
|
|
|
|
|
2021-10-18 23:05:39 +00:00
|
|
|
case 'r':
|
2021-12-17 20:48:02 +00:00
|
|
|
options.source = optarg;
|
2021-10-18 23:05:39 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_OPT_UNK:
|
2021-12-17 20:48:02 +00:00
|
|
|
options.infile = optarg;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
return 1;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
char* tr_getcwd(void)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* result;
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
result = tr_sys_dir_get_current(&error);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (result == nullptr)
|
2011-02-09 06:10:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "getcwd error: \"%s\"", error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
result = tr_strdup("");
|
2011-02-09 06:10:01 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return result;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_metainfo_builder* b = nullptr;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logSetLevel(TR_LOG_ERROR);
|
2021-12-28 17:23:47 +00:00
|
|
|
tr_formatter_mem_init(MemK, MemKStr, MemMStr, MemGStr, MemTStr);
|
|
|
|
tr_formatter_size_init(DiskK, DiskKStr, DiskMStr, DiskGStr, DiskTStr);
|
|
|
|
tr_formatter_speed_init(SpeedK, SpeedKStr, SpeedMStr, SpeedGStr, SpeedTStr);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
auto options = app_options{};
|
|
|
|
if (parseCommandLine(options, argc, (char const* const*)argv) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.show_version)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
fprintf(stderr, "%s %s\n", MyName, LONG_VERSION_STRING);
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_SUCCESS;
|
2010-08-08 22:53:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.infile == nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: No input file or directory specified.\n");
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_getopt_usage(MyName, Usage, std::data(Options));
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
if (std::empty(options.outfile))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2022-03-21 20:22:50 +00:00
|
|
|
auto const base = tr_sys_path_basename(options.infile, &error);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
if (std::empty(base))
|
2016-03-13 10:41:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: Cannot deduce output path from input path: %s\n", error->message);
|
|
|
|
return EXIT_FAILURE;
|
2016-03-13 10:41:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 22:26:59 +00:00
|
|
|
char* const cwd = tr_getcwd();
|
|
|
|
options.outfile = tr_strvDup(tr_pathbuf{ std::string_view{ cwd }, '/', base, ".torrent"sv });
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(cwd);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (std::empty(options.trackers))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.is_private)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: no trackers specified for a private torrent\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("WARNING: no trackers specified\n");
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
printf("Creating torrent \"%s\"\n", options.outfile.c_str());
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
b = tr_metaInfoBuilderCreate(options.infile);
|
2013-01-17 18:55:51 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (b == nullptr)
|
2018-01-24 20:17:05 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Cannot find specified input file or directory.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-02-23 23:38:07 +00:00
|
|
|
for (uint32_t i = 0; i < b->fileCount; ++i)
|
|
|
|
{
|
|
|
|
if (auto const& file = b->files[i]; !file.is_portable)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WARNING: consider renaming nonportable filename \"%s\".\n", file.filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.piecesize_kib != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_metaInfoBuilderSetPieceSize(b, options.piecesize_kib * KiB);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
b->fileCount > 1 ? " %" PRIu32 " files, %s\n" : " %" PRIu32 " file, %s\n",
|
|
|
|
b->fileCount,
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_formatter_size_B(b->totalSize).c_str());
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
b->pieceCount > 1 ? " %" PRIu32 " pieces, %s each\n" : " %" PRIu32 " piece, %s\n",
|
|
|
|
b->pieceCount,
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_formatter_size_B(b->pieceSize).c_str());
|
2021-08-07 10:15:06 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_makeMetaInfo(
|
|
|
|
b,
|
2022-01-02 17:51:59 +00:00
|
|
|
options.outfile.c_str(),
|
2021-12-17 20:48:02 +00:00
|
|
|
std::data(options.trackers),
|
2022-02-14 05:44:38 +00:00
|
|
|
static_cast<int>(std::size(options.trackers)),
|
2022-02-12 18:50:47 +00:00
|
|
|
std::data(options.webseeds),
|
2022-02-14 05:44:38 +00:00
|
|
|
static_cast<int>(std::size(options.webseeds)),
|
2021-12-17 20:48:02 +00:00
|
|
|
options.comment,
|
|
|
|
options.is_private,
|
|
|
|
options.source);
|
2013-01-17 18:55:51 +00:00
|
|
|
|
2021-08-07 10:15:06 +00:00
|
|
|
uint32_t last = UINT32_MAX;
|
2017-04-19 12:04:45 +00:00
|
|
|
while (!b->isDone)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_wait_msec(500);
|
2021-08-07 10:15:06 +00:00
|
|
|
|
|
|
|
uint32_t current = b->pieceIndex;
|
|
|
|
if (current != last)
|
|
|
|
{
|
|
|
|
printf("\rPiece %" PRIu32 "/%" PRIu32 " ...", current, b->pieceCount);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
last = current;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
putc(' ', stdout);
|
|
|
|
|
|
|
|
switch (b->result)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-02-09 02:25:19 +00:00
|
|
|
case TrMakemetaResult::OK:
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("done!");
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case TrMakemetaResult::ERR_URL:
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("bad announce URL: \"%s\"", b->errfile);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case TrMakemetaResult::ERR_IO_READ:
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("error reading \"%s\": %s", b->errfile, tr_strerror(b->my_errno));
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case TrMakemetaResult::ERR_IO_WRITE:
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("error writing \"%s\": %s", b->errfile, tr_strerror(b->my_errno));
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
case TrMakemetaResult::CANCELLED:
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("cancelled");
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
putc('\n', stdout);
|
|
|
|
|
|
|
|
tr_metaInfoBuilderFree(b);
|
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|