Two semi-related issues:
writeFileSync bumps position incorrectly, causing it to drift in iteration three and onwards. It seems that written is though to be the amount just written but it is in fact the accumulated amount. This bug is not available in the async writeFile.
- Append mode files will get corrupted in the middle if
writeFile or writeFileSync iterates multiple times, unless running on Linux. position starts out as null so first write is OK, but then position will refer to a location inside an existing file, corrupting that data. Linux ignores position for append mode files so it doesn't happen there.
See https://github.com/iojs/io.js/blob/78581c8d902f2ed32712b342091d89016030e4ba/lib/fs.js#L1151-L1155 (also writeFile and writeAll).