From fdc10e4e5f5abfcfdcda903ca7bd2668debe25d7 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Wed, 18 Mar 2015 22:16:37 +0530 Subject: [PATCH] Allow reading empty file from asar archive --- atom/common/lib/asar.coffee | 42 ++++++++++++++++++++++------------ spec/asar-spec.coffee | 13 +++++++++++ spec/fixtures/asar/empty.asar | Bin 0 -> 60 bytes 3 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 spec/fixtures/asar/empty.asar diff --git a/atom/common/lib/asar.coffee b/atom/common/lib/asar.coffee index 38ac2fc3b727..1b3326e8d7d2 100644 --- a/atom/common/lib/asar.coffee +++ b/atom/common/lib/asar.coffee @@ -3,6 +3,8 @@ child_process = require 'child_process' path = require 'path' util = require 'util' +kEmptyBufferLength = 0 + # Cache asar archive objects. cachedArchives = {} getOrCreateArchive = (p) -> @@ -228,12 +230,17 @@ exports.wrapFsWithAsar = (fs) -> 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 + bufferLength = info.size || kEmptyBufferLength + buffer = new Buffer(bufferLength) + + if info.size + open archive.path, flag, (error, fd) -> + return callback error if error + fs.read fd, buffer, 0, info.size, info.offset, (error, bytesRead, buf) -> + fs.close fd, -> + callback error, if encoding then buf.toString encoding, 0 ,bytesRead else buf + else + callback null, buffer openSync = fs.openSync readFileSync = fs.readFileSync @@ -257,15 +264,20 @@ exports.wrapFsWithAsar = (fs) -> 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 - if encoding then buffer.toString encoding else buffer + bufferLength = info.size || kEmptyBufferLength + buffer = new Buffer(bufferLength) + + if info.size + fd = openSync archive.path, flag + try + bytesRead = fs.readSync fd, buffer, 0, info.size, info.offset + catch e + throw e + finally + fs.closeSync fd + if encoding then buffer.toString encoding, 0, bytesRead else buffer + else + buffer readdir = fs.readdir fs.readdir = (p, callback) -> diff --git a/spec/asar-spec.coffee b/spec/asar-spec.coffee index 222faf9a5e2e..624461245b2b 100644 --- a/spec/asar-spec.coffee +++ b/spec/asar-spec.coffee @@ -15,6 +15,12 @@ describe 'asar package', -> file3 = path.join fixtures, 'asar', 'a.asar', 'file3' assert.equal fs.readFileSync(file3).toString(), 'file3\n' + it 'reads from a empty file', -> + file = path.join fixtures, 'asar', 'empty.asar', 'file1' + buffer = fs.readFileSync(file) + assert.equal buffer.length, 0 + assert.equal buffer.toString(), '' + it 'reads a linked file', -> p = path.join fixtures, 'asar', 'a.asar', 'link1' assert.equal fs.readFileSync(p).toString(), 'file1\n' @@ -38,6 +44,13 @@ describe 'asar package', -> assert.equal String(content), 'file1\n' done() + it 'reads from a empty file', (done) -> + p = path.join fixtures, 'asar', 'empty.asar', 'file1' + fs.readFile p, (err, content) -> + assert.equal err, null + assert.equal String(content), '' + done() + it 'reads a linked file', (done) -> p = path.join fixtures, 'asar', 'a.asar', 'link1' fs.readFile p, (err, content) -> diff --git a/spec/fixtures/asar/empty.asar b/spec/fixtures/asar/empty.asar new file mode 100644 index 0000000000000000000000000000000000000000..10cddfb67654e9b2bc455529148a04045060ec50 GIT binary patch literal 60 zcmZQ!U|=u-Vgn%72I6X^w9K5;VkIjG!w|$M&a6sRvNF(7%1=uxPAyTgQZi7gt*vDM E0ATbEY5)KL literal 0 HcmV?d00001