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

Commit 749b7a2

Browse files
committed
fix: handle copying files onto each other
1 parent 04a2fe2 commit 749b7a2

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"tmp": "~0.0.33"
4848
},
4949
"dependencies": {
50-
"async": "^2.6.0",
50+
"async": "^2.6.1",
5151
"blob": "~0.0.4",
5252
"bs58": "^4.0.1",
5353
"cids": "~0.5.3",
@@ -61,7 +61,7 @@
6161
"is-pull-stream": "~0.0.0",
6262
"is-stream": "^1.1.0",
6363
"joi": "^13.4.0",
64-
"joi-browser": "^13.0.1",
64+
"joi-browser": "^13.4.0",
6565
"mortice": "^1.2.0",
6666
"once": "^1.4.0",
6767
"promisify-es6": "^1.0.3",

src/core/cp.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module.exports = (ipfs) => {
5858
return copyToDirectory(ipfs, sources, destination, options, callback)
5959
}
6060

61-
callback(new Error('Directory already has entry by that name'))
61+
callback(new Error('Error: directory already has entry by that name'))
6262
})
6363
}
6464
}
@@ -70,7 +70,7 @@ const copyToFile = (ipfs, source, destination, options, callback) => {
7070
(next) => stat(ipfs)(source.path, options, next),
7171
(next) => stat(ipfs)(destination.path, options, (error) => {
7272
if (!error) {
73-
return next(new Error('Directory already has entry by that name'))
73+
return next(new Error('Error: directory already has entry by that name'))
7474
}
7575

7676
next()
@@ -95,7 +95,7 @@ const copyToFile = (ipfs, source, destination, options, callback) => {
9595
(newRoot, cb) => updateMfsRoot(ipfs, newRoot.node.multihash, cb)
9696
], cb)
9797
}
98-
], callback)
98+
], (error) => callback(error))
9999
}
100100

101101
const copyToDirectory = (ipfs, sources, destination, options, callback) => {
@@ -118,29 +118,51 @@ const copyToDirectory = (ipfs, sources, destination, options, callback) => {
118118
const sourceStats = results[0]
119119

120120
waterfall([
121-
(next) => waterfall([
122-
(done) => done(null, dest.node)
123-
].concat(
124-
sourceStats.map((sourceStat, index) => {
125-
return (dest, done) => {
126-
return addLink(ipfs, {
127-
parent: dest,
128-
child: {
129-
size: sourceStat.cumulativeSize,
130-
hash: sourceStat.hash
131-
},
132-
name: sources[index].name
133-
}, done)
134-
}
135-
})
136-
), next),
121+
// ensure targets do not exist
122+
(next) => {
123+
parallel(
124+
sources.map(source => {
125+
return (cb) => {
126+
stat(ipfs)(`${destination.path}/${source.name}`, options, (error) => {
127+
if (!error) {
128+
return cb(new Error('Error: directory already has entry by that name'))
129+
}
130+
131+
cb()
132+
})
133+
}
134+
}),
135+
(error) => next(error)
136+
)
137+
},
138+
// add links to target directory
139+
(next) => {
140+
waterfall([
141+
(done) => done(null, dest.node)
142+
].concat(
143+
sourceStats.map((sourceStat, index) => {
144+
return (dest, done) => {
145+
return addLink(ipfs, {
146+
parent: dest,
147+
child: {
148+
size: sourceStat.cumulativeSize,
149+
hash: sourceStat.hash
150+
},
151+
name: sources[index].name
152+
}, done)
153+
}
154+
})
155+
), next)
156+
},
157+
// update mfs tree
137158
(newParent, next) => {
138159
dest.node = newParent
139160

140161
updateTree(ipfs, dest, next)
141162
},
163+
// save new root CID
142164
(newRoot, cb) => updateMfsRoot(ipfs, newRoot.node.multihash, cb)
143165
], cb)
144166
}
145-
], callback)
167+
], (error) => callback(error))
146168
}

src/core/mkdir.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = (ipfs) => {
3333
path = path.trim()
3434

3535
if (path === FILE_SEPARATOR) {
36-
return callback(options.parents ? null : new Error(`cannot create directory '${FILE_SEPARATOR}': file already exists`))
36+
return callback(options.parents ? null : new Error(`Error: cannot create directory '${FILE_SEPARATOR}': Already exists`))
3737
}
3838

3939
log(`Creating ${path}`)
@@ -65,7 +65,7 @@ module.exports = (ipfs) => {
6565
(result, cb) => updateTree(ipfs, result, cb),
6666
(newRoot, next) => updateMfsRoot(ipfs, newRoot.node.multihash, next)
6767
], (error) => {
68-
if (error && error.message === 'file already exists' && options.parents) {
68+
if (error && error.message === 'Already exists' && options.parents) {
6969
// when the directory already exists and we are creating intermediate
7070
// directories, do not error out (consistent with mkdir -p)
7171
error = null

test/cp.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,22 @@ describe('cp', function () {
8181
throw new Error('No error was thrown for a non-existent file')
8282
})
8383
.catch(error => {
84-
expect(error.message).to.contain('Directory already has entry by that name')
84+
expect(error.message).to.contain('directory already has entry by that name')
85+
})
86+
})
87+
88+
it('refuses to copy a file to itself', () => {
89+
const source = `/source-file-${Math.random()}.txt`
90+
91+
return mfs.write(source, bufferStream(100), {
92+
create: true
93+
})
94+
.then(() => mfs.cp(source, source))
95+
.then(() => {
96+
throw new Error('No error was thrown for a non-existent file')
97+
})
98+
.catch(error => {
99+
expect(error.message).to.contain('directory already has entry by that name')
85100
})
86101
})
87102

0 commit comments

Comments
 (0)