2010-06-16 14:27:24 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2012-2014 Mnemosyne LLC
|
2010-06-16 14:27:24 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2010-06-16 14:27:24 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <array>
|
2013-01-17 18:55:51 +00:00
|
|
|
#include <stdio.h> /* fprintf() */
|
|
|
|
#include <stdlib.h> /* strtoul(), EXIT_FAILURE */
|
2021-08-07 10:15:06 +00:00
|
|
|
#include <inttypes.h> /* PRIu32 */
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2014-09-21 17:55:39 +00:00
|
|
|
#include <libtransmission/error.h>
|
2014-07-08 00:08:43 +00:00
|
|
|
#include <libtransmission/file.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/makemeta.h>
|
|
|
|
#include <libtransmission/tr-getopt.h>
|
|
|
|
#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;
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
static char constexpr MyName[] = "transmission-create";
|
|
|
|
static char constexpr Usage[] = "Usage: transmission-create [options] <file|directory>";
|
|
|
|
|
|
|
|
static uint32_t constexpr KiB = 1024;
|
|
|
|
|
|
|
|
static auto constexpr Options = std::array<tr_option, 8>{
|
|
|
|
{ { '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>" },
|
|
|
|
{ '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;
|
|
|
|
bool is_private = false;
|
|
|
|
bool show_version = false;
|
|
|
|
char const* comment = nullptr;
|
|
|
|
char const* outfile = nullptr;
|
|
|
|
char const* infile = nullptr;
|
|
|
|
uint32_t piecesize_kib = 0;
|
|
|
|
char const* source = nullptr;
|
|
|
|
};
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
static 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':
|
2021-12-17 20:48:02 +00:00
|
|
|
options.trackers.push_back(tr_tracker_info{ 0, const_cast<char*>(optarg), nullptr, 0 });
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
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
|
|
|
char* out2 = nullptr;
|
|
|
|
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);
|
|
|
|
tr_formatter_mem_init(MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR);
|
|
|
|
tr_formatter_size_init(DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR);
|
|
|
|
tr_formatter_speed_init(SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR);
|
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
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
if (options.outfile == nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2021-12-17 20:48:02 +00:00
|
|
|
char* base = tr_sys_path_basename(options.infile, &error);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (base == nullptr)
|
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
|
|
|
}
|
|
|
|
|
2021-12-16 02:09:46 +00:00
|
|
|
auto const end = tr_strvJoin(base, ".torrent"sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
char* cwd = tr_getcwd();
|
2021-12-17 20:48:02 +00:00
|
|
|
options.outfile = out2 = tr_buildPath(cwd, end.c_str(), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(cwd);
|
|
|
|
tr_free(base);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
printf("Creating torrent \"%s\"\n", options.outfile);
|
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;
|
|
|
|
}
|
|
|
|
|
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-07 10:15:06 +00:00
|
|
|
char buf[128];
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
b->fileCount > 1 ? " %" PRIu32 " files, %s\n" : " %" PRIu32 " file, %s\n",
|
|
|
|
b->fileCount,
|
2021-08-07 10:15:06 +00:00
|
|
|
tr_formatter_size_B(buf, b->totalSize, sizeof(buf)));
|
2021-08-15 09:41:48 +00:00
|
|
|
printf(
|
|
|
|
b->pieceCount > 1 ? " %" PRIu32 " pieces, %s each\n" : " %" PRIu32 " piece, %s\n",
|
|
|
|
b->pieceCount,
|
2021-08-07 10:15:06 +00:00
|
|
|
tr_formatter_size_B(buf, b->pieceSize, sizeof(buf)));
|
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_makeMetaInfo(
|
|
|
|
b,
|
|
|
|
options.outfile,
|
|
|
|
std::data(options.trackers),
|
|
|
|
std::size(options.trackers),
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_MAKEMETA_OK:
|
|
|
|
printf("done!");
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_MAKEMETA_URL:
|
|
|
|
printf("bad announce URL: \"%s\"", b->errfile);
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_MAKEMETA_IO_READ:
|
|
|
|
printf("error reading \"%s\": %s", b->errfile, tr_strerror(b->my_errno));
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_MAKEMETA_IO_WRITE:
|
|
|
|
printf("error writing \"%s\": %s", b->errfile, tr_strerror(b->my_errno));
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_MAKEMETA_CANCELLED:
|
|
|
|
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);
|
|
|
|
tr_free(out2);
|
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|