Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --no-prefetch flag to disable downloading the whole torrent #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions man/btfs.1
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ maximum download rate (in kilobytes per second)
.TP
\fB\-\-max-upload-rate=\fIRATE\fR
maximum upload rate (in kilobytes per second)
.TP
\fB\-\-no\-prefetch\fR
do not download files unless an application requests it
.SH EXAMPLES
mounting a torrent file:
btfs video.torrent ~/mnt
Expand Down
24 changes: 21 additions & 3 deletions src/btfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ jump(int piece, int size) {

static void
advance() {
jump(cursor, 0);
if(!params.no_prefetch) {
jump(cursor, 0);
}
}

Read::Read(char *buf, int index, off_t offset, size_t size) {
Expand All @@ -118,6 +120,10 @@ Read::Read(char *buf, int index, off_t offset, size_t size) {
int64_t file_size = ti->files().file_size(index);
#endif

if(params.no_prefetch) {
handle.file_priority(index, 1);
}

while (size > 0 && offset < file_size) {
libtorrent::peer_request part = ti->map_file(index, offset,
(int) size);
Expand Down Expand Up @@ -182,8 +188,15 @@ int Read::read() {
// Trigger reads of finished pieces
trigger();

// Move sliding window to first piece to serve this request
jump(parts.front().part.piece, size());
if (params.no_prefetch) {
// Set priority of needed pieces
for (parts_iter i = parts.begin(); i != parts.end(); ++i) {
handle.piece_priority(i->part.piece, 7);
}
} else {
// Move sliding window to first piece to serve this request
jump(parts.front().part.piece, size());
}

while (!finished() && !failed)
// Wait for any piece to downloaded
Expand All @@ -206,6 +219,9 @@ setup() {

for (int i = 0; i < ti->num_files(); ++i) {
std::string parent("");
if(params.no_prefetch) {
handle.file_priority(i, 0);
}

#if LIBTORRENT_VERSION_NUM < 10100
char *p = strdup(ti->file_at(i).path.c_str());
Expand Down Expand Up @@ -935,6 +951,7 @@ static const struct fuse_opt btfs_opts[] = {
BTFS_OPT("--max-port=%lu", max_port, 4),
BTFS_OPT("--max-download-rate=%lu", max_download_rate, 4),
BTFS_OPT("--max-upload-rate=%lu", max_upload_rate, 4),
BTFS_OPT("--no-prefetch", no_prefetch, 1),
FUSE_OPT_END
};

Expand Down Expand Up @@ -973,6 +990,7 @@ print_help() {
printf(" --max-port=N end of listen port range\n");
printf(" --max-download-rate=N max download rate (in kB/s)\n");
printf(" --max-upload-rate=N max upload rate (in kB/s)\n");
printf(" --no-prefetch don't prefetch files in the background\n");
}

int
Expand Down
1 change: 1 addition & 0 deletions src/btfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ struct btfs_params {
int max_download_rate;
int max_upload_rate;
const char *metadata;
int no_prefetch;
};

}
Expand Down