@@ -58,7 +58,7 @@ module.exports = (ipfs) => {
58
58
return copyToDirectory ( ipfs , sources , destination , options , callback )
59
59
}
60
60
61
- callback ( new Error ( 'Directory already has entry by that name' ) )
61
+ callback ( new Error ( 'Error: directory already has entry by that name' ) )
62
62
} )
63
63
}
64
64
}
@@ -70,7 +70,7 @@ const copyToFile = (ipfs, source, destination, options, callback) => {
70
70
( next ) => stat ( ipfs ) ( source . path , options , next ) ,
71
71
( next ) => stat ( ipfs ) ( destination . path , options , ( error ) => {
72
72
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' ) )
74
74
}
75
75
76
76
next ( )
@@ -95,7 +95,7 @@ const copyToFile = (ipfs, source, destination, options, callback) => {
95
95
( newRoot , cb ) => updateMfsRoot ( ipfs , newRoot . node . multihash , cb )
96
96
] , cb )
97
97
}
98
- ] , callback )
98
+ ] , ( error ) => callback ( error ) )
99
99
}
100
100
101
101
const copyToDirectory = ( ipfs , sources , destination , options , callback ) => {
@@ -118,29 +118,51 @@ const copyToDirectory = (ipfs, sources, destination, options, callback) => {
118
118
const sourceStats = results [ 0 ]
119
119
120
120
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
137
158
( newParent , next ) => {
138
159
dest . node = newParent
139
160
140
161
updateTree ( ipfs , dest , next )
141
162
} ,
163
+ // save new root CID
142
164
( newRoot , cb ) => updateMfsRoot ( ipfs , newRoot . node . multihash , cb )
143
165
] , cb )
144
166
}
145
- ] , callback )
167
+ ] , ( error ) => callback ( error ) )
146
168
}
0 commit comments