|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const doesNotExist = path.join(common.tmpDir, '__this_should_not_exist');
|
|
|
|
const readOnlyFile = path.join(common.tmpDir, 'read_only_file');
|
|
|
|
const readWriteFile = path.join(common.tmpDir, 'read_write_file');
|
|
|
|
|
|
|
|
const removeFile = function(file) {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
} catch (err) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createFileWithPerms = function(file, mode) {
|
|
|
|
removeFile(file);
|
|
|
|
fs.writeFileSync(file, '');
|
|
|
|
fs.chmodSync(file, mode);
|
|
|
|
};
|
|
|
|
|
|
|
|
common.refreshTmpDir();
|
|
|
|
createFileWithPerms(readOnlyFile, 0o444);
|
|
|
|
createFileWithPerms(readWriteFile, 0o666);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On non-Windows supported platforms, fs.access(readOnlyFile, W_OK, ...)
|
|
|
|
* always succeeds if node runs as the super user, which is sometimes the
|
|
|
|
* case for tests running on our continuous testing platform agents.
|
|
|
|
*
|
|
|
|
* In this case, this test tries to change its process user id to a
|
|
|
|
* non-superuser user so that the test that checks for write access to a
|
|
|
|
* read-only file can be more meaningful.
|
|
|
|
*
|
|
|
|
* The change of user id is done after creating the fixtures files for the same
|
|
|
|
* reason: the test may be run as the superuser within a directory in which
|
|
|
|
* only the superuser can create files, and thus it may need superuser
|
|
|
|
* priviledges to create them.
|
|
|
|
*
|
|
|
|
* There's not really any point in resetting the process' user id to 0 after
|
|
|
|
* changing it to 'nobody', since in the case that the test runs without
|
|
|
|
* superuser priviledge, it is not possible to change its process user id to
|
|
|
|
* superuser.
|
|
|
|
*
|
|
|
|
* It can prevent the test from removing files created before the change of user
|
|
|
|
* id, but that's fine. In this case, it is the responsability of the
|
|
|
|
* continuous integration platform to take care of that.
|
|
|
|
*/
|
|
|
|
let hasWriteAccessForReadonlyFile = false;
|
|
|
|
if (!common.isWindows && process.getuid() === 0) {
|
|
|
|
hasWriteAccessForReadonlyFile = true;
|
|
|
|
try {
|
|
|
|
process.setuid('nobody');
|
|
|
|
hasWriteAccessForReadonlyFile = false;
|
|
|
|
} catch (err) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(typeof fs.F_OK, 'number');
|
|
|
|
assert.strictEqual(typeof fs.R_OK, 'number');
|
|
|
|
assert.strictEqual(typeof fs.W_OK, 'number');
|
|
|
|
assert.strictEqual(typeof fs.X_OK, 'number');
|
|
|
|
|
|
|
|
fs.access(__filename, common.mustCall((err) => {
|
|
|
|
assert.ifError(err);
|
|
|
|
}));
|
|
|
|
|
|
|
|
fs.access(__filename, fs.R_OK, common.mustCall((err) => {
|
|
|
|
assert.ifError(err);
|
|
|
|
}));
|
|
|
|
|
|
|
|
fs.access(doesNotExist, common.mustCall((err) => {
|
|
|
|
assert.notStrictEqual(err, null, 'error should exist');
|
|
|
|
assert.strictEqual(err.code, 'ENOENT');
|
|
|
|
assert.strictEqual(err.path, doesNotExist);
|
|
|
|
}));
|
|
|
|
|
|
|
|
fs.access(readOnlyFile, fs.F_OK | fs.R_OK, common.mustCall((err) => {
|
|
|
|
assert.ifError(err);
|
|
|
|
}));
|
|
|
|
|
|
|
|
fs.access(readOnlyFile, fs.W_OK, common.mustCall((err) => {
|
|
|
|
if (hasWriteAccessForReadonlyFile) {
|
|
|
|
assert.ifError(err);
|
|
|
|
} else {
|
|
|
|
assert.notStrictEqual(err, null, 'error should exist');
|
|
|
|
assert.strictEqual(err.path, readOnlyFile);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
fs.access(100, fs.F_OK, (err) => {});
|
fs: Buffer and encoding enhancements to fs API
This makes several changes:
1. Allow path/filename to be passed in as a Buffer on fs methods
2. Add `options.encoding` to fs.readdir, fs.readdirSync, fs.readlink,
fs.readlinkSync and fs.watch.
3. Documentation updates
For 1... it's now possible to do:
```js
fs.open(Buffer('/fs/foo/bar'), 'w+', (err, fd) => { });
```
For 2...
```js
fs.readdir('/fs/foo/bar', {encoding:'hex'}, (err,list) => { });
fs.readdir('/fs/foo/bar', {encoding:'buffer'}, (err, list) => { });
```
encoding can also be passed as a string
```js
fs.readdir('/fs/foo/bar', 'hex', (err,list) => { });
```
The default encoding is set to UTF8 so this addresses the
discrepency that existed previously between fs.readdir and
fs.watch handling filenames differently.
Fixes: https://github.com/nodejs/node/issues/2088
Refs: https://github.com/nodejs/node/issues/3519
PR-URL: https://github.com/nodejs/node/pull/5616
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
9 years ago
|
|
|
}, /path must be a string or Buffer/);
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
fs.access(__filename, fs.F_OK);
|
|
|
|
}, /"callback" argument must be a function/);
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
fs.access(__filename, fs.F_OK, {});
|
|
|
|
}, /"callback" argument must be a function/);
|
|
|
|
|
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
fs.accessSync(__filename);
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.doesNotThrow(function() {
|
|
|
|
const mode = fs.F_OK | fs.R_OK | fs.W_OK;
|
|
|
|
|
|
|
|
fs.accessSync(readWriteFile, mode);
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
fs.accessSync(doesNotExist);
|
|
|
|
}, (err) => {
|
|
|
|
return err.code === 'ENOENT' && err.path === doesNotExist;
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
removeFile(readOnlyFile);
|
|
|
|
removeFile(readWriteFile);
|
|
|
|
});
|