Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat: copy directories
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <[email protected]>
  • Loading branch information
achingbrain committed May 8, 2018
1 parent 60baf97 commit cb0135c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 23 deletions.
8 changes: 0 additions & 8 deletions src/cli/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ module.exports = {
describe: 'Copy files between locations in the mfs.',

builder: {
recursive: {
alias: 'r',
type: 'boolean',
default: false,
describe: 'Copy directories recursively'
},
parents: {
alias: 'p',
type: 'boolean',
Expand All @@ -25,12 +19,10 @@ module.exports = {
source,
dest,
ipfs,
recursive,
parents
} = argv

ipfs.mfs.cp(source, dest, {
recursive,
parents
}, (error) => {
if (error) {
Expand Down
24 changes: 18 additions & 6 deletions src/core/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
const path = require('path')
const UnixFs = require('ipfs-unixfs')
const {
traverseTo,
addLink,
Expand All @@ -14,7 +15,6 @@ const {
const stat = require('./stat')

const defaultOptions = {
recursive: false,
parents: false,
flush: true,
format: 'dag-pb',
Expand Down Expand Up @@ -56,11 +56,23 @@ module.exports = function mfsCp (ipfs) {
return callback(new Error('Please specify a path to copy'))
}

if (sources.length === 1) {
return copyToFile(ipfs, sources.pop(), destination, options, callback)
}
traverseTo(ipfs, destination.path, {}, (error, result) => {
if (error) {
if (sources.length === 1) {
return copyToFile(ipfs, sources.pop(), destination, options, callback)
} else {
return copyToDirectory(ipfs, sources, destination, options, callback)
}
}

const meta = UnixFs.unmarshal(result.node.data)

if (meta.type === 'directory') {
return copyToDirectory(ipfs, sources, destination, options, callback)
}

copyToDirectory(ipfs, sources, destination, options, callback)
callback(new Error('Directory already has entry by that name'))
})
})
}

Expand All @@ -71,7 +83,7 @@ const copyToFile = (ipfs, source, destination, options, callback) => {
(next) => stat(ipfs)(source.path, options, next),
(next) => stat(ipfs)(destination.path, options, (error) => {
if (!error) {
return next(new Error('Destination exists'))
return next(new Error('Directory already has entry by that name'))
}

next()
Expand Down
79 changes: 70 additions & 9 deletions test/cp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const bufferStream = require('./fixtures/buffer-stream')
const bs58 = require('bs58')

const {
createMfs
Expand Down Expand Up @@ -66,8 +67,23 @@ describe('cp', function () {
})
})

it.skip('refuses to copy multiple files to one file', () => {
it('refuses to copy files to an exsting file', () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/dest-file-${Math.random()}.txt`

return mfs.write(source, bufferStream(100), {
create: true
})
.then(() => mfs.write(destination, bufferStream(100), {
create: true
}))
.then(() => mfs.cp(source, destination))
.then(() => {
throw new Error('No error was thrown for a non-existent file')
})
.catch(error => {
expect(error.message).to.contain('Directory already has entry by that name')
})
})

it('copies a file to new location', () => {
Expand All @@ -89,20 +105,50 @@ describe('cp', function () {
})
})

it.skip('copies a file to a directory', () => {

})

it.skip('copies directories', () => {
it('copies a file to a pre-existing directory', () => {
const source = `/source-file-${Math.random()}.txt`
const directory = `/dest-directory-${Math.random()}`
const destination = `${directory}${source}`

return mfs.write(source, bufferStream(500), {
create: true
})
.then(() => mfs.mkdir(directory))
.then(() => mfs.cp(source, directory))
.then(() => mfs.stat(destination))
.then((stats) => {
expect(stats.size).to.equal(500)
})
})

it.skip('refuses to copy directories recursively without the recursive flag', () => {
it('copies directories', () => {
const source = `/source-directory-${Math.random()}`
const destination = `/dest-directory-${Math.random()}`

return mfs.mkdir(source)
.then(() => mfs.cp(source, destination))
.then(() => mfs.stat(destination))
.then((stats) => {
expect(stats.type).to.equal('directory')
})
})

it.skip('copies directories recursively', () => {

it('copies directories recursively', () => {
const directory = `/source-directory-${Math.random()}`
const subDirectory = `/source-directory-${Math.random()}`
const source = `${directory}/${subDirectory}`
const destination = `/dest-directory-${Math.random()}`

return mfs.mkdir(source)
.then(() => mfs.cp(directory, destination))
.then(() => mfs.stat(destination))
.then((stats) => {
expect(stats.type).to.equal('directory')
})
.then(() => mfs.stat(`${destination}/${subDirectory}`))
.then((stats) => {
expect(stats.type).to.equal('directory')
})
})

it('copies multiple files to new location', () => {
Expand Down Expand Up @@ -141,4 +187,19 @@ describe('cp', function () {
)
))
})

it.skip('copies files from ipfs paths', () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/dest-file-${Math.random()}.txt`

return mfs.write(source, bufferStream(100), {
create: true
})
.then(() => mfs.stat(source))
.then((stats) => mfs.cp(`/ipfs/${bs58.encode(stats.hash)}`, destination))
.then(() => mfs.stat(destination))
.then((stats) => {
expect(stats.size).to.equal(100)
})
})
})

0 comments on commit cb0135c

Please sign in to comment.