Allow reading empty file from asar archive
This commit is contained in:
parent
2c0b50a7e9
commit
fdc10e4e5f
3 changed files with 40 additions and 15 deletions
|
@ -3,6 +3,8 @@ child_process = require 'child_process'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
util = require 'util'
|
util = require 'util'
|
||||||
|
|
||||||
|
kEmptyBufferLength = 0
|
||||||
|
|
||||||
# Cache asar archive objects.
|
# Cache asar archive objects.
|
||||||
cachedArchives = {}
|
cachedArchives = {}
|
||||||
getOrCreateArchive = (p) ->
|
getOrCreateArchive = (p) ->
|
||||||
|
@ -228,12 +230,17 @@ exports.wrapFsWithAsar = (fs) ->
|
||||||
flag = options.flag || 'r'
|
flag = options.flag || 'r'
|
||||||
encoding = options.encoding
|
encoding = options.encoding
|
||||||
|
|
||||||
buffer = new Buffer(info.size)
|
bufferLength = info.size || kEmptyBufferLength
|
||||||
open archive.path, flag, (error, fd) ->
|
buffer = new Buffer(bufferLength)
|
||||||
return callback error if error
|
|
||||||
fs.read fd, buffer, 0, info.size, info.offset, (error) ->
|
if info.size
|
||||||
fs.close fd, ->
|
open archive.path, flag, (error, fd) ->
|
||||||
callback error, if encoding then buffer.toString encoding else buffer
|
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
|
openSync = fs.openSync
|
||||||
readFileSync = fs.readFileSync
|
readFileSync = fs.readFileSync
|
||||||
|
@ -257,15 +264,20 @@ exports.wrapFsWithAsar = (fs) ->
|
||||||
flag = options.flag || 'r'
|
flag = options.flag || 'r'
|
||||||
encoding = options.encoding
|
encoding = options.encoding
|
||||||
|
|
||||||
buffer = new Buffer(info.size)
|
bufferLength = info.size || kEmptyBufferLength
|
||||||
fd = openSync archive.path, flag
|
buffer = new Buffer(bufferLength)
|
||||||
try
|
|
||||||
fs.readSync fd, buffer, 0, info.size, info.offset
|
if info.size
|
||||||
catch e
|
fd = openSync archive.path, flag
|
||||||
throw e
|
try
|
||||||
finally
|
bytesRead = fs.readSync fd, buffer, 0, info.size, info.offset
|
||||||
fs.closeSync fd
|
catch e
|
||||||
if encoding then buffer.toString encoding else buffer
|
throw e
|
||||||
|
finally
|
||||||
|
fs.closeSync fd
|
||||||
|
if encoding then buffer.toString encoding, 0, bytesRead else buffer
|
||||||
|
else
|
||||||
|
buffer
|
||||||
|
|
||||||
readdir = fs.readdir
|
readdir = fs.readdir
|
||||||
fs.readdir = (p, callback) ->
|
fs.readdir = (p, callback) ->
|
||||||
|
|
|
@ -15,6 +15,12 @@ describe 'asar package', ->
|
||||||
file3 = path.join fixtures, 'asar', 'a.asar', 'file3'
|
file3 = path.join fixtures, 'asar', 'a.asar', 'file3'
|
||||||
assert.equal fs.readFileSync(file3).toString(), 'file3\n'
|
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', ->
|
it 'reads a linked file', ->
|
||||||
p = path.join fixtures, 'asar', 'a.asar', 'link1'
|
p = path.join fixtures, 'asar', 'a.asar', 'link1'
|
||||||
assert.equal fs.readFileSync(p).toString(), 'file1\n'
|
assert.equal fs.readFileSync(p).toString(), 'file1\n'
|
||||||
|
@ -38,6 +44,13 @@ describe 'asar package', ->
|
||||||
assert.equal String(content), 'file1\n'
|
assert.equal String(content), 'file1\n'
|
||||||
done()
|
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) ->
|
it 'reads a linked file', (done) ->
|
||||||
p = path.join fixtures, 'asar', 'a.asar', 'link1'
|
p = path.join fixtures, 'asar', 'a.asar', 'link1'
|
||||||
fs.readFile p, (err, content) ->
|
fs.readFile p, (err, content) ->
|
||||||
|
|
BIN
spec/fixtures/asar/empty.asar
vendored
Normal file
BIN
spec/fixtures/asar/empty.asar
vendored
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue