diff --git a/libtransmission/fastresume.c b/libtransmission/fastresume.c index e3fbdd896..1dcf0499a 100644 --- a/libtransmission/fastresume.c +++ b/libtransmission/fastresume.c @@ -103,7 +103,10 @@ enum FR_ID_CORRUPT = 10, /* IPs and ports of connectable peers */ - FR_ID_PEERS = 11 + FR_ID_PEERS = 11, + + /* destination of the torrent: zero-terminated string */ + FR_ID_DESTINATION = 12 }; @@ -282,6 +285,13 @@ tr_fastResumeSave( const tr_torrent * tor ) fastResumeWriteData( FR_ID_RUN, &is_running, 1, 1, file ); } + if( TRUE ) /* FR_ID_DESTINATION */ + { + const char * d = tor->destination ? tor->destination : ""; + const int byteCount = strlen( d ) + 1; + fastResumeWriteData( FR_ID_DESTINATION, d, 1, byteCount, file ); + } + /* Write download and upload totals */ total = tor->downloadedCur + tor->downloadedPrev; @@ -309,6 +319,31 @@ tr_fastResumeSave( const tr_torrent * tor ) tr_dbg( "Resume file '%s' written", path ); } +static int +loadDestination( tr_torrent * tor, FILE * fp ) +{ + int pathlen = 0; + char path[MAX_PATH_LENGTH]; + + for( ;; ) { + const int ch = fgetc( fp ); + if( ch==EOF ) /* end of file */ + return TR_ERROR_IO_OTHER; + if( ch=='\0' ) /* end of string */ + break; + path[pathlen++] = (char) ch; + } + + path[pathlen] = '\0'; + + if( pathlen ) { + tr_free( tor->destination ); + tor->destination = tr_strdup( path ); + } + + return TR_OK; +} + static int loadSpeeds( tr_torrent * tor, FILE * file ) { @@ -580,6 +615,21 @@ fastResumeLoadImpl ( tr_torrent * tor, } break; + case FR_ID_DESTINATION: + { + const int rret = loadDestination( tor, file ); + + if( rret && ( feof(file) || ferror(file) ) ) + { + fclose( file ); + return ret; + } + + ret |= TR_FR_DESTINATION;; + continue; + } + break; + case FR_ID_RUN: { char ch; diff --git a/libtransmission/fastresume.h b/libtransmission/fastresume.h index 176506348..7c6951805 100644 --- a/libtransmission/fastresume.h +++ b/libtransmission/fastresume.h @@ -36,7 +36,8 @@ enum TR_FR_PROGRESS = (1<<4), TR_FR_PRIORITY = (1<<5), TR_FR_SPEEDLIMIT = (1<<6), - TR_FR_RUN = (1<<7) + TR_FR_RUN = (1<<7), + TR_FR_DESTINATION = (1<<8), }; /** diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 09d2644e2..993bc887a 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -269,8 +269,6 @@ torrentRealInit( tr_handle * h, tr_globalLock( h ); - tor->destination = tr_strdup( destination ); - tor->handle = h; tor->pexDisabled = 0; @@ -342,6 +340,13 @@ torrentRealInit( tr_handle * h, uncheckedPieces = tr_bitfieldNew( tor->info.pieceCount ); loaded = tr_fastResumeLoad( tor, uncheckedPieces ); + /* a path passed in by the user overrides + the one loaded by fastresume... */ + if( destination && *destination ) { + tr_free( tor->destination ); + tor->destination = tr_strdup( destination ); + } + /* the `paused' flag has highest precedence... after that, the fastresume setting is used... if that's not found, default to RUNNING */