Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: support -p flag on cp (#56)
Browse files Browse the repository at this point in the history
Creates intermediate directories if `-p` flag is passed
  • Loading branch information
achingbrain authored Jul 18, 2019
1 parent eb04d6d commit 0743d90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const mkdir = require('./mkdir')
const stat = require('./stat')
const log = require('debug')('ipfs:mfs:cp')
const errCode = require('err-code')
const updateTree = require('./utils/update-tree')
Expand Down Expand Up @@ -56,12 +57,31 @@ module.exports = (context) => {
log('Destination does not exist')

if (sources.length > 1) {
// copying multiple files to one location, destination will be a directory
if (!options.parents) {
throw errCode(new Error('destination did not exist, pass -p to create intermediate directories'), 'ERR_INVALID_PARAMS')
}

await mkdir(context)(destination.path, options)
destination = await toMfsPath(context, destination.path)
} else if (destination.parts.length > 1) {
// copying to a folder, create it if necessary
const parentFolder = `/${destination.parts.slice(0, -1).join('/')}`

try {
await stat(context)(parentFolder, options)
} catch (err) {
if (err.code !== 'ERR_NOT_FOUND') {
throw err
}

if (!options.parents) {
throw errCode(new Error('destination did not exist, pass -p to create intermediate directories'), 'ERR_INVALID_PARAMS')
}

await mkdir(context)(parentFolder, options)
destination = await toMfsPath(context, destination.path)
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/cp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,38 @@ describe('cp', () => {
expect(destinationStats.size).to.equal(100)
})

it('copies files to deep mfs paths and creates intermediate directories', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/really/deep/path/to/dest-file-${Math.random()}.txt`

await mfs.write(source, crypto.randomBytes(100), {
create: true
})

await mfs.cp(source, destination, {
parents: true
})

const destinationStats = await mfs.stat(destination)
expect(destinationStats.size).to.equal(100)
})

it('fails to copy files to deep mfs paths when intermediate directories do not exist', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/really/deep/path-${Math.random()}/to-${Math.random()}/dest-file-${Math.random()}.txt`

await mfs.write(source, crypto.randomBytes(100), {
create: true
})

try {
await mfs.cp(source, destination)
throw new Error('No error was thrown when copying to deep directory with missing intermediate directories')
} catch (err) {
expect(err).to.have.property('code', 'ERR_INVALID_PARAMS')
}
})

it('copies a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(mfs)

Expand Down

0 comments on commit 0743d90

Please sign in to comment.