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 13625 2012-12-05 17:29:46Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <errno.h> |
---|
14 | #include <stdio.h> /* fprintf () */ |
---|
15 | #include <stdlib.h> /* EXIT_FAILURE */ |
---|
16 | #include <unistd.h> /* getcwd () */ |
---|
17 | |
---|
18 | #include <libtransmission/transmission.h> |
---|
19 | #include <libtransmission/makemeta.h> |
---|
20 | #include <libtransmission/tr-getopt.h> |
---|
21 | #include <libtransmission/utils.h> |
---|
22 | #include <libtransmission/version.h> |
---|
23 | |
---|
24 | #define MY_NAME "transmission-create" |
---|
25 | |
---|
26 | #define MAX_TRACKERS 128 |
---|
27 | static tr_tracker_info trackers[MAX_TRACKERS]; |
---|
28 | static int trackerCount = 0; |
---|
29 | static bool isPrivate = false; |
---|
30 | static bool showVersion = false; |
---|
31 | const char * comment = NULL; |
---|
32 | const char * outfile = NULL; |
---|
33 | const char * infile = NULL; |
---|
34 | |
---|
35 | static tr_option options[] = |
---|
36 | { |
---|
37 | { 'p', "private", "Allow this torrent to only be used with the specified tracker(s)", "p", 0, NULL }, |
---|
38 | { 'o', "outfile", "Save the generated .torrent to this filename", "o", 1, "<file>" }, |
---|
39 | { 'c', "comment", "Add a comment", "c", 1, "<comment>" }, |
---|
40 | { 't', "tracker", "Add a tracker's announce URL", "t", 1, "<url>" }, |
---|
41 | { 'V', "version", "Show version number and exit", "V", 0, NULL }, |
---|
42 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
43 | }; |
---|
44 | |
---|
45 | static const char * |
---|
46 | getUsage (void) |
---|
47 | { |
---|
48 | return "Usage: " MY_NAME " [options] <file|directory>"; |
---|
49 | } |
---|
50 | |
---|
51 | static int |
---|
52 | parseCommandLine (int argc, const char ** argv) |
---|
53 | { |
---|
54 | int c; |
---|
55 | const char * optarg; |
---|
56 | |
---|
57 | while ((c = tr_getopt (getUsage (), argc, argv, options, &optarg))) |
---|
58 | { |
---|
59 | switch (c) |
---|
60 | { |
---|
61 | case 'V': |
---|
62 | showVersion = true; |
---|
63 | break; |
---|
64 | |
---|
65 | case 'p': |
---|
66 | isPrivate = true; |
---|
67 | break; |
---|
68 | |
---|
69 | case 'o': |
---|
70 | outfile = optarg; |
---|
71 | break; |
---|
72 | |
---|
73 | case 'c': |
---|
74 | comment = optarg; |
---|
75 | break; |
---|
76 | |
---|
77 | case 't': |
---|
78 | if (trackerCount + 1 < MAX_TRACKERS) |
---|
79 | { |
---|
80 | trackers[trackerCount].tier = trackerCount; |
---|
81 | trackers[trackerCount].announce = (char*) optarg; |
---|
82 | ++trackerCount; |
---|
83 | } |
---|
84 | break; |
---|
85 | |
---|
86 | case TR_OPT_UNK: |
---|
87 | infile = optarg; |
---|
88 | break; |
---|
89 | |
---|
90 | default: |
---|
91 | return 1; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | return 0; |
---|
96 | } |
---|
97 | |
---|
98 | static char* |
---|
99 | tr_getcwd (void) |
---|
100 | { |
---|
101 | char * result; |
---|
102 | char buf[2048]; |
---|
103 | |
---|
104 | #ifdef WIN32 |
---|
105 | result = _getcwd (buf, sizeof (buf)); |
---|
106 | #else |
---|
107 | result = getcwd (buf, sizeof (buf)); |
---|
108 | #endif |
---|
109 | |
---|
110 | if (result == NULL) |
---|
111 | { |
---|
112 | fprintf (stderr, "getcwd error: \"%s\"", tr_strerror (errno)); |
---|
113 | *buf = '\0'; |
---|
114 | } |
---|
115 | |
---|
116 | return tr_strdup (buf); |
---|
117 | } |
---|
118 | |
---|
119 | int |
---|
120 | main (int argc, char * argv[]) |
---|
121 | { |
---|
122 | char * out2 = NULL; |
---|
123 | tr_metainfo_builder * b = NULL; |
---|
124 | |
---|
125 | tr_setMessageLevel (TR_MSG_ERR); |
---|
126 | |
---|
127 | if (parseCommandLine (argc, (const char**)argv)) |
---|
128 | return EXIT_FAILURE; |
---|
129 | |
---|
130 | if (showVersion) |
---|
131 | { |
---|
132 | fprintf (stderr, MY_NAME" "LONG_VERSION_STRING"\n"); |
---|
133 | return EXIT_SUCCESS; |
---|
134 | } |
---|
135 | |
---|
136 | if (!infile) |
---|
137 | { |
---|
138 | fprintf (stderr, "ERROR: No input file or directory specified.\n"); |
---|
139 | tr_getopt_usage (MY_NAME, getUsage (), options); |
---|
140 | fprintf (stderr, "\n"); |
---|
141 | return EXIT_FAILURE; |
---|
142 | } |
---|
143 | |
---|
144 | if (outfile == NULL) |
---|
145 | { |
---|
146 | char * base = tr_basename (infile); |
---|
147 | char * end = tr_strdup_printf ("%s.torrent", base); |
---|
148 | char * cwd = tr_getcwd (); |
---|
149 | outfile = out2 = tr_buildPath (cwd, end, NULL); |
---|
150 | tr_free (cwd); |
---|
151 | tr_free (end); |
---|
152 | tr_free (base); |
---|
153 | } |
---|
154 | |
---|
155 | if (!trackerCount) |
---|
156 | { |
---|
157 | if (isPrivate) |
---|
158 | { |
---|
159 | fprintf (stderr, "ERROR: no trackers specified for a private torrent\n"); |
---|
160 | return EXIT_FAILURE; |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | printf ("WARNING: no trackers specified\n"); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | printf ("Creating torrent \"%s\" ...", outfile); |
---|
169 | fflush (stdout); |
---|
170 | |
---|
171 | b = tr_metaInfoBuilderCreate (infile); |
---|
172 | tr_makeMetaInfo (b, outfile, trackers, trackerCount, comment, isPrivate); |
---|
173 | while (!b->isDone) |
---|
174 | { |
---|
175 | tr_wait_msec (500); |
---|
176 | putc ('.', stdout); |
---|
177 | fflush (stdout); |
---|
178 | } |
---|
179 | |
---|
180 | putc (' ', stdout); |
---|
181 | switch (b->result) |
---|
182 | { |
---|
183 | case TR_MAKEMETA_OK: |
---|
184 | printf ("done!"); |
---|
185 | break; |
---|
186 | |
---|
187 | case TR_MAKEMETA_URL: |
---|
188 | printf ("bad announce URL: \"%s\"", b->errfile); |
---|
189 | break; |
---|
190 | |
---|
191 | case TR_MAKEMETA_IO_READ: |
---|
192 | printf ("error reading \"%s\": %s", b->errfile, tr_strerror (b->my_errno)); |
---|
193 | break; |
---|
194 | |
---|
195 | case TR_MAKEMETA_IO_WRITE: |
---|
196 | printf ("error writing \"%s\": %s", b->errfile, tr_strerror (b->my_errno)); |
---|
197 | break; |
---|
198 | |
---|
199 | case TR_MAKEMETA_CANCELLED: |
---|
200 | printf ("cancelled"); |
---|
201 | break; |
---|
202 | } |
---|
203 | putc ('\n', stdout); |
---|
204 | |
---|
205 | tr_metaInfoBuilderFree (b); |
---|
206 | tr_free (out2); |
---|
207 | return EXIT_SUCCESS; |
---|
208 | } |
---|