Skip to content

Conversation

@Tilo15
Copy link

@Tilo15 Tilo15 commented Jan 4, 2024

I've been playing with making peer-to-peer software repositories for DNF/RPM. This tool is very useful since I can create a torrent of my repository and mount it to make the repo accessible to the package manager.

I noticed in #72 that btfs by design downloads the whole torrent. In my use case, I only want the files that the package manager needs to read to be downloaded from the torrent, and it seems from that issue that other users may have similar use cases.

In this pull request I have added the --no-prefetch flag which when used changes the behaviour of btfs in the following ways:

  1. Sets all files to priority 0 in the setup() function.
  2. Sets a file to priority 1 when read.
  3. Sets the pieces that are requested in the read to priority 7

This means that once a file is partly read, pieces of that file only will be downloaded in the background, but the priority will still be on the parts of the file the application requested from the filesystem.

Thank you for your time and consideration.

@nivkner
Copy link
Contributor

nivkner commented Apr 15, 2025

for cases where you want to avoid downloading the rest of the file once you start reading it
(for example if you are extracting a single file out of a large zip),
you can use piece priorities exclusively, otherwise all pieces needed for the file are automatically set to the same priority as the file:

diff --git a/src/btfs.cc b/src/btfs.cc
index 67c39ac..81166f8 100644
--- a/src/btfs.cc
+++ b/src/btfs.cc
@@ -120,10 +120,6 @@ 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);
@@ -217,11 +213,13 @@ setup() {
 	if (params.browse_only)
 		handle.pause();
 
+        if(params.no_prefetch) {
+                std::vector<libtorrent::download_priority_t> priorities(ti->num_pieces());
+                handle.prioritize_pieces(priorities);
+	}
+
 	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());

if you still want the file-based pre-fetch, you can change the pre-fetch flag to accept a value instead

@Raizo62
Copy link

Raizo62 commented Jul 23, 2025

Hi
https://github.com/Raizo62/livebtfs is a btfs fork optimized to not download the whole torrent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants