From 4c4d8c5bfa975f7dda97862c432cdb3ee28e5ea1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 4 Oct 2018 01:36:20 +0900 Subject: [PATCH] fix: support ASAR in fs.copyFile (#14942) --- lib/common/asar.js | 6 ++++++ spec/asar-spec.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/common/asar.js b/lib/common/asar.js index d170ce9f70d9..d0c76c0e5ec4 100644 --- a/lib/common/asar.js +++ b/lib/common/asar.js @@ -748,6 +748,12 @@ } } + // Strictly implementing the flags of fs.copyFile is hard, just do a simple + // implementation for now. Doing 2 copies won't spend much time more as OS + // has filesystem caching. + overrideAPI(fs, 'copyFile') + overrideAPISync(fs, 'copyFileSync') + overrideAPI(fs, 'open') overrideAPI(childProcess, 'execFile') overrideAPISync(process, 'dlopen', 1) diff --git a/spec/asar-spec.js b/spec/asar-spec.js index ff6ecca3456a..560f248ec35b 100644 --- a/spec/asar-spec.js +++ b/spec/asar-spec.js @@ -3,6 +3,7 @@ const ChildProcess = require('child_process') const { expect } = require('chai') const fs = require('fs') const path = require('path') +const temp = require('temp').track() const util = require('util') const { closeWindow } = require('./window-helpers') @@ -137,6 +138,44 @@ describe('asar package', function () { }) }) + describe('fs.copyFile', function () { + it('copies a normal file', function (done) { + const p = path.join(fixtures, 'asar', 'a.asar', 'file1') + const dest = temp.path() + fs.copyFile(p, dest, function (err) { + assert.strictEqual(err, null) + assert(fs.readFileSync(p).equals(fs.readFileSync(dest))) + done() + }) + }) + + it('copies a unpacked file', function (done) { + const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt') + const dest = temp.path() + fs.copyFile(p, dest, function (err) { + assert.strictEqual(err, null) + assert(fs.readFileSync(p).equals(fs.readFileSync(dest))) + done() + }) + }) + }) + + describe('fs.copyFileSync', function () { + it('copies a normal file', function () { + const p = path.join(fixtures, 'asar', 'a.asar', 'file1') + const dest = temp.path() + fs.copyFileSync(p, dest) + assert(fs.readFileSync(p).equals(fs.readFileSync(dest))) + }) + + it('copies a unpacked file', function () { + const p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt') + const dest = temp.path() + fs.copyFileSync(p, dest) + assert(fs.readFileSync(p).equals(fs.readFileSync(dest))) + }) + }) + describe('fs.lstatSync', function () { it('handles path with trailing slash correctly', function () { const p = path.join(fixtures, 'asar', 'a.asar', 'link2', 'link2', 'file1')