-
-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make it work with pull and sync, with some refactoring
- Loading branch information
1 parent
59b975b
commit 4403259
Showing
5 changed files
with
94 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,32 @@ | ||
var debug = require('debug')('dat') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var EventEmitter = require('events').EventEmitter | ||
|
||
module.exports = function selectiveSync (state, bus) { | ||
var archive = state.dat.archive | ||
debug('sparse mode. downloading metadata') | ||
var emitter = new EventEmitter() | ||
|
||
function download (entry) { | ||
debug('selected', entry) | ||
archive.stat(entry, function (err, stat) { | ||
if (err) return state.warnings.push(err.message) | ||
if (stat.isDirectory()) downloadDir(entry, stat) | ||
if (stat.isFile()) downloadFile(entry, stat) | ||
}) | ||
module.exports = function (state, opts) { | ||
// selective sync stuff | ||
var parsing = opts.selectFromFile !== '.datdownload' ? opts.selectFromFile : path.join(opts.dir, '.datdownload') | ||
opts.selectedFiles = parseFiles(parsing) | ||
if (opts.select && typeof opts.select === 'string') opts.selectedFiles = opts.select.split(',') | ||
if (opts.selectedFiles) { | ||
state.title = 'Syncing' | ||
state.selectedByteLength = 0 | ||
opts.sparse = true | ||
} | ||
return state | ||
} | ||
|
||
function downloadDir (dirname, stat) { | ||
debug('downloading dir', dirname) | ||
archive.readdir(dirname, function (err, entries) { | ||
if (err) return bus.emit('exit:error', err) | ||
entries.forEach(function (entry) { | ||
emitter.emit('download', path.join(dirname, entry)) | ||
}) | ||
}) | ||
} | ||
function parseFiles (input) { | ||
var parsed = null | ||
|
||
function downloadFile (entry, stat) { | ||
var start = stat.offset | ||
var end = stat.offset + stat.blocks | ||
state.selectedByteLength += stat.size | ||
bus.emit('render') | ||
if (start === 0 && end === 0) return | ||
debug('downloading', entry, start, end) | ||
archive.content.download({start, end}, function () { | ||
debug('success', entry) | ||
}) | ||
try { | ||
if (fs.statSync(input).isFile()) { | ||
parsed = fs.readFileSync(input).toString().trim().split(/\r?\n/) | ||
} | ||
} catch (err) { | ||
if (err && !err.name === 'ENOENT') { | ||
console.error(err) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
emitter.on('download', download) | ||
if (state.opts.selectedFiles) state.opts.selectedFiles.forEach(download) | ||
|
||
archive.metadata.update(function () { | ||
return bus.emit('exit:warn', `Dat successfully created in empty mode. Download files using pull or sync.`) | ||
}) | ||
|
||
archive.on('update', function () { | ||
debug('archive update') | ||
bus.emit('render') | ||
}) | ||
return parsed | ||
} |
This file was deleted.
Oops, something went wrong.