1 | /* |
---|
2 | * This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: makemeta.c 8720 2009-06-20 00:39:30Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <stdio.h> /* FILE, stderr */ |
---|
16 | #include <stdlib.h> /* qsort */ |
---|
17 | #include <string.h> /* strcmp, strlen */ |
---|
18 | |
---|
19 | #include <sys/types.h> |
---|
20 | #include <sys/stat.h> |
---|
21 | #include <unistd.h> |
---|
22 | #include <dirent.h> |
---|
23 | |
---|
24 | #include "crypto.h" /* tr_sha1 */ |
---|
25 | #include "transmission.h" |
---|
26 | #include "session.h" |
---|
27 | #include "bencode.h" |
---|
28 | #include "makemeta.h" |
---|
29 | #include "platform.h" /* threads, locks */ |
---|
30 | #include "utils.h" /* buildpath */ |
---|
31 | #include "version.h" |
---|
32 | |
---|
33 | /**** |
---|
34 | ***** |
---|
35 | ****/ |
---|
36 | |
---|
37 | struct FileList |
---|
38 | { |
---|
39 | struct FileList * next; |
---|
40 | uint64_t size; |
---|
41 | char * filename; |
---|
42 | }; |
---|
43 | |
---|
44 | static struct FileList* |
---|
45 | getFiles( const char * dir, |
---|
46 | const char * base, |
---|
47 | struct FileList * list ) |
---|
48 | { |
---|
49 | int i; |
---|
50 | char * buf; |
---|
51 | struct stat sb; |
---|
52 | DIR * odir = NULL; |
---|
53 | |
---|
54 | sb.st_size = 0; |
---|
55 | |
---|
56 | buf = tr_buildPath( dir, base, NULL ); |
---|
57 | i = stat( buf, &sb ); |
---|
58 | if( i ) |
---|
59 | { |
---|
60 | tr_err( _( "Torrent Creator is skipping file \"%s\": %s" ), |
---|
61 | buf, tr_strerror( errno ) ); |
---|
62 | tr_free( buf ); |
---|
63 | return list; |
---|
64 | } |
---|
65 | |
---|
66 | if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) ) |
---|
67 | { |
---|
68 | struct dirent *d; |
---|
69 | for( d = readdir( odir ); d != NULL; d = readdir( odir ) ) |
---|
70 | if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles */ |
---|
71 | list = getFiles( buf, d->d_name, list ); |
---|
72 | closedir( odir ); |
---|
73 | } |
---|
74 | else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) ) |
---|
75 | { |
---|
76 | struct FileList * node = tr_new( struct FileList, 1 ); |
---|
77 | node->size = sb.st_size; |
---|
78 | if( ( buf[0] == '.' ) && ( buf[1] == '/' ) ) |
---|
79 | node->filename = tr_strdup( buf + 2 ); |
---|
80 | else |
---|
81 | node->filename = tr_strdup( buf ); |
---|
82 | node->next = list; |
---|
83 | list = node; |
---|
84 | } |
---|
85 | |
---|
86 | tr_free( buf ); |
---|
87 | return list; |
---|
88 | } |
---|
89 | |
---|
90 | static int |
---|
91 | bestPieceSize( uint64_t totalSize ) |
---|
92 | { |
---|
93 | static const uint64_t GiB = 1073741824; |
---|
94 | static const uint64_t MiB = 1048576; |
---|
95 | static const uint64_t KiB = 1024; |
---|
96 | |
---|
97 | if( totalSize >= ( 2 * GiB ) ) return 2 * MiB; |
---|
98 | if( totalSize >= ( 1 * GiB ) ) return 1 * MiB; |
---|
99 | if( totalSize >= ( 512 * MiB ) ) return 512 * KiB; |
---|
100 | if( totalSize >= ( 350 * MiB ) ) return 256 * KiB; |
---|
101 | if( totalSize >= ( 150 * MiB ) ) return 128 * KiB; |
---|
102 | if( totalSize >= ( 50 * MiB ) ) return 64 * KiB; |
---|
103 | return 32 * KiB; /* less than 50 meg */ |
---|
104 | } |
---|
105 | |
---|
106 | static int |
---|
107 | builderFileCompare( const void * va, |
---|
108 | const void * vb ) |
---|
109 | { |
---|
110 | const tr_metainfo_builder_file * a = va; |
---|
111 | const tr_metainfo_builder_file * b = vb; |
---|
112 | |
---|
113 | return strcmp( a->filename, b->filename ); |
---|
114 | } |
---|
115 | |
---|
116 | tr_metainfo_builder* |
---|
117 | tr_metaInfoBuilderCreate( const char * topFile ) |
---|
118 | { |
---|
119 | int i; |
---|
120 | struct FileList * files; |
---|
121 | struct FileList * walk; |
---|
122 | tr_metainfo_builder * ret = tr_new0( tr_metainfo_builder, 1 ); |
---|
123 | |
---|
124 | ret->top = tr_strdup( topFile ); |
---|
125 | |
---|
126 | { |
---|
127 | struct stat sb; |
---|
128 | stat( topFile, &sb ); |
---|
129 | ret->isSingleFile = !S_ISDIR( sb.st_mode ); |
---|
130 | } |
---|
131 | |
---|
132 | /* build a list of files containing topFile and, |
---|
133 | if it's a directory, all of its children */ |
---|
134 | { |
---|
135 | char * dir = tr_dirname( topFile ); |
---|
136 | char * base = tr_basename( topFile ); |
---|
137 | files = getFiles( dir, base, NULL ); |
---|
138 | tr_free( base ); |
---|
139 | tr_free( dir ); |
---|
140 | } |
---|
141 | |
---|
142 | for( walk = files; walk != NULL; walk = walk->next ) |
---|
143 | ++ret->fileCount; |
---|
144 | |
---|
145 | ret->files = tr_new0( tr_metainfo_builder_file, ret->fileCount ); |
---|
146 | |
---|
147 | for( i = 0, walk = files; walk != NULL; ++i ) |
---|
148 | { |
---|
149 | struct FileList * tmp = walk; |
---|
150 | tr_metainfo_builder_file * file = &ret->files[i]; |
---|
151 | walk = walk->next; |
---|
152 | file->filename = tmp->filename; |
---|
153 | file->size = tmp->size; |
---|
154 | ret->totalSize += tmp->size; |
---|
155 | tr_free( tmp ); |
---|
156 | } |
---|
157 | |
---|
158 | qsort( ret->files, |
---|
159 | ret->fileCount, |
---|
160 | sizeof( tr_metainfo_builder_file ), |
---|
161 | builderFileCompare ); |
---|
162 | |
---|
163 | ret->pieceSize = bestPieceSize( ret->totalSize ); |
---|
164 | ret->pieceCount = ret->pieceSize |
---|
165 | ? (int)( ret->totalSize / ret->pieceSize ) |
---|
166 | : 0; |
---|
167 | if( ret->totalSize % ret->pieceSize ) |
---|
168 | ++ret->pieceCount; |
---|
169 | |
---|
170 | return ret; |
---|
171 | } |
---|
172 | |
---|
173 | void |
---|
174 | tr_metaInfoBuilderFree( tr_metainfo_builder * builder ) |
---|
175 | { |
---|
176 | if( builder ) |
---|
177 | { |
---|
178 | tr_file_index_t t; |
---|
179 | int i; |
---|
180 | for( t = 0; t < builder->fileCount; ++t ) |
---|
181 | tr_free( builder->files[t].filename ); |
---|
182 | tr_free( builder->files ); |
---|
183 | tr_free( builder->top ); |
---|
184 | tr_free( builder->comment ); |
---|
185 | for( i = 0; i < builder->trackerCount; ++i ) |
---|
186 | tr_free( builder->trackers[i].announce ); |
---|
187 | tr_free( builder->trackers ); |
---|
188 | tr_free( builder->outputFile ); |
---|
189 | tr_free( builder ); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /**** |
---|
194 | ***** |
---|
195 | ****/ |
---|
196 | |
---|
197 | static uint8_t* |
---|
198 | getHashInfo( tr_metainfo_builder * b ) |
---|
199 | { |
---|
200 | uint32_t fileIndex = 0; |
---|
201 | uint8_t *ret = tr_new0( uint8_t, SHA_DIGEST_LENGTH * b->pieceCount ); |
---|
202 | uint8_t *walk = ret; |
---|
203 | uint8_t *buf; |
---|
204 | uint64_t totalRemain; |
---|
205 | uint64_t off = 0; |
---|
206 | FILE * fp; |
---|
207 | |
---|
208 | if( !b->totalSize ) |
---|
209 | return ret; |
---|
210 | |
---|
211 | buf = tr_new( uint8_t, b->pieceSize ); |
---|
212 | b->pieceIndex = 0; |
---|
213 | totalRemain = b->totalSize; |
---|
214 | fp = fopen( b->files[fileIndex].filename, "rb" ); |
---|
215 | if( !fp ) |
---|
216 | { |
---|
217 | b->my_errno = errno; |
---|
218 | tr_strlcpy( b->errfile, |
---|
219 | b->files[fileIndex].filename, |
---|
220 | sizeof( b->errfile ) ); |
---|
221 | b->result = TR_MAKEMETA_IO_READ; |
---|
222 | tr_free( buf ); |
---|
223 | tr_free( ret ); |
---|
224 | return NULL; |
---|
225 | } |
---|
226 | while( totalRemain ) |
---|
227 | { |
---|
228 | uint8_t * bufptr = buf; |
---|
229 | const uint64_t thisPieceSize = |
---|
230 | MIN( (uint32_t)b->pieceSize, totalRemain ); |
---|
231 | uint64_t pieceRemain = thisPieceSize; |
---|
232 | |
---|
233 | assert( b->pieceIndex < b->pieceCount ); |
---|
234 | |
---|
235 | while( pieceRemain ) |
---|
236 | { |
---|
237 | const uint64_t n_this_pass = |
---|
238 | MIN( ( b->files[fileIndex].size - off ), pieceRemain ); |
---|
239 | fread( bufptr, 1, n_this_pass, fp ); |
---|
240 | bufptr += n_this_pass; |
---|
241 | off += n_this_pass; |
---|
242 | pieceRemain -= n_this_pass; |
---|
243 | if( off == b->files[fileIndex].size ) |
---|
244 | { |
---|
245 | off = 0; |
---|
246 | fclose( fp ); |
---|
247 | fp = NULL; |
---|
248 | if( ++fileIndex < b->fileCount ) |
---|
249 | { |
---|
250 | fp = fopen( b->files[fileIndex].filename, "rb" ); |
---|
251 | if( !fp ) |
---|
252 | { |
---|
253 | b->my_errno = errno; |
---|
254 | tr_strlcpy( b->errfile, |
---|
255 | b->files[fileIndex].filename, |
---|
256 | sizeof( b->errfile ) ); |
---|
257 | b->result = TR_MAKEMETA_IO_READ; |
---|
258 | tr_free( buf ); |
---|
259 | tr_free( ret ); |
---|
260 | return NULL; |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | assert( bufptr - buf == (int)thisPieceSize ); |
---|
267 | assert( pieceRemain == 0 ); |
---|
268 | tr_sha1( walk, buf, thisPieceSize, NULL ); |
---|
269 | walk += SHA_DIGEST_LENGTH; |
---|
270 | |
---|
271 | if( b->abortFlag ) |
---|
272 | { |
---|
273 | b->result = TR_MAKEMETA_CANCELLED; |
---|
274 | break; |
---|
275 | } |
---|
276 | |
---|
277 | totalRemain -= thisPieceSize; |
---|
278 | ++b->pieceIndex; |
---|
279 | } |
---|
280 | |
---|
281 | assert( b->abortFlag |
---|
282 | || ( walk - ret == (int)( SHA_DIGEST_LENGTH * b->pieceCount ) ) ); |
---|
283 | assert( b->abortFlag || !totalRemain ); |
---|
284 | |
---|
285 | if( fp ) |
---|
286 | fclose( fp ); |
---|
287 | |
---|
288 | tr_free( buf ); |
---|
289 | return ret; |
---|
290 | } |
---|
291 | |
---|
292 | static void |
---|
293 | getFileInfo( const char * topFile, |
---|
294 | const tr_metainfo_builder_file * file, |
---|
295 | tr_benc * uninitialized_length, |
---|
296 | tr_benc * uninitialized_path ) |
---|
297 | { |
---|
298 | const char * pch, *prev; |
---|
299 | size_t topLen; |
---|
300 | int n; |
---|
301 | |
---|
302 | /* get the file size */ |
---|
303 | tr_bencInitInt( uninitialized_length, file->size ); |
---|
304 | |
---|
305 | /* how much of file->filename to walk past */ |
---|
306 | topLen = strlen( topFile ); |
---|
307 | if( topLen>0 && topFile[topLen-1]!=TR_PATH_DELIMITER ) |
---|
308 | ++topLen; /* +1 for the path delimiter */ |
---|
309 | |
---|
310 | /* build the path list */ |
---|
311 | n = 1; |
---|
312 | for( pch = file->filename + topLen; *pch; ++pch ) |
---|
313 | if( *pch == TR_PATH_DELIMITER ) |
---|
314 | ++n; |
---|
315 | tr_bencInitList( uninitialized_path, n ); |
---|
316 | for( prev = pch = file->filename + topLen; ; ++pch ) |
---|
317 | { |
---|
318 | char buf[MAX_PATH_LENGTH]; |
---|
319 | |
---|
320 | if( *pch && *pch != TR_PATH_DELIMITER ) |
---|
321 | continue; |
---|
322 | |
---|
323 | memcpy( buf, prev, pch - prev ); |
---|
324 | buf[pch - prev] = '\0'; |
---|
325 | |
---|
326 | tr_bencListAddStr( uninitialized_path, buf ); |
---|
327 | |
---|
328 | prev = pch + 1; |
---|
329 | if( !*pch ) |
---|
330 | break; |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | static void |
---|
335 | makeInfoDict( tr_benc * dict, |
---|
336 | tr_metainfo_builder * builder ) |
---|
337 | { |
---|
338 | uint8_t * pch; |
---|
339 | char * base; |
---|
340 | |
---|
341 | tr_bencDictReserve( dict, 5 ); |
---|
342 | |
---|
343 | if( builder->isSingleFile ) |
---|
344 | tr_bencDictAddInt( dict, "length", builder->files[0].size ); |
---|
345 | else |
---|
346 | { |
---|
347 | uint32_t i; |
---|
348 | tr_benc * list = tr_bencDictAddList( dict, "files", |
---|
349 | builder->fileCount ); |
---|
350 | for( i = 0; i < builder->fileCount; ++i ) |
---|
351 | { |
---|
352 | tr_benc * d = tr_bencListAddDict( list, 2 ); |
---|
353 | tr_benc * length = tr_bencDictAdd( d, "length" ); |
---|
354 | tr_benc * pathVal = tr_bencDictAdd( d, "path" ); |
---|
355 | getFileInfo( builder->top, &builder->files[i], length, pathVal ); |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | base = tr_basename( builder->top ); |
---|
360 | tr_bencDictAddStr( dict, "name", base ); |
---|
361 | tr_free( base ); |
---|
362 | |
---|
363 | tr_bencDictAddInt( dict, "piece length", builder->pieceSize ); |
---|
364 | |
---|
365 | if( ( pch = getHashInfo( builder ) ) ) |
---|
366 | { |
---|
367 | tr_bencDictAddRaw( dict, "pieces", pch, |
---|
368 | SHA_DIGEST_LENGTH * builder->pieceCount ); |
---|
369 | tr_free( pch ); |
---|
370 | } |
---|
371 | |
---|
372 | tr_bencDictAddInt( dict, "private", builder->isPrivate ? 1 : 0 ); |
---|
373 | } |
---|
374 | |
---|
375 | static void |
---|
376 | tr_realMakeMetaInfo( tr_metainfo_builder * builder ) |
---|
377 | { |
---|
378 | int i; |
---|
379 | tr_benc top; |
---|
380 | |
---|
381 | /* allow an empty set, but if URLs *are* listed, verify them. #814, #971 */ |
---|
382 | for( i = 0; i < builder->trackerCount && !builder->result; ++i ) |
---|
383 | if( !tr_httpIsValidURL( builder->trackers[i].announce ) ) |
---|
384 | builder->result = TR_MAKEMETA_URL; |
---|
385 | |
---|
386 | tr_bencInitDict( &top, 6 ); |
---|
387 | |
---|
388 | if( !builder->result && builder->trackerCount ) |
---|
389 | { |
---|
390 | int prevTier = -1; |
---|
391 | tr_benc * tier = NULL; |
---|
392 | |
---|
393 | if( builder->trackerCount > 1 ) |
---|
394 | { |
---|
395 | tr_benc * annList = tr_bencDictAddList( &top, "announce-list", |
---|
396 | 0 ); |
---|
397 | for( i = 0; i < builder->trackerCount; ++i ) |
---|
398 | { |
---|
399 | if( prevTier != builder->trackers[i].tier ) |
---|
400 | { |
---|
401 | prevTier = builder->trackers[i].tier; |
---|
402 | tier = tr_bencListAddList( annList, 0 ); |
---|
403 | } |
---|
404 | tr_bencListAddStr( tier, builder->trackers[i].announce ); |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | tr_bencDictAddStr( &top, "announce", builder->trackers[0].announce ); |
---|
409 | } |
---|
410 | |
---|
411 | if( !builder->result && !builder->abortFlag ) |
---|
412 | { |
---|
413 | if( builder->comment && *builder->comment ) |
---|
414 | tr_bencDictAddStr( &top, "comment", builder->comment ); |
---|
415 | tr_bencDictAddStr( &top, "created by", |
---|
416 | TR_NAME "/" LONG_VERSION_STRING ); |
---|
417 | tr_bencDictAddInt( &top, "creation date", time( NULL ) ); |
---|
418 | tr_bencDictAddStr( &top, "encoding", "UTF-8" ); |
---|
419 | makeInfoDict( tr_bencDictAddDict( &top, "info", 666 ), builder ); |
---|
420 | } |
---|
421 | |
---|
422 | /* save the file */ |
---|
423 | if( !builder->result && !builder->abortFlag ) |
---|
424 | { |
---|
425 | if( tr_bencToFile( &top, TR_FMT_BENC, builder->outputFile ) ) |
---|
426 | { |
---|
427 | builder->my_errno = errno; |
---|
428 | tr_strlcpy( builder->errfile, builder->outputFile, |
---|
429 | sizeof( builder->errfile ) ); |
---|
430 | builder->result = TR_MAKEMETA_IO_WRITE; |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | /* cleanup */ |
---|
435 | tr_bencFree( &top ); |
---|
436 | if( builder->abortFlag ) |
---|
437 | builder->result = TR_MAKEMETA_CANCELLED; |
---|
438 | builder->isDone = 1; |
---|
439 | } |
---|
440 | |
---|
441 | /*** |
---|
442 | **** |
---|
443 | **** A threaded builder queue |
---|
444 | **** |
---|
445 | ***/ |
---|
446 | |
---|
447 | static tr_metainfo_builder * queue = NULL; |
---|
448 | |
---|
449 | static tr_thread * workerThread = NULL; |
---|
450 | |
---|
451 | static tr_lock* |
---|
452 | getQueueLock( void ) |
---|
453 | { |
---|
454 | static tr_lock * lock = NULL; |
---|
455 | |
---|
456 | if( !lock ) |
---|
457 | lock = tr_lockNew( ); |
---|
458 | |
---|
459 | return lock; |
---|
460 | } |
---|
461 | |
---|
462 | static void |
---|
463 | makeMetaWorkerFunc( void * unused UNUSED ) |
---|
464 | { |
---|
465 | for( ;; ) |
---|
466 | { |
---|
467 | tr_metainfo_builder * builder = NULL; |
---|
468 | |
---|
469 | /* find the next builder to process */ |
---|
470 | tr_lock * lock = getQueueLock( ); |
---|
471 | tr_lockLock( lock ); |
---|
472 | if( queue ) |
---|
473 | { |
---|
474 | builder = queue; |
---|
475 | queue = queue->nextBuilder; |
---|
476 | } |
---|
477 | tr_lockUnlock( lock ); |
---|
478 | |
---|
479 | /* if no builders, this worker thread is done */ |
---|
480 | if( builder == NULL ) |
---|
481 | break; |
---|
482 | |
---|
483 | tr_realMakeMetaInfo ( builder ); |
---|
484 | } |
---|
485 | |
---|
486 | workerThread = NULL; |
---|
487 | } |
---|
488 | |
---|
489 | void |
---|
490 | tr_makeMetaInfo( tr_metainfo_builder * builder, |
---|
491 | const char * outputFile, |
---|
492 | const tr_tracker_info * trackers, |
---|
493 | int trackerCount, |
---|
494 | const char * comment, |
---|
495 | int isPrivate ) |
---|
496 | { |
---|
497 | int i; |
---|
498 | tr_lock * lock; |
---|
499 | |
---|
500 | /* free any variables from a previous run */ |
---|
501 | for( i = 0; i < builder->trackerCount; ++i ) |
---|
502 | tr_free( builder->trackers[i].announce ); |
---|
503 | tr_free( builder->trackers ); |
---|
504 | tr_free( builder->comment ); |
---|
505 | tr_free( builder->outputFile ); |
---|
506 | |
---|
507 | /* initialize the builder variables */ |
---|
508 | builder->abortFlag = 0; |
---|
509 | builder->isDone = 0; |
---|
510 | builder->trackerCount = trackerCount; |
---|
511 | builder->trackers = tr_new0( tr_tracker_info, builder->trackerCount ); |
---|
512 | for( i = 0; i < builder->trackerCount; ++i ) |
---|
513 | { |
---|
514 | builder->trackers[i].tier = trackers[i].tier; |
---|
515 | builder->trackers[i].announce = tr_strdup( trackers[i].announce ); |
---|
516 | } |
---|
517 | builder->comment = tr_strdup( comment ); |
---|
518 | builder->isPrivate = isPrivate; |
---|
519 | if( outputFile && *outputFile ) |
---|
520 | builder->outputFile = tr_strdup( outputFile ); |
---|
521 | else |
---|
522 | builder->outputFile = tr_strdup_printf( "%s.torrent", builder->top ); |
---|
523 | |
---|
524 | /* enqueue the builder */ |
---|
525 | lock = getQueueLock ( ); |
---|
526 | tr_lockLock( lock ); |
---|
527 | builder->nextBuilder = queue; |
---|
528 | queue = builder; |
---|
529 | if( !workerThread ) |
---|
530 | workerThread = tr_threadNew( makeMetaWorkerFunc, NULL ); |
---|
531 | tr_lockUnlock( lock ); |
---|
532 | } |
---|
533 | |
---|