Changeset 5611
- Timestamp:
- Apr 14, 2008, 11:52:50 AM (14 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/bencode.c
r5608 r5611 314 314 tr_bencLoad( const void * buf_in, 315 315 int buflen, 316 tr_benc * setme_benc,316 tr_benc * setme_benc, 317 317 char ** setme_end ) 318 318 { … … 569 569 570 570 return item; 571 } 572 573 tr_benc * 574 tr_bencListAddInt( tr_benc * list, int64_t i ) 575 { 576 tr_benc * node = tr_bencListAdd( list ); 577 tr_bencInitInt( node, i ); 578 return node; 571 579 } 572 580 … … 1052 1060 return ret; 1053 1061 } 1062 1063 /*** 1064 **** 1065 ***/ 1066 1067 int 1068 tr_bencSaveFile( const char * filename, const tr_benc * b ) 1069 { 1070 int ret = TR_OK; 1071 int len; 1072 char * content = tr_bencSave( b, &len ); 1073 FILE * out = NULL; 1074 1075 out = fopen( filename, "wb+" ); 1076 if( !out ) 1077 { 1078 tr_err( _( "Couldn't open \"%1$s\": %2$s" ), 1079 filename, tr_strerror( errno ) ); 1080 ret = TR_EINVALID; 1081 } 1082 else if( fwrite( content, sizeof( char ), len, out ) != (size_t)len ) 1083 { 1084 tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), 1085 filename, tr_strerror( errno ) ); 1086 ret = TR_EINVALID; 1087 } 1088 1089 tr_dbg( "tr_bencSaveFile returned %d when saving \"%s\"", ret, filename ); 1090 tr_free( content ); 1091 if( out ) 1092 fclose( out ); 1093 return ret; 1094 } 1095 1096 int 1097 tr_bencLoadFile( const char * filename, tr_benc * b ) 1098 { 1099 int ret = TR_ERROR; 1100 size_t contentLen; 1101 uint8_t * content = tr_loadFile( filename, &contentLen ); 1102 ret = content ? tr_bencLoad( content, contentLen, b, NULL ) 1103 : TR_ERROR_IO_OTHER; 1104 tr_free( content ); 1105 return ret; 1106 } -
trunk/libtransmission/bencode.h
r5608 r5611 100 100 int tr_bencDictReserve( tr_benc * dict, int count ); 101 101 tr_benc * tr_bencListAdd( tr_benc * list ); 102 tr_benc * tr_bencListAddInt( tr_benc * list, int64_t val ); 102 103 /* note: key must not be freed or modified while val is in use */ 103 104 tr_benc * tr_bencDictAdd( tr_benc * dict, const char * key ); … … 108 109 109 110 char* tr_bencSave( const tr_benc * val, int * len ); 110 char* tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len ); 111 char* tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len ); 112 int tr_bencSaveFile( const char * filename, const tr_benc * ); 113 int tr_bencLoadFile( const char * filename, tr_benc * ); 111 114 112 115 int tr_bencGetInt( const tr_benc * val, int64_t * setme ); -
trunk/libtransmission/metainfo.c
r5517 r5611 126 126 127 127 static void 128 savedname( const tr_handle * handle, 129 char * name, 130 size_t len, 131 const char * hash ) 128 getTorrentFilename( const tr_handle * handle, 129 const tr_info * inf, 130 char * buf, 131 size_t buflen ) 132 { 133 const char * dir = tr_getTorrentDir( handle ); 134 char base[MAX_PATH_LENGTH]; 135 snprintf( base, sizeof( base ), "%s.%16.16s.torrent", inf->name, inf->hashString ); 136 tr_buildPath( buf, buflen, dir, base, NULL ); 137 } 138 139 static void 140 getTorrentOldFilename( const tr_handle * handle, 141 const tr_info * info, 142 char * name, 143 size_t len ) 132 144 { 133 145 const char * torDir = tr_getTorrentDir( handle ); … … 135 147 if( !handle->tag ) 136 148 { 137 tr_buildPath( name, len, torDir, hash, NULL );149 tr_buildPath( name, len, torDir, info->hashString, NULL ); 138 150 } 139 151 else 140 152 { 141 153 char base[1024]; 142 snprintf( base, sizeof(base), "%s-%s", hash, handle->tag );154 snprintf( base, sizeof(base), "%s-%s", info->hashString, handle->tag ); 143 155 tr_buildPath( name, len, torDir, base, NULL ); 144 156 } 145 157 } 146 158 159 void 160 tr_metainfoMigrate( const tr_handle * handle, 161 const tr_info * inf ) 162 { 163 struct stat new_sb; 164 char new_name[MAX_PATH_LENGTH]; 165 166 getTorrentFilename( handle, inf, new_name, sizeof( new_name ) ); 167 168 if( stat( new_name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) ) 169 { 170 char old_name[MAX_PATH_LENGTH]; 171 size_t contentLen; 172 uint8_t * content; 173 174 getTorrentOldFilename( handle, inf, old_name, sizeof( old_name ) ); 175 if(( content = tr_loadFile( old_name, &contentLen ))) 176 { 177 FILE * out = fopen( new_name, "wb+" ); 178 if( fwrite( content, sizeof( uint8_t ), contentLen, out ) == contentLen ) 179 unlink( old_name ); 180 fclose( out ); 181 } 182 183 tr_free( content ); 184 } 185 } 147 186 148 187 int … … 173 212 174 213 tr_sha1_to_hex( inf->hashString, inf->hash ); 175 savedname( handle, buf, sizeof( buf ), inf->hashString );176 tr_free( inf->torrent );177 inf->torrent = tr_strdup( buf );178 214 179 215 /* comment */ … … 275 311 } 276 312 313 /* filename of Transmission's copy */ 314 getTorrentFilename( handle, inf, buf, sizeof( buf ) ); 315 tr_free( inf->torrent ); 316 inf->torrent = tr_strdup( buf ); 317 fprintf( stderr, "inf->torrent is [%s]\n", inf->torrent ); 318 277 319 return TR_OK; 278 320 … … 570 612 void 571 613 tr_metainfoRemoveSaved( const tr_handle * handle, 572 const char * hashString ) 573 { 574 char file[MAX_PATH_LENGTH]; 575 savedname( handle, file, sizeof file, hashString ); 576 unlink( file ); 577 } 578 579 /* Save a copy of the torrent file in the saved torrent directory */ 580 int 581 tr_metainfoSave( const tr_handle * handle, 582 const char * hash, 583 const uint8_t * buf, 584 size_t buflen ) 585 { 586 char path[MAX_PATH_LENGTH]; 587 FILE * file; 588 589 savedname( handle, path, sizeof path, hash ); 590 file = fopen( path, "wb+" ); 591 if( !file ) 592 { 593 tr_err( _( "Couldn't open \"%1$s\": %2$s" ), path, tr_strerror( errno ) ); 594 return TR_EINVALID; 595 } 596 fseek( file, 0, SEEK_SET ); 597 if( fwrite( buf, 1, buflen, file ) != buflen ) 598 { 599 tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), path, tr_strerror( errno ) ); 600 fclose( file ); 601 return TR_EINVALID; 602 } 603 fclose( file ); 604 605 return TR_OK; 614 const tr_info * inf ) 615 { 616 char filename[MAX_PATH_LENGTH]; 617 618 getTorrentFilename( handle, inf, filename, sizeof( filename ) ); 619 unlink( filename ); 620 621 getTorrentOldFilename( handle, inf, filename, sizeof( filename ) ); 622 unlink( filename ); 606 623 } 607 624 -
trunk/libtransmission/metainfo.h
r5517 r5611 37 37 38 38 void tr_metainfoRemoveSaved( const tr_handle * handle, 39 const char * hashString);39 const tr_info * info ); 40 40 41 int tr_metainfoSave( const tr_handle * handle, 42 const char * hashString, 43 const uint8_t * metainfo, 44 size_t len ); 41 void tr_metainfoMigrate( const tr_handle * handle, 42 const tr_info * inf ); 45 43 46 44 #endif -
trunk/libtransmission/resume.c
r5610 r5611 11 11 */ 12 12 13 #include <sys/types.h> /* stat */ 14 #include <sys/stat.h> /* stat */ 15 #include <unistd.h> /* unlink, stat */ 13 #include <unistd.h> /* unlink */ 16 14 17 15 #include <string.h> … … 46 44 #define KEY_PROGRESS_BITFIELD "bitfield" 47 45 48 /***49 ****50 ***/51 52 static time_t*53 getMTimes( const tr_torrent * tor, int * setme_n )54 {55 int i;56 const int n = tor->info.fileCount;57 time_t * m = tr_new( time_t, n );58 59 for( i=0; i<n; ++i ) {60 char fname[MAX_PATH_LENGTH];61 struct stat sb;62 tr_buildPath( fname, sizeof(fname),63 tor->destination, tor->info.files[i].name, NULL );64 if ( !stat( fname, &sb ) && S_ISREG( sb.st_mode ) ) {65 #ifdef SYS_DARWIN66 m[i] = sb.st_mtimespec.tv_sec;67 #else68 m[i] = sb.st_mtime;69 #endif70 }71 }72 73 *setme_n = n;74 return m;75 }76 77 46 static void 78 47 getResumeFilename( char * buf, size_t buflen, const tr_torrent * tor ) … … 80 49 const char * dir = tr_getResumeDir( tor->handle ); 81 50 char base[4096]; 82 snprintf( base, sizeof( base ), "%s.%16.16s.resume", tor->info.name, tor->info.hashString ); 51 snprintf( base, sizeof( base ), "%s.%16.16s.resume", 52 tor->info.name, 53 tor->info.hashString ); 83 54 tr_buildPath( buf, buflen, dir, base, NULL ); 84 55 } … … 92 63 { 93 64 tr_pex * pex; 94 const int count = tr_peerMgrGetPeers( tor->handle->peerMgr, tor->info.hash, &pex ); 65 const int count = tr_peerMgrGetPeers( tor->handle->peerMgr, 66 tor->info.hash, &pex ); 95 67 if( count > 0 ) { 96 68 tr_benc * child = tr_bencDictAdd( dict, KEY_PEERS ); … … 115 87 tr_pex pex; 116 88 memcpy( &pex, str + (i*sizeof(tr_pex)), sizeof(tr_pex) ); 117 tr_peerMgrAddPex( tor->handle->peerMgr, tor->info.hash, TR_PEER_FROM_CACHE, &pex ); 89 tr_peerMgrAddPex( tor->handle->peerMgr, 90 tor->info.hash, TR_PEER_FROM_CACHE, &pex ); 118 91 } 119 92 tr_tordbg( tor, "Loaded %d peers from resume file", count ); … … 123 96 return ret; 124 97 } 98 99 /*** 100 **** 101 ***/ 125 102 126 103 static void … … 145 122 tr_benc * list; 146 123 147 if( tr_bencDictFindList( dict, KEY_PRIORITY, &list ) && ( list->val.l.count == (int)n ) ) 124 if( tr_bencDictFindList( dict, KEY_PRIORITY, &list ) 125 && ( list->val.l.count == (int)n ) ) 148 126 { 149 127 int64_t tmp; … … 157 135 return ret; 158 136 } 137 138 /*** 139 **** 140 ***/ 159 141 160 142 static void … … 195 177 } 196 178 179 /*** 180 **** 181 ***/ 182 197 183 static void 198 184 saveProgress( tr_benc * dict, const tr_torrent * tor ) … … 210 196 211 197 /* add the mtimes */ 212 m = tr_bencDictAdd( p, KEY_PROGRESS_MTIMES ); 213 mtimes = getMTimes( tor, &n ); 214 tr_bencInitList( m, n ); 198 mtimes = tr_torrentGetMTimes( tor, &n ); 199 m = tr_bencDictAddList( p, KEY_PROGRESS_MTIMES, n ); 215 200 for( i=0; i<n; ++i ) { 216 201 if( !tr_torrentIsFileChecked( tor, i ) ) 217 202 mtimes[i] = ~(time_t)0; /* force a recheck next time */ 218 tr_benc InitInt( tr_bencListAdd( m ), mtimes[i] );203 tr_bencListAddInt( m, mtimes[i] ); 219 204 } 220 205 … … 239 224 tr_benc * b; 240 225 int n; 241 time_t * curMTimes = getMTimes( tor, &n );226 time_t * curMTimes = tr_torrentGetMTimes( tor, &n ); 242 227 243 228 if( tr_bencDictFindList( p, KEY_PROGRESS_MTIMES, &m ) … … 246 231 { 247 232 int i; 233 const time_t recheck = ~(time_t)0; 248 234 for( i=0; i<m->val.l.count; ++i ) 249 235 { 250 int64_t tmp; 251 const time_t t = tr_bencGetInt( &m->val.l.vals[i], &tmp ) 252 ? tmp : ~(time_t)0; 253 if( curMTimes[i] == t ) 236 int64_t x; 237 time_t t = tr_bencGetInt( &m->val.l.vals[i], &x ) ? x : recheck; 238 if( ( t != recheck ) && ( curMTimes[i] == t ) ) 254 239 tr_torrentSetFileChecked( tor, i, TRUE ); 255 240 else { … … 288 273 } 289 274 275 /*** 276 **** 277 ***/ 278 290 279 void 291 280 tr_torrentSaveResume( const tr_torrent * tor ) 292 281 { 293 282 tr_benc top; 294 char * encoded; 295 int len; 283 char filename[MAX_PATH_LENGTH]; 296 284 297 285 /* populate the bencoded data */ … … 299 287 tr_bencDictAddInt( &top, KEY_CORRUPT, tor->corruptPrev + tor->corruptCur ); 300 288 tr_bencDictAddStr( &top, KEY_DESTINATION, tor->destination ); 301 tr_bencDictAddInt( &top, KEY_DOWNLOADED, tor->downloadedPrev + tor->downloadedCur ); 302 tr_bencDictAddInt( &top, KEY_UPLOADED, tor->uploadedPrev + tor->uploadedCur ); 289 tr_bencDictAddInt( &top, KEY_DOWNLOADED, 290 tor->downloadedPrev + tor->downloadedCur ); 291 tr_bencDictAddInt( &top, KEY_UPLOADED, 292 tor->uploadedPrev + tor->uploadedCur ); 303 293 tr_bencDictAddInt( &top, KEY_MAX_PEERS, tor->maxConnectedPeers ); 304 294 tr_bencDictAddInt( &top, KEY_PAUSED, tor->isRunning?0:1 ); … … 308 298 saveSpeedLimits( &top, tor ); 309 299 310 /* save the bencoded data */ 311 if(( encoded = tr_bencSave( &top, &len ))) 312 { 313 char filename[MAX_PATH_LENGTH]; 314 FILE * fp; 315 getResumeFilename( filename, sizeof( filename ), tor ); 316 fp = fopen( filename, "wb+" ); 317 fwrite( encoded, len, 1, fp ); 318 fclose( fp ); 319 tr_free( encoded ); 320 } 321 322 /* cleanup */ 300 getResumeFilename( filename, sizeof( filename ), tor ); 301 tr_bencSaveFile( filename, &top ); 302 323 303 tr_bencFree( &top ); 324 304 } … … 331 311 int64_t i; 332 312 const char * str; 333 int benc_loaded = FALSE;334 313 uint64_t fieldsLoaded = 0; 335 uint8_t * content = NULL;336 size_t contentLen;337 314 char filename[MAX_PATH_LENGTH]; 338 315 tr_benc top; … … 340 317 getResumeFilename( filename, sizeof( filename ), tor ); 341 318 342 content = tr_loadFile( filename, &contentLen ); 343 benc_loaded = content && !tr_bencLoad( content, contentLen, &top, NULL ); 344 if( !benc_loaded ) 345 { 346 tr_free( content ); 347 tr_tordbg( tor, "Couldn't read \"%s\"; trying old resume file format.", filename ); 319 if( tr_bencLoadFile( filename, &top ) ) 320 { 321 tr_tordbg( tor, "Couldn't read \"%s\"; trying old format.", filename ); 348 322 fieldsLoaded = tr_fastResumeLoad( tor, fieldsToLoad, ctor ); 349 323 … … 360 334 tr_tordbg( tor, "Read resume file \"%s\"", filename ); 361 335 362 if( tr_bencDictFindInt( &top, KEY_CORRUPT, &i ) ) { 336 if( ( fieldsToLoad & TR_FR_CORRUPT ) 337 && tr_bencDictFindInt( &top, KEY_CORRUPT, &i ) ) { 363 338 tor->corruptPrev = i; 364 339 fieldsLoaded |= TR_FR_CORRUPT; 365 340 } 366 341 367 if( tr_bencDictFindStr( &top, KEY_DESTINATION, &str ) ) { 342 if( ( fieldsToLoad & TR_FR_DESTINATION ) 343 && tr_bencDictFindStr( &top, KEY_DESTINATION, &str ) ) { 368 344 tr_free( tor->destination ); 369 345 tor->destination = tr_strdup( str ); … … 371 347 } 372 348 373 if( tr_bencDictFindInt( &top, KEY_DOWNLOADED, &i ) ) { 349 if( ( fieldsToLoad & TR_FR_DOWNLOADED ) 350 && tr_bencDictFindInt( &top, KEY_DOWNLOADED, &i ) ) { 374 351 tor->downloadedPrev = i; 375 352 fieldsLoaded |= TR_FR_DOWNLOADED; 376 353 } 377 354 378 if( tr_bencDictFindInt( &top, KEY_UPLOADED, &i ) ) { 355 if( ( fieldsToLoad & TR_FR_UPLOADED ) 356 && tr_bencDictFindInt( &top, KEY_UPLOADED, &i ) ) { 379 357 tor->uploadedPrev = i; 380 358 fieldsLoaded |= TR_FR_UPLOADED; 381 359 } 382 360 383 if( tr_bencDictFindInt( &top, KEY_MAX_PEERS, &i ) ) { 361 if( ( fieldsToLoad & TR_FR_MAX_PEERS ) 362 && tr_bencDictFindInt( &top, KEY_MAX_PEERS, &i ) ) { 384 363 tor->maxConnectedPeers = i; 385 364 fieldsLoaded |= TR_FR_MAX_PEERS; 386 365 } 387 366 388 if( tr_bencDictFindInt( &top, KEY_PAUSED, &i ) ) { 367 if( ( fieldsToLoad & TR_FR_RUN ) 368 && tr_bencDictFindInt( &top, KEY_PAUSED, &i ) ) { 389 369 tor->isRunning = i ? 0 : 1; 390 370 fieldsLoaded |= TR_FR_RUN; 391 371 } 392 372 393 fieldsLoaded |= loadPeers( &top, tor ); 394 fieldsLoaded |= loadPriorities( &top, tor ); 395 fieldsLoaded |= loadProgress( &top, tor ); 396 fieldsLoaded |= loadSpeedLimits( &top, tor ); 373 if( fieldsToLoad & TR_FR_PEERS ) 374 fieldsLoaded |= loadPeers( &top, tor ); 375 376 if( fieldsToLoad & TR_FR_PRIORITY ) 377 fieldsLoaded |= loadPriorities( &top, tor ); 378 379 if( fieldsToLoad & TR_FR_PROGRESS ) 380 fieldsLoaded |= loadProgress( &top, tor ); 381 382 if( fieldsToLoad & TR_FR_SPEEDLIMIT ) 383 fieldsLoaded |= loadSpeedLimits( &top, tor ); 397 384 398 385 tr_bencFree( &top ); -
trunk/libtransmission/stats.c
r5608 r5611 82 82 83 83 static void 84 saveCumulativeStats( const tr_handle * handle, const tr_session_stats * stats ) 85 { 86 FILE * fp; 87 char * str; 84 saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s ) 85 { 88 86 char filename[MAX_PATH_LENGTH]; 89 int len;90 87 tr_benc top; 91 88 92 89 tr_bencInitDict( &top, 5 ); 93 tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), stats->uploadedBytes ); 94 tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), stats->downloadedBytes ); 95 tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), stats->filesAdded ); 96 tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), stats->sessionCount ); 97 tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), stats->secondsActive ); 98 99 str = tr_bencSave( &top, &len ); 90 tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), s->uploadedBytes ); 91 tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), s->downloadedBytes ); 92 tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), s->filesAdded ); 93 tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), s->sessionCount ); 94 tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), s->secondsActive ); 95 100 96 getFilename( handle, filename, sizeof(filename) ); 101 fp = fopen( filename, "wb+" ); 102 fwrite( str, 1, len, fp ); 103 fclose( fp ); 104 tr_free( str ); 97 tr_bencSaveFile( filename, &top ); 105 98 106 99 tr_bencFree( &top ); -
trunk/libtransmission/torrent.c
r5606 r5611 23 23 *****************************************************************************/ 24 24 25 #include <sys/types.h> /* stat */ 26 #include <sys/stat.h> /* stat */ 27 #include <unistd.h> /* stat */ 28 25 29 #include <assert.h> 26 30 #include <string.h> /* memcmp */ … … 382 386 const tr_benc * val; 383 387 if( !tr_ctorGetMetainfo( ctor, &val ) ) { 384 int len; 385 uint8_t * text = (uint8_t*) tr_bencSave( val, &len ); 386 tr_metainfoSave( tor->handle, 387 tor->info.hashString, 388 text, len ); 389 tr_free( text ); 388 const char * filename = tor->info.torrent; 389 tr_bencSaveFile( filename, val ); 390 390 } 391 391 } 392 393 tr_metainfoMigrate( h, &tor->info ); 392 394 393 395 if( doStart ) … … 813 815 tr_torrentRemoveSaved( tr_torrent * tor ) 814 816 { 815 tr_metainfoRemoveSaved( tor->handle, tor->info.hashString);817 tr_metainfoRemoveSaved( tor->handle, &tor->info ); 816 818 817 819 tr_torrentRemoveResume( tor ); … … 1420 1422 return tor->info.pieceCount - tr_bitfieldCountTrueBits( tor->checkedPieces ); 1421 1423 } 1424 1425 time_t* 1426 tr_torrentGetMTimes( const tr_torrent * tor, int * setme_n ) 1427 { 1428 int i; 1429 const int n = tor->info.fileCount; 1430 time_t * m = tr_new( time_t, n ); 1431 1432 for( i=0; i<n; ++i ) { 1433 char fname[MAX_PATH_LENGTH]; 1434 struct stat sb; 1435 tr_buildPath( fname, sizeof(fname), 1436 tor->destination, tor->info.files[i].name, NULL ); 1437 if ( !stat( fname, &sb ) && S_ISREG( sb.st_mode ) ) { 1438 #ifdef SYS_DARWIN 1439 m[i] = sb.st_mtimespec.tv_sec; 1440 #else 1441 m[i] = sb.st_mtime; 1442 #endif 1443 } 1444 } 1445 1446 *setme_n = n; 1447 return m; 1448 } -
trunk/libtransmission/torrent.h
r5462 r5611 102 102 void tr_torrentUncheck ( tr_torrent * ); 103 103 104 time_t* tr_torrentGetMTimes ( const tr_torrent *, int * setmeCount ); 105 104 106 typedef enum 105 107 {
Note: See TracChangeset
for help on using the changeset viewer.