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

Option to destroy store on torrent removal (delete files from disk) #1364

Open
wants to merge 2 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -162,12 +162,14 @@ destroyed and all torrents are removed and cleaned up when this occurs.

Always listen for the 'error' event.

## `client.remove(torrentId, [function callback (err) {}])`
## `client.remove(torrentId, [opts], [function callback (err) {}])`

Remove a torrent from the client. Destroy all connections to peers and delete all saved
file data. If `callback` is specified, it will be called when file data is removed.
Remove a torrent from the client. Destroy all connections to peers and delete all saved file metadata.

*Note: This method does not currently delete torrent data (in e.g. `/tmp/webtorrent/...`, see the `path` option to `client.add`). Until this is fixed, please implement it yourself (consider using the `rimraf` npm package).
If `opts.destroyStore` is truthy, `store.destroy()` will be called, which will delete the torrent's files from the disk.

If `callback` is provided, it will be called when the torrent is fully destroyed,
i.e. all open sockets are closed, and the storage is either closed or destroyed.

## `client.destroy([function callback (err) {}])`

@@ -262,11 +264,14 @@ Number of peers in the torrent swarm.

Torrent download location.

## `torrent.destroy([callback])`
## `torrent.destroy([opts], [callback])`

Remove the torrent from its client. Destroy all connections to peers and delete all saved file metadata.

If `opts.destroyStore` is truthy, `store.destroy()` will be called, which will delete the torrent's files from the disk.

Alias for `client.remove(torrent)`. If `callback` is provided, it will be called when
the torrent is fully destroyed, i.e. all open sockets are closed, and the storage is
closed.
If `callback` is provided, it will be called when the torrent is fully destroyed,
i.e. all open sockets are closed, and the storage is either closed or destroyed.

## `torrent.addPeer(peer)`

@@ -327,18 +327,22 @@ class WebTorrent extends EventEmitter {
* @param {string|Buffer|Torrent} torrentId
* @param {function} cb
*/
remove (torrentId, cb) {
remove (torrentId, opts, cb) {
if (typeof opts === 'function') return this.remove(torrentId, null, opts)

this._debug('remove')
const torrent = this.get(torrentId)
if (!torrent) throw new Error(`No torrent with id ${torrentId}`)
this._remove(torrentId, cb)
this._remove(torrentId, opts, cb)
}

_remove (torrentId, cb) {
_remove (torrentId, opts, cb) {
if (typeof opts === 'function') return this._remove(torrentId, null, opts)

const torrent = this.get(torrentId)
if (!torrent) return
this.torrents.splice(this.torrents.indexOf(torrent), 1)
torrent.destroy(cb)
torrent.destroy(opts, cb)
}

address () {
@@ -604,11 +604,14 @@ class Torrent extends EventEmitter {
this._updateSelections()
}

destroy (cb) {
this._destroy(null, cb)
destroy (opts, cb) {
if (typeof opts === 'function') return this.destroy(null, opts)

this._destroy(null, opts, cb)
}

_destroy (err, cb) {
_destroy (err, opts, cb) {
if (typeof opts === 'function') return this._destroy(err, null, opts)
if (this.destroyed) return
this.destroyed = true
this._debug('destroy')
@@ -645,7 +648,11 @@ class Torrent extends EventEmitter {

if (this.store) {
tasks.push(cb => {
this.store.close(cb)
if (opts && opts.destroyStore) {
this.store.destroy(cb)
} else {
this.store.close(cb)
}
})
}

@@ -1,4 +1,6 @@
var fixtures = require('webtorrent-fixtures')
var fs = require('fs')
var path = require('path')
var http = require('http')
var test = require('tape')
var WebTorrent = require('../../')
@@ -157,3 +159,49 @@ test('client.add: invalid torrent id: invalid filesystem path', function (t) {

client.add('/invalid/filesystem/path/123')
})

test('client.remove: opts.destroyStore', function (t) {
t.plan(2)

var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(fixtures.alice.content, { name: 'alice.txt', announce: [] }, function (torrent) {
var torrentPath = torrent.path
client.remove(torrent, { destroyStore: true }, function (err) {
if (err) t.fail(err)

fs.stat(path.join(torrentPath, 'alice.txt'), function (err) {
if (err && err.code === 'ENOENT') t.pass('file deleted')
else t.fail('file still exists')

client.destroy(function (err) { t.error(err, 'client destroyed') })
})
})
})
})

test('torrent.destroy: opts.destroyStore', function (t) {
t.plan(2)

var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(fixtures.alice.content, { name: 'alice.txt', announce: [] }, function (torrent) {
var torrentPath = torrent.path
torrent.destroy({ destroyStore: true }, function (err) {
if (err) t.fail(err)

fs.stat(path.join(torrentPath, 'alice.txt'), function (err) {
if (err && err.code === 'ENOENT') t.pass('file deleted')
else t.fail('file still exists')

client.destroy(function (err) { t.error(err, 'client destroyed') })
})
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.