Changeset 2093


Ignore:
Timestamp:
Jun 16, 2007, 5:56:30 PM (15 years ago)
Author:
charles
Message:

more progress on the makemeta backend.

Location:
branches/file_selection/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/file_selection/libtransmission/makemeta.c

    r2083 r2093  
    7272        struct FileList * node = malloc( sizeof( struct FileList ) );
    7373        snprintf( node->filename, sizeof( node->filename ), "%s", buf );
    74         tr_dbg( "putting absolute path in list [%s]\n", node->filename );
    7574        node->next = list;
    7675        list = node;
     
    9998}
    10099
    101 /****
    102 *****
    103 ****/
    104 
    105100static off_t
    106101getFileSize ( const char * filename )
     
    111106    return sb.st_size;
    112107}
     108
     109#define MiB 1048576ul
     110#define GiB 1073741824ul
     111
     112static size_t
     113bestPieceSize( size_t totalSize )
     114{
     115    /* almost always best to have a piee size of 512 or 256 kb.
     116       common practice seems to be to bump up to 1MB pieces at
     117       at total size of around 8GiB or so */
     118
     119    if (totalSize >= (8 * GiB) )
     120        return MiB;
     121
     122    if (totalSize >= GiB )
     123        return MiB / 2;
     124
     125    return MiB / 4;
     126}
     127
     128/****
     129*****
     130****/
     131
     132static int pstrcmp( const void * va, const void * vb)
     133{
     134    const char * a = *(const char**) va;
     135    const char * b = *(const char**) vb;
     136    return strcmp( a, b );
     137}
     138
     139MetaInfoBuilder*
     140tr_metaInfoBuilderCreate( const char * topFile )
     141{
     142    size_t i;
     143    struct FileList * files;
     144    const struct FileList * walk;
     145    MetaInfoBuilder * ret = calloc( 1, sizeof(MetaInfoBuilder) );
     146    ret->top = tr_strdup( topFile );
     147
     148    /* build a list of files containing topFile and,
     149       if it's a directory, all of its children */
     150    if (1) {
     151        char *dir, *base;
     152        char dirbuf[MAX_PATH_LENGTH];
     153        char basebuf[MAX_PATH_LENGTH];
     154        strlcpy( dirbuf, topFile, sizeof( dirbuf ) );
     155        strlcpy( basebuf, topFile, sizeof( basebuf ) );
     156        dir = dirname( dirbuf );
     157        base = basename( basebuf );
     158        files = getFiles( dir, base, NULL );
     159        assert( files != NULL  );
     160    }
     161
     162    for( walk=files; walk!=NULL; walk=walk->next )
     163        ++ret->fileCount;
     164    ret->files = calloc( ret->fileCount, sizeof(char*) );
     165    ret->fileLengths = calloc( ret->fileCount, sizeof(size_t) );
     166
     167    for( i=0, walk=files; walk!=NULL; walk=walk->next, ++i )
     168        ret->files[i] = tr_strdup( walk->filename );
     169
     170    qsort( ret->files, ret->fileCount, sizeof(char*), pstrcmp );
     171    for( i=0; i<ret->fileCount; ++i ) {
     172        ret->fileLengths[i] = getFileSize( ret->files[i] );
     173        ret->totalSize += ret->fileLengths[i];
     174    }
     175
     176    freeFileList( files );
     177   
     178    ret->pieceSize = bestPieceSize( ret->totalSize );
     179    ret->pieceCount = ret->totalSize / ret->pieceSize;
     180    return ret;
     181}
     182
     183void
     184tr_metaInfoBuilderFree( MetaInfoBuilder * builder )
     185{
     186    size_t i;
     187
     188    for( i=0; i<builder->fileCount; ++i )
     189        free( builder->files[i] );
     190    free( builder->top );
     191    free( builder );
     192}
     193
     194/****
     195*****
     196****/
    113197
    114198static uint8_t*
  • branches/file_selection/libtransmission/makemeta.h

    r2062 r2093  
    2626#define TR_MAKEMETA_H 1
    2727
     28typedef struct
     29{
     30    char * top;
     31    char ** files;
     32    size_t * fileLengths;
     33    size_t totalSize;
     34    size_t fileCount;
     35    size_t pieceSize;
     36    size_t pieceCount;
     37   
     38}
     39MetaInfoBuilder;
     40
     41MetaInfoBuilder*
     42tr_metaInfoBuilderCreate( const char * topFile );
     43
     44void
     45tr_metaInfoBuilderFree( MetaInfoBuilder* );
     46
     47
    2848int
    29 tr_makeMetaInfo ( const char   * outputFile,  /* the torrent to create */
    30                   const char   * announce,    /* announcement URL */
    31                   const char   * comment,     /* optional comment */
    32                   const char   * filename,    /* file or dir to torrentize */
    33                   int            isPrivate );
     49tr_makeMetaInfo( const char   * outputFile,
     50                 const char   * announce,
     51                 const char   * comment,
     52                 const char   * topFile,
     53                 int            isPrivate );
    3454
    3555#endif
  • branches/file_selection/libtransmission/utils.c

    r2090 r2093  
    377377****/
    378378
     379char* tr_strdup( const char * in )
     380{
     381    char * out = NULL;
     382    if( in != NULL )
     383    {
     384        const size_t len = strlen( in );
     385        out = malloc( len + 1 );
     386        memcpy( out, in, len );
     387        out[len] = '\0';
     388    }
     389    return out;
     390}
     391
     392/****
     393*****
     394****/
     395
    379396/* note that the argument is how many bits are needed, not bytes */
    380397tr_bitfield_t*
  • branches/file_selection/libtransmission/utils.h

    r2090 r2093  
    174174}
    175175
     176
     177char* tr_strdup( const char * pch );
     178
    176179/***
    177180****
Note: See TracChangeset for help on using the changeset viewer.