webtorrent: file.deselect supposedly not working
Heard from someone on IRC that after calling file.deselect on a file, it still gets fully downloaded.
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Comments: 23 (6 by maintainers)
Heard from someone on IRC that after calling file.deselect on a file, it still gets fully downloaded.
I can confirm in v0.108 that torrent.deselect(0, torrent.pieces.length - 1, false) doesn’t work. And I remember this line working time ago. Is there any alternative to this? any workaround?
Yeah, the
deselect()API is janky.You have to deselect the whole torrent first, then select individual files.
Here’s how we made it work in WebTorrent Desktop:
@feross we should probably improve this API before WebTorrent 1.0, because afterward we don’t want to change it.
Is there a reason to let people deselect individual blocks within a file? What if the API only allowed selecting which files in a torrent to download?
Here’s an API proposal
Remove the
selectanddeselectmethods from File and Torrent.Let users specify which files they want when adding a torrent:
When you do that, the files that aren’t selected are never created on disk.
Let users specify which files they want after adding a torrent:
Doing that doesn’t delete any files. Partially downloaded files just stay there, unless the user deletes them separately. This lets you pause and resume individual files in a torrent.
This alternate API would require the library to do a bit more work to keep track of pieces overlapping a file boundary. It’s simpler, though. You always declare what you want to download for the whole torrent, instead of calling
select()anddeselect()on individual files and piece ranges. This API also lets you download a torrent without creating all the files on disk.@feross
Fixed by this:
Doing tests and tests and tests, I can confirm this seems to work:
I started implementing @dcposch proposal, in the fashion described by @kocoten1992, with a list of wanted files. It’s over there - it needs a bit more testing but seem to work fine on my side.
#2757
Just to give my two cents your comment above @detarkende, I would say A and B are both valid depending on the scenario.
Given a torrent which has the piece layout:
Calling
file1.select()would select piece 1. Callingfile2.select()would select piece 1.Calling
file1.deselect()would currently lead tofile2being deselected which is not desired behaviour as both files depend fully on the same piece.Say we had the further piece layout:
Calling
file1.select()would select piece 1 and 2. Callingfile2.select()would select piece 2 and 3.Both
file1andfile2depend on piece 2, but deselecting either would currently cause a piece required by another file to be deselected, this again would not be desired behaviour.I believe Alex’s changes are an attempt to allow overlapping ranges and priorities, so that multiple selections can include the same piece, and the algorithm used will flatten this to the linear ranges based on ranges and priorities required, e.g.
In the second example above, calling
file1.select()andtorrent.select(2, 3, 1)would look like this, as the higher priority takes precedent:But calling
file1.select()andfile2.select()could look like either:And when deselecting content, all individual ranges are evaluated, so calling
file1.deselect()would update the selections to look like:and calling
file2.deselect()would leave look like:Note: The lack of changes in
// With prioritiesis due tofile.deselect()only deselecting with a priority of 0, not 1, which typically means it’s being used by a stream that we don’t want to interrupt.If that all makes sense?
@RangerMauve Unfortunately you aren’t going to be able to completely prevent the files themselves from being created. The real problem is that Webtorrent actually has no concept of selecting a file. Instead, you are only allowed to select the pieces of the torrent which you wish to download. Since a piece can overlap multiple files (a piece may contain the end of one file and the beginning of the next), Webtorrent will end up writing the bytes to their corresponding files, which will likely result in creating files that are directly adjacent to the selected files.
So the result is that you may get some extra files created that you didn’t want, but the size of them should be fairly negligible as they shouldn’t really be more than a few kb in size (256kb in the worst case if I’m correct, but I could be wrong). Your best bet is to maintain the selected files yourself (you’ll have to anyway to figure out which are selected), and just throw the other files away when you’re done with them. If you plan on seeding the torrent, you’ll want to keep them around as they have parts of the pieces.
I also want to note that I had found issues with using
so = -1due to how it was searching for selected torrents. I recommend using a non-numerical value (something likeso = '-')Thanks @PavelShar I just tried that with a full example https://codepen.io/Jolg42/pen/qBOeYej?editors=1010