Skip to content

Commit

Permalink
(#209) lib/mkdirs: tidy invalid-win32-path
Browse files Browse the repository at this point in the history
  • Loading branch information
jprichardson committed Apr 17, 2016
1 parent 94a3e89 commit c39d173
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/mkdirs/__tests__/issue-209.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error
it('should return a cleaner error than inifinite loop, stack crash', function (done) {
var file = './bad?dir'
fse.mkdirp(file, function (err) {
assert(err, 'error is present')
assert.strictEqual(err.code, 'EINVAL')
done()
})
Expand Down
11 changes: 11 additions & 0 deletions lib/mkdirs/invalid-win32-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

// http://stackoverflow.com/a/62888/10333 contains more accurate
// TODO: expand to include the rest
var INVALID_PATH_CHARS = /[<>:"|?*]/

function invalidWin32Path (path) {
return INVALID_PATH_CHARS.test(path)
}

module.exports = invalidWin32Path
6 changes: 3 additions & 3 deletions lib/mkdirs/mkdirs-sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('graceful-fs')
var path = require('path')
var invalidWin32Path = require('./invalid-win32-path')

var o777 = parseInt('0777', 8)

Expand All @@ -11,11 +12,10 @@ function mkdirsSync (p, opts, made) {
var mode = opts.mode
var xfs = opts.fs || fs

if (process.platform === 'win32') {
if (process.platform === 'win32' && invalidWin32Path(p)) {
var errInval = new Error(p + ' contains invalid WIN32 path characters.')
errInval.code = 'EINVAL'
// http://stackoverflow.com/a/62888/10333 contains more accurate
if (/[<>:"|?*]/.test(p)) throw errInval
throw errInval
}

if (mode === undefined) {
Expand Down
6 changes: 3 additions & 3 deletions lib/mkdirs/mkdirs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('graceful-fs')
var path = require('path')
var invalidWin32Path = require('./invalid-win32-path')

var o777 = parseInt('0777', 8)

Expand All @@ -11,11 +12,10 @@ function mkdirs (p, opts, callback, made) {
opts = { mode: opts }
}

if (process.platform === 'win32') {
if (process.platform === 'win32' && invalidWin32Path(p)) {
var errInval = new Error(p + ' contains invalid WIN32 path characters.')
errInval.code = 'EINVAL'
// http://stackoverflow.com/a/62888/10333 contains more accurate
if (/[<>:"|?*]/.test(p)) return callback(errInval)
return callback(errInval)
}

var mode = opts.mode
Expand Down

0 comments on commit c39d173

Please sign in to comment.