mirror of
https://github.com/nodejs/node.git
synced 2025-05-06 09:02:40 +00:00

These changes affect the following functions and their synchronous counterparts: * fs.readFile() * fs.writeFile() * fs.appendFile() If the first parameter is a uint32, it is treated as a file descriptor. In all other cases, the original implementation is used to ensure backwards compatibility. File descriptor ownership is never taken from the user. The documentation was adjusted to reflect these API changes. A note was added to make the user aware of file descriptor ownership and the conditions under which a file descriptor can be used by each of these functions. Tests were extended to test for file descriptor parameters under the conditions noted in the relevant documentation. PR-URL: https://github.com/nodejs/node/pull/3163 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
74 lines
1.7 KiB
JavaScript
74 lines
1.7 KiB
JavaScript
'use strict';
|
|
var common = require('../common');
|
|
var assert = require('assert');
|
|
var path = require('path');
|
|
var fs = require('fs');
|
|
var openCount = 0;
|
|
var mode;
|
|
var content;
|
|
|
|
// Need to hijack fs.open/close to make sure that things
|
|
// get closed once they're opened.
|
|
fs._openSync = fs.openSync;
|
|
fs.openSync = openSync;
|
|
fs._closeSync = fs.closeSync;
|
|
fs.closeSync = closeSync;
|
|
|
|
// Reset the umask for testing
|
|
var mask = process.umask(0o000);
|
|
|
|
// On Windows chmod is only able to manipulate read-only bit. Test if creating
|
|
// the file in read-only mode works.
|
|
if (common.isWindows) {
|
|
mode = 0o444;
|
|
} else {
|
|
mode = 0o755;
|
|
}
|
|
|
|
common.refreshTmpDir();
|
|
|
|
// Test writeFileSync
|
|
var file1 = path.join(common.tmpDir, 'testWriteFileSync.txt');
|
|
|
|
fs.writeFileSync(file1, '123', {mode: mode});
|
|
|
|
content = fs.readFileSync(file1, {encoding: 'utf8'});
|
|
assert.equal('123', content);
|
|
|
|
assert.equal(mode, fs.statSync(file1).mode & 0o777);
|
|
|
|
// Test appendFileSync
|
|
var file2 = path.join(common.tmpDir, 'testAppendFileSync.txt');
|
|
|
|
fs.appendFileSync(file2, 'abc', {mode: mode});
|
|
|
|
content = fs.readFileSync(file2, {encoding: 'utf8'});
|
|
assert.equal('abc', content);
|
|
|
|
assert.equal(mode, fs.statSync(file2).mode & mode);
|
|
|
|
// Test writeFileSync with file descriptor
|
|
var file3 = path.join(common.tmpDir, 'testWriteFileSyncFd.txt');
|
|
|
|
var fd = fs.openSync(file3, 'w+', mode);
|
|
fs.writeFileSync(fd, '123');
|
|
fs.closeSync(fd);
|
|
|
|
content = fs.readFileSync(file3, {encoding: 'utf8'});
|
|
assert.equal('123', content);
|
|
|
|
assert.equal(mode, fs.statSync(file3).mode & 0o777);
|
|
|
|
// Verify that all opened files were closed.
|
|
assert.equal(0, openCount);
|
|
|
|
function openSync() {
|
|
openCount++;
|
|
return fs._openSync.apply(fs, arguments);
|
|
}
|
|
|
|
function closeSync() {
|
|
openCount--;
|
|
return fs._closeSync.apply(fs, arguments);
|
|
}
|