source: trunk/utils/create.c @ 11709

Last change on this file since 11709 was 11709, checked in by jordan, 11 years ago

Update the copyright year in the source code comments.

The Berne Convention says that the copyright year is moot, so instead of adding another year to each file as in previous years, I've removed the year altogether from the source code comments in libtransmission, gtk, qt, utils, daemon, and cli.

Juliusz's copyright notice in tr-dht and Johannes' copyright notice in tr-lpd have been left alone; it didn't seem appropriate to modify them.

  • Property svn:keywords set to Date Rev Author Id
File size: 4.4 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
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.
7 *
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9 *
10 * $Id: create.c 11709 2011-01-19 13:48:47Z jordan $
11 */
12
13#include <stdio.h>
14#include <unistd.h> /* getcwd() */
15
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>
21
22#define MY_NAME "transmission-create"
23
24#define MAX_TRACKERS 128
25static tr_tracker_info trackers[MAX_TRACKERS];
26static int trackerCount = 0;
27static tr_bool isPrivate = FALSE;
28static tr_bool showVersion = FALSE;
29const char * comment = NULL;
30const char * outfile = NULL;
31const char * infile = NULL;
32
33static tr_option options[] =
34{
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 }
41};
42
43static const char *
44getUsage( void )
45{
46    return "Usage: " MY_NAME " [options] <file|directory>";
47}
48
49static int
50parseCommandLine( int argc, const char ** argv )
51{
52    int c;
53    const char * optarg;
54
55    while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg )))
56    {
57        switch( c )
58        {
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;
67                      }
68                      break;
69            case TR_OPT_UNK: infile = optarg; break;
70            default: return 1;
71        }
72    }
73
74    return 0;
75}
76
77static char*
78tr_getcwd( void )
79{
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 );
88}
89
90int
91main( int argc, char * argv[] )
92{
93    char * out2 = NULL;
94    tr_metainfo_builder * b = NULL;
95
96    tr_setMessageLevel( TR_MSG_ERR );
97
98    if( parseCommandLine( argc, (const char**)argv ) )
99        return EXIT_FAILURE;
100
101    if( showVersion ) {
102        fprintf( stderr, MY_NAME" "LONG_VERSION_STRING"\n" );
103        return 0;
104    }
105
106    if( !infile )
107    {
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;
112    }
113
114    if( outfile == NULL )
115    {
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 );
123    }
124
125    if( !trackerCount )
126    {
127        if( isPrivate )
128        {
129            fprintf( stderr, "ERROR: no trackers specified for a private torrent\n" );
130            return EXIT_FAILURE;
131        }
132        else
133        {
134            printf( "WARNING: no trackers specified\n" );
135        }
136    }
137
138    printf( "Creating torrent \"%s\" ...", outfile );
139    fflush( stdout );
140
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 );
147    }
148
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;
156    }
157    putc( '\n', stdout );
158
159    tr_metaInfoBuilderFree( b );
160    tr_free( out2 );
161    return 0;
162}
Note: See TracBrowser for help on using the repository browser.