Repository navigation
Add option to save a backup file before saving buffer - #87
Conversation
This will make it easier to mock fs.
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.
|
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. |
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. |
|
@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? |
The Looking at the native code for |
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. |
There was a problem hiding this comment.
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/
|
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.) |
There was a problem hiding this comment.
Why fs.readFileSync(backupFilePath)? The text is still in @getText() isn't it?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
@bronson Thanks for the helpful comments... I really appreciate it! |
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.
|
@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. 🙇 |
4e3e1bb to
fc02f93
Compare
|
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 hereThis 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. |
When I tried to
I'm just worried about blowing away a file someone cares about that happens to end in |
|
Dammit, I forgot that 'w' would truncate the file.
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. |
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. |
|
That would be great. With the limit in place, as far as I'm concerned, this can't be merged fast enough. |
|
Getting some weird failures on Travis... |
|
Those failures existed before intermittently. |
Add option to save a backup file before saving buffer
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
saveAswas asynchronous right now...Also: Does anyone know if Node's
writeFileSyncmethod does anfsyncautomatically?