Throw ENOTDIR when calling mkdir inside asar archive

This commit is contained in:
Cheng Zhao 2016-01-06 12:04:16 +08:00
Родитель ace7c62e5e
Коммит c4071a7f66
3 изменённых файлов: 47 добавлений и 0 удалений

Просмотреть файл

@ -63,6 +63,15 @@ notFoundError = (asarPath, filePath, callback) ->
throw error throw error
process.nextTick -> callback error process.nextTick -> callback error
# Create a ENOTDIR error.
notDirError = (callback) ->
error = new Error('ENOTDIR, not a directory')
error.code = 'ENOTDIR'
error.errno = -20
unless typeof callback is 'function'
throw error
process.nextTick -> callback error
# Create invalid archive error. # Create invalid archive error.
invalidArchiveError = (asarPath, callback) -> invalidArchiveError = (asarPath, callback) ->
error = new Error("Invalid package #{asarPath}") error = new Error("Invalid package #{asarPath}")
@ -351,6 +360,24 @@ exports.wrapFsWithAsar = (fs) ->
if stats.isDirectory then return 1 else return 0 if stats.isDirectory then return 1 else return 0
# 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 is 'win32'
mkdir = fs.mkdir
fs.mkdir = (p, mode, callback) ->
callback = mode if typeof mode is 'function'
[isAsar, asarPath, filePath] = splitPath p
return notDirError callback if isAsar and filePath.length
mkdir p, mode, callback
mkdirSync = fs.mkdirSync
fs.mkdirSync = (p, mode) ->
[isAsar, asarPath, filePath] = splitPath p
notDirError() if isAsar and filePath.length
mkdirSync p, mode
overrideAPI fs, 'open' overrideAPI fs, 'open'
overrideAPI child_process, 'execFile' overrideAPI child_process, 'execFile'
overrideAPISync process, 'dlopen', 1 overrideAPISync process, 'dlopen', 1

Просмотреть файл

@ -374,6 +374,18 @@ describe 'asar package', ->
assert.equal err.code, 'ENOENT' assert.equal err.code, 'ENOENT'
done() done()
describe 'fs.mkdir', ->
it 'throws error when calling inside asar archive', (done) ->
p = path.join fixtures, 'asar', 'a.asar', 'not-exist'
fs.mkdir p, (err) ->
assert.equal err.code, 'ENOTDIR'
done()
describe 'fs.mkdirSync', ->
it 'throws error when calling inside asar archive', ->
p = path.join fixtures, 'asar', 'a.asar', 'not-exist'
assert.throws (-> fs.mkdirSync p), new RegExp('ENOTDIR')
describe 'child_process.fork', -> describe 'child_process.fork', ->
child_process = require 'child_process' child_process = require 'child_process'
@ -547,6 +559,13 @@ describe 'asar package', ->
it 'does not touch global fs object', -> it 'does not touch global fs object', ->
assert.notEqual fs.readdir, gfs.readdir assert.notEqual fs.readdir, gfs.readdir
describe 'mkdirp module', ->
mkdirp = require 'mkdirp'
it 'throws error when calling inside asar archive', ->
p = path.join fixtures, 'asar', 'a.asar', 'not-exist'
assert.throws (-> mkdirp.sync p), new RegExp('ENOTDIR')
describe 'native-image', -> describe 'native-image', ->
it 'reads image from asar archive', -> it 'reads image from asar archive', ->
p = path.join fixtures, 'asar', 'logo.asar', 'logo.png' p = path.join fixtures, 'asar', 'logo.asar', 'logo.png'

Просмотреть файл

@ -7,6 +7,7 @@
"basic-auth": "^1.0.0", "basic-auth": "^1.0.0",
"graceful-fs": "3.0.5", "graceful-fs": "3.0.5",
"mocha": "2.1.0", "mocha": "2.1.0",
"mkdirp": "0.5.1",
"multiparty": "4.1.2", "multiparty": "4.1.2",
"q": "0.9.7", "q": "0.9.7",
"temp": "0.8.1", "temp": "0.8.1",