Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Add option to save a backup file before saving buffer - #87

Merged
nathansobo merged 14 commits into
masterfrom
ns-backup-files
Jul 29, 2015
Merged

nathansobo merged 14 commits into
masterfrom
ns-backup-files

Conversation

@nathansobo

Copy link
Copy Markdown
Contributor

Refs atom/atom#3158

When writing directly to an existing file, it's possible to truncate the original file if an I/O error occurs during the write. This PR adds the option to save a backup file to the path of the original file with a ~ appended. If any exceptions occur, we restore from the backup and delete it.

I'm honestly not super well-versed in all the nuances of safely writing files... I'm interested in input on this. I realize that buffering the entire file through memory before writing the backup isn't that awesome, but I'm not sure there's a synchronous cross-platform alternative. I'm really wishing saveAs was asynchronous right now...

Also: Does anyone know if Node's writeFileSync method does an fsync automatically?

Nathan Sobo added 5 commits July 27, 2015 16:12
If backup is true and the file being written exists, we save a backup
copy before writing. If anything goes wrong, we restore the contents
of the file from the backup.
Continue appending ~ until we find a path that doesn’t correspond to
an existing file.
If there’s an error copying the backup file contents back to the
original file, we’ll want to leave the backup file in place.
@alexandernst

Copy link
Copy Markdown

Here you go another thing you might want to work on. INstead of saving the entire file, try writing only the bits of the file that actually changed. This will tackle atom/atom#8091 automatically.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

try writing only the bits of the file that actually changed

This would only be useful in very specific cases, right? Because AFAIK even if a single character is inserted at the beginning of the file, the entire file needs to be overwritten.

@alexandernst

Copy link
Copy Markdown

@maxbrunsfeld Indeed, that would help only in some cases. Also, I'm not really sure how Atom handles writing currently. Are you using some custom methods or do you go low level?

Comment thread spec/text-buffer-spec.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 extra line break

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Does anyone know if Node's writeFileSync method does an fsync automatically?

The fs module does expose an fsyncSync method, but you need to pass the file descriptor, which means we'd need to use fs.writeSync instead of fs.writeFileSync.

Looking at the native code for fs, the only mentions of the fysnc(2) function are in the implementations of fs.fynsc and fs.fsyncSync, so I don't think that writeFileSync and friends do this automatically.

@nathansobo

Copy link
Copy Markdown
Contributor Author

so I don't think that writeFileSync and friends do this automatically.

Seems like our documentation on fs-plus is probably wrong if my interpretation of the word "flush" is correct. Seems like I should probably use the raw file handle API.

Comment thread src/text-buffer.coffee Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth using fdatasync to make sure the backup file is on disk before truncating the main file. Otherwise there's probably a couple-second window where, if you lose power, the user will find that the backup file isn't on disk yet and the main file's metadata is truncated. Total loss.

Doing this after calling writeFileSync should be good enough:

fd = fs.openSync(path, 'w')
fs.fdatasyncSync(fd) 
fs.closeSync(fd)

If you just created the backup file, you'll need to ensure its directory entry is on disk too. Otherwise, there's no point to ensuring the file contents are written!

path = require 'path'
parent = path.dirname(@file.getPath())
fd = fs.openSync(parent, 'r')  // 'r' is important, 'w' doesn't work for dirs
fs.fdatasyncSync(fd) 
fs.closeSync(fd)

Just stumbled across this, matches my findings: http://www.daveeddy.com/2013/03/26/synchronous-file-io-in-nodejs/

@bronson

bronson commented Jul 28, 2015

Copy link
Copy Markdown

I left some pseudocode to fsync the files in an inline comment. Tell me if you want me to expand that into a clean PR. Should be pretty straightforward.

