2015-10-03 02:06:42 +02:00
|
|
|
'use strict';
|
2018-10-17 14:40:08 +05:30
|
|
|
const common = require('../common');
|
2017-12-07 12:44:42 -05:00
|
|
|
|
|
|
|
// Test fs.readFile using a file descriptor.
|
|
|
|
|
2017-10-09 16:38:31 -07:00
|
|
|
const fixtures = require('../common/fixtures');
|
2016-01-13 21:42:45 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2017-10-09 16:38:31 -07:00
|
|
|
const fn = fixtures.path('empty.txt');
|
2018-10-17 14:40:08 +05:30
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2015-10-03 02:06:42 +02:00
|
|
|
|
|
|
|
tempFd(function(fd, close) {
|
|
|
|
fs.readFile(fd, function(err, data) {
|
|
|
|
assert.ok(data);
|
|
|
|
close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tempFd(function(fd, close) {
|
|
|
|
fs.readFile(fd, 'utf8', function(err, data) {
|
2018-11-06 17:29:36 +03:00
|
|
|
assert.strictEqual(data, '');
|
2015-10-03 02:06:42 +02:00
|
|
|
close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tempFdSync(function(fd) {
|
|
|
|
assert.ok(fs.readFileSync(fd));
|
|
|
|
});
|
|
|
|
|
|
|
|
tempFdSync(function(fd) {
|
2018-11-06 17:29:36 +03:00
|
|
|
assert.strictEqual(fs.readFileSync(fd, 'utf8'), '');
|
2015-10-03 02:06:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function tempFd(callback) {
|
|
|
|
fs.open(fn, 'r', function(err, fd) {
|
2016-12-30 10:54:01 -05:00
|
|
|
assert.ifError(err);
|
2015-10-03 02:06:42 +02:00
|
|
|
callback(fd, function() {
|
|
|
|
fs.close(fd, function(err) {
|
2016-12-30 10:54:01 -05:00
|
|
|
assert.ifError(err);
|
2015-10-03 02:06:42 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function tempFdSync(callback) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const fd = fs.openSync(fn, 'r');
|
2015-10-03 02:06:42 +02:00
|
|
|
callback(fd);
|
|
|
|
fs.closeSync(fd);
|
|
|
|
}
|
2018-10-17 14:40:08 +05:30
|
|
|
|
|
|
|
{
|
2020-10-03 13:01:57 -07:00
|
|
|
// This test makes sure that `readFile()` always reads from the current
|
|
|
|
// position of the file, instead of reading from the beginning of the file,
|
|
|
|
// when used with file descriptors.
|
2018-10-17 14:40:08 +05:30
|
|
|
|
2023-08-15 22:45:14 +09:00
|
|
|
const filename = tmpdir.resolve('test.txt');
|
2018-10-17 14:40:08 +05:30
|
|
|
fs.writeFileSync(filename, 'Hello World');
|
|
|
|
|
|
|
|
{
|
2020-10-03 13:01:57 -07:00
|
|
|
// Tests the fs.readFileSync().
|
2018-10-17 14:40:08 +05:30
|
|
|
const fd = fs.openSync(filename, 'r');
|
|
|
|
|
2020-10-03 13:01:57 -07:00
|
|
|
// Read only five bytes, so that the position moves to five.
|
2018-10-17 14:40:08 +05:30
|
|
|
const buf = Buffer.alloc(5);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(fs.readSync(fd, buf, 0, 5), 5);
|
|
|
|
assert.strictEqual(buf.toString(), 'Hello');
|
2018-10-17 14:40:08 +05:30
|
|
|
|
2020-10-03 13:01:57 -07:00
|
|
|
// readFileSync() should read from position five, instead of zero.
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(fd).toString(), ' World');
|
2019-07-30 07:50:21 +01:00
|
|
|
|
|
|
|
fs.closeSync(fd);
|
2018-10-17 14:40:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-10-03 13:01:57 -07:00
|
|
|
// Tests the fs.readFile().
|
2020-09-06 22:27:07 +02:00
|
|
|
fs.open(filename, 'r', common.mustSucceed((fd) => {
|
2018-10-17 14:40:08 +05:30
|
|
|
const buf = Buffer.alloc(5);
|
|
|
|
|
2020-10-03 13:01:57 -07:00
|
|
|
// Read only five bytes, so that the position moves to five.
|
2020-09-06 22:27:07 +02:00
|
|
|
fs.read(fd, buf, 0, 5, null, common.mustSucceed((bytes) => {
|
2018-10-17 14:40:08 +05:30
|
|
|
assert.strictEqual(bytes, 5);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(buf.toString(), 'Hello');
|
2018-10-17 14:40:08 +05:30
|
|
|
|
2020-09-06 22:27:07 +02:00
|
|
|
fs.readFile(fd, common.mustSucceed((data) => {
|
2020-10-03 13:01:57 -07:00
|
|
|
// readFile() should read from position five, instead of zero.
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(data.toString(), ' World');
|
2019-07-30 07:50:21 +01:00
|
|
|
|
|
|
|
fs.closeSync(fd);
|
2018-10-17 14:40:08 +05:30
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|