This commit is contained in:
Cheng Zhao 2015-03-21 19:20:52 +08:00
parent 5e8c478b2c
commit 1479f73612

View file

@ -3,8 +3,6 @@ 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) ->
@ -219,6 +217,7 @@ exports.wrapFsWithAsar = (fs) ->
info = archive.getFileInfo filePath info = archive.getFileInfo filePath
return callback createNotFoundError(asarPath, filePath) unless info return callback createNotFoundError(asarPath, filePath) unless info
return callback null, new Buffer(0) if info.size is 0
if info.unpacked if info.unpacked
realPath = archive.copyFileOut filePath realPath = archive.copyFileOut filePath
@ -234,17 +233,12 @@ exports.wrapFsWithAsar = (fs) ->
flag = options.flag || 'r' flag = options.flag || 'r'
encoding = options.encoding encoding = options.encoding
bufferLength = info.size || kEmptyBufferLength buffer = new Buffer(info.size)
buffer = new Buffer(bufferLength)
if info.size
open archive.path, flag, (error, fd) -> open archive.path, flag, (error, fd) ->
return callback error if error return callback error if error
fs.read fd, buffer, 0, info.size, info.offset, (error, bytesRead, buf) -> fs.read fd, buffer, 0, info.size, info.offset, (error) ->
fs.close fd, -> fs.close fd, ->
callback error, if encoding then buf.toString encoding, 0 ,bytesRead else buf callback error, if encoding then buffer.toString encoding else buffer
else
callback null, buffer
openSync = fs.openSync openSync = fs.openSync
readFileSync = fs.readFileSync readFileSync = fs.readFileSync
@ -257,6 +251,7 @@ exports.wrapFsWithAsar = (fs) ->
info = archive.getFileInfo filePath info = archive.getFileInfo filePath
throw createNotFoundError(asarPath, filePath) unless info throw createNotFoundError(asarPath, filePath) unless info
return new Buffer(0) if info.size is 0
if info.unpacked if info.unpacked
realPath = archive.copyFileOut filePath realPath = archive.copyFileOut filePath
@ -272,20 +267,15 @@ exports.wrapFsWithAsar = (fs) ->
flag = options.flag || 'r' flag = options.flag || 'r'
encoding = options.encoding encoding = options.encoding
bufferLength = info.size || kEmptyBufferLength buffer = new Buffer(info.size)
buffer = new Buffer(bufferLength)
if info.size
fd = openSync archive.path, flag fd = openSync archive.path, flag
try try
bytesRead = fs.readSync fd, buffer, 0, info.size, info.offset fs.readSync fd, buffer, 0, info.size, info.offset
catch e catch e
throw e throw e
finally finally
fs.closeSync fd fs.closeSync fd
if encoding then buffer.toString encoding, 0, bytesRead else buffer if encoding then buffer.toString encoding else buffer
else
buffer
readdir = fs.readdir readdir = fs.readdir
fs.readdir = (p, callback) -> fs.readdir = (p, callback) ->