mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
Allow reading empty file from asar archive
This commit is contained in:
@@ -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) ->
|
||||
|
||||
Reference in New Issue
Block a user