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-08-17 16:08:36 +00:00
|
|
|
#include <cstdlib> // for strtoul()
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <chrono>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstdint> // for uint32_t
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <future>
|
2022-01-02 17:51:59 +00:00
|
|
|
#include <string>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string_view>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
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>
|
|
|
|
#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-07-19 21:48:24 +00:00
|
|
|
auto constexpr Options = std::array<tr_option, 10>{
|
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>" },
|
2022-07-19 21:48:24 +00:00
|
|
|
{ 'x', "anonymize", "Omit \"Creation date\" and \"Created by\" info", nullptr, false, nullptr },
|
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
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
tr_announce_list trackers;
|
|
|
|
std::vector<std::string> webseeds;
|
2022-01-02 17:51:59 +00:00
|
|
|
std::string outfile;
|
2022-08-02 19:46:08 +00:00
|
|
|
std::string_view comment;
|
|
|
|
std::string_view infile;
|
|
|
|
std::string_view source;
|
|
|
|
uint32_t piece_size = 0;
|
|
|
|
bool anonymize = false;
|
2022-01-02 17:51:59 +00:00
|
|
|
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-08-02 19:46:08 +00:00
|
|
|
options.trackers.add(optarg, options.trackers.nextTier());
|
2022-02-12 18:50:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
2022-08-02 19:46:08 +00:00
|
|
|
options.webseeds.emplace_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;
|
2022-08-02 19:46:08 +00:00
|
|
|
options.piece_size = strtoul(optarg, &endptr, 10) * KiB;
|
2021-10-06 16:32:17 +00:00
|
|
|
if (endptr != nullptr && *endptr == 'M')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
options.piece_size *= 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;
|
|
|
|
|
2022-07-19 21:48:24 +00:00
|
|
|
case 'x':
|
|
|
|
options.anonymize = true;
|
|
|
|
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-08-02 19:46:08 +00:00
|
|
|
std::string tr_getcwd()
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2022-08-13 17:11:07 +00:00
|
|
|
auto cur = tr_sys_dir_get_current(&error);
|
2022-09-28 15:22:58 +00:00
|
|
|
if (error != nullptr)
|
2011-02-09 06:10:01 +00:00
|
|
|
{
|
2022-08-13 17:11:07 +00:00
|
|
|
fprintf(stderr, "getcwd error: \"%s\"", error->message);
|
|
|
|
tr_error_free(error);
|
2011-02-09 06:10:01 +00:00
|
|
|
}
|
2022-08-13 17:11:07 +00:00
|
|
|
return cur;
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
if (std::empty(options.infile))
|
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-08-02 19:46:08 +00:00
|
|
|
options.outfile = fmt::format("{:s}/{:s}.torrent"sv, tr_getcwd(), 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
fmt::print("Creating torrent \"{:s}\"\n", options.outfile);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
auto builder = tr_metainfo_builder(options.infile);
|
|
|
|
auto const n_files = builder.fileCount();
|
|
|
|
if (n_files == 0U)
|
2018-01-24 20:17:05 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Cannot find specified input file or directory.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
for (tr_file_index_t i = 0; i < n_files; ++i)
|
2022-02-23 23:38:07 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const& path = builder.path(i);
|
|
|
|
if (!tr_torrent_files::isSubpathPortable(path))
|
2022-02-23 23:38:07 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
fmt::print(stderr, "WARNING\n");
|
|
|
|
fmt::print(stderr, "filename \"{:s}\" may not be portable on all systems.\n", path);
|
|
|
|
fmt::print(stderr, "consider \"{:s}\" instead.\n", tr_torrent_files::makeSubpathPortable(path));
|
2022-02-23 23:38:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
if (options.piece_size != 0 && !builder.setPieceSize(options.piece_size))
|
|
|
|
{
|
|
|
|
fmt::print(stderr, "ERROR: piece size must be at least 16 KiB and must be a power of two.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt::print(
|
2022-12-26 21:13:21 +00:00
|
|
|
tr_ngettext("{file_count:L} file, {total_size}\n", "{file_count:L} files, {total_size}\n", builder.fileCount()),
|
2022-09-28 15:22:58 +00:00
|
|
|
fmt::arg("file_count", builder.fileCount()),
|
|
|
|
fmt::arg("total_size", tr_formatter_size_B(builder.totalSize())));
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
fmt::print(
|
2022-12-26 21:13:21 +00:00
|
|
|
tr_ngettext(
|
|
|
|
"{piece_count:L} piece, {piece_size}\n",
|
|
|
|
"{piece_count:L} pieces, {piece_size} each\n",
|
|
|
|
builder.pieceCount()),
|
2022-09-28 15:22:58 +00:00
|
|
|
fmt::arg("piece_count", builder.pieceCount()),
|
|
|
|
fmt::arg("piece_size", tr_formatter_size_B(builder.pieceSize())));
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
if (!std::empty(options.comment))
|
|
|
|
{
|
|
|
|
builder.setComment(options.comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!std::empty(options.source))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
builder.setSource(options.source);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
builder.setPrivate(options.is_private);
|
|
|
|
builder.setAnonymize(options.anonymize);
|
|
|
|
builder.setWebseeds(std::move(options.webseeds));
|
|
|
|
builder.setAnnounceList(std::move(options.trackers));
|
|
|
|
|
|
|
|
auto future = builder.makeChecksums();
|
|
|
|
auto last = std::optional<tr_piece_index_t>{};
|
|
|
|
while (future.wait_for(std::chrono::milliseconds(500)) != std::future_status::ready)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const [current, total] = builder.checksumStatus();
|
2021-08-07 10:15:06 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
if (!last || current != *last)
|
2021-08-07 10:15:06 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
fmt::print("\rPiece {:d}/{:d} ...", current, total);
|
2021-08-07 10:15:06 +00:00
|
|
|
fflush(stdout);
|
|
|
|
last = current;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
fmt::print(" ");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
if (tr_error* error = future.get(); error != nullptr)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
fmt::print("ERROR: {:s} {:d}\n", error->message, error->code);
|
|
|
|
tr_error_free(error);
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
if (tr_error* error = nullptr; !builder.save(options.outfile, &error))
|
|
|
|
{
|
|
|
|
fmt::print("ERROR: could not save \"{:s}\": {:s} {:d}\n", options.outfile, error->message, error->code);
|
|
|
|
tr_error_free(error);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
fmt::print("done!\n");
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|