Skip to content

Commit 8a9c45a

Browse files
papandreouFishrock123
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 bbd5853 commit 8a9c45a

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
@@ -1816,13 +1816,13 @@ _Note: [`fs.watch()`][] is more efficient than `fs.watchFile` and
18161816
`fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and
18171817
`fs.unwatchFile` when possible._
18181818

1819-
## fs.write(fd, buffer, offset, length[, position], callback)
1819+
## fs.write(fd, buffer[, offset[, length[, position]]], callback)
18201820
<!-- YAML
18211821
added: v0.0.2
18221822
-->
18231823

18241824
* `fd` {Integer}
1825-
* `buffer` {String | Buffer}
1825+
* `buffer` {Buffer}
18261826
* `offset` {Integer}
18271827
* `length` {Integer}
18281828
* `position` {Integer}
@@ -1847,19 +1847,19 @@ On Linux, positional writes don't work when the file is opened in append mode.
18471847
The kernel ignores the position argument and always appends the data to
18481848
the end of the file.
18491849

1850-
## fs.write(fd, data[, position[, encoding]], callback)
1850+
## fs.write(fd, string[, position[, encoding]], callback)
18511851
<!-- YAML
18521852
added: v0.11.5
18531853
-->
18541854

18551855
* `fd` {Integer}
1856-
* `data` {String | Buffer}
1856+
* `string` {String}
18571857
* `position` {Integer}
18581858
* `encoding` {String}
18591859
* `callback` {Function}
18601860

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

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

19411941
The synchronous version of [`fs.writeFile()`][]. Returns `undefined`.
19421942

1943-
## fs.writeSync(fd, buffer, offset, length[, position])
1943+
## fs.writeSync(fd, buffer[, offset[, length[, position]]])
19441944
<!-- YAML
19451945
added: v0.1.21
19461946
-->
19471947

19481948
* `fd` {Integer}
1949-
* `buffer` {String | Buffer}
1949+
* `buffer` {Buffer}
19501950
* `offset` {Integer}
19511951
* `length` {Integer}
19521952
* `position` {Integer}
19531953

1954-
## fs.writeSync(fd, data[, position[, encoding]])
1954+
## fs.writeSync(fd, string[, position[, encoding]])
19551955
<!-- YAML
19561956
added: v0.11.5
19571957
-->
19581958

19591959
* `fd` {Integer}
1960-
* `data` {String | Buffer}
1960+
* `string` {String}
19611961
* `position` {Integer}
19621962
* `encoding` {String}
19631963

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)