Esempio n. 1
0
int seg_readbuf( btFileSet *fs, kBitSet *writeCache, int piece, int start, char *buf, int len) {
    btFile *f;
    _int64 addr = ((_int64)piece * fs->blocksize)+start;
    int ifile;

    if (!bs_isSet(writeCache, piece) && !bs_isSet(&fs->completed, piece)) 
    {
        //printf("Attempted to read uncompleted block %d from disk.\n", piece);
        return -1;
    }
    for (ifile=0; ifile < fs->nfiles; ifile++) {
	f=fs->file[ifile];
	if ( f->start+f->len >= addr &&
		f->start < addr + len) 
	{
	    /* this file (partly) includes this piece */
	    int fd = cacheopen( f->path, O_RDONLY, 0);
	    off_t fpos = addr - f->start;	

	    _int64 rlen;
	    _int64 beg = 0;
	    ssize_t res;

	    if (fd == -1)  {
		bts_perror(errno, "readbuf.cacheopen");
		return -1;
	    }
	    if (fpos < 0) {
		beg = -fpos;
		fpos = 0;
	    }
	    rlen = min(f->len - fpos, len - beg);

	    if (lseek( fd, fpos, SEEK_SET) != fpos) {
		bts_perror(errno, "readbuf.read");
		return -1;
	    }


	    bt_assert( rlen <= fs->blocksize);
	    res = read(fd, buf+beg, rlen);
	    if (res != rlen) {
	        if (res == 0) {
		    /* EOF */
		    return -1;
		}
		bts_perror(errno, "readbuf.read");
		return -1;
	    }
	}
    }
    return 0;
}
Esempio n. 2
0
int
seg_readbuf( btFileSet *fs, int piece, int start, char *buf, int len) {
    btFile *f;
    _int64 addr = ((_int64)piece * fs->blocksize)+start;
    int ifile;

    if (!bs_isSet(&fs->completed, piece)) {
        printf("Attempted to read uncompleted block %d from disk.\n", piece);
        return -1;
    }
    for (ifile=0; ifile < fs->nfiles; ifile++) {
	f=fs->file[ifile];
	if ( f->start+f->len >= addr &&
		f->start < addr + len) 
	{
	    /* this file (partly) includes this piece */
	    int fd = cacheopen( f->path, O_RDONLY, 0);
	    off_t fpos = addr - f->start;	

	    _int64 rlen;
	    _int64 beg = 0;
	    ssize_t res;

#if 0
	    fprintf( stderr, "cacheopen( %s, O_RDONLY)\n", f->path);
#endif
	    if (fd == -1)  {
#if 0
		bts_perror(errno, "readbuf.cacheopen");
#endif
		return -1;
	    }
	    if (fpos < 0) {
		beg = -fpos;
		fpos = 0;
	    }
	    rlen = min(f->len - fpos, len - beg);

	    if (lseek( fd, fpos, SEEK_SET) != fpos) {
		SYSDIE("lseek failed");
		return -1;
	    }


	    DIE_UNLESS( rlen <= fs->blocksize);
	    res = read(fd, buf+beg, rlen);
	    if (res != rlen) {
	        if (res == 0) {
		    /* EOF */
		    return -1;
		}
		fprintf(stderr, "On file '%s'\n", f->path);
		fprintf(stderr, "read( ..., buf[%lld], %lld) = %d\n", beg, rlen, res);
		bts_perror(errno, "readbuf.read");
		return -1;
	    }
	}
    }
    return 0;
}