From 846857232115fe1c8de62ab362290b1229c96f1b Mon Sep 17 00:00:00 2001 From: vyv03354 Date: Sat, 14 Oct 2017 19:28:45 +0900 Subject: [PATCH] return EISDIR if path is a directory readLinkOrShim of gentry-rm will not work correctly without this change --- index.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6a22654..1a4cf4f 100644 --- a/index.js +++ b/index.js @@ -35,14 +35,29 @@ function notaShim (path, er) { return er } +function isDir (path, er) { + er.code = 'EISDIR' + er.message = "'" + path + "' is a directory" + return er +} + var readCmdShim = module.exports = function (path, cb) { var er = new Error() Error.captureStackTrace(er, readCmdShim) fs.readFile(path, function (readFileEr, contents) { - if (readFileEr) return cb(wrapError(readFileEr, er)) - var destination = extractPath(path, contents.toString()) - if (destination) return cb(null, destination) - return cb(notaShim(path, er)) + if (readFileEr) { + // fs.readFile will return EINVAL rather than EISDIR + // if path is a directory. + if (readFileEr.code !== 'EINVAL') return cb(wrapError(readFileEr, er)) + fs.stat(path, function (statEr, stats) { + if (!statEr && stats.isDirectory()) return cb(isDir(path, er)) + return cb(wrapError(readFileEr, er)) + }) + } else { + var destination = extractPath(path, contents.toString()) + if (destination) return cb(null, destination) + return cb(notaShim(path, er)) + } }) }