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

feat: support -p flag on cp #56

Merged
merged 2 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 16 additions & 0 deletions test/cp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ describe('cp', () => {
expect(destinationStats.size).to.equal(100)
})

it('copies files to deep mfs paths and creates interim 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('copies a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(mfs)

Expand Down