Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Throw ENOTDIR when calling mkdir inside asar archive
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jan 6, 2016
1 parent ace7c62 commit c4071a7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
27 changes: 27 additions & 0 deletions atom/common/lib/asar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ notFoundError = (asarPath, filePath, callback) ->
throw 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.
invalidArchiveError = (asarPath, callback) ->
error = new Error("Invalid package #{asarPath}")
Expand Down Expand Up @@ -351,6 +360,24 @@ exports.wrapFsWithAsar = (fs) ->

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 child_process, 'execFile'
overrideAPISync process, 'dlopen', 1
Expand Down
19 changes: 19 additions & 0 deletions spec/asar-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ describe 'asar package', ->
assert.equal err.code, 'ENOENT'
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', ->
child_process = require 'child_process'

Expand Down Expand Up @@ -547,6 +559,13 @@ describe 'asar package', ->
it 'does not touch global fs object', ->
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', ->
it 'reads image from asar archive', ->
p = path.join fixtures, 'asar', 'logo.asar', 'logo.png'
Expand Down
1 change: 1 addition & 0 deletions spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"basic-auth": "^1.0.0",
"graceful-fs": "3.0.5",
"mocha": "2.1.0",
"mkdirp": "0.5.1",
"multiparty": "4.1.2",
"q": "0.9.7",
"temp": "0.8.1",
Expand Down

0 comments on commit c4071a7

Please sign in to comment.