-
-
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.
- Loading branch information
1 parent
6d46485
commit ee25724
Showing
7 changed files
with
95 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var debug = require('debug')('dat') | ||
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 bus.emit('exit:error', err) | ||
if (stat.isDirectory()) downloadDir(entry, stat) | ||
if (stat.isFile()) downloadFile(entry, stat) | ||
}) | ||
} | ||
|
||
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 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) | ||
}) | ||
} | ||
|
||
emitter.on('download', download) | ||
if (state.opts.select) state.opts.select.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') | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var fs = require('fs') | ||
|
||
module.exports = function (input) { | ||
var parsed = input.split(',') | ||
|
||
try { | ||
if (fs.statSync(input).isFile()) { | ||
parsed = fs.readFileSync(input).toString().trim().split('\n') | ||
} | ||
} catch (err) { | ||
if (err && !err.name === 'ENOENT') { | ||
console.error(err) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
return parsed | ||
} |
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