Skip to content

Commit

Permalink
(Closes #209, Closes #237) lib/mkdirs: if invalid path char, return c…
Browse files Browse the repository at this point in the history
…allback OR throw err
  • Loading branch information
jprichardson committed Apr 18, 2016
1 parent a0cb04b commit 8c56e5b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/mkdirs/__tests__/README.md

This file was deleted.

38 changes: 38 additions & 0 deletions lib/mkdirs/__tests__/issue-209.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var assert = require('assert')
var fse = require(process.cwd())

/* global describe, it */

describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error', function () {
// only seems to be an issue on Windows.
if (process.platform !== 'win32') return

it('should return a callback', function (done) {
var file = './bad?dir'
fse.mkdirp(file, function (err) {
assert(err, 'error is present')
assert.strictEqual(err.code, 'EINVAL')

var file2 = 'c:\\tmp\foo:moo'
fse.mkdirp(file2, function (err) {
assert(err, 'error is present')
assert.strictEqual(err.code, 'EINVAL')
done()
})
})
})

describe('> sync', function () {
it('should throw an error', function () {
var didErr
try {
var file = 'c:\\tmp\foo:moo'
fse.mkdirpSync(file)
} catch (err) {
// console.error(err)
didErr = true
}
assert(didErr)
})
})
})
7 changes: 7 additions & 0 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('./win32').invalidWin32Path

var o777 = parseInt('0777', 8)

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

if (process.platform === 'win32' && invalidWin32Path(p)) {
var errInval = new Error(p + ' contains invalid WIN32 path characters.')
errInval.code = 'EINVAL'
throw errInval
}

if (mode === undefined) {
mode = o777 & (~process.umask())
}
Expand Down
7 changes: 7 additions & 0 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('./win32').invalidWin32Path

var o777 = parseInt('0777', 8)

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

if (process.platform === 'win32' && invalidWin32Path(p)) {
var errInval = new Error(p + ' contains invalid WIN32 path characters.')
errInval.code = 'EINVAL'
return callback(errInval)
}

var mode = opts.mode
var xfs = opts.fs || fs

Expand Down
24 changes: 24 additions & 0 deletions lib/mkdirs/win32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'
var path = require('path')

// get drive on windows
function getRootPath (p) {
p = path.normalize(path.resolve(p)).split(path.sep)
if (p.length > 0) return p[0]
else return null
}

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

function invalidWin32Path (p) {
var rp = getRootPath(p)
p = p.replace(rp, '')
return INVALID_PATH_CHARS.test(p)
}

module.exports = {
getRootPath: getRootPath,
invalidWin32Path: invalidWin32Path
}

0 comments on commit 8c56e5b

Please sign in to comment.