Skip to content

Commit a8a8148

Browse files
Zsolt Borbélyyichoi
authored andcommitted
Make fs.write() and fs.writeSync() consistent (#960)
Now fs.write() can accept `null` or `undefined` as `position` argument like fs.writeSync(). IoT.js-DCO-1.0-Signed-off-by: Zsolt Borbély [email protected]
1 parent 4fc4871 commit a8a8148

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

docs/api/IoT.js-API-File-System.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ fs.unlinkSync('test.txt');
571571
* `buffer` {Buffer} Buffer that the data will be written from.
572572
* `offset` {number} Offset of the buffer where from start reading.
573573
* `length` {number} Number of bytes to write.
574-
* `position` {number} Specifying where to start write data to the file.
574+
* `position` {number} Specifying where to start write data to the file, if `null` or `undefined`, write at the current position.
575575
* `callback` {Function}
576576
* `err` {Error|null}
577577
* `bytesWrite` {integer}

src/js/fs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
164164
if (util.isFunction(position)) {
165165
callback = position;
166166
position = -1; // write at current position.
167+
} else if (util.isNullOrUndefined(position)) {
168+
position = -1; // write at current position.
167169
}
168170

169171
callback = checkArgFunction(callback, 'callback');

test/run_pass/test_fs_write.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var fs = require('fs');
17+
var assert = require('assert');
18+
19+
var dstFilePath = process.cwd() + "/tmp/test_fs3.txt";
20+
var buff1 = new Buffer("IoT");
21+
var buff2 = new Buffer(".js");
22+
23+
fs.open(dstFilePath, 'w+', function(err, fd) {
24+
if (err) {
25+
throw err;
26+
}
27+
28+
// Test the position argument of fs.write().
29+
fs.write(fd, buff1, 0, buff1.length, undefined, function(err, bytes, buffer) {
30+
if (err) {
31+
throw err;
32+
}
33+
34+
fs.write(fd, buff2, 0, buff2.length, null, function(err, bytes, buffer) {
35+
if (err) {
36+
throw err;
37+
}
38+
39+
// Check the result.
40+
var result = new Buffer(6);
41+
fs.read(fd, result, 0, result.length, 0, function(err, bytes, buffer) {
42+
if (err) {
43+
throw err;
44+
}
45+
var init_buffers = Buffer.concat([buff1, buff2]);
46+
assert.assert(result.equals(init_buffers));
47+
48+
fs.close(fd, function(err) {
49+
if (err) {
50+
throw err;
51+
}
52+
});
53+
});
54+
});
55+
});
56+
});

test/testsets.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
{ "name": "test_fs_rename.js" },
3333
{ "name": "test_fs_rename_sync.js" },
3434
{ "name": "test_fs_stat.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" },
35+
{ "name": "test_fs_write.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" },
3536
{ "name": "test_fs_writefile.js" },
3637
{ "name": "test_fs_writefile_sync.js" },
3738
{ "name": "test_fs_writefile_unlink.js" },
@@ -56,7 +57,7 @@
5657
{ "name": "test_net_8.js" },
5758
{ "name": "test_net_9.js" },
5859
{ "name": "test_net_10.js" },
59-
{ "name": "test_net_connect.js", "timeout": 10 },
60+
{ "name": "test_net_connect.js", "timeout": 10 },
6061
{ "name": "test_net_headers.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" },
6162
{ "name": "test_net_http_get.js", "timeout": 20, "skip": ["nuttx"], "reason": "not implemented for nuttx" },
6263
{ "name": "test_net_http_response_twice.js", "timeout": 10, "skip": ["nuttx"], "reason": "not implemented for nuttx" },

0 commit comments

Comments
 (0)