btStream* bts_create_strstream( btsIo iodir) { btStrStream *bts = btcalloc( 1, sizeof(btStrStream)); bts->bts.type = BTSTRSTREAM; bts->bts.iodir = iodir; bts->bts.read = bts_strstream_read; bts->bts.write = bts_strstream_write; bts->bts.peek = bts_strstream_peek; bts->bts.destroy = bts_strstream_destroy; bts->bts.rewind = bts_strstream_rewind; return &bts->bts; }
btFileSet* btFileSet_create( btFileSet *fs, int npieces, int blocksize, const char *hashbuf) { if (!fs) { fs = btcalloc( 1, sizeof(btFileSet)); bt_assert(fs); } fs->npieces = npieces; fs->blocksize = blocksize; fs->dl = 0; fs->ul = 0; fs->hashes = btmalloc( npieces * SHA_DIGEST_LENGTH); memcpy(fs->hashes, hashbuf, npieces * SHA_DIGEST_LENGTH); kBitSet_create(&fs->completed, npieces); return fs; }
int btFileSet_addfile( btFileSet *fs, const char *path, _int64 len) { int ifile=fs->nfiles++; btFile *f; fs->file = btrealloc(fs->file, sizeof(btFile *)*fs->nfiles); bt_assert(fs->file); f=btcalloc(1,sizeof(btFile)); bt_assert(f); f->path = strdup(path); f->len = len; if (ifile > 0) { btFile *lf = fs->file[ifile-1]; f->start = lf->start + lf->len; } fs->file[ifile]=f; return 0; }
btPartialPiece * seg_getPiece( btFileSet *fs, int piece) { btPartialPiece *p; bt_assert(piece>=0 && piece<fs->npieces); p=fs->partial; while(p && p->piecenumber!=piece) { p=p->next; } if(!p) { int blocksize=seg_piecelen(fs, piece); /* Allocation trick: the memory after the structure * is the piece contents */ p=btcalloc(1, sizeof(btPartialPiece)+blocksize); p->piecenumber=piece; kBitSet_create( &p->filled, blocksize); pp_addtail( fs, p); } return p; }
int btFileSet_addfile( btFileSet *fs, const char *path, _int64 len) { int ifile=fs->nfiles++; btFile *f; #if 0 printf("addfile( ..., %s, %d)\n", path, len); #endif fs->file = btrealloc(fs->file, sizeof(btFile *)*fs->nfiles); DIE_UNLESS(fs->file); f=btcalloc(1,sizeof(btFile)); DIE_UNLESS(f); f->path = strdup(path); f->len = len; if (ifile > 0) { btFile *lf = fs->file[ifile-1]; f->start = lf->start + lf->len; } fs->file[ifile]=f; return 0; }
btStream* bts_create_filestream_fp( FILE *fp, btsIo iodir) { btFileStream *bts = btcalloc( 1, sizeof(btFileStream)); bts->bts.type = BTFILESTREAM; #if 0 if (fp->flags & _IO_NO_READS) { iodir&=~BTS_OUTPUT; } if (fp->flags & _IO_NO_WRITES) { iodir&=~BTS_INPUT; } #endif bts->bts.iodir = iodir; bts->bts.read = bts_filestream_read; bts->bts.write = bts_filestream_write; bts->bts.peek = bts_filestream_peek; bts->bts.destroy = bts_filestream_destroy; bts->bts.rewind = bts_filestream_rewind; bts->fp = fp; return &bts->bts; }