summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcjihrig <cjihrig@gmail.com>2014-12-15 13:58:37 -0500
committercjihrig <cjihrig@gmail.com>2014-12-19 10:27:48 -0500
commit56785958565378c9ba43c5bdf5c631f7738f95ab (patch)
treef1620dc26c8c4394e34fa07cdafb7434ac9c30bb
parenta5532674b09ebb10bee1bdd3c5c8ff44011a4912 (diff)
downloadnode-new-56785958565378c9ba43c5bdf5c631f7738f95ab.tar.gz
fs: deprecate exists() and existsSync()
These methods don't follow standard conventions, and shouldn't be used anyway. Fixes: https://github.com/iojs/io.js/issues/103 PR-URL: https://github.com/iojs/io.js/pull/166 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
-rw-r--r--benchmark/misc/module-loader.js4
-rw-r--r--doc/api/fs.markdown4
-rw-r--r--lib/fs.js8
-rw-r--r--test/common.js14
-rw-r--r--test/parallel/test-child-process-spawn-error.js3
-rw-r--r--test/parallel/test-fs-mkdir.js6
-rw-r--r--test/parallel/test-fs-symlink-dir-junction.js4
-rw-r--r--test/pummel/test-fs-watch-file-slow.js6
-rw-r--r--test/sequential/test-regress-GH-3739.js10
9 files changed, 37 insertions, 22 deletions
diff --git a/benchmark/misc/module-loader.js b/benchmark/misc/module-loader.js
index 96f8e7df1e..00a9369650 100644
--- a/benchmark/misc/module-loader.js
+++ b/benchmark/misc/module-loader.js
@@ -56,7 +56,7 @@ function measure(n) {
}
function rmrf(location) {
- if (fs.existsSync(location)) {
+ try {
var things = fs.readdirSync(location);
things.forEach(function(thing) {
var cur = path.join(location, thing),
@@ -68,5 +68,7 @@ function rmrf(location) {
fs.unlinkSync(cur);
});
fs.rmdirSync(location);
+ } catch (err) {
+ // Ignore error
}
}
diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown
index 17bc55ca5e..364bf4ef25 100644
--- a/doc/api/fs.markdown
+++ b/doc/api/fs.markdown
@@ -656,13 +656,13 @@ that leaves you vulnerable to race conditions: another process may remove the
file between the calls to `fs.exists()` and `fs.open()`. Just open the file
and handle the error when it's not there.
-`fs.exists()` will be deprecated.
+`fs.exists()` is **deprecated**.
## fs.existsSync(path)
Synchronous version of `fs.exists`.
-`fs.existsSync()` will be deprecated.
+`fs.existsSync()` is **deprecated**.
## fs.access(path[, mode], callback)
diff --git a/lib/fs.js b/lib/fs.js
index aa7b51df40..66549ee5a7 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -220,7 +220,7 @@ fs.accessSync = function(path, mode) {
binding.access(pathModule._makeLong(path), mode);
};
-fs.exists = function(path, callback) {
+fs.exists = util.deprecate(function(path, callback) {
if (!nullCheck(path, cb)) return;
var req = new FSReqWrap();
req.oncomplete = cb;
@@ -228,9 +228,9 @@ fs.exists = function(path, callback) {
function cb(err, stats) {
if (callback) callback(err ? false : true);
}
-};
+}, 'fs.exists() is deprecated. Use fs.access() instead.');
-fs.existsSync = function(path) {
+fs.existsSync = util.deprecate(function(path) {
try {
nullCheck(path);
binding.stat(pathModule._makeLong(path));
@@ -238,7 +238,7 @@ fs.existsSync = function(path) {
} catch (e) {
return false;
}
-};
+}, 'fs.existsSync() is deprecated. Use fs.accessSync() instead.');
fs.readFile = function(path, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
diff --git a/test/common.js b/test/common.js
index 82fc2e2862..6908a31537 100644
--- a/test/common.js
+++ b/test/common.js
@@ -58,8 +58,11 @@ if (process.env.NODE_COMMON_PIPE) {
}
}
-if (!fs.existsSync(exports.opensslCli))
+try {
+ fs.accessSync(exports.opensslCli);
+} catch (err) {
exports.opensslCli = false;
+}
if (process.platform === 'win32') {
exports.faketimeCli = false;
@@ -319,3 +322,12 @@ exports.isValidHostname = function(str) {
return !!str.match(re) && str.length <= 255;
}
+
+exports.fileExists = function(pathname) {
+ try {
+ fs.accessSync(pathname);
+ return true;
+ } catch (err) {
+ return false;
+ }
+};
diff --git a/test/parallel/test-child-process-spawn-error.js b/test/parallel/test-child-process-spawn-error.js
index 5ecfbff263..794dafaf88 100644
--- a/test/parallel/test-child-process-spawn-error.js
+++ b/test/parallel/test-child-process-spawn-error.js
@@ -19,6 +19,7 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
+var common = require('../common');
var fs = require('fs');
var spawn = require('child_process').spawn;
var assert = require('assert');
@@ -26,7 +27,7 @@ var assert = require('assert');
var errors = 0;
var enoentPath = 'foo123';
-assert.equal(fs.existsSync(enoentPath), false);
+assert.equal(common.fileExists(enoentPath), false);
var enoentChild = spawn(enoentPath);
enoentChild.on('error', function (err) {
diff --git a/test/parallel/test-fs-mkdir.js b/test/parallel/test-fs-mkdir.js
index d99cd371b3..240bcd4beb 100644
--- a/test/parallel/test-fs-mkdir.js
+++ b/test/parallel/test-fs-mkdir.js
@@ -38,7 +38,7 @@ function unlink(pathname) {
fs.mkdir(pathname, function(err) {
assert.equal(err, null);
- assert.equal(fs.existsSync(pathname), true);
+ assert.equal(common.fileExists(pathname), true);
ncalls++;
});
@@ -56,7 +56,7 @@ function unlink(pathname) {
fs.mkdir(pathname, 511 /*=0777*/, function(err) {
assert.equal(err, null);
- assert.equal(fs.existsSync(pathname), true);
+ assert.equal(common.fileExists(pathname), true);
ncalls++;
});
@@ -72,7 +72,7 @@ function unlink(pathname) {
unlink(pathname);
fs.mkdirSync(pathname);
- var exists = fs.existsSync(pathname);
+ var exists = common.fileExists(pathname);
unlink(pathname);
assert.equal(exists, true);
diff --git a/test/parallel/test-fs-symlink-dir-junction.js b/test/parallel/test-fs-symlink-dir-junction.js
index c675b920e3..ed0bea4921 100644
--- a/test/parallel/test-fs-symlink-dir-junction.js
+++ b/test/parallel/test-fs-symlink-dir-junction.js
@@ -54,8 +54,8 @@ fs.symlink(linkData, linkPath, 'junction', function(err) {
fs.unlink(linkPath, function(err) {
if (err) throw err;
- assert(!fs.existsSync(linkPath));
- assert(fs.existsSync(linkData));
+ assert(!common.fileExists(linkPath));
+ assert(common.fileExists(linkData));
completed++;
});
});
diff --git a/test/pummel/test-fs-watch-file-slow.js b/test/pummel/test-fs-watch-file-slow.js
index 2e4bc10016..9240ad01f0 100644
--- a/test/pummel/test-fs-watch-file-slow.js
+++ b/test/pummel/test-fs-watch-file-slow.js
@@ -40,14 +40,14 @@ fs.watchFile(FILENAME, {interval:TIMEOUT - 250}, function(curr, prev) {
console.log([curr, prev]);
switch (++nevents) {
case 1:
- assert.equal(fs.existsSync(FILENAME), false);
+ assert.equal(common.fileExists(FILENAME), false);
break;
case 2:
case 3:
- assert.equal(fs.existsSync(FILENAME), true);
+ assert.equal(common.fileExists(FILENAME), true);
break;
case 4:
- assert.equal(fs.existsSync(FILENAME), false);
+ assert.equal(common.fileExists(FILENAME), false);
fs.unwatchFile(FILENAME);
break;
default:
diff --git a/test/sequential/test-regress-GH-3739.js b/test/sequential/test-regress-GH-3739.js
index 937068795a..4a7e8e1521 100644
--- a/test/sequential/test-regress-GH-3739.js
+++ b/test/sequential/test-regress-GH-3739.js
@@ -44,17 +44,17 @@ for (var i = 0; i < 50; i++) {
}
// Test existsSync
-var r = fs.existsSync(dir);
+var r = common.fileExists(dir);
if (r !== true) {
cleanup();
- throw new Error('fs.existsSync returned false');
+ throw new Error('fs.accessSync returned false');
}
// Text exists
-fs.exists(dir, function(r) {
+fs.access(dir, function(err) {
cleanup();
- if (r !== true) {
- throw new Error('fs.exists reported false');
+ if (err) {
+ throw new Error('fs.access reported false');
}
});