transmission/utils/create.cc

272 lines
6.9 KiB
C++
Raw Normal View History

// This file Copyright © 2012-2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
2021-12-17 20:48:02 +00:00
#include <array>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <libtransmission/transmission.h>
#include <libtransmission/error.h>
#include <libtransmission/file.h>
#include <libtransmission/makemeta.h>
#include <libtransmission/tr-getopt.h>
#include <libtransmission/utils.h>
#include <libtransmission/version.h>
#include "units.h"
using namespace std::literals;
namespace
{
char constexpr MyName[] = "transmission-create";
char constexpr Usage[] = "Usage: transmission-create [options] <file|directory>";
2021-12-17 20:48:02 +00:00
uint32_t constexpr KiB = 1024;
2021-12-17 20:48:02 +00:00
auto constexpr Options = std::array<tr_option, 8>{
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>" },
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
{ 0, nullptr, nullptr, nullptr, false, nullptr } }
};
2021-12-17 20:48:02 +00:00
struct app_options
{
2021-12-17 20:48:02 +00:00
std::vector<tr_tracker_info> trackers;
std::string outfile;
2021-12-17 20:48:02 +00:00
char const* comment = nullptr;
char const* infile = nullptr;
char const* source = nullptr;
uint32_t piecesize_kib = 0;
bool is_private = false;
bool show_version = false;
2021-12-17 20:48:02 +00:00
};
int parseCommandLine(app_options& options, int argc, char const* const* argv)
{
int c;
char const* optarg;
2021-12-17 20:48:02 +00:00
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &optarg)) != TR_OPT_DONE)
{
switch (c)
{
case 'V':
2021-12-17 20:48:02 +00:00
options.show_version = true;
break;
case 'p':
2021-12-17 20:48:02 +00:00
options.is_private = true;
break;
case 'o':
2021-12-17 20:48:02 +00:00
options.outfile = optarg;
break;
case 'c':
2021-12-17 20:48:02 +00:00
options.comment = optarg;
break;
case 't':
2021-12-17 20:48:02 +00:00
options.trackers.push_back(tr_tracker_info{ 0, const_cast<char*>(optarg), nullptr, 0 });
break;
case 's':
if (optarg != nullptr)
{
char* endptr = nullptr;
2021-12-17 20:48:02 +00:00
options.piecesize_kib = strtoul(optarg, &endptr, 10);
if (endptr != nullptr && *endptr == 'M')
{
2021-12-17 20:48:02 +00:00
options.piecesize_kib *= KiB;
}
}
break;
case 'r':
2021-12-17 20:48:02 +00:00
options.source = optarg;
break;
case TR_OPT_UNK:
2021-12-17 20:48:02 +00:00
options.infile = optarg;
break;
default:
return 1;
}
}
return 0;
}
char* tr_getcwd(void)
{
char* result;
tr_error* error = nullptr;
result = tr_sys_dir_get_current(&error);
if (result == nullptr)
{
fprintf(stderr, "getcwd error: \"%s\"", error->message);
tr_error_free(error);
result = tr_strdup("");
}
return result;
}
} // namespace
int tr_main(int argc, char* argv[])
{
tr_metainfo_builder* b = nullptr;
tr_logSetLevel(TR_LOG_ERROR);
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);
2021-12-17 20:48:02 +00:00
auto options = app_options{};
if (parseCommandLine(options, argc, (char const* const*)argv) != 0)
{
return EXIT_FAILURE;
}
2021-12-17 20:48:02 +00:00
if (options.show_version)
{
2021-12-17 20:48:02 +00:00
fprintf(stderr, "%s %s\n", MyName, LONG_VERSION_STRING);
return EXIT_SUCCESS;
}
2021-12-17 20:48:02 +00:00
if (options.infile == nullptr)
{
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));
fprintf(stderr, "\n");
return EXIT_FAILURE;
}
if (std::empty(options.outfile))
{
tr_error* error = nullptr;
2021-12-17 20:48:02 +00:00
char* base = tr_sys_path_basename(options.infile, &error);
if (base == nullptr)
{
fprintf(stderr, "ERROR: Cannot deduce output path from input path: %s\n", error->message);
return EXIT_FAILURE;
}
auto const end = tr_strvJoin(base, ".torrent"sv);
char* cwd = tr_getcwd();
options.outfile = tr_strvDup(tr_strvPath(cwd, end.c_str()));
tr_free(cwd);
tr_free(base);
}
2021-12-17 20:48:02 +00:00
if (std::empty(options.trackers))
{
2021-12-17 20:48:02 +00:00
if (options.is_private)
{
fprintf(stderr, "ERROR: no trackers specified for a private torrent\n");
return EXIT_FAILURE;
}
else
{
printf("WARNING: no trackers specified\n");
}
}
printf("Creating torrent \"%s\"\n", options.outfile.c_str());
2021-12-17 20:48:02 +00:00
b = tr_metaInfoBuilderCreate(options.infile);
if (b == nullptr)
{
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)
{
2021-12-17 20:48:02 +00:00
tr_metaInfoBuilderSetPieceSize(b, options.piecesize_kib * KiB);
}
printf(
b->fileCount > 1 ? " %" PRIu32 " files, %s\n" : " %" PRIu32 " file, %s\n",
b->fileCount,
tr_formatter_size_B(b->totalSize).c_str());
printf(
b->pieceCount > 1 ? " %" PRIu32 " pieces, %s each\n" : " %" PRIu32 " piece, %s\n",
b->pieceCount,
tr_formatter_size_B(b->pieceSize).c_str());
2021-12-17 20:48:02 +00:00
tr_makeMetaInfo(
b,
options.outfile.c_str(),
2021-12-17 20:48:02 +00:00
std::data(options.trackers),
std::size(options.trackers),
options.comment,
options.is_private,
options.source);
uint32_t last = UINT32_MAX;
while (!b->isDone)
{
tr_wait_msec(500);
uint32_t current = b->pieceIndex;
if (current != last)
{
printf("\rPiece %" PRIu32 "/%" PRIu32 " ...", current, b->pieceCount);
fflush(stdout);
last = current;
}
}
putc(' ', stdout);
switch (b->result)
{
case TR_MAKEMETA_OK:
printf("done!");
break;
case TR_MAKEMETA_URL:
printf("bad announce URL: \"%s\"", b->errfile);
break;
case TR_MAKEMETA_IO_READ:
printf("error reading \"%s\": %s", b->errfile, tr_strerror(b->my_errno));
break;
case TR_MAKEMETA_IO_WRITE:
printf("error writing \"%s\": %s", b->errfile, tr_strerror(b->my_errno));
break;
case TR_MAKEMETA_CANCELLED:
printf("cancelled");
break;
}
putc('\n', stdout);
tr_metaInfoBuilderFree(b);
return EXIT_SUCCESS;
}