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 torrent.rescanFiles() to allow manual verify #1650

Merged
merged 1 commit into from Jul 9, 2019
Merged
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

Add torrent.rescanFiles() to allow manual verify

Useful if files are modified externally to webtorrent.
  • Loading branch information
jhiesey committed Mar 19, 2019
commit ae8988848f4328727b321079922a2a5ed735d6ec
@@ -366,6 +366,14 @@ connections, nor does it pause the streams of existing connections or their wire

Resume connecting to new peers.

## `torrent.rescanFiles([function callback (err) {}])`

Verify the hashes of all pieces in the store and update the bitfield for any new valid
pieces. Useful if data has been added to the store outside WebTorrent, e.g. if another
process puts a valid file in the right place. Once the scan is complete,
`callback(null)` will be called (if provided), unless the torrent was destroyed during
the scan, in which case `callback` will be called with an error.

## `torrent.on('infoHash', function () {})`

Emitted when the info hash of the torrent has been determined.
@@ -505,6 +505,12 @@ class Torrent extends EventEmitter {
this._markAllVerified()
this._onStore()
} else {
const onPiecesVerified = (err) => {
if (err) return this._destroy(err)
this._debug('done verifying')
this._onStore()
}

this._debug('verifying existing torrent data')
if (this._fileModtimes && this._store === FSChunkStore) {
// don't verify if the files haven't been modified since we last checked
@@ -517,11 +523,11 @@ class Torrent extends EventEmitter {
this._markAllVerified()
this._onStore()
} else {
this._verifyPieces()
this._verifyPieces(onPiecesVerified)
}
})
} else {
this._verifyPieces()
this._verifyPieces(onPiecesVerified)
}
}

@@ -547,8 +553,8 @@ class Torrent extends EventEmitter {
})
}

_verifyPieces () {
parallelLimit(this.pieces.map((_, index) => cb => {
_verifyPieces (cb) {
parallelLimit(this.pieces.map((piece, index) => cb => {
if (this.destroyed) return cb(new Error('torrent is destroyed'))

this.store.get(index, (err, buf) => {
@@ -568,10 +574,21 @@ class Torrent extends EventEmitter {
cb(null)
})
})
}), FILESYSTEM_CONCURRENCY, err => {
if (err) return this._destroy(err)
this._debug('done verifying')
this._onStore()
}), FILESYSTEM_CONCURRENCY, cb)
}

rescanFiles (cb) {
if (this.destroyed) throw new Error('torrent is destroyed')
if (!cb) cb = noop

this._verifyPieces((err) => {
if (err) {
this._destroy(err)
return cb(err)
}

this._checkDone()
cb(null)
})
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.