+FileName Buffer::getBackupName() const {
+ FileName const & fn = fileName();
+ string const fname = fn.onlyFileNameWithoutExt();
+ string const fext = fn.extension();
+ string const fpath = lyxrc.backupdir_path.empty() ?
+ fn.onlyPath().absFileName() :
+ lyxrc.backupdir_path;
+ string const fform = convert<string>(d->file_format);
+ string const backname = fname + "-lyxformat-" + fform;
+ FileName backup(addName(fpath, addExtension(backname, fext)));
+
+ // limit recursion, just in case
+ int v = 1;
+ unsigned long orig_checksum = 0;
+ while (backup.exists() && v < 100) {
+ if (orig_checksum == 0)
+ orig_checksum = fn.checksum();
+ unsigned long new_checksum = backup.checksum();
+ if (orig_checksum == new_checksum) {
+ LYXERR(Debug::FILES, "Not backing up " << fn <<
+ "since " << backup << "has the same checksum.");
+ // a bit of a hack, but we have to check this anyway
+ // below, and setting this is simpler than introducing
+ // a special boolean for this purpose.
+ v = 1000;
+ break;
+ }
+ string const newbackname = backname + "-" + convert<string>(v);
+ backup.set(addName(fpath, addExtension(newbackname, fext)));
+ v++;
+ }
+ return v < 100 ? backup : FileName();
+}
+
+