Implement atomic save

Ping @fperez, this should avoid issues with corrupted/lost notebooks
when the disk is full, though I haven't worked out how to test it just
yet.

Closes gh-6254
Rework atomic_writing with tests & docstring
Add fsync() call in atomic_writing
Merge pull request #6269 from takluyver/atomic-save

Implement atomic save