From eded3230396aced36192cd41ea11c5c74d2e98ab Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 16 Oct 2017 14:07:11 -0400 Subject: [PATCH] Avoid calling files.stat(source) in symlinkWithOverwrite. This was dangerous because source is often a path relative to the old target file, whereas files.stat was interpreting source as a path relative to process.cwd(). Fixes #9203. --- tools/isobuild/builder.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tools/isobuild/builder.js b/tools/isobuild/builder.js index a6c7333083..b879ec4f9c 100644 --- a/tools/isobuild/builder.js +++ b/tools/isobuild/builder.js @@ -729,23 +729,19 @@ function atomicallyRewriteFile(path, data, options) { // create a symlink, overwriting the target link, file, or directory // if it exists function symlinkWithOverwrite(source, target) { - const args = [source, target]; - - if (process.platform === "win32") { - if (! files.stat(source).isDirectory()) { - throw new Error("symlink source must be a directory: " + source); - } - - args[2] = "junction"; - } - try { - files.symlink(...args); + files.symlink(source, target); } catch (e) { - if (e.code === 'EEXIST') { + if (e.code === "EEXIST") { // overwrite existing link, file, or directory files.rm_recursive(target); - files.symlink(...args); + files.symlink(source, target); + } else if (e.code === "EPERM" && + process.platform === "win32") { + files.rm_recursive(target); + // This will work only if source refers to a directory, but that's a + // chance worth taking. + files.symlink(source, target, "junction"); } else { throw e; }