Skip to content

Commit 0b5191f

Browse files
papandreousam-github
authored andcommitted
fs: Fix default params for fs.write(Sync)
Add support for fs.write(fd, buffer, cb) and fs.write(fd, buffer, offset, cb) as documented at https://nodejs.org/api/fs.html#fs_fs_write_fd_data_position_encoding_callback and equivalently for fs.writeSync Update docs and code comments to reflect the implementation. PR-URL: #7856 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Yorkie Liu <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent 5d9d415 commit 0b5191f

File tree

4 files changed

+171
-52
lines changed

4 files changed

+171
-52
lines changed

doc/api/fs.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1812,13 +1812,13 @@ _Note: [`fs.watch()`][] is more efficient than `fs.watchFile` and
18121812
`fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and
18131813
`fs.unwatchFile` when possible._
18141814

1815-
## fs.write(fd, buffer, offset, length[, position], callback)
1815+
## fs.write(fd, buffer[, offset[, length[, position]]], callback)
18161816
<!-- YAML
18171817
added: v0.0.2
18181818
-->
18191819

18201820
* `fd` {Integer}
1821-
* `buffer` {String | Buffer}
1821+
* `buffer` {Buffer}
18221822
* `offset` {Integer}
18231823
* `length` {Integer}
18241824
* `position` {Integer}
@@ -1843,19 +1843,19 @@ On Linux, positional writes don't work when the file is opened in append mode.
18431843
The kernel ignores the position argument and always appends the data to
18441844
the end of the file.
18451845

1846-
## fs.write(fd, data[, position[, encoding]], callback)
1846+
## fs.write(fd, string[, position[, encoding]], callback)
18471847
<!-- YAML
18481848
added: v0.11.5
18491849
-->
18501850

18511851
* `fd` {Integer}
1852-
* `data` {String | Buffer}
1852+
* `string` {String}
18531853
* `position` {Integer}
18541854
* `encoding` {String}
18551855
* `callback` {Function}
18561856

1857-
Write `data` to the file specified by `fd`. If `data` is not a Buffer instance
1858-
then the value will be coerced to a string.
1857+
Write `string` to the file specified by `fd`. If `string` is not a string, then
1858+
the value will be coerced to one.
18591859

18601860
`position` refers to the offset from the beginning of the file where this data
18611861
should be written. If `typeof position !== 'number'` the data will be written at
@@ -1936,24 +1936,24 @@ added: v0.1.29
19361936

19371937
The synchronous version of [`fs.writeFile()`][]. Returns `undefined`.
19381938

1939-
## fs.writeSync(fd, buffer, offset, length[, position])
1939+
## fs.writeSync(fd, buffer[, offset[, length[, position]]])
19401940
<!-- YAML
19411941
added: v0.1.21
19421942
-->
19431943

19441944
* `fd` {Integer}
1945-
* `buffer` {String | Buffer}
1945+
* `buffer` {Buffer}
19461946
* `offset` {Integer}
19471947
* `length` {Integer}
19481948
* `position` {Integer}
19491949

1950-
## fs.writeSync(fd, data[, position[, encoding]])
1950+
## fs.writeSync(fd, string[, position[, encoding]])
19511951
<!-- YAML
19521952
added: v0.11.5
19531953
-->
19541954

19551955
* `fd` {Integer}
1956-
* `data` {String | Buffer}
1956+
* `string` {String}
19571957
* `position` {Integer}
19581958
* `encoding` {String}
19591959

lib/fs.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ fs.readSync = function(fd, buffer, offset, length, position) {
662662
};
663663

664664
// usage:
665-
// fs.write(fd, buffer, offset, length[, position], callback);
665+
// fs.write(fd, buffer[, offset[, length[, position]]], callback);
666666
// OR
667667
// fs.write(fd, string[, position[, encoding]], callback);
668668
fs.write = function(fd, buffer, offset, length, position, callback) {
@@ -675,12 +675,16 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
675675
req.oncomplete = wrapper;
676676

677677
if (buffer instanceof Buffer) {
678-
// if no position is passed then assume null
679-
if (typeof position === 'function') {
680-
callback = position;
678+
callback = maybeCallback(callback || position || length || offset);
679+
if (typeof offset !== 'number') {
680+
offset = 0;
681+
}
682+
if (typeof length !== 'number') {
683+
length = buffer.length - offset;
684+
}
685+
if (typeof position !== 'number') {
681686
position = null;
682687
}
683-
callback = maybeCallback(callback);
684688
return binding.writeBuffer(fd, buffer, offset, length, position, req);
685689
}
686690

@@ -700,13 +704,17 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
700704
};
701705

702706
// usage:
703-
// fs.writeSync(fd, buffer, offset, length[, position]);
707+
// fs.writeSync(fd, buffer[, offset[, length[, position]]]);
704708
// OR
705709
// fs.writeSync(fd, string[, position[, encoding]]);
706710
fs.writeSync = function(fd, buffer, offset, length, position) {
707711
if (buffer instanceof Buffer) {
708712
if (position === undefined)
709713
position = null;
714+
if (typeof offset !== 'number')
715+
offset = 0;
716+
if (typeof length !== 'number')
717+
length = buffer.length - offset;
710718
return binding.writeBuffer(fd, buffer, offset, length, position);
711719
}
712720
if (typeof buffer !== 'string')

test/parallel/test-fs-write-buffer.js

+99-21
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,107 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
5-
const Buffer = require('buffer').Buffer;
65
const fs = require('fs');
7-
const filename = path.join(common.tmpDir, 'write.txt');
86
const expected = Buffer.from('hello');
97

108
common.refreshTmpDir();
119

12-
fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
13-
if (err) throw err;
14-
15-
fs.write(fd,
16-
expected,
17-
0,
18-
expected.length,
19-
null,
20-
common.mustCall(function(err, written) {
21-
if (err) throw err;
22-
23-
assert.equal(expected.length, written);
24-
fs.closeSync(fd);
25-
26-
var found = fs.readFileSync(filename, 'utf8');
27-
assert.deepStrictEqual(expected.toString(), found);
28-
fs.unlinkSync(filename);
29-
}));
30-
}));
10+
// fs.write with all parameters provided:
11+
{
12+
const filename = path.join(common.tmpDir, 'write1.txt');
13+
fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
14+
assert.ifError(err);
15+
16+
const cb = common.mustCall(function(err, written) {
17+
assert.ifError(err);
18+
19+
assert.strictEqual(expected.length, written);
20+
fs.closeSync(fd);
21+
22+
var found = fs.readFileSync(filename, 'utf8');
23+
assert.deepStrictEqual(expected.toString(), found);
24+
});
25+
26+
fs.write(fd, expected, 0, expected.length, null, cb);
27+
}));
28+
}
29+
30+
// fs.write with a buffer, without the length parameter:
31+
{
32+
const filename = path.join(common.tmpDir, 'write2.txt');
33+
fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
34+
assert.ifError(err);
35+
36+
const cb = common.mustCall(function(err, written) {
37+
assert.ifError(err);
38+
39+
assert.strictEqual(2, written);
40+
fs.closeSync(fd);
41+
42+
const found = fs.readFileSync(filename, 'utf8');
43+
assert.deepStrictEqual('lo', found);
44+
});
45+
46+
fs.write(fd, Buffer.from('hello'), 3, cb);
47+
}));
48+
}
49+
50+
// fs.write with a buffer, without the offset and length parameters:
51+
{
52+
const filename = path.join(common.tmpDir, 'write3.txt');
53+
fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
54+
assert.ifError(err);
55+
56+
const cb = common.mustCall(function(err, written) {
57+
assert.ifError(err);
58+
59+
assert.strictEqual(expected.length, written);
60+
fs.closeSync(fd);
61+
62+
const found = fs.readFileSync(filename, 'utf8');
63+
assert.deepStrictEqual(expected.toString(), found);
64+
});
65+
66+
fs.write(fd, expected, cb);
67+
}));
68+
}
69+
70+
// fs.write with the offset passed as undefined followed by the callback:
71+
{
72+
const filename = path.join(common.tmpDir, 'write4.txt');
73+
fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
74+
assert.ifError(err);
75+
76+
const cb = common.mustCall(function(err, written) {
77+
assert.ifError(err);
78+
79+
assert.strictEqual(expected.length, written);
80+
fs.closeSync(fd);
81+
82+
const found = fs.readFileSync(filename, 'utf8');
83+
assert.deepStrictEqual(expected.toString(), found);
84+
});
85+
86+
fs.write(fd, expected, undefined, cb);
87+
}));
88+
}
89+
90+
// fs.write with offset and length passed as undefined followed by the callback:
91+
{
92+
const filename = path.join(common.tmpDir, 'write5.txt');
93+
fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
94+
assert.ifError(err);
95+
96+
const cb = common.mustCall(function(err, written) {
97+
assert.ifError(err);
98+
99+
assert.strictEqual(expected.length, written);
100+
fs.closeSync(fd);
101+
102+
const found = fs.readFileSync(filename, 'utf8');
103+
assert.deepStrictEqual(expected.toString(), found);
104+
});
105+
106+
fs.write(fd, expected, undefined, undefined, cb);
107+
}));
108+
}

test/parallel/test-fs-write-sync.js

+48-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var path = require('path');
5-
var fs = require('fs');
6-
var fn = path.join(common.tmpDir, 'write.txt');
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
const fs = require('fs');
6+
const filename = path.join(common.tmpDir, 'write.txt');
77

88
common.refreshTmpDir();
99

10-
var foo = 'foo';
11-
var fd = fs.openSync(fn, 'w');
10+
// fs.writeSync with all parameters provided:
11+
{
12+
const fd = fs.openSync(filename, 'w');
1213

13-
var written = fs.writeSync(fd, '');
14-
assert.strictEqual(0, written);
14+
let written = fs.writeSync(fd, '');
15+
assert.strictEqual(0, written);
1516

16-
fs.writeSync(fd, foo);
17+
fs.writeSync(fd, 'foo');
1718

18-
var bar = 'bár';
19-
written = fs.writeSync(fd, Buffer.from(bar), 0, Buffer.byteLength(bar));
20-
assert.ok(written > 3);
21-
fs.closeSync(fd);
19+
written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár'));
20+
assert.ok(written > 3);
21+
fs.closeSync(fd);
2222

23-
assert.equal(fs.readFileSync(fn), 'foobár');
23+
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
24+
}
25+
26+
// fs.writeSync with a buffer, without the length parameter:
27+
{
28+
const fd = fs.openSync(filename, 'w');
29+
30+
let written = fs.writeSync(fd, '');
31+
assert.strictEqual(0, written);
32+
33+
fs.writeSync(fd, 'foo');
34+
35+
written = fs.writeSync(fd, Buffer.from('bár'), 0);
36+
assert.ok(written > 3);
37+
fs.closeSync(fd);
38+
39+
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
40+
}
41+
42+
// fs.writeSync with a buffer, without the offset and length parameters:
43+
{
44+
const fd = fs.openSync(filename, 'w');
45+
46+
let written = fs.writeSync(fd, '');
47+
assert.strictEqual(0, written);
48+
49+
fs.writeSync(fd, 'foo');
50+
51+
written = fs.writeSync(fd, Buffer.from('bár'));
52+
assert.ok(written > 3);
53+
fs.closeSync(fd);
54+
55+
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
56+
}

0 commit comments

Comments
 (0)