From d9c769fa696d1ad2b19adf7ef4b2843f2f6990d3 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 11 May 2015 11:10:50 +0800 Subject: [PATCH] Reuse archive's fd in Node asar API --- atom/common/lib/asar.coffee | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/atom/common/lib/asar.coffee b/atom/common/lib/asar.coffee index f36a149a7cff..f70d2c456d78 100644 --- a/atom/common/lib/asar.coffee +++ b/atom/common/lib/asar.coffee @@ -236,21 +236,20 @@ exports.wrapFsWithAsar = (fs) -> return fs.readFile realPath, options, callback if not options - options = encoding: null, flag: 'r' + options = encoding: null else if util.isString options - options = encoding: options, flag: 'r' + options = encoding: options else if not util.isObject options throw new TypeError('Bad arguments') - flag = options.flag || 'r' encoding = options.encoding buffer = new Buffer(info.size) - open archive.path, flag, (error, fd) -> - return callback error if error - fs.read fd, buffer, 0, info.size, info.offset, (error) -> - fs.close fd, -> - callback error, if encoding then buffer.toString encoding else buffer + fd = archive.getFd() + return notFoundError asarPath, filePath, callback unless fd >= 0 + + fs.read fd, buffer, 0, info.size, info.offset, (error) -> + callback error, if encoding then buffer.toString encoding else buffer openSync = fs.openSync readFileSync = fs.readFileSync @@ -270,23 +269,19 @@ exports.wrapFsWithAsar = (fs) -> return fs.readFileSync realPath, options if not options - options = encoding: null, flag: 'r' + options = encoding: null else if util.isString options - options = encoding: options, flag: 'r' + options = encoding: options else if not util.isObject options throw new TypeError('Bad arguments') - flag = options.flag || 'r' encoding = options.encoding buffer = new Buffer(info.size) - fd = openSync archive.path, flag - try - fs.readSync fd, buffer, 0, info.size, info.offset - catch e - throw e - finally - fs.closeSync fd + fd = archive.getFd() + notFoundError asarPath, filePath unless fd >= 0 + + fs.readSync fd, buffer, 0, info.size, info.offset if encoding then buffer.toString encoding else buffer readdir = fs.readdir