electron/atom/common/lib/asar.coffee

236 lines
6.6 KiB
CoffeeScript
Raw Normal View History

2014-09-24 07:06:36 +00:00
asar = process.atomBinding 'asar'
fs = require 'fs'
path = require 'path'
2014-09-24 08:24:22 +00:00
util = require 'util'
2014-09-24 07:06:36 +00:00
# Cache asar archive objects.
cachedArchives = {}
getOrCreateArchive = (p) ->
archive = cachedArchives[p]
return archive if archive?
archive = asar.createArchive p
return false unless archive
cachedArchives[p] = archive
# Clean cache on quit.
process.on 'exit', ->
archive.destroy() for p, archive of cachedArchives
2014-09-24 07:06:36 +00:00
# Separate asar package's path from full path.
splitPath = (p) ->
return [false, p] unless typeof p is 'string'
2014-09-24 07:06:36 +00:00
components = p.split path.sep
for c, i in components by -1
if path.extname(c) is '.asar'
asarPath = components.slice(0, i + 1).join path.sep
filePath = components.slice(i + 1).join path.sep
return [true, asarPath, filePath]
return [false, p]
# Convert asar archive's Stats object to fs's Stats object.
nextInode = 0
uid = if process.getuid? then process.getuid() else 0
gid = if process.getgid? then process.getgid() else 0
2014-09-24 07:06:36 +00:00
asarStatsToFsStats = (stats) ->
{
dev: 1,
ino: ++nextInode,
mode: 33188,
nlink: 1,
uid: uid,
gid: gid,
rdev: 0,
2014-09-24 07:06:36 +00:00
size: stats.size
isFile: -> stats.isFile
isDirectory: -> stats.isDirectory
isSymbolicLink: -> stats.isLink
isBlockDevice: -> false
isCharacterDevice: -> false
isFIFO: -> false
isSocket: -> false
}
2014-09-24 07:38:07 +00:00
# Create a ENOENT error.
createNotFoundError = (asarPath, filePath) ->
error = new Error("ENOENT, #{filePath} not found in #{asarPath}")
error.code = "ENOENT"
error.errno = -2
error
2014-09-24 07:06:36 +00:00
# Override fs APIs.
2014-09-24 08:24:22 +00:00
lstatSync = fs.lstatSync
fs.lstatSync = (p) ->
2014-09-24 07:06:36 +00:00
[isAsar, asarPath, filePath] = splitPath p
2014-09-24 08:24:22 +00:00
return lstatSync p unless isAsar
2014-09-24 07:06:36 +00:00
archive = getOrCreateArchive asarPath
2014-09-24 07:06:36 +00:00
throw new Error("Invalid package #{asarPath}") unless archive
stats = archive.stat filePath
2014-09-24 07:38:07 +00:00
throw createNotFoundError(asarPath, filePath) unless stats
2014-09-24 07:06:36 +00:00
asarStatsToFsStats stats
2014-09-24 08:24:22 +00:00
lstat = fs.lstat
fs.lstat = (p, callback) ->
2014-09-24 07:06:36 +00:00
[isAsar, asarPath, filePath] = splitPath p
2014-09-24 08:24:22 +00:00
return lstat p, callback unless isAsar
2014-09-24 07:06:36 +00:00
archive = getOrCreateArchive asarPath
2014-09-24 07:06:36 +00:00
return callback throw new Error("Invalid package #{asarPath}") unless archive
stats = getOrCreateArchive(asarPath).stat filePath
2014-09-24 07:38:07 +00:00
return callback createNotFoundError(asarPath, filePath) unless stats
2014-09-24 07:06:36 +00:00
callback undefined, asarStatsToFsStats stats
2014-09-24 08:24:22 +00:00
statSync = fs.statSync
fs.statSync = (p) ->
[isAsar, asarPath, filePath] = splitPath p
return statSync p unless isAsar
# Do not distinguish links for now.
fs.lstatSync p
stat = fs.stat
fs.stat = (p, callback) ->
[isAsar, asarPath, filePath] = splitPath p
return stat p, callback unless isAsar
# Do not distinguish links for now.
fs.lstat p, callback
2014-09-24 07:06:36 +00:00
statSyncNoException = fs.statSyncNoException
fs.statSyncNoException = (p) ->
[isAsar, asarPath, filePath] = splitPath p
return statSyncNoException p unless isAsar
archive = getOrCreateArchive asarPath
2014-09-24 07:06:36 +00:00
return false unless archive
stats = archive.stat filePath
2014-09-24 07:06:36 +00:00
return false unless stats
asarStatsToFsStats stats
2014-09-24 07:38:07 +00:00
exists = fs.exists
fs.exists = (p, callback) ->
[isAsar, asarPath, filePath] = splitPath p
return exists p, callback unless isAsar
archive = getOrCreateArchive asarPath
2014-09-24 07:38:07 +00:00
return callback throw new Error("Invalid package #{asarPath}") unless archive
callback archive.stat(filePath) isnt false
existsSync = fs.existsSync
fs.existsSync = (p) ->
[isAsar, asarPath, filePath] = splitPath p
return existsSync p unless isAsar
archive = getOrCreateArchive asarPath
2014-09-24 07:38:07 +00:00
return false unless archive
archive.stat(filePath) isnt false
readFile = fs.readFile
2014-09-24 08:24:22 +00:00
fs.readFile = (p, options, callback) ->
2014-09-24 07:38:07 +00:00
[isAsar, asarPath, filePath] = splitPath p
return readFile.apply this, arguments unless isAsar
archive = getOrCreateArchive asarPath
2014-09-24 07:38:07 +00:00
return callback throw new Error("Invalid package #{asarPath}") unless archive
info = archive.getFileInfo filePath
return callback createNotFoundError(asarPath, filePath) unless info
2014-09-24 08:24:22 +00:00
if typeof options is 'function'
callback = options
options = undefined
if not options
options = encoding: null, flag: 'r'
else if util.isString options
options = encoding: options, flag: 'r'
else if not util.isObject options
throw new TypeError('Bad arguments')
flag = options.flag || 'r'
encoding = options.encoding
2014-09-24 07:38:07 +00:00
buffer = new Buffer(info.size)
2014-09-24 08:24:22 +00:00
fs.open archive.path, flag, (error, fd) ->
2014-09-24 07:38:07 +00:00
return callback error if error
fs.read fd, buffer, 0, info.size, info.offset, (error) ->
fs.close fd, ->
2014-09-24 08:24:22 +00:00
callback error, if encoding then buffer.toString encoding else buffer
2014-09-24 07:38:07 +00:00
readFileSync = fs.readFileSync
2014-09-24 08:24:22 +00:00
fs.readFileSync = (p, options) ->
2014-09-24 07:38:07 +00:00
[isAsar, asarPath, filePath] = splitPath p
return readFileSync.apply this, arguments unless isAsar
archive = getOrCreateArchive asarPath
2014-09-24 07:38:07 +00:00
throw new Error("Invalid package #{asarPath}") unless archive
info = archive.getFileInfo filePath
throw createNotFoundError(asarPath, filePath) unless info
2014-09-24 08:24:22 +00:00
if not options
options = encoding: null, flag: 'r'
else if util.isString options
options = encoding: options, flag: 'r'
else if not util.isObject options
throw new TypeError('Bad arguments')
flag = options.flag || 'r'
encoding = options.encoding
2014-09-24 07:38:07 +00:00
buffer = new Buffer(info.size)
2014-09-24 08:24:22 +00:00
fd = fs.openSync archive.path, flag
2014-09-24 10:44:00 +00:00
try
fs.readSync fd, buffer, 0, info.size, info.offset
catch e
throw e
finally
fs.closeSync fd
2014-09-24 08:24:22 +00:00
if encoding then buffer.toString encoding else buffer
2014-09-24 10:44:00 +00:00
readdir = fs.readdir
fs.readdir = (p, callback) ->
[isAsar, asarPath, filePath] = splitPath p
return readdir.apply this, arguments unless isAsar
archive = getOrCreateArchive asarPath
2014-09-24 10:44:00 +00:00
return callback throw new Error("Invalid package #{asarPath}") unless archive
files = archive.readdir filePath
return callback createNotFoundError(asarPath, filePath) unless files
process.nextTick ->
callback undefined, files
2014-09-24 10:44:00 +00:00
readdirSync = fs.readdirSync
fs.readdirSync = (p) ->
[isAsar, asarPath, filePath] = splitPath p
return readdirSync.apply this, arguments unless isAsar
archive = getOrCreateArchive asarPath
2014-09-24 10:44:00 +00:00
throw new Error("Invalid package #{asarPath}") unless archive
files = archive.readdir filePath
throw createNotFoundError(asarPath, filePath) unless files
files
dlopen = process.dlopen
require('module')._extensions['.node'] = process.dlopen = (module, p) ->
[isAsar, asarPath, filePath] = splitPath p
return dlopen.apply this, arguments unless isAsar
archive = getOrCreateArchive asarPath
throw new Error("Invalid package #{asarPath}") unless archive
newPath = archive.copyFileOut filePath
throw createNotFoundError(asarPath, filePath) unless newPath
dlopen module, newPath