+ // if the file does not yet exist, none of the backup activity
+ // that follows is necessary
+ if (!fileName().exists())
+ return writeFile(fileName());
+
+ // we first write the file to a new name, then move it to its
+ // proper location once that has been done successfully. that
+ // way we preserve the original file if something goes wrong.
+ string const justname = fileName().onlyFileNameWithoutExt();
+ boost::scoped_ptr<TempFile>
+ tempfile(new TempFile(fileName().onlyPath(),
+ justname + "-XXXXXX.lyx"));
+ bool const symlink = fileName().isSymLink();
+ if (!symlink)
+ tempfile->setAutoRemove(false);
+
+ FileName savefile(tempfile->name());
+ LYXERR(Debug::FILES, "Saving to " << savefile.absFileName());
+ if (!writeFile(savefile))
+ return false;