Skip to content

Commit 7c20ba9

Browse files
committed
doc: improve fs.truncate functions' documentation
The default value of the `len` parameter is zero and it is included in the documenetation. This patch also has examples of how `ftruncate` can be used.
1 parent 2e54b5a commit 7c20ba9

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

doc/api/fs.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -739,19 +739,59 @@ added: v0.8.6
739739
-->
740740

741741
* `fd` {Integer}
742-
* `len` {Integer}
742+
* `len` {Integer} default = `0`
743743
* `callback` {Function}
744744

745745
Asynchronous ftruncate(2). No arguments other than a possible exception are
746746
given to the completion callback.
747747

748+
If the file referred to by the file descriptor was larger than `len` bytes, only
749+
the first `len` bytes will be retained in the file.
750+
751+
For example, the following program retains only the first four bytes of the file
752+
753+
```js
754+
console.log(fs.readFileSync('temp.txt', 'utf8');
755+
// prints Node.js
756+
757+
// get the file descriptor of the file to be truncated
758+
const fd = fs.openSync('temp.txt', 'r+');
759+
760+
// truncate the file to first four bytes
761+
fs.ftruncate(fd, 4, (err) => {
762+
assert.ifError(err);
763+
console.log(fs.readFileSync('temp.txt', 'utf8'));
764+
});
765+
// prints Node
766+
```
767+
768+
If the file previously was shorter than `len` bytes, it is extended, and the
769+
extended part is filled with null bytes ('\0'). For example,
770+
771+
```js
772+
fs.readFileSync('temp.txt', 'utf-8');
773+
// prints Node.js
774+
775+
// get the file descriptor of the file to be truncated
776+
const fd = fs.openSync('temp.txt', 'r+');
777+
778+
// truncate the file to 10 bytes, whereas the actual size is 7 bytes
779+
fs.ftruncate(fd, 10, (err) => {
780+
assert.ifError(!err);
781+
console.log(fs.readFileSync('temp.txt'));
782+
});
783+
// prints <Buffer 4e 6f 64 65 2e 6a 73 00 00 00>
784+
```
785+
786+
The last three bytes are zeroes, to compensate the over-truncation.
787+
748788
## fs.ftruncateSync(fd, len)
749789
<!-- YAML
750790
added: v0.8.6
751791
-->
752792
753793
* `fd` {Integer}
754-
* `len` {Integer}
794+
* `len` {Integer} default = `0`
755795
756796
Synchronous ftruncate(2). Returns `undefined`.
757797
@@ -1368,7 +1408,7 @@ added: v0.8.6
13681408
-->
13691409
13701410
* `path` {String | Buffer}
1371-
* `len` {Integer}
1411+
* `len` {Integer} default = `0`
13721412
* `callback` {Function}
13731413
13741414
Asynchronous truncate(2). No arguments other than a possible exception are
@@ -1381,9 +1421,10 @@ added: v0.8.6
13811421
-->
13821422
13831423
* `path` {String | Buffer}
1384-
* `len` {Integer}
1424+
* `len` {Integer} default = `0`
13851425
1386-
Synchronous truncate(2). Returns `undefined`.
1426+
Synchronous truncate(2). Returns `undefined`. A file descriptor can also be
1427+
passed as the first argument. In this case, `fs.ftruncateSync()` is called.
13871428
13881429
## fs.unlink(path, callback)
13891430
<!-- YAML

0 commit comments

Comments
 (0)