2016-01-12 01:39:11 +00:00
|
|
|
(function () {
|
2016-03-25 19:50:43 +00:00
|
|
|
const asar = process.binding('atom_common_asar')
|
2016-09-08 17:17:06 +00:00
|
|
|
const {Buffer} = require('buffer')
|
2016-05-25 23:20:49 +00:00
|
|
|
const childProcess = require('child_process')
|
2016-03-25 19:50:43 +00:00
|
|
|
const path = require('path')
|
|
|
|
const util = require('util')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const hasProp = {}.hasOwnProperty
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Cache asar archive objects.
|
2016-07-25 17:07:39 +00:00
|
|
|
const cachedArchives = {}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const getOrCreateArchive = function (p) {
|
|
|
|
let archive = cachedArchives[p]
|
2016-01-19 18:25:03 +00:00
|
|
|
if (archive != null) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return archive
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
archive = asar.createArchive(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return false
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
cachedArchives[p] = archive
|
|
|
|
return archive
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Clean cache on quit.
|
2016-03-25 19:50:43 +00:00
|
|
|
process.on('exit', function () {
|
2016-07-25 17:07:39 +00:00
|
|
|
for (let p in cachedArchives) {
|
2016-03-25 19:50:43 +00:00
|
|
|
if (!hasProp.call(cachedArchives, p)) continue
|
2016-07-25 17:07:39 +00:00
|
|
|
cachedArchives[p].destroy()
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Separate asar package's path from full path.
|
2016-07-25 17:07:39 +00:00
|
|
|
const splitPath = function (p) {
|
2016-01-19 18:25:03 +00:00
|
|
|
// shortcut to disable asar.
|
|
|
|
if (process.noAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return [false]
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 18:10:36 +00:00
|
|
|
if (Buffer.isBuffer(p)) {
|
|
|
|
p = p.toString()
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
if (typeof p !== 'string') {
|
2016-03-25 19:50:43 +00:00
|
|
|
return [false]
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
if (p.substr(-5) === '.asar') {
|
2016-03-25 19:50:43 +00:00
|
|
|
return [true, p, '']
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
p = path.normalize(p)
|
2016-07-25 17:07:39 +00:00
|
|
|
const index = p.lastIndexOf('.asar' + path.sep)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (index === -1) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return [false]
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
return [true, p.substr(0, index + 5), p.substr(index + 6)]
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Convert asar archive's Stats object to fs's Stats object.
|
2016-07-25 17:07:39 +00:00
|
|
|
let nextInode = 0
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const uid = process.getuid != null ? process.getuid() : 0
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const gid = process.getgid != null ? process.getgid() : 0
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const fakeTime = new Date()
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const asarStatsToFsStats = function (stats) {
|
2016-01-19 18:25:03 +00:00
|
|
|
return {
|
|
|
|
dev: 1,
|
|
|
|
ino: ++nextInode,
|
|
|
|
mode: 33188,
|
|
|
|
nlink: 1,
|
|
|
|
uid: uid,
|
|
|
|
gid: gid,
|
|
|
|
rdev: 0,
|
|
|
|
atime: stats.atime || fakeTime,
|
|
|
|
birthtime: stats.birthtime || fakeTime,
|
|
|
|
mtime: stats.mtime || fakeTime,
|
|
|
|
ctime: stats.ctime || fakeTime,
|
|
|
|
size: stats.size,
|
2016-03-25 19:50:43 +00:00
|
|
|
isFile: function () {
|
|
|
|
return stats.isFile
|
2016-01-19 18:25:03 +00:00
|
|
|
},
|
2016-03-25 19:50:43 +00:00
|
|
|
isDirectory: function () {
|
|
|
|
return stats.isDirectory
|
2016-01-19 18:25:03 +00:00
|
|
|
},
|
2016-03-25 19:50:43 +00:00
|
|
|
isSymbolicLink: function () {
|
|
|
|
return stats.isLink
|
2016-01-19 18:25:03 +00:00
|
|
|
},
|
2016-03-25 19:50:43 +00:00
|
|
|
isBlockDevice: function () {
|
|
|
|
return false
|
2016-01-19 18:25:03 +00:00
|
|
|
},
|
2016-03-25 19:50:43 +00:00
|
|
|
isCharacterDevice: function () {
|
|
|
|
return false
|
2016-01-19 18:25:03 +00:00
|
|
|
},
|
2016-03-25 19:50:43 +00:00
|
|
|
isFIFO: function () {
|
|
|
|
return false
|
2016-01-19 18:25:03 +00:00
|
|
|
},
|
2016-03-25 19:50:43 +00:00
|
|
|
isSocket: function () {
|
|
|
|
return false
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
|
|
|
// Create a ENOENT error.
|
2016-07-25 17:07:39 +00:00
|
|
|
const notFoundError = function (asarPath, filePath, callback) {
|
|
|
|
const error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
|
2016-03-25 19:50:43 +00:00
|
|
|
error.code = 'ENOENT'
|
|
|
|
error.errno = -2
|
2016-01-19 18:25:03 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2016-03-25 19:50:43 +00:00
|
|
|
throw error
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
callback(error)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Create a ENOTDIR error.
|
2016-07-25 17:07:39 +00:00
|
|
|
const notDirError = function (callback) {
|
|
|
|
const error = new Error('ENOTDIR, not a directory')
|
2016-03-25 19:50:43 +00:00
|
|
|
error.code = 'ENOTDIR'
|
|
|
|
error.errno = -20
|
2016-01-19 18:25:03 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2016-03-25 19:50:43 +00:00
|
|
|
throw error
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
callback(error)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:49:20 +00:00
|
|
|
// Create a EACCES error.
|
|
|
|
const accessError = function (asarPath, filePath, callback) {
|
|
|
|
const error = new Error(`EACCES: permission denied, access '${filePath}'`)
|
|
|
|
error.code = 'EACCES'
|
|
|
|
error.errno = -13
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
process.nextTick(function () {
|
|
|
|
callback(error)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Create invalid archive error.
|
2016-07-25 17:07:39 +00:00
|
|
|
const invalidArchiveError = function (asarPath, callback) {
|
|
|
|
const error = new Error(`Invalid package ${asarPath}`)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2016-03-25 19:50:43 +00:00
|
|
|
throw error
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
callback(error)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Override APIs that rely on passing file path instead of content to C++.
|
2016-07-25 17:07:39 +00:00
|
|
|
const overrideAPISync = function (module, name, arg) {
|
2016-01-19 18:25:03 +00:00
|
|
|
if (arg == null) {
|
2016-03-25 19:50:43 +00:00
|
|
|
arg = 0
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const old = module[name]
|
2016-03-25 19:50:43 +00:00
|
|
|
module[name] = function () {
|
2016-07-25 17:07:39 +00:00
|
|
|
const p = arguments[arg]
|
2016-03-25 19:50:43 +00:00
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return old.apply(this, arguments)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
invalidArchiveError(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const newPath = archive.copyFileOut(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!newPath) {
|
2016-03-25 19:50:43 +00:00
|
|
|
notFoundError(asarPath, filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
arguments[arg] = newPath
|
|
|
|
return old.apply(this, arguments)
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const overrideAPI = function (module, name, arg) {
|
2016-01-19 18:25:03 +00:00
|
|
|
if (arg == null) {
|
2016-03-25 19:50:43 +00:00
|
|
|
arg = 0
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const old = module[name]
|
2016-03-25 19:50:43 +00:00
|
|
|
module[name] = function () {
|
2016-07-25 17:07:39 +00:00
|
|
|
const p = arguments[arg]
|
2016-03-25 19:50:43 +00:00
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return old.apply(this, arguments)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const callback = arguments[arguments.length - 1]
|
2016-01-19 18:25:03 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2016-03-25 19:50:43 +00:00
|
|
|
return overrideAPISync(module, name, arg)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return invalidArchiveError(asarPath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const newPath = archive.copyFileOut(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!newPath) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return notFoundError(asarPath, filePath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
arguments[arg] = newPath
|
|
|
|
return old.apply(this, arguments)
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
|
|
|
// Override fs APIs.
|
2016-03-25 19:50:43 +00:00
|
|
|
exports.wrapFsWithAsar = function (fs) {
|
2016-07-25 17:07:39 +00:00
|
|
|
const logFDs = {}
|
|
|
|
const logASARAccess = function (asarPath, filePath, offset) {
|
2015-12-22 19:20:50 +00:00
|
|
|
if (!process.env.ELECTRON_LOG_ASAR_READS) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return
|
2015-12-22 19:20:50 +00:00
|
|
|
}
|
|
|
|
if (!logFDs[asarPath]) {
|
2016-03-25 19:50:43 +00:00
|
|
|
const path = require('path')
|
2016-07-25 17:07:39 +00:00
|
|
|
const logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'
|
|
|
|
const logPath = path.join(require('os').tmpdir(), logFilename)
|
2016-03-25 19:50:43 +00:00
|
|
|
logFDs[asarPath] = fs.openSync(logPath, 'a')
|
|
|
|
console.log('Logging ' + asarPath + ' access to ' + logPath)
|
|
|
|
}
|
|
|
|
fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n')
|
|
|
|
}
|
2015-12-22 19:20:50 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const {lstatSync} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.lstatSync = function (p) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return lstatSync(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
invalidArchiveError(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const stats = archive.stat(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!stats) {
|
2016-03-25 19:50:43 +00:00
|
|
|
notFoundError(asarPath, filePath)
|
|
|
|
}
|
|
|
|
return asarStatsToFsStats(stats)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {lstat} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.lstat = function (p, callback) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return lstat(p, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return invalidArchiveError(asarPath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const stats = getOrCreateArchive(asarPath).stat(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!stats) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return notFoundError(asarPath, filePath, callback)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
callback(null, asarStatsToFsStats(stats))
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {statSync} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.statSync = function (p) {
|
|
|
|
const [isAsar] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return statSync(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Do not distinguish links for now.
|
2016-03-25 19:50:43 +00:00
|
|
|
return fs.lstatSync(p)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {stat} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.stat = function (p, callback) {
|
|
|
|
const [isAsar] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return stat(p, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Do not distinguish links for now.
|
2016-07-25 17:07:39 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
fs.lstat(p, callback)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {statSyncNoException} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.statSyncNoException = function (p) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return statSyncNoException(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return false
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const stats = archive.stat(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!stats) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return asarStatsToFsStats(stats)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {realpathSync} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.realpathSync = function (p) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return realpathSync.apply(this, arguments)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
invalidArchiveError(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const real = archive.realpath(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (real === false) {
|
2016-03-25 19:50:43 +00:00
|
|
|
notFoundError(asarPath, filePath)
|
|
|
|
}
|
|
|
|
return path.join(realpathSync(asarPath), real)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {realpath} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.realpath = function (p, cache, callback) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return realpath.apply(this, arguments)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (typeof cache === 'function') {
|
2016-03-25 19:50:43 +00:00
|
|
|
callback = cache
|
|
|
|
cache = void 0
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return invalidArchiveError(asarPath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const real = archive.realpath(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (real === false) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return notFoundError(asarPath, filePath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
return realpath(asarPath, function (err, p) {
|
2016-01-19 18:25:03 +00:00
|
|
|
if (err) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return callback(err)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
return callback(null, path.join(p, real))
|
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {exists} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.exists = function (p, callback) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return exists(p, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return invalidArchiveError(asarPath, callback)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
callback(archive.stat(filePath) !== false)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {existsSync} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.existsSync = function (p) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return existsSync(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return archive.stat(filePath) !== false
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2016-07-25 17:49:20 +00:00
|
|
|
const {access} = fs
|
|
|
|
fs.access = function (p, mode, callback) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
|
|
|
if (!isAsar) {
|
|
|
|
return access.apply(this, arguments)
|
|
|
|
}
|
|
|
|
if (typeof mode === 'function') {
|
|
|
|
callback = mode
|
|
|
|
mode = fs.constants.F_OK
|
|
|
|
}
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
|
|
|
if (!archive) {
|
|
|
|
return invalidArchiveError(asarPath, callback)
|
|
|
|
}
|
|
|
|
const info = archive.getFileInfo(filePath)
|
|
|
|
if (!info) {
|
|
|
|
return notFoundError(asarPath, filePath, callback)
|
|
|
|
}
|
|
|
|
if (info.unpacked) {
|
|
|
|
const realPath = archive.copyFileOut(filePath)
|
|
|
|
return fs.access(realPath, mode, callback)
|
|
|
|
}
|
|
|
|
const stats = getOrCreateArchive(asarPath).stat(filePath)
|
|
|
|
if (!stats) {
|
|
|
|
return notFoundError(asarPath, filePath, callback)
|
|
|
|
}
|
|
|
|
if (mode & fs.constants.W_OK) {
|
|
|
|
return accessError(asarPath, filePath, callback)
|
|
|
|
}
|
|
|
|
process.nextTick(function () {
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-25 18:05:18 +00:00
|
|
|
const {accessSync} = fs
|
|
|
|
fs.accessSync = function (p, mode) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
|
|
|
if (!isAsar) {
|
|
|
|
return accessSync.apply(this, arguments)
|
|
|
|
}
|
|
|
|
if (mode == null) {
|
|
|
|
mode = fs.constants.F_OK
|
|
|
|
}
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
|
|
|
if (!archive) {
|
|
|
|
invalidArchiveError(asarPath)
|
|
|
|
}
|
|
|
|
const info = archive.getFileInfo(filePath)
|
|
|
|
if (!info) {
|
|
|
|
notFoundError(asarPath, filePath)
|
|
|
|
}
|
|
|
|
if (info.unpacked) {
|
|
|
|
const realPath = archive.copyFileOut(filePath)
|
|
|
|
return fs.accessSync(realPath, mode)
|
|
|
|
}
|
|
|
|
const stats = getOrCreateArchive(asarPath).stat(filePath)
|
|
|
|
if (!stats) {
|
|
|
|
notFoundError(asarPath, filePath)
|
|
|
|
}
|
|
|
|
if (mode & fs.constants.W_OK) {
|
|
|
|
accessError(asarPath, filePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const {readFile} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.readFile = function (p, options, callback) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return readFile.apply(this, arguments)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (typeof options === 'function') {
|
2016-03-25 19:50:43 +00:00
|
|
|
callback = options
|
|
|
|
options = void 0
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return invalidArchiveError(asarPath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!info) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return notFoundError(asarPath, filePath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (info.size === 0) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return process.nextTick(function () {
|
2016-07-25 17:07:39 +00:00
|
|
|
callback(null, new Buffer(0))
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (info.unpacked) {
|
2016-07-25 17:07:39 +00:00
|
|
|
const realPath = archive.copyFileOut(filePath)
|
2016-03-25 19:50:43 +00:00
|
|
|
return fs.readFile(realPath, options, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (!options) {
|
|
|
|
options = {
|
|
|
|
encoding: null
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
} else if (util.isString(options)) {
|
|
|
|
options = {
|
|
|
|
encoding: options
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
} else if (!util.isObject(options)) {
|
2016-03-25 19:50:43 +00:00
|
|
|
throw new TypeError('Bad arguments')
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const {encoding} = options
|
|
|
|
const buffer = new Buffer(info.size)
|
|
|
|
const fd = archive.getFd()
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!(fd >= 0)) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return notFoundError(asarPath, filePath, callback)
|
|
|
|
}
|
|
|
|
logASARAccess(asarPath, filePath, info.offset)
|
2016-07-25 17:07:39 +00:00
|
|
|
fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
|
|
|
|
callback(error, encoding ? buffer.toString(encoding) : buffer)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {readFileSync} = fs
|
|
|
|
fs.readFileSync = function (p, options) {
|
2016-03-25 19:50:43 +00:00
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return readFileSync.apply(this, arguments)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
invalidArchiveError(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!info) {
|
2016-03-25 19:50:43 +00:00
|
|
|
notFoundError(asarPath, filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (info.size === 0) {
|
|
|
|
if (options) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return ''
|
2016-01-19 18:25:03 +00:00
|
|
|
} else {
|
2016-03-25 19:50:43 +00:00
|
|
|
return new Buffer(0)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info.unpacked) {
|
2016-07-25 17:07:39 +00:00
|
|
|
const realPath = archive.copyFileOut(filePath)
|
2016-03-25 19:50:43 +00:00
|
|
|
return fs.readFileSync(realPath, options)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (!options) {
|
|
|
|
options = {
|
|
|
|
encoding: null
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
} else if (util.isString(options)) {
|
|
|
|
options = {
|
|
|
|
encoding: options
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
} else if (!util.isObject(options)) {
|
2016-03-25 19:50:43 +00:00
|
|
|
throw new TypeError('Bad arguments')
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const {encoding} = options
|
|
|
|
const buffer = new Buffer(info.size)
|
|
|
|
const fd = archive.getFd()
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!(fd >= 0)) {
|
2016-03-25 19:50:43 +00:00
|
|
|
notFoundError(asarPath, filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
logASARAccess(asarPath, filePath, info.offset)
|
|
|
|
fs.readSync(fd, buffer, 0, info.size, info.offset)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (encoding) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return buffer.toString(encoding)
|
2016-01-19 18:25:03 +00:00
|
|
|
} else {
|
2016-03-25 19:50:43 +00:00
|
|
|
return buffer
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {readdir} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.readdir = function (p, callback) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return readdir.apply(this, arguments)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return invalidArchiveError(asarPath, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const files = archive.readdir(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!files) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return notFoundError(asarPath, filePath, callback)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
process.nextTick(function () {
|
|
|
|
callback(null, files)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {readdirSync} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.readdirSync = function (p) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return readdirSync.apply(this, arguments)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
invalidArchiveError(asarPath)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const files = archive.readdir(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!files) {
|
2016-03-25 19:50:43 +00:00
|
|
|
notFoundError(asarPath, filePath)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {internalModuleReadFile} = process.binding('fs')
|
2016-03-25 19:50:43 +00:00
|
|
|
process.binding('fs').internalModuleReadFile = function (p) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return internalModuleReadFile(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!archive) {
|
2016-07-25 17:07:39 +00:00
|
|
|
return
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!info) {
|
2016-07-25 17:07:39 +00:00
|
|
|
return
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (info.size === 0) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return ''
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (info.unpacked) {
|
2016-07-25 17:07:39 +00:00
|
|
|
const realPath = archive.copyFileOut(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
return fs.readFileSync(realPath, {
|
|
|
|
encoding: 'utf8'
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const buffer = new Buffer(info.size)
|
|
|
|
const fd = archive.getFd()
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!(fd >= 0)) {
|
2016-07-25 17:07:39 +00:00
|
|
|
return
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
|
|
|
logASARAccess(asarPath, filePath, info.offset)
|
|
|
|
fs.readSync(fd, buffer, 0, info.size, info.offset)
|
|
|
|
return buffer.toString('utf8')
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {internalModuleStat} = process.binding('fs')
|
2016-03-25 19:50:43 +00:00
|
|
|
process.binding('fs').internalModuleStat = function (p) {
|
|
|
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!isAsar) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return internalModuleStat(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2016-01-19 18:25:03 +00:00
|
|
|
|
|
|
|
// -ENOENT
|
|
|
|
if (!archive) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return -34
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
const stats = archive.stat(filePath)
|
2016-01-19 18:25:03 +00:00
|
|
|
|
|
|
|
// -ENOENT
|
|
|
|
if (!stats) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return -34
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
|
|
|
if (stats.isDirectory) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return 1
|
2016-01-19 18:25:03 +00:00
|
|
|
} else {
|
2016-03-25 19:50:43 +00:00
|
|
|
return 0
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
|
|
|
// Calling mkdir for directory inside asar archive should throw ENOTDIR
|
|
|
|
// error, but on Windows it throws ENOENT.
|
|
|
|
// This is to work around the recursive looping bug of mkdirp since it is
|
|
|
|
// widely used.
|
|
|
|
if (process.platform === 'win32') {
|
2016-07-25 17:07:39 +00:00
|
|
|
const {mkdir} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.mkdir = function (p, mode, callback) {
|
2016-01-19 18:25:03 +00:00
|
|
|
if (typeof mode === 'function') {
|
2016-03-25 19:50:43 +00:00
|
|
|
callback = mode
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
const [isAsar, , filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (isAsar && filePath.length) {
|
2016-03-25 19:50:43 +00:00
|
|
|
return notDirError(callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
mkdir(p, mode, callback)
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const {mkdirSync} = fs
|
2016-03-25 19:50:43 +00:00
|
|
|
fs.mkdirSync = function (p, mode) {
|
|
|
|
const [isAsar, , filePath] = splitPath(p)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (isAsar && filePath.length) {
|
2016-03-25 19:50:43 +00:00
|
|
|
notDirError()
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
return mkdirSync(p, mode)
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 15:08:08 +00:00
|
|
|
|
|
|
|
// Executing a command string containing a path to an asar
|
2016-05-25 23:20:49 +00:00
|
|
|
// archive confuses `childProcess.execFile`, which is internally
|
|
|
|
// called by `childProcess.{exec,execSync}`, causing
|
2016-05-19 15:08:08 +00:00
|
|
|
// Electron to consider the full command as a single path
|
|
|
|
// to an archive.
|
2016-07-25 17:07:39 +00:00
|
|
|
['exec', 'execSync'].forEach(function (functionName) {
|
|
|
|
const old = childProcess[functionName]
|
2016-05-25 23:20:49 +00:00
|
|
|
childProcess[functionName] = function () {
|
2016-07-25 17:07:39 +00:00
|
|
|
const processNoAsarOriginalValue = process.noAsar
|
2016-05-19 15:08:08 +00:00
|
|
|
process.noAsar = true
|
2016-09-06 20:40:25 +00:00
|
|
|
try {
|
|
|
|
return old.apply(this, arguments)
|
|
|
|
} finally {
|
|
|
|
process.noAsar = processNoAsarOriginalValue
|
|
|
|
}
|
2016-05-19 15:08:08 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
overrideAPI(fs, 'open')
|
2016-05-25 23:20:49 +00:00
|
|
|
overrideAPI(childProcess, 'execFile')
|
2016-03-25 19:50:43 +00:00
|
|
|
overrideAPISync(process, 'dlopen', 1)
|
|
|
|
overrideAPISync(require('module')._extensions, '.node', 1)
|
|
|
|
overrideAPISync(fs, 'openSync')
|
2016-07-25 17:07:39 +00:00
|
|
|
overrideAPISync(childProcess, 'execFileSync')
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
|
|
|
})()
|