From ef296f4a5e06a417c66797cf41bf58fe2dea2714 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Mon, 30 Nov 2015 17:57:01 -0500 Subject: [PATCH] fs: un-deprecate existsSync(), throw error on EPERM MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fs.existSync() is actually a pretty useful convenience function, “fix” its technical shortcoming by throwing on EPERM errors so we can un-deprecate it. --- doc/api/fs.markdown | 7 +++++-- lib/fs.js | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown index 20aef1147f0157..2d5d6901485d70 100644 --- a/doc/api/fs.markdown +++ b/doc/api/fs.markdown @@ -367,11 +367,14 @@ non-existent. ## fs.existsSync(path) - Stability: 0 - Deprecated: Use [fs.statSync][] or [fs.accessSync][] instead. - Synchronous version of [`fs.exists`][]. Returns `true` if the file exists, `false` otherwise. +Note: this function will throw on Windows if an EPERM error is returned by the +underlying `fs.statSync()` call. However, it is not suggested to attempt to +catch this error. Instead, the end user probably needs to fix the file's +permissions by hand. + ## fs.fchmod(fd, mode, callback) Asynchronous fchmod(2). No arguments other than a possible exception diff --git a/lib/fs.js b/lib/fs.js index 650ede67af8bae..1a19aa2a18c500 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -223,6 +223,9 @@ fs.existsSync = function(path) { binding.stat(pathModule._makeLong(path)); return true; } catch (e) { + if (e.code === 'EPERM') + throw e; // EPERM means we no longer knows if it exists or not. + return false; } };