Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / utils / create.c
blob6419d1e8bfafa32bcc44bbf304ca8e9947c78823
1 /*
2 * This file Copyright (C) 2010 Mnemosyne LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 * $Id: create.c 11266 2010-09-27 15:07:57Z charles $
13 #include <stdio.h>
14 #include <unistd.h> /* getcwd() */
16 #include <libtransmission/transmission.h>
17 #include <libtransmission/makemeta.h>
18 #include <libtransmission/tr-getopt.h>
19 #include <libtransmission/utils.h>
20 #include <libtransmission/version.h>
22 #define MY_NAME "transmission-create"
24 #define MAX_TRACKERS 128
25 static tr_tracker_info trackers[MAX_TRACKERS];
26 static int trackerCount = 0;
27 static tr_bool isPrivate = FALSE;
28 static tr_bool showVersion = FALSE;
29 const char * comment = NULL;
30 const char * outfile = NULL;
31 const char * infile = NULL;
33 static tr_option options[] =
35 { 'p', "private", "Allow this torrent to only be used with the specified tracker(s)", "p", 0, NULL },
36 { 'o', "outfile", "Save the generated .torrent to this filename", "o", 1, "<file>" },
37 { 'c', "comment", "Add a comment", "c", 1, "<comment>" },
38 { 't', "tracker", "Add a tracker's announce URL", "t", 1, "<url>" },
39 { 'V', "version", "Show version number and exit", "V", 0, NULL },
40 { 0, NULL, NULL, NULL, 0, NULL }
43 static const char *
44 getUsage( void )
46 return "Usage: " MY_NAME " [options] <file|directory>";
49 static int
50 parseCommandLine( int argc, const char ** argv )
52 int c;
53 const char * optarg;
55 while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg )))
57 switch( c )
59 case 'V': showVersion = TRUE; break;
60 case 'p': isPrivate = TRUE; break;
61 case 'o': outfile = optarg; break;
62 case 'c': comment = optarg; break;
63 case 't': if( trackerCount + 1 < MAX_TRACKERS ) {
64 trackers[trackerCount].tier = trackerCount;
65 trackers[trackerCount].announce = (char*) optarg;
66 ++trackerCount;
68 break;
69 case TR_OPT_UNK: infile = optarg; break;
70 default: return 1;
74 return 0;
77 static char*
78 tr_getcwd( void )
80 char buf[2048];
81 *buf = '\0';
82 #ifdef WIN32
83 _getcwd( buf, sizeof( buf ) );
84 #else
85 getcwd( buf, sizeof( buf ) );
86 #endif
87 return tr_strdup( buf );
90 int
91 main( int argc, char * argv[] )
93 char * out2 = NULL;
94 tr_metainfo_builder * b = NULL;
96 tr_setMessageLevel( TR_MSG_ERR );
98 if( parseCommandLine( argc, (const char**)argv ) )
99 return EXIT_FAILURE;
101 if( showVersion ) {
102 fprintf( stderr, MY_NAME" "LONG_VERSION_STRING"\n" );
103 return 0;
106 if( !infile )
108 fprintf( stderr, "ERROR: No input file or directory specified.\n" );
109 tr_getopt_usage( MY_NAME, getUsage( ), options );
110 fprintf( stderr, "\n" );
111 return EXIT_FAILURE;
114 if( outfile == NULL )
116 char * base = tr_basename( infile );
117 char * end = tr_strdup_printf( "%s.torrent", base );
118 char * cwd = tr_getcwd( );
119 outfile = out2 = tr_buildPath( cwd, end, NULL );
120 tr_free( cwd );
121 tr_free( end );
122 tr_free( base );
125 if( !trackerCount )
127 if( isPrivate )
129 fprintf( stderr, "ERROR: no trackers specified for a private torrent\n" );
130 return EXIT_FAILURE;
132 else
134 printf( "WARNING: no trackers specified\n" );
138 printf( "Creating torrent \"%s\" ...", outfile );
139 fflush( stdout );
141 b = tr_metaInfoBuilderCreate( infile );
142 tr_makeMetaInfo( b, outfile, trackers, trackerCount, comment, isPrivate );
143 while( !b->isDone ) {
144 tr_wait_msec( 500 );
145 putc( '.', stdout );
146 fflush( stdout );
149 putc( ' ', stdout );
150 switch( b->result ) {
151 case TR_MAKEMETA_OK: printf( "done!" ); break;
152 case TR_MAKEMETA_URL: printf( "bad announce URL: \"%s\"", b->errfile ); break;
153 case TR_MAKEMETA_IO_READ: printf( "error reading \"%s\": %s", b->errfile, tr_strerror(b->my_errno) ); break;
154 case TR_MAKEMETA_IO_WRITE: printf( "error writing \"%s\": %s", b->errfile, tr_strerror(b->my_errno) ); break;
155 case TR_MAKEMETA_CANCELLED: printf( "cancelled" ); break;
157 putc( '\n', stdout );
159 tr_metaInfoBuilderFree( b );
160 tr_free( out2 );
161 return 0;