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