Skip to content

Commit

Permalink
make it work with pull and sync, with some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
okdistribute committed Aug 3, 2017
1 parent 59b975b commit 4403259
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 76 deletions.
15 changes: 15 additions & 0 deletions src/commands/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ module.exports = {
default: true,
help: 'announce your address on link (improves connection capability) and upload data to other downloaders.'
},
{
name: 'selectFromFile',
boolean: false,
default: '.datdownload',
help: 'Sync only the list of selected files or directories in the given file.',
abbr: 'select-from-file'
},
{
name: 'select',
boolean: false,
default: false,
help: 'Sync only the list of selected files or directories.'
},
{
name: 'show-key',
boolean: true,
Expand All @@ -28,6 +41,7 @@ function pull (opts) {
var neatLog = require('neat-log')
var archiveUI = require('../ui/archive')
var trackArchive = require('../lib/archive')
var selectiveSync = require('../lib/selective-sync')
var discoveryExit = require('../lib/discovery-exit')
var onExit = require('../lib/exit')
var parseArgs = require('../parse-args')
Expand All @@ -52,6 +66,7 @@ function pull (opts) {
neat.use(onExit)
neat.use(function (state, bus) {
state.opts = opts
selectiveSync(state, opts)

Dat(opts.dir, opts, function (err, dat) {
if (err && err.name === 'MissingError') return bus.emit('exit:warn', 'No existing archive in this directory. Use clone to download a new archive.')
Expand Down
16 changes: 3 additions & 13 deletions src/commands/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
name: 'selectFromFile',
boolean: false,
default: '.datdownload',
help: 'Sync only the list of selected files or directories in the given file. (default: .datdownload)',
help: 'Sync only the list of selected files or directories in the given file.',
abbr: 'select-from-file'
},
{
Expand All @@ -51,12 +51,11 @@ module.exports = {

function sync (opts) {
var Dat = require('dat-node')
var path = require('path')
var neatLog = require('neat-log')
var archiveUI = require('../ui/archive')
var selectiveSync = require('../lib/selective-sync')
var trackArchive = require('../lib/archive')
var onExit = require('../lib/exit')
var parseFiles = require('../parse-files')
var parseArgs = require('../parse-args')
var debug = require('debug')('dat')

Expand All @@ -75,16 +74,7 @@ function sync (opts) {
neat.use(onExit)
neat.use(function (state, bus) {
state.opts = opts
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
}

selectiveSync(state, opts)
Dat(opts.dir, opts, function (err, dat) {
if (err && err.name === 'MissingError') return bus.emit('exit:warn', 'No existing archive in this directory.')
if (err) return bus.emit('exit:error', err)
Expand Down
53 changes: 52 additions & 1 deletion src/lib/archive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var selectiveSync = require('./selective-sync')
var debug = require('debug')('dat')
var path = require('path')
var EventEmitter = require('events').EventEmitter
var doImport = require('./import-progress')
var stats = require('./stats')
var network = require('./network')
Expand Down Expand Up @@ -28,3 +30,52 @@ module.exports = function (state, bus) {
state.hasContent = true
})
}

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)
})
}

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.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')
})
}
68 changes: 24 additions & 44 deletions src/lib/selective-sync.js
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
}
18 changes: 0 additions & 18 deletions src/parse-files.js

This file was deleted.

0 comments on commit 4403259

Please sign in to comment.