Merge pull request #6598 from electron/fs-access-asar
Add asar implementation of fs.access/accessSync
This commit is contained in:
Коммит
b139d5ad1c
|
@ -4,14 +4,13 @@
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
|
|
||||||
var hasProp = {}.hasOwnProperty
|
const hasProp = {}.hasOwnProperty
|
||||||
|
|
||||||
// Cache asar archive objects.
|
// Cache asar archive objects.
|
||||||
var cachedArchives = {}
|
const cachedArchives = {}
|
||||||
|
|
||||||
var getOrCreateArchive = function (p) {
|
const getOrCreateArchive = function (p) {
|
||||||
var archive
|
let archive = cachedArchives[p]
|
||||||
archive = cachedArchives[p]
|
|
||||||
if (archive != null) {
|
if (archive != null) {
|
||||||
return archive
|
return archive
|
||||||
}
|
}
|
||||||
|
@ -25,31 +24,33 @@
|
||||||
|
|
||||||
// Clean cache on quit.
|
// Clean cache on quit.
|
||||||
process.on('exit', function () {
|
process.on('exit', function () {
|
||||||
var archive, p
|
for (let p in cachedArchives) {
|
||||||
for (p in cachedArchives) {
|
|
||||||
if (!hasProp.call(cachedArchives, p)) continue
|
if (!hasProp.call(cachedArchives, p)) continue
|
||||||
archive = cachedArchives[p]
|
cachedArchives[p].destroy()
|
||||||
archive.destroy()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Separate asar package's path from full path.
|
// Separate asar package's path from full path.
|
||||||
var splitPath = function (p) {
|
const splitPath = function (p) {
|
||||||
var index
|
|
||||||
|
|
||||||
// shortcut to disable asar.
|
// shortcut to disable asar.
|
||||||
if (process.noAsar) {
|
if (process.noAsar) {
|
||||||
return [false]
|
return [false]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Buffer.isBuffer(p)) {
|
||||||
|
p = p.toString()
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof p !== 'string') {
|
if (typeof p !== 'string') {
|
||||||
return [false]
|
return [false]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p.substr(-5) === '.asar') {
|
if (p.substr(-5) === '.asar') {
|
||||||
return [true, p, '']
|
return [true, p, '']
|
||||||
}
|
}
|
||||||
|
|
||||||
p = path.normalize(p)
|
p = path.normalize(p)
|
||||||
index = p.lastIndexOf('.asar' + path.sep)
|
const index = p.lastIndexOf('.asar' + path.sep)
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
return [false]
|
return [false]
|
||||||
}
|
}
|
||||||
|
@ -57,15 +58,15 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert asar archive's Stats object to fs's Stats object.
|
// Convert asar archive's Stats object to fs's Stats object.
|
||||||
var nextInode = 0
|
let nextInode = 0
|
||||||
|
|
||||||
var uid = process.getuid != null ? process.getuid() : 0
|
const uid = process.getuid != null ? process.getuid() : 0
|
||||||
|
|
||||||
var gid = process.getgid != null ? process.getgid() : 0
|
const gid = process.getgid != null ? process.getgid() : 0
|
||||||
|
|
||||||
var fakeTime = new Date()
|
const fakeTime = new Date()
|
||||||
|
|
||||||
var asarStatsToFsStats = function (stats) {
|
const asarStatsToFsStats = function (stats) {
|
||||||
return {
|
return {
|
||||||
dev: 1,
|
dev: 1,
|
||||||
ino: ++nextInode,
|
ino: ++nextInode,
|
||||||
|
@ -104,97 +105,110 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a ENOENT error.
|
// Create a ENOENT error.
|
||||||
var notFoundError = function (asarPath, filePath, callback) {
|
const notFoundError = function (asarPath, filePath, callback) {
|
||||||
var error
|
const error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
|
||||||
error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
|
|
||||||
error.code = 'ENOENT'
|
error.code = 'ENOENT'
|
||||||
error.errno = -2
|
error.errno = -2
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
return process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
return callback(error)
|
callback(error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a ENOTDIR error.
|
// Create a ENOTDIR error.
|
||||||
var notDirError = function (callback) {
|
const notDirError = function (callback) {
|
||||||
var error
|
const error = new Error('ENOTDIR, not a directory')
|
||||||
error = new Error('ENOTDIR, not a directory')
|
|
||||||
error.code = 'ENOTDIR'
|
error.code = 'ENOTDIR'
|
||||||
error.errno = -20
|
error.errno = -20
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
return process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
return callback(error)
|
callback(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create invalid archive error.
|
// Create invalid archive error.
|
||||||
var invalidArchiveError = function (asarPath, callback) {
|
const invalidArchiveError = function (asarPath, callback) {
|
||||||
var error
|
const error = new Error(`Invalid package ${asarPath}`)
|
||||||
error = new Error(`Invalid package ${asarPath}`)
|
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
return process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
return callback(error)
|
callback(error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override APIs that rely on passing file path instead of content to C++.
|
// Override APIs that rely on passing file path instead of content to C++.
|
||||||
var overrideAPISync = function (module, name, arg) {
|
const overrideAPISync = function (module, name, arg) {
|
||||||
var old
|
|
||||||
if (arg == null) {
|
if (arg == null) {
|
||||||
arg = 0
|
arg = 0
|
||||||
}
|
}
|
||||||
old = module[name]
|
const old = module[name]
|
||||||
module[name] = function () {
|
module[name] = function () {
|
||||||
var archive, newPath, p
|
const p = arguments[arg]
|
||||||
p = arguments[arg]
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return old.apply(this, arguments)
|
return old.apply(this, arguments)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
|
||||||
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
invalidArchiveError(asarPath)
|
invalidArchiveError(asarPath)
|
||||||
}
|
}
|
||||||
newPath = archive.copyFileOut(filePath)
|
|
||||||
|
const newPath = archive.copyFileOut(filePath)
|
||||||
if (!newPath) {
|
if (!newPath) {
|
||||||
notFoundError(asarPath, filePath)
|
notFoundError(asarPath, filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
arguments[arg] = newPath
|
arguments[arg] = newPath
|
||||||
return old.apply(this, arguments)
|
return old.apply(this, arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var overrideAPI = function (module, name, arg) {
|
const overrideAPI = function (module, name, arg) {
|
||||||
var old
|
|
||||||
if (arg == null) {
|
if (arg == null) {
|
||||||
arg = 0
|
arg = 0
|
||||||
}
|
}
|
||||||
old = module[name]
|
const old = module[name]
|
||||||
module[name] = function () {
|
module[name] = function () {
|
||||||
var archive, callback, newPath, p
|
const p = arguments[arg]
|
||||||
p = arguments[arg]
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return old.apply(this, arguments)
|
return old.apply(this, arguments)
|
||||||
}
|
}
|
||||||
callback = arguments[arguments.length - 1]
|
|
||||||
|
const callback = arguments[arguments.length - 1]
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
return overrideAPISync(module, name, arg)
|
return overrideAPISync(module, name, arg)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
|
||||||
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return invalidArchiveError(asarPath, callback)
|
return invalidArchiveError(asarPath, callback)
|
||||||
}
|
}
|
||||||
newPath = archive.copyFileOut(filePath)
|
|
||||||
|
const newPath = archive.copyFileOut(filePath)
|
||||||
if (!newPath) {
|
if (!newPath) {
|
||||||
return notFoundError(asarPath, filePath, callback)
|
return notFoundError(asarPath, filePath, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
arguments[arg] = newPath
|
arguments[arg] = newPath
|
||||||
return old.apply(this, arguments)
|
return old.apply(this, arguments)
|
||||||
}
|
}
|
||||||
|
@ -202,61 +216,58 @@
|
||||||
|
|
||||||
// Override fs APIs.
|
// Override fs APIs.
|
||||||
exports.wrapFsWithAsar = function (fs) {
|
exports.wrapFsWithAsar = function (fs) {
|
||||||
var exists, existsSync, internalModuleReadFile, internalModuleStat, lstat, lstatSync, mkdir, mkdirSync, readFile, readFileSync, readdir, readdirSync, realpath, realpathSync, stat, statSync, statSyncNoException, logFDs, logASARAccess
|
const logFDs = {}
|
||||||
|
const logASARAccess = function (asarPath, filePath, offset) {
|
||||||
logFDs = {}
|
|
||||||
logASARAccess = function (asarPath, filePath, offset) {
|
|
||||||
if (!process.env.ELECTRON_LOG_ASAR_READS) {
|
if (!process.env.ELECTRON_LOG_ASAR_READS) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!logFDs[asarPath]) {
|
if (!logFDs[asarPath]) {
|
||||||
var logFilename, logPath
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'
|
const logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'
|
||||||
logPath = path.join(require('os').tmpdir(), logFilename)
|
const logPath = path.join(require('os').tmpdir(), logFilename)
|
||||||
logFDs[asarPath] = fs.openSync(logPath, 'a')
|
logFDs[asarPath] = fs.openSync(logPath, 'a')
|
||||||
console.log('Logging ' + asarPath + ' access to ' + logPath)
|
console.log('Logging ' + asarPath + ' access to ' + logPath)
|
||||||
}
|
}
|
||||||
fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n')
|
fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
lstatSync = fs.lstatSync
|
const {lstatSync} = fs
|
||||||
fs.lstatSync = function (p) {
|
fs.lstatSync = function (p) {
|
||||||
var archive, stats
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return lstatSync(p)
|
return lstatSync(p)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
invalidArchiveError(asarPath)
|
invalidArchiveError(asarPath)
|
||||||
}
|
}
|
||||||
stats = archive.stat(filePath)
|
const stats = archive.stat(filePath)
|
||||||
if (!stats) {
|
if (!stats) {
|
||||||
notFoundError(asarPath, filePath)
|
notFoundError(asarPath, filePath)
|
||||||
}
|
}
|
||||||
return asarStatsToFsStats(stats)
|
return asarStatsToFsStats(stats)
|
||||||
}
|
}
|
||||||
lstat = fs.lstat
|
|
||||||
|
const {lstat} = fs
|
||||||
fs.lstat = function (p, callback) {
|
fs.lstat = function (p, callback) {
|
||||||
var archive, stats
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return lstat(p, callback)
|
return lstat(p, callback)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return invalidArchiveError(asarPath, callback)
|
return invalidArchiveError(asarPath, callback)
|
||||||
}
|
}
|
||||||
stats = getOrCreateArchive(asarPath).stat(filePath)
|
const stats = getOrCreateArchive(asarPath).stat(filePath)
|
||||||
if (!stats) {
|
if (!stats) {
|
||||||
return notFoundError(asarPath, filePath, callback)
|
return notFoundError(asarPath, filePath, callback)
|
||||||
}
|
}
|
||||||
return process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
return callback(null, asarStatsToFsStats(stats))
|
callback(null, asarStatsToFsStats(stats))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
statSync = fs.statSync
|
|
||||||
|
const {statSync} = fs
|
||||||
fs.statSync = function (p) {
|
fs.statSync = function (p) {
|
||||||
const [isAsar] = splitPath(p)
|
const [isAsar] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
|
@ -266,7 +277,8 @@
|
||||||
// Do not distinguish links for now.
|
// Do not distinguish links for now.
|
||||||
return fs.lstatSync(p)
|
return fs.lstatSync(p)
|
||||||
}
|
}
|
||||||
stat = fs.stat
|
|
||||||
|
const {stat} = fs
|
||||||
fs.stat = function (p, callback) {
|
fs.stat = function (p, callback) {
|
||||||
const [isAsar] = splitPath(p)
|
const [isAsar] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
|
@ -274,47 +286,47 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not distinguish links for now.
|
// Do not distinguish links for now.
|
||||||
return process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
return fs.lstat(p, callback)
|
fs.lstat(p, callback)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
statSyncNoException = fs.statSyncNoException
|
|
||||||
|
const {statSyncNoException} = fs
|
||||||
fs.statSyncNoException = function (p) {
|
fs.statSyncNoException = function (p) {
|
||||||
var archive, stats
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return statSyncNoException(p)
|
return statSyncNoException(p)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
stats = archive.stat(filePath)
|
const stats = archive.stat(filePath)
|
||||||
if (!stats) {
|
if (!stats) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return asarStatsToFsStats(stats)
|
return asarStatsToFsStats(stats)
|
||||||
}
|
}
|
||||||
realpathSync = fs.realpathSync
|
|
||||||
|
const {realpathSync} = fs
|
||||||
fs.realpathSync = function (p) {
|
fs.realpathSync = function (p) {
|
||||||
var archive, real
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return realpathSync.apply(this, arguments)
|
return realpathSync.apply(this, arguments)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
invalidArchiveError(asarPath)
|
invalidArchiveError(asarPath)
|
||||||
}
|
}
|
||||||
real = archive.realpath(filePath)
|
const real = archive.realpath(filePath)
|
||||||
if (real === false) {
|
if (real === false) {
|
||||||
notFoundError(asarPath, filePath)
|
notFoundError(asarPath, filePath)
|
||||||
}
|
}
|
||||||
return path.join(realpathSync(asarPath), real)
|
return path.join(realpathSync(asarPath), real)
|
||||||
}
|
}
|
||||||
realpath = fs.realpath
|
|
||||||
|
const {realpath} = fs
|
||||||
fs.realpath = function (p, cache, callback) {
|
fs.realpath = function (p, cache, callback) {
|
||||||
var archive, real
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return realpath.apply(this, arguments)
|
return realpath.apply(this, arguments)
|
||||||
|
@ -323,11 +335,11 @@
|
||||||
callback = cache
|
callback = cache
|
||||||
cache = void 0
|
cache = void 0
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return invalidArchiveError(asarPath, callback)
|
return invalidArchiveError(asarPath, callback)
|
||||||
}
|
}
|
||||||
real = archive.realpath(filePath)
|
const real = archive.realpath(filePath)
|
||||||
if (real === false) {
|
if (real === false) {
|
||||||
return notFoundError(asarPath, filePath, callback)
|
return notFoundError(asarPath, filePath, callback)
|
||||||
}
|
}
|
||||||
|
@ -338,37 +350,101 @@
|
||||||
return callback(null, path.join(p, real))
|
return callback(null, path.join(p, real))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
exists = fs.exists
|
|
||||||
|
const {exists} = fs
|
||||||
fs.exists = function (p, callback) {
|
fs.exists = function (p, callback) {
|
||||||
var archive
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return exists(p, callback)
|
return exists(p, callback)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return invalidArchiveError(asarPath, callback)
|
return invalidArchiveError(asarPath, callback)
|
||||||
}
|
}
|
||||||
return process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
return callback(archive.stat(filePath) !== false)
|
callback(archive.stat(filePath) !== false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
existsSync = fs.existsSync
|
|
||||||
|
const {existsSync} = fs
|
||||||
fs.existsSync = function (p) {
|
fs.existsSync = function (p) {
|
||||||
var archive
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return existsSync(p)
|
return existsSync(p)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return archive.stat(filePath) !== false
|
return archive.stat(filePath) !== false
|
||||||
}
|
}
|
||||||
readFile = fs.readFile
|
|
||||||
|
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()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const {readFile} = fs
|
||||||
fs.readFile = function (p, options, callback) {
|
fs.readFile = function (p, options, callback) {
|
||||||
var archive, buffer, encoding, fd, info, realPath
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return readFile.apply(this, arguments)
|
return readFile.apply(this, arguments)
|
||||||
|
@ -377,21 +453,21 @@
|
||||||
callback = options
|
callback = options
|
||||||
options = void 0
|
options = void 0
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return invalidArchiveError(asarPath, callback)
|
return invalidArchiveError(asarPath, callback)
|
||||||
}
|
}
|
||||||
info = archive.getFileInfo(filePath)
|
const info = archive.getFileInfo(filePath)
|
||||||
if (!info) {
|
if (!info) {
|
||||||
return notFoundError(asarPath, filePath, callback)
|
return notFoundError(asarPath, filePath, callback)
|
||||||
}
|
}
|
||||||
if (info.size === 0) {
|
if (info.size === 0) {
|
||||||
return process.nextTick(function () {
|
return process.nextTick(function () {
|
||||||
return callback(null, new Buffer(0))
|
callback(null, new Buffer(0))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (info.unpacked) {
|
if (info.unpacked) {
|
||||||
realPath = archive.copyFileOut(filePath)
|
const realPath = archive.copyFileOut(filePath)
|
||||||
return fs.readFile(realPath, options, callback)
|
return fs.readFile(realPath, options, callback)
|
||||||
}
|
}
|
||||||
if (!options) {
|
if (!options) {
|
||||||
|
@ -405,31 +481,29 @@
|
||||||
} else if (!util.isObject(options)) {
|
} else if (!util.isObject(options)) {
|
||||||
throw new TypeError('Bad arguments')
|
throw new TypeError('Bad arguments')
|
||||||
}
|
}
|
||||||
encoding = options.encoding
|
const {encoding} = options
|
||||||
buffer = new Buffer(info.size)
|
const buffer = new Buffer(info.size)
|
||||||
fd = archive.getFd()
|
const fd = archive.getFd()
|
||||||
if (!(fd >= 0)) {
|
if (!(fd >= 0)) {
|
||||||
return notFoundError(asarPath, filePath, callback)
|
return notFoundError(asarPath, filePath, callback)
|
||||||
}
|
}
|
||||||
logASARAccess(asarPath, filePath, info.offset)
|
logASARAccess(asarPath, filePath, info.offset)
|
||||||
return fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
|
fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
|
||||||
return callback(error, encoding ? buffer.toString(encoding) : buffer)
|
callback(error, encoding ? buffer.toString(encoding) : buffer)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
readFileSync = fs.readFileSync
|
|
||||||
fs.readFileSync = function (p, opts) {
|
const {readFileSync} = fs
|
||||||
// this allows v8 to optimize this function
|
fs.readFileSync = function (p, options) {
|
||||||
var archive, buffer, encoding, fd, info, options, realPath
|
|
||||||
options = opts
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return readFileSync.apply(this, arguments)
|
return readFileSync.apply(this, arguments)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
invalidArchiveError(asarPath)
|
invalidArchiveError(asarPath)
|
||||||
}
|
}
|
||||||
info = archive.getFileInfo(filePath)
|
const info = archive.getFileInfo(filePath)
|
||||||
if (!info) {
|
if (!info) {
|
||||||
notFoundError(asarPath, filePath)
|
notFoundError(asarPath, filePath)
|
||||||
}
|
}
|
||||||
|
@ -441,7 +515,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (info.unpacked) {
|
if (info.unpacked) {
|
||||||
realPath = archive.copyFileOut(filePath)
|
const realPath = archive.copyFileOut(filePath)
|
||||||
return fs.readFileSync(realPath, options)
|
return fs.readFileSync(realPath, options)
|
||||||
}
|
}
|
||||||
if (!options) {
|
if (!options) {
|
||||||
|
@ -455,9 +529,9 @@
|
||||||
} else if (!util.isObject(options)) {
|
} else if (!util.isObject(options)) {
|
||||||
throw new TypeError('Bad arguments')
|
throw new TypeError('Bad arguments')
|
||||||
}
|
}
|
||||||
encoding = options.encoding
|
const {encoding} = options
|
||||||
buffer = new Buffer(info.size)
|
const buffer = new Buffer(info.size)
|
||||||
fd = archive.getFd()
|
const fd = archive.getFd()
|
||||||
if (!(fd >= 0)) {
|
if (!(fd >= 0)) {
|
||||||
notFoundError(asarPath, filePath)
|
notFoundError(asarPath, filePath)
|
||||||
}
|
}
|
||||||
|
@ -469,89 +543,89 @@
|
||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
readdir = fs.readdir
|
|
||||||
|
const {readdir} = fs
|
||||||
fs.readdir = function (p, callback) {
|
fs.readdir = function (p, callback) {
|
||||||
var archive, files
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return readdir.apply(this, arguments)
|
return readdir.apply(this, arguments)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return invalidArchiveError(asarPath, callback)
|
return invalidArchiveError(asarPath, callback)
|
||||||
}
|
}
|
||||||
files = archive.readdir(filePath)
|
const files = archive.readdir(filePath)
|
||||||
if (!files) {
|
if (!files) {
|
||||||
return notFoundError(asarPath, filePath, callback)
|
return notFoundError(asarPath, filePath, callback)
|
||||||
}
|
}
|
||||||
return process.nextTick(function () {
|
process.nextTick(function () {
|
||||||
return callback(null, files)
|
callback(null, files)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
readdirSync = fs.readdirSync
|
|
||||||
|
const {readdirSync} = fs
|
||||||
fs.readdirSync = function (p) {
|
fs.readdirSync = function (p) {
|
||||||
var archive, files
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return readdirSync.apply(this, arguments)
|
return readdirSync.apply(this, arguments)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
invalidArchiveError(asarPath)
|
invalidArchiveError(asarPath)
|
||||||
}
|
}
|
||||||
files = archive.readdir(filePath)
|
const files = archive.readdir(filePath)
|
||||||
if (!files) {
|
if (!files) {
|
||||||
notFoundError(asarPath, filePath)
|
notFoundError(asarPath, filePath)
|
||||||
}
|
}
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
internalModuleReadFile = process.binding('fs').internalModuleReadFile
|
|
||||||
|
const {internalModuleReadFile} = process.binding('fs')
|
||||||
process.binding('fs').internalModuleReadFile = function (p) {
|
process.binding('fs').internalModuleReadFile = function (p) {
|
||||||
var archive, buffer, fd, info, realPath
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return internalModuleReadFile(p)
|
return internalModuleReadFile(p)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return void 0
|
return
|
||||||
}
|
}
|
||||||
info = archive.getFileInfo(filePath)
|
const info = archive.getFileInfo(filePath)
|
||||||
if (!info) {
|
if (!info) {
|
||||||
return void 0
|
return
|
||||||
}
|
}
|
||||||
if (info.size === 0) {
|
if (info.size === 0) {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
if (info.unpacked) {
|
if (info.unpacked) {
|
||||||
realPath = archive.copyFileOut(filePath)
|
const realPath = archive.copyFileOut(filePath)
|
||||||
return fs.readFileSync(realPath, {
|
return fs.readFileSync(realPath, {
|
||||||
encoding: 'utf8'
|
encoding: 'utf8'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
buffer = new Buffer(info.size)
|
const buffer = new Buffer(info.size)
|
||||||
fd = archive.getFd()
|
const fd = archive.getFd()
|
||||||
if (!(fd >= 0)) {
|
if (!(fd >= 0)) {
|
||||||
return void 0
|
return
|
||||||
}
|
}
|
||||||
logASARAccess(asarPath, filePath, info.offset)
|
logASARAccess(asarPath, filePath, info.offset)
|
||||||
fs.readSync(fd, buffer, 0, info.size, info.offset)
|
fs.readSync(fd, buffer, 0, info.size, info.offset)
|
||||||
return buffer.toString('utf8')
|
return buffer.toString('utf8')
|
||||||
}
|
}
|
||||||
internalModuleStat = process.binding('fs').internalModuleStat
|
|
||||||
|
const {internalModuleStat} = process.binding('fs')
|
||||||
process.binding('fs').internalModuleStat = function (p) {
|
process.binding('fs').internalModuleStat = function (p) {
|
||||||
var archive, stats
|
|
||||||
const [isAsar, asarPath, filePath] = splitPath(p)
|
const [isAsar, asarPath, filePath] = splitPath(p)
|
||||||
if (!isAsar) {
|
if (!isAsar) {
|
||||||
return internalModuleStat(p)
|
return internalModuleStat(p)
|
||||||
}
|
}
|
||||||
archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
|
|
||||||
// -ENOENT
|
// -ENOENT
|
||||||
if (!archive) {
|
if (!archive) {
|
||||||
return -34
|
return -34
|
||||||
}
|
}
|
||||||
stats = archive.stat(filePath)
|
const stats = archive.stat(filePath)
|
||||||
|
|
||||||
// -ENOENT
|
// -ENOENT
|
||||||
if (!stats) {
|
if (!stats) {
|
||||||
|
@ -569,7 +643,7 @@
|
||||||
// This is to work around the recursive looping bug of mkdirp since it is
|
// This is to work around the recursive looping bug of mkdirp since it is
|
||||||
// widely used.
|
// widely used.
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
mkdir = fs.mkdir
|
const {mkdir} = fs
|
||||||
fs.mkdir = function (p, mode, callback) {
|
fs.mkdir = function (p, mode, callback) {
|
||||||
if (typeof mode === 'function') {
|
if (typeof mode === 'function') {
|
||||||
callback = mode
|
callback = mode
|
||||||
|
@ -578,9 +652,10 @@
|
||||||
if (isAsar && filePath.length) {
|
if (isAsar && filePath.length) {
|
||||||
return notDirError(callback)
|
return notDirError(callback)
|
||||||
}
|
}
|
||||||
return mkdir(p, mode, callback)
|
mkdir(p, mode, callback)
|
||||||
}
|
}
|
||||||
mkdirSync = fs.mkdirSync
|
|
||||||
|
const {mkdirSync} = fs
|
||||||
fs.mkdirSync = function (p, mode) {
|
fs.mkdirSync = function (p, mode) {
|
||||||
const [isAsar, , filePath] = splitPath(p)
|
const [isAsar, , filePath] = splitPath(p)
|
||||||
if (isAsar && filePath.length) {
|
if (isAsar && filePath.length) {
|
||||||
|
@ -595,12 +670,12 @@
|
||||||
// called by `childProcess.{exec,execSync}`, causing
|
// called by `childProcess.{exec,execSync}`, causing
|
||||||
// Electron to consider the full command as a single path
|
// Electron to consider the full command as a single path
|
||||||
// to an archive.
|
// to an archive.
|
||||||
[ 'exec', 'execSync' ].forEach(function (functionName) {
|
['exec', 'execSync'].forEach(function (functionName) {
|
||||||
var old = childProcess[functionName]
|
const old = childProcess[functionName]
|
||||||
childProcess[functionName] = function () {
|
childProcess[functionName] = function () {
|
||||||
var processNoAsarOriginalValue = process.noAsar
|
const processNoAsarOriginalValue = process.noAsar
|
||||||
process.noAsar = true
|
process.noAsar = true
|
||||||
var result = old.apply(this, arguments)
|
const result = old.apply(this, arguments)
|
||||||
process.noAsar = processNoAsarOriginalValue
|
process.noAsar = processNoAsarOriginalValue
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -611,6 +686,6 @@
|
||||||
overrideAPISync(process, 'dlopen', 1)
|
overrideAPISync(process, 'dlopen', 1)
|
||||||
overrideAPISync(require('module')._extensions, '.node', 1)
|
overrideAPISync(require('module')._extensions, '.node', 1)
|
||||||
overrideAPISync(fs, 'openSync')
|
overrideAPISync(fs, 'openSync')
|
||||||
return overrideAPISync(childProcess, 'execFileSync')
|
overrideAPISync(childProcess, 'execFileSync')
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
|
@ -13,6 +13,11 @@ describe('asar package', function () {
|
||||||
var fixtures = path.join(__dirname, 'fixtures')
|
var fixtures = path.join(__dirname, 'fixtures')
|
||||||
|
|
||||||
describe('node api', function () {
|
describe('node api', function () {
|
||||||
|
it('supports paths specified as a Buffer', function () {
|
||||||
|
var file = new Buffer(path.join(fixtures, 'asar', 'a.asar', 'file1'))
|
||||||
|
assert.equal(fs.existsSync(file), true)
|
||||||
|
})
|
||||||
|
|
||||||
describe('fs.readFileSync', function () {
|
describe('fs.readFileSync', function () {
|
||||||
it('does not leak fd', function () {
|
it('does not leak fd', function () {
|
||||||
var readCalls = 1
|
var readCalls = 1
|
||||||
|
@ -534,6 +539,70 @@ describe('asar package', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('fs.access', function () {
|
||||||
|
it('accesses a normal file', function (done) {
|
||||||
|
var p = path.join(fixtures, 'asar', 'a.asar', 'file1')
|
||||||
|
fs.access(p, function (err) {
|
||||||
|
assert(err == null)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws an error when called with write mode', function (done) {
|
||||||
|
var p = path.join(fixtures, 'asar', 'a.asar', 'file1')
|
||||||
|
fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
|
||||||
|
assert.equal(err.code, 'EACCES')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws an error when called on non-existent file', function (done) {
|
||||||
|
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
|
||||||
|
fs.access(p, function (err) {
|
||||||
|
assert.equal(err.code, 'ENOENT')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('allows write mode for unpacked files', function (done) {
|
||||||
|
var p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
|
||||||
|
fs.access(p, fs.constants.R_OK | fs.constants.W_OK, function (err) {
|
||||||
|
assert(err == null)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('fs.accessSync', function () {
|
||||||
|
it('accesses a normal file', function () {
|
||||||
|
var p = path.join(fixtures, 'asar', 'a.asar', 'file1')
|
||||||
|
assert.doesNotThrow(function () {
|
||||||
|
fs.accessSync(p)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws an error when called with write mode', function () {
|
||||||
|
var p = path.join(fixtures, 'asar', 'a.asar', 'file1')
|
||||||
|
assert.throws(function () {
|
||||||
|
fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
|
||||||
|
}, /EACCES/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws an error when called on non-existent file', function () {
|
||||||
|
var p = path.join(fixtures, 'asar', 'a.asar', 'not-exist')
|
||||||
|
assert.throws(function () {
|
||||||
|
fs.accessSync(p)
|
||||||
|
}, /ENOENT/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('allows write mode for unpacked files', function () {
|
||||||
|
var p = path.join(fixtures, 'asar', 'unpack.asar', 'a.txt')
|
||||||
|
assert.doesNotThrow(function () {
|
||||||
|
fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('child_process.fork', function () {
|
describe('child_process.fork', function () {
|
||||||
it('opens a normal js file', function (done) {
|
it('opens a normal js file', function (done) {
|
||||||
var child = ChildProcess.fork(path.join(fixtures, 'asar', 'a.asar', 'ping.js'))
|
var child = ChildProcess.fork(path.join(fixtures, 'asar', 'a.asar', 'ping.js'))
|
||||||
|
|
Загрузка…
Ссылка в новой задаче