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
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2013-01-17 18:55:51 +00:00
|
|
|
#include <stdio.h> /* fprintf() */
|
|
|
|
#include <stdlib.h> /* strtoul(), EXIT_FAILURE */
|
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"
|
|
|
|
|
2010-06-16 14:27:24 +00:00
|
|
|
#define MY_NAME "transmission-create"
|
|
|
|
|
|
|
|
#define MAX_TRACKERS 128
|
2013-01-17 18:55:51 +00:00
|
|
|
static const uint32_t KiB = 1024;
|
2010-06-16 14:27:24 +00:00
|
|
|
static tr_tracker_info trackers[MAX_TRACKERS];
|
|
|
|
static int trackerCount = 0;
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool isPrivate = false;
|
|
|
|
static bool showVersion = false;
|
2013-01-17 18:55:51 +00:00
|
|
|
static const char * comment = NULL;
|
|
|
|
static const char * outfile = NULL;
|
|
|
|
static const char * infile = NULL;
|
|
|
|
static uint32_t piecesize_kib = 0;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
static tr_option options[] =
|
|
|
|
{
|
|
|
|
{ 'p', "private", "Allow this torrent to only be used with the specified tracker(s)", "p", 0, NULL },
|
|
|
|
{ 'o', "outfile", "Save the generated .torrent to this filename", "o", 1, "<file>" },
|
2013-01-17 18:55:51 +00:00
|
|
|
{ 's', "piecesize", "Set how many KiB each piece should be, overriding the preferred default", "s", 1, "<size in KiB>" },
|
2010-06-16 14:27:24 +00:00
|
|
|
{ 'c', "comment", "Add a comment", "c", 1, "<comment>" },
|
|
|
|
{ 't', "tracker", "Add a tracker's announce URL", "t", 1, "<url>" },
|
2010-08-08 22:53:24 +00:00
|
|
|
{ 'V', "version", "Show version number and exit", "V", 0, NULL },
|
2010-06-16 14:27:24 +00:00
|
|
|
{ 0, NULL, NULL, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
getUsage (void)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return "Usage: " MY_NAME " [options] <file|directory>";
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-12-06 22:39:14 +00:00
|
|
|
parseCommandLine (int argc, const char * const * argv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int c;
|
|
|
|
const char * optarg;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while ((c = tr_getopt (getUsage (), argc, argv, options, &optarg)))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (c)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
case 'V':
|
|
|
|
showVersion = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
isPrivate = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
outfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
comment = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (trackerCount + 1 < MAX_TRACKERS)
|
|
|
|
{
|
|
|
|
trackers[trackerCount].tier = trackerCount;
|
|
|
|
trackers[trackerCount].announce = (char*) optarg;
|
|
|
|
++trackerCount;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-01-17 18:55:51 +00:00
|
|
|
case 's':
|
2015-01-02 11:15:31 +00:00
|
|
|
if (optarg)
|
|
|
|
{
|
2013-01-17 18:55:51 +00:00
|
|
|
char * endptr = NULL;
|
|
|
|
piecesize_kib = strtoul (optarg, &endptr, 10);
|
|
|
|
if (endptr && *endptr=='M')
|
2015-01-02 11:15:31 +00:00
|
|
|
piecesize_kib *= KiB;
|
2013-01-17 18:55:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
case TR_OPT_UNK:
|
|
|
|
infile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 1;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return 0;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getcwd (void)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * result;
|
2014-09-21 17:55:39 +00:00
|
|
|
tr_error * error = NULL;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2014-09-21 17:55:39 +00:00
|
|
|
result = tr_sys_dir_get_current (&error);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
if (result == NULL)
|
2011-02-09 06:10:01 +00:00
|
|
|
{
|
2014-09-21 17:55:39 +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
|
|
|
|
2014-09-21 17:55:39 +00:00
|
|
|
return result;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-04-21 10:07:57 +00:00
|
|
|
tr_main (int argc,
|
|
|
|
char * argv[])
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * out2 = NULL;
|
|
|
|
tr_metainfo_builder * b = NULL;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logSetLevel (TR_LOG_ERROR);
|
2014-06-10 00:43:21 +00:00
|
|
|
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
|
|
|
|
2015-12-06 22:13:10 +00:00
|
|
|
if (parseCommandLine (argc, (const char* const *)argv))
|
2012-12-05 17:29:46 +00:00
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (showVersion)
|
|
|
|
{
|
|
|
|
fprintf (stderr, MY_NAME" "LONG_VERSION_STRING"\n");
|
|
|
|
return EXIT_SUCCESS;
|
2010-08-08 22:53:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!infile)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
fprintf (stderr, "ERROR: No input file or directory specified.\n");
|
|
|
|
tr_getopt_usage (MY_NAME, getUsage (), options);
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (outfile == NULL)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2014-07-08 00:08:43 +00:00
|
|
|
char * base = tr_sys_path_basename (infile, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
char * end = tr_strdup_printf ("%s.torrent", base);
|
|
|
|
char * cwd = tr_getcwd ();
|
|
|
|
outfile = out2 = tr_buildPath (cwd, end, NULL);
|
|
|
|
tr_free (cwd);
|
|
|
|
tr_free (end);
|
|
|
|
tr_free (base);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!trackerCount)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isPrivate)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +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
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("WARNING: no trackers specified\n");
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
printf ("Creating torrent \"%s\" ...", outfile);
|
|
|
|
fflush (stdout);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
b = tr_metaInfoBuilderCreate (infile);
|
2013-01-17 18:55:51 +00:00
|
|
|
|
|
|
|
if (piecesize_kib != 0)
|
|
|
|
tr_metaInfoBuilderSetPieceSize (b, piecesize_kib * KiB);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_makeMetaInfo (b, outfile, trackers, trackerCount, comment, isPrivate);
|
|
|
|
while (!b->isDone)
|
|
|
|
{
|
|
|
|
tr_wait_msec (500);
|
|
|
|
putc ('.', stdout);
|
|
|
|
fflush (stdout);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
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;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
putc ('\n', stdout);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_metaInfoBuilderFree (b);
|
|
|
|
tr_free (out2);
|
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|