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 $
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
}
46 return "Usage: " MY_NAME
" [options] <file|directory>";
50 parseCommandLine( int argc
, const char ** argv
)
55 while(( c
= tr_getopt( getUsage( ), argc
, argv
, options
, &optarg
)))
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
;
69 case TR_OPT_UNK
: infile
= optarg
; break;
83 _getcwd( buf
, sizeof( buf
) );
85 getcwd( buf
, sizeof( buf
) );
87 return tr_strdup( buf
);
91 main( int argc
, char * argv
[] )
94 tr_metainfo_builder
* b
= NULL
;
96 tr_setMessageLevel( TR_MSG_ERR
);
98 if( parseCommandLine( argc
, (const char**)argv
) )
102 fprintf( stderr
, MY_NAME
" "LONG_VERSION_STRING
"\n" );
108 fprintf( stderr
, "ERROR: No input file or directory specified.\n" );
109 tr_getopt_usage( MY_NAME
, getUsage( ), options
);
110 fprintf( stderr
, "\n" );
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
);
129 fprintf( stderr
, "ERROR: no trackers specified for a private torrent\n" );
134 printf( "WARNING: no trackers specified\n" );
138 printf( "Creating torrent \"%s\" ...", outfile
);
141 b
= tr_metaInfoBuilderCreate( infile
);
142 tr_makeMetaInfo( b
, outfile
, trackers
, trackerCount
, comment
, isPrivate
);
143 while( !b
->isDone
) {
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
);