@@ -739,19 +739,60 @@ added: v0.8.6
739
739
-->
740
740
741
741
* ` fd ` {Integer}
742
- * ` len ` {Integer}
742
+ * ` len ` {Integer} default = ` 0 `
743
743
* ` callback ` {Function}
744
744
745
745
Asynchronous ftruncate(2). No arguments other than a possible exception are
746
746
given to the completion callback.
747
747
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
+ console .log (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
+ // ('Node.js\0\0\0' in UTF8)
785
+ ```
786
+
787
+ The last three bytes are null bytes ('\0'), to compensate the over-truncation.
788
+
748
789
## fs.ftruncateSync(fd, len)
749
790
<!-- YAML
750
791
added: v0.8.6
751
792
-->
752
793
753
794
* ` fd ` {Integer}
754
- * ` len ` {Integer}
795
+ * ` len ` {Integer} default = ` 0 `
755
796
756
797
Synchronous ftruncate(2). Returns ` undefined ` .
757
798
@@ -1368,7 +1409,7 @@ added: v0.8.6
1368
1409
-->
1369
1410
1370
1411
* ` path ` {String | Buffer}
1371
- * ` len ` {Integer}
1412
+ * ` len ` {Integer} default = ` 0 `
1372
1413
* ` callback ` {Function}
1373
1414
1374
1415
Asynchronous truncate(2). No arguments other than a possible exception are
@@ -1381,9 +1422,10 @@ added: v0.8.6
1381
1422
-->
1382
1423
1383
1424
* ` path ` {String | Buffer}
1384
- * ` len ` {Integer}
1425
+ * ` len ` {Integer} default = ` 0 `
1385
1426
1386
- Synchronous truncate(2). Returns ` undefined ` .
1427
+ Synchronous truncate(2). Returns ` undefined ` . A file descriptor can also be
1428
+ passed as the first argument. In this case, ` fs.ftruncateSync() ` is called.
1387
1429
1388
1430
## fs.unlink(path, callback)
1389
1431
<!-- YAML
0 commit comments