Syncing the data is required on posix and windows, syncing the directory is only needed on posix. (I assume fsyncing a dir is a no-op on windows but I've never tried.)

Comment thread src/text-buffer.coffee Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why fs.readFileSync(backupFilePath)? The text is still in @getText() isn't it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'm trying to restore the former contents of the file, as if nothing had ever been written to it. The value of @getText() is the new contents of the file that failed to write. Am I missing something? Do you think I shouldn't try to restore the contents of the file from the backup? It seems nice if we can avoid forcing people to manually fall back to the backup file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that makes sense. I just assumed you were doing something different and read it my way. :)

I figured you were writing the new contents to the backup file... That way, if the main write fails, you still have the new contents in the backup file. And, if the backup write fails, you still have the old contents in the main file. When users hit save, they don't care about the old contents anymore, right?

Either way sounds fine to me.

@nathansobo

Copy link
Copy Markdown
Contributor Author

@bronson Thanks for the helpful comments... I really appreciate it!

Nathan Sobo added 4 commits July 28, 2015 14:48
We shouldn’t proceed to write the main file until we know the backup
is on disk. We shouldn’t delete the backup until we know the main file
has been written.
@nathansobo

Copy link
Copy Markdown
Contributor Author

@bronson Okay, pretty sure this addresses everything you pointed out. Thanks again. It would have taken us much more time (and probably pain) to get this right without your expertise. 🙇

@bronson

bronson commented Jul 28, 2015

Copy link
Copy Markdown

Looks great! Just two more thoughts:

It looks like you're going to have two filehandles open for writing on the same file at the same time.

        fd = fs.openSync(filePath, 'w') # We open fd so we can call fdatasync
        @file.writeSync(@getText()) # File::writeSync does privelege escalation, so we don't use fd here

This will work fine on all OSs/filesystems I've used in the last decade but I remember computers being cranky about this. Maybe writeSync before openSync?

And, you sure you want to keep adding ~ to find a free backup file? Personally I like the idea of blowing away the existing backup file but, if that worries you, then I'd suggest putting a limit on the number of ~s you add.

@nathansobo

Copy link
Copy Markdown
Contributor Author

Maybe writeSync before openSync?

When I tried to openSync with a w flag after the writeSync, it truncated the file. It worked fine with r, but since you suggested using w I decided to move it earlier. Maybe opening it r after the writeSync call is the lesser of two evils?

And, you sure you want to keep adding ~ to find a free backup file?

I'm just worried about blowing away a file someone cares about that happens to end in ~ for some other random reason. Maybe I'm being paranoid. The additional ~s will be finite, though I guess we could end up with a growing list of dead backup files if someone repeatedly attempts to write such that backups succeed but writing to the main file doesn't. My preference is to keep it this way because I'm worried about clobbering something someone wants.

@bronson

bronson commented Jul 28, 2015

Copy link
Copy Markdown

Dammit, I forgot that 'w' would truncate the file.

Maybe opening it r after the writeSync call is the lesser of two evils?

Probably. 'a' might be good too.

Vim gets around the stale swapfile issue by stopping the world and making you deal with it before even opening the file. Emacs, of course, lets you do whatever you want but, by default, I think it overwrites backup files once when it starts, then leaves them alone.

Your current behavior sounds fine but I'd really suggest a limit, even if it's in the tens of thousands... otherwise a bad interaction with a plugin or something, especially if it's on a *fat or older filesystem, could leave your files in a directory that can't be accessed anymore.

@nathansobo

Copy link
Copy Markdown
Contributor Author

Your current behavior sounds fine but I'd really suggest a limit

Maybe I could throw an exception after 10 or something and say that we couldn't save because no unused path exists for a backup file? Seems like the only option. We could potentially catch this exception in the notifications package and provide a UI to remediate the situation.

@bronson

bronson commented Jul 28, 2015

Copy link
Copy Markdown

That would be great. With the limit in place, as far as I'm concerned, this can't be merged fast enough.

@nathansobo

Copy link
Copy Markdown
Contributor Author

Getting some weird failures on Travis... illegal access and the specs just terminate. I'm not pulling up much information on the Google about them.

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Those failures existed before intermittently.

nathansobo pushed a commit that referenced this pull request Jul 29, 2015
Add option to save a backup file before saving buffer
@nathansobo
nathansobo merged commit a1ba403 into master Jul 29, 2015
@nathansobo
nathansobo deleted the ns-backup-files branch July 29, 2015 16:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants