Example #1
0
void
tr_magnetCreateMetainfo( const tr_magnet_info * info, tr_benc * top )
{
    int i;
    tr_benc * d;
    tr_bencInitDict( top, 4 );

    /* announce list */
    if( info->trackerCount == 1 )
        tr_bencDictAddStr( top, "announce", info->trackers[0] );
    else {
        tr_benc * trackers = tr_bencDictAddList( top, "announce-list", info->trackerCount );
        for( i=0; i<info->trackerCount; ++i )
            tr_bencListAddStr( tr_bencListAddList( trackers, 1 ), info->trackers[i] );
    }

    /* webseeds */
    if( info->webseedCount > 0 ) {
        tr_benc * urls = tr_bencDictAddList( top, "url-list", info->webseedCount );
        for( i=0; i<info->webseedCount; ++i )
            tr_bencListAddStr( urls, info->webseeds[i] );
    }

    /* nonstandard keys */
    d = tr_bencDictAddDict( top, "magnet-info", 2 );
    tr_bencDictAddRaw( d, "info_hash", info->hash, 20 );
    if( info->displayName != NULL )
        tr_bencDictAddStr( d, "display-name", info->displayName );
}
Example #2
0
static bool
addURL (tr_benc * metainfo, const char * url)
{
  const char * announce = NULL;
  tr_benc * announce_list = NULL;
  bool changed = false;
  const bool had_announce = tr_bencDictFindStr (metainfo, "announce", &announce);
  const bool had_announce_list = tr_bencDictFindList (metainfo, "announce-list", &announce_list);

  if (!had_announce && !had_announce_list)
    {
      /* this new tracker is the only one, so add it to "announce"... */
      printf ("\tAdded \"%s\" in \"announce\"\n", url);
      tr_bencDictAddStr (metainfo, "announce", url);
      changed = true;
    }
  else
    {
      if (!had_announce_list)
        {
          announce_list = tr_bencDictAddList (metainfo, "announce-list", 2);

          if (had_announce)
            {
              /* we're moving from an 'announce' to an 'announce-list',
               * so copy the old announce URL to the list */
              tr_benc * tier = tr_bencListAddList (announce_list, 1);
              tr_bencListAddStr (tier, announce);
              changed = true;
            }
        }

      /* If the user-specified URL isn't in the announce list yet, add it */
      if (!announce_list_has_url (announce_list, url))
        {
          tr_benc * tier = tr_bencListAddList (announce_list, 1);
          tr_bencListAddStr (tier, url);
          printf ("\tAdded \"%s\" to \"announce-list\" tier %zu\n", url, tr_bencListSize (announce_list));
          changed = true;
        }
    }

  return changed;
}
Example #3
0
static tr_bool
addURL( tr_benc * metainfo, const char * url )
{
    const char * str;
    tr_benc * announce_list;
    tr_bool changed = FALSE;
    tr_bool match = FALSE;

    /* maybe add it to "announce" */
    if( !tr_bencDictFindStr( metainfo, "announce", &str ) )
    {
        printf( "\tAdded \"%s\" in \"announce\"\n", url );
        tr_bencDictAddStr( metainfo, "announce", url );
        changed = TRUE;
    }

    /* see if it's already in announce-list */
    if( tr_bencDictFindList( metainfo, "announce-list", &announce_list ) ) {
        tr_benc * tier;
        int tierCount = 0;
        while(( tier = tr_bencListChild( announce_list, tierCount++ ))) {
            tr_benc * node;
            int nodeCount = 0;
            while(( node = tr_bencListChild( tier, nodeCount++ )))
                if( tr_bencGetStr( node, &str ) && !strcmp( str, url ) )
                    match = TRUE;
        }
    }

    /* if it's not in announce-list, add it now */
    if( !match )
    {
        tr_benc * tier;

        if( !tr_bencDictFindList( metainfo, "announce-list", &announce_list ) )
            announce_list = tr_bencDictAddList( metainfo, "announce-list", 1 );

        tier = tr_bencListAddList( announce_list, 1 );
        tr_bencListAddStr( tier, url );
        printf( "\tAdded \"%s\" to \"announce-list\" tier %zu\n", url, tr_bencListSize( announce_list ) );
        changed = TRUE;
    }

    return changed;
}
Example #4
0
static void
saveProgress( tr_benc * dict, tr_torrent * tor )
{
    tr_benc * l;
    tr_benc * prog;
    tr_file_index_t fi;
    const tr_info * inf = tr_torrentInfo( tor );
    const time_t now = tr_time( );

    prog = tr_bencDictAddDict( dict, KEY_PROGRESS, 3 );

    /* add the file/piece check timestamps... */
    l = tr_bencDictAddList( prog, KEY_PROGRESS_CHECKTIME, inf->fileCount );
    for( fi=0; fi<inf->fileCount; ++fi )
    {
        const tr_piece * p;
        const tr_piece * pend;
        time_t oldest_nonzero = now;
        time_t newest = 0;
        tr_bool has_zero = FALSE;
        const time_t mtime = tr_torrentGetFileMTime( tor, fi );
        const tr_file * f = &inf->files[fi];

        /* get the oldest and newest nonzero timestamps for pieces in this file */
        for( p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]; p!=pend; ++p )
        {
            if( !p->timeChecked )
                has_zero = TRUE;
            else if( oldest_nonzero > p->timeChecked )
                oldest_nonzero = p->timeChecked;
            if( newest < p->timeChecked )
                newest = p->timeChecked;
        }

        /* If some of a file's pieces have been checked more recently than
           the file's mtime, and some lest recently, then that file will
           have a list containing timestamps for each piece.
           
           However, the most common use case is that the file doesn't change
           after it's downloaded. To reduce overhead in the .resume file,
           only a single timestamp is saved for the file if *all* or *none*
           of the pieces were tested more recently than the file's mtime. */

        if( !has_zero && ( mtime <= oldest_nonzero ) ) /* all checked */
            tr_bencListAddInt( l, oldest_nonzero );
        else if( newest < mtime ) /* none checked */
            tr_bencListAddInt( l, newest );
        else { /* some are checked, some aren't... so list piece by piece */
            const int offset = oldest_nonzero - 1;
            tr_benc * ll = tr_bencListAddList( l, 2 + f->lastPiece - f->firstPiece );
            tr_bencListAddInt( ll, offset );
            for( p=&inf->pieces[f->firstPiece], pend=&inf->pieces[f->lastPiece]+1; p!=pend; ++p )
                tr_bencListAddInt( ll, p->timeChecked ? p->timeChecked - offset : 0 );
        }
    }

    /* add the progress */
    if( tor->completeness == TR_SEED )
        tr_bencDictAddStr( prog, KEY_PROGRESS_HAVE, "all" );

    /* add the blocks bitfield */
    tr_bitsetToBenc( tr_cpBlockBitset( &tor->completion ),
                     tr_bencDictAdd( prog, KEY_PROGRESS_BLOCKS ) );
}