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

Commit 0743d90

Browse files
authored
feat: support -p flag on cp (#56)
Creates intermediate directories if `-p` flag is passed
1 parent eb04d6d commit 0743d90

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/core/cp.js

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

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

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

6365
await mkdir(context)(destination.path, options)
6466
destination = await toMfsPath(context, destination.path)
67+
} else if (destination.parts.length > 1) {
68+
// copying to a folder, create it if necessary
69+
const parentFolder = `/${destination.parts.slice(0, -1).join('/')}`
70+
71+
try {
72+
await stat(context)(parentFolder, options)
73+
} catch (err) {
74+
if (err.code !== 'ERR_NOT_FOUND') {
75+
throw err
76+
}
77+
78+
if (!options.parents) {
79+
throw errCode(new Error('destination did not exist, pass -p to create intermediate directories'), 'ERR_INVALID_PARAMS')
80+
}
81+
82+
await mkdir(context)(parentFolder, options)
83+
destination = await toMfsPath(context, destination.path)
84+
}
6585
}
6686
}
6787

test/cp.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,38 @@ describe('cp', () => {
207207
expect(destinationStats.size).to.equal(100)
208208
})
209209

210+
it('copies files to deep mfs paths and creates intermediate directories', async () => {
211+
const source = `/source-file-${Math.random()}.txt`
212+
const destination = `/really/deep/path/to/dest-file-${Math.random()}.txt`
213+
214+
await mfs.write(source, crypto.randomBytes(100), {
215+
create: true
216+
})
217+
218+
await mfs.cp(source, destination, {
219+
parents: true
220+
})
221+
222+
const destinationStats = await mfs.stat(destination)
223+
expect(destinationStats.size).to.equal(100)
224+
})
225+
226+
it('fails to copy files to deep mfs paths when intermediate directories do not exist', async () => {
227+
const source = `/source-file-${Math.random()}.txt`
228+
const destination = `/really/deep/path-${Math.random()}/to-${Math.random()}/dest-file-${Math.random()}.txt`
229+
230+
await mfs.write(source, crypto.randomBytes(100), {
231+
create: true
232+
})
233+
234+
try {
235+
await mfs.cp(source, destination)
236+
throw new Error('No error was thrown when copying to deep directory with missing intermediate directories')
237+
} catch (err) {
238+
expect(err).to.have.property('code', 'ERR_INVALID_PARAMS')
239+
}
240+
})
241+
210242
it('copies a sharded directory to a normal directory', async () => {
211243
const shardedDirPath = await createShardedDirectory(mfs)
212244

0 commit comments

Comments
 (0)