joyent/node — pull request #6320 by bnoordhuis (2013-10-08 09:39:08+00:00)
Title: fs: fix fs.truncate() file content zeroing bug

fs.truncate() and its synchronous sibling are implemented in terms of
open() + ftruncate(). Unfortunately, it opened the target file with
mode 'w' a.k.a. 'write-only and create or truncate at open'.

The subsequent call to ftruncate() then moved the end-of-file pointer
from zero to the requested offset with the net result of a file that's
neatly truncated at the right offset and filled with zero bytes only.

This bug was introduced in commit 168a5557 but in fairness, before that
commit fs.truncate() worked like fs.ftruncate() so it seems we've never
had a working fs.truncate() until now.

Fixes #6233.

Suggested reviewer: @indutny?