Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Saving files in a safer way #10609

Closed
Closed

Description

@jiangyy

We have found that Atom is overwriting file using fs.writeFileSync() in both TextBuffer::saveAs and writing a configuration file. Note that fs.writeFileSync() opens a file with O_TRUNC flag. Imagine that there is a system crash in between, all data may be gone, leaving a blank file in the file-system. Our automated tool found this potential crash unsafety. This is particularly important when there is no backup file -- even if no backup file means less guarantee (the backup file treatment is correct).

The correct way to save a file: (1) write contents to a backup file; (2) rename the backup to the original file. The key is that (2) is ensured to be atomic by modern file-system implementations (like ext4). It is optional to add a fdatasync() between (1) and (2) in Linux, though I am not quite sure of behaviors under Windows.

Here are some discussions FYI: http://stackoverflow.com/questions/1812115/how-to-safely-write-to-a-file.

Activity

  1. lee-dohm commented on Jan 28, 2016

    @lee-dohm
    Contributor

    This is a duplicate of #3158. See the thread there for our investigations into and the inherent problems with atomic writes.

  2. lock commented on Apr 16, 2018

    @lock

    This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

  3. locked and limited conversation to collaborators on Apr 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions