Skip to content

Commit 337568d

Browse files
committed
feat: copy will return actual copied path
1 parent 9cc629e commit 337568d

5 files changed

Lines changed: 32 additions & 19 deletions

File tree

dist/virtualfs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/virtualfs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/filerlib_copy.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async function _copyFile(srcFile, dst) {
8686
if(dstStat && dstStat.isDirectory()){
8787
let dstFilePath =`${parentDir}/${dstFileName}`;
8888
await _copyFileContents(srcFile, dstFilePath);
89-
return;
89+
return dstFilePath;
9090
} else {
9191
throw new Errors.EIO(`_copyFile Cannot create destination file: ${dst}`);
9292
}
@@ -96,6 +96,7 @@ async function _copyFile(srcFile, dst) {
9696
if(dstStat && dstStat.isDirectory()){
9797
let dstFilePath =`${dst}/${srcFileName}`;
9898
await _copyFileContents(srcFile, dstFilePath);
99+
return dstFilePath;
99100
} else if(dstStat && dstStat.isFile()){
100101
throw new Errors.EEXIST(`_copyFile Destination file already exists: ${dst}`);
101102
} else {
@@ -130,9 +131,11 @@ async function _copyFolder(srcFolder, dst) {
130131
}
131132
await _mkdirIfNotPresent(destSubFolderPath);
132133
await _copyTree(srcFolder, destSubFolderPath);
134+
return destSubFolderPath;
133135
} else {
134136
await _mkdirIfNotPresent(dst);
135137
await _copyTree(srcFolder, dst);
138+
return dst;
136139
}
137140
}
138141

@@ -144,11 +147,11 @@ async function copy(src, dst, callback) {
144147
return;
145148
}
146149
if (srcStat.isFile()) {
147-
await _copyFile(src, dst);
148-
callback(null);
150+
let copiedPath = await _copyFile(src, dst);
151+
callback(null, copiedPath);
149152
} else if (srcStat.isDirectory()) {
150-
await _copyFolder(src, dst);
151-
callback(null);
153+
let copiedPath = await _copyFolder(src, dst);
154+
callback(null, copiedPath);
152155
}
153156
} catch (e) {
154157
callback(new Errors.EIO(`${e}: Cannot copy src: ${src} to ${dst}`));

src/fslib_native.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ async function _getDestinationHandleForCopy(dst, srcBaseName, handleKindToCreate
272272
} else if (dstHandle && dstHandle.kind === Constants.KIND_DIRECTORY
273273
&& handleKindToCreate === Constants.KIND_FILE) {
274274
const fileHandle = await dstHandle.getFileHandle(srcBaseName, {create: true});
275-
resolve(fileHandle);
275+
const dstPath = `${dst}/${srcBaseName}`;
276+
resolve({handle: fileHandle, path:dstPath});
276277
} else if (dstHandle && dstHandle.kind === Constants.KIND_DIRECTORY
277278
&& handleKindToCreate === Constants.KIND_DIRECTORY) {
278279
let dstChildHandle = await Mounts.getHandleFromPathIfPresent(`${dst}/${srcBaseName}`);
@@ -281,15 +282,18 @@ async function _getDestinationHandleForCopy(dst, srcBaseName, handleKindToCreate
281282
return;
282283
}
283284
const directoryHandle = await dstHandle.getDirectoryHandle(srcBaseName, {create: true});
284-
resolve(directoryHandle);
285+
const dstPath = `${dst}/${srcBaseName}`;
286+
resolve({handle: directoryHandle, path: dstPath});
285287
} else if (!dstHandle && dstParentHandle && dstParentHandle.kind === Constants.KIND_DIRECTORY
286288
&& handleKindToCreate === Constants.KIND_FILE) {
287289
const fileHandle = await dstParentHandle.getFileHandle(dstBaseName, {create: true});
288-
resolve(fileHandle);
290+
const dstPath = `${dirPath}/${dstBaseName}`;
291+
resolve({handle: fileHandle, path: dstPath});
289292
} else if (!dstHandle && dstParentHandle && dstParentHandle.kind === Constants.KIND_DIRECTORY
290293
&& handleKindToCreate === Constants.KIND_DIRECTORY) {
291294
const fileHandle = await dstParentHandle.getDirectoryHandle(dstBaseName, {create: true});
292-
resolve(fileHandle);
295+
const dstPath = `${dirPath}/${dstBaseName}`;
296+
resolve({handle: fileHandle, path: dstPath});
293297
} else {
294298
reject(new Errors.ENOENT(`Copy destination doesnt exist: ${dst}`));
295299
}
@@ -314,9 +318,9 @@ async function _copyFileFromHandles(srcFileHandle, dstHandle, optionalName) {
314318

315319
async function _copyFileWithHandle(srcFileHandle, dst, srcFileName, callback) {
316320
try {
317-
let dstHandle = await _getDestinationHandleForCopy(dst, srcFileName, Constants.KIND_FILE);
318-
await _copyFileFromHandles(srcFileHandle, dstHandle);
319-
callback(null);
321+
let {handle, path} = await _getDestinationHandleForCopy(dst, srcFileName, Constants.KIND_FILE);
322+
await _copyFileFromHandles(srcFileHandle, handle);
323+
callback(null, path);
320324
} catch (e) {
321325
callback(e);
322326
}
@@ -339,9 +343,9 @@ async function _treeCopy(srcFolderHandle, dstFolderHandle, recursive) {
339343

340344
async function _copyFolderWithHandle(srcFolderHandle, dst, srcFileName, callback, recursive) {
341345
try {
342-
let dstFolderHandle = await _getDestinationHandleForCopy(dst, srcFileName, Constants.KIND_DIRECTORY);
343-
await _treeCopy(srcFolderHandle, dstFolderHandle, recursive);
344-
callback(null);
346+
let {handle, path} = await _getDestinationHandleForCopy(dst, srcFileName, Constants.KIND_DIRECTORY);
347+
await _treeCopy(srcFolderHandle, handle, recursive);
348+
callback(null, path);
345349
} catch (e) {
346350
callback(e);
347351
}

test/test.browser.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,20 @@ describe('Browser main tests', function () {
242242

243243
async function _verifyCopyFolder(srcPath, dstPath, expectedPath) {
244244
let success = false;
245-
fs.copy(srcPath, dstPath, (err)=>{
245+
let actualCopiedPath = null;
246+
fs.copy(srcPath, dstPath, (err, path)=>{
246247
if(!err){
247248
success = true;
248249
}
250+
actualCopiedPath = path;
249251
});
250252
await waitForTrue(()=>{return success;},1000);
251253
expect(success).to.be.true;
252254
expectedPath = expectedPath || dstPath;
253255
expect(await _shouldExist(`${expectedPath}/a.txt`)).to.be.true;
254256
expect(await _shouldExist(`${expectedPath}/y`)).to.be.true;
255257
expect(await _shouldExist(`${expectedPath}/y/c.txt`)).to.be.true;
258+
expect(actualCopiedPath).to.equal(expectedPath);
256259
}
257260

258261
it('Should phoenix copy fail if dst is a subpath of src', async function () {
@@ -334,15 +337,18 @@ describe('Browser main tests', function () {
334337
// file copy tests
335338
async function _verifyCopyFile(srcPath, dstPath, expectedPath) {
336339
let success = false;
337-
fs.copy(srcPath, dstPath, (err)=>{
340+
let actualCopiedPath = null;
341+
fs.copy(srcPath, dstPath, (err, path)=>{
338342
if(!err){
339343
success = true;
340344
}
345+
actualCopiedPath = path;
341346
});
342347
await waitForTrue(()=>{return success;},1000);
343348
expect(success).to.be.true;
344349
expectedPath = expectedPath || dstPath;
345350
expect(await _shouldExist(expectedPath)).to.be.true;
351+
expect(actualCopiedPath).to.equal(expectedPath);
346352
}
347353
it('Should phoenix copy file from mount to filer path', async function () {
348354
let dir = 'testDir10';

0 commit comments

Comments
 (0)