Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.writeFile[Sync] may corrupt files upon partial write #1058

Closed
olov opened this issue Mar 4, 2015 · 2 comments
Closed

fs.writeFile[Sync] may corrupt files upon partial write #1058

olov opened this issue Mar 4, 2015 · 2 comments

Comments

@olov
Copy link
Contributor

olov commented Mar 4, 2015

Two semi-related issues:

  1. 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.
  2. 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).

@piscisaureus
Copy link
Contributor

Good catch, this looks like a serious issue.
You want to submit a PR that fixes it?

@piscisaureus
Copy link
Contributor

Continues in #1063

rvagg added a commit that referenced this issue Mar 31, 2015
Notable changes:

 * fs: corruption can be caused by fs.writeFileSync() and append-mode
   fs.writeFile() and fs.writeFileSync() under certain circumstances,
   reported in #1058, fixed in #1063 (Olov Lassus).
 * iojs: an "internal modules" API has been introduced to allow core
   code to share JavaScript modules internally only without having to
   expose them as a public API, this feature is for core-only #848
   (Vladimir Kurchatkin).
 * timers: two minor problems with timers have been fixed:
   - Timer#close() is now properly idempotent #1288 (Petka Antonov).
   - setTimeout() will only run the callback once now after an
     unref() during the callback #1231 (Roman Reiss).
 * Windows: a "delay-load hook" has been added for compiled add-ons
   on Windows that should alleviate some of the problems that Windows
   users may be experiencing with add-ons in io.js #1251
   (Bert Belder).
 * V8: minor bug-fix upgrade for V8 to 4.1.0.27.
 * npm: upgrade npm to 2.7.4. See npm CHANGELOG.md for details.
ilgrosso pushed a commit to OpenRock/OpenAM that referenced this issue Nov 3, 2015
The current version of Node.js and/or npm seems to be sporadically
corrupting files (possibly related to
nodejs/node#1058).

v4.2.1 is the latest LTS version of Node.js.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants