Updated: September 2, 2016
What I'm going to talk about today used to be a big problem in the past. There was a bug in version 4.X of LibreOffice, which would lead to weird save errors, causing people to lose data and whatnot. Now, since this is a thing of past, you might not consider it relevant anymore to your daily usage. But what if this happens again, and your LibreOffice cannot save the changes you made to a document?
Indeed, I was working on a file on a host that has LibreOffice 4.X, and for some reason, autosave caused the lock file to vanish, after which the program started complaining about save errors. To be more specific: error in writing sub-document content.xml. If you ever encounter this, how do you not lose data?
Well, you should copy data into a text editor, to begin with. Then, try to launch another instance of LibreOffice and/or create a new file and copy the content into it, then save and move on. Then, try to save the file with a new name (Save as). Should these methods fail, then you need to consider a bit of hackery.
LibreOffice creates lock files when you open documents, to prevent multiple writes to the same file. A trivial thing really. The lock files have a format of: .~lock.<name>.odt#. Since they begin with a dot, you might not see them in a Linux file explorer, but they are definitely there.
This file could be missing - if an autosave or another bug caused it to go away, and in that case, LibreOffice may struggle to do its due diligence and commit changes to disk. If this happens, you may want to consider the following - create a dummy lock file. If you're struggling with the format, open a random LibreOffice document in the same format as the problematic file, say ODT, then copy its lock file and rename it to match the file with the save error. In fact, let us begin by copying a lock file from another document.
I tried this, and indeed, made some progress in my problem. Now, LibreOffice was complaining about the file being locked for editing. You can open the file as a read-only copy, but then you will lose your changes. Or open a copy, which could work. No guarantee.
You need to pay attention to the name of the person (user) that has locked the file, because this will determine if you can use the lock file, and if you are going to have the right permissions to save the file. Initially, you may see the following error: The object cannot be accessed due to insufficient user rights. This is because we've just copied a lock file, and the information therein may not match what we need or want.
Let's digest the lock file content. They are simple text files, and you can open them in any text editor and make necessary modifications. For example, you may see the following:
We have the name of the person locking the file, hostname, username, timestamp, and the path to the LibreOffice profile. The data will differ ever so slightly on Windows and Linux, but you get the idea. If not, email me please.
To be able to use the lock file from another document, you need to make sure that the user name, profile and all other details match the owner of the document with the save error. Change them accordingly, then try to save the file. You should be fine now. Honest. And now you know how to craft dummy lock files should you ever hit this bug again.
If by some chance LibreOffice gets into a crazy loop and starts saving the file, and then failing, because it may not have the right permissions, you might need to kill the program and then recover the document. But then, you will most likely lose the changes, which does not really help us.
To sum it up, if you ever encounter a save error, check if there's a lock file present, it most likely won't be there. Create a new lock file - or copy one, but do make sure you stick to the right naming convention, and then add the right details into the lock file so you have the permissions to save the file. This should get you out of the deadlock and hopefully allow you to preserve all your hard work.
Anyhow, if you read online, there's a lot of mention about updating your software. Sure that works, but it does not help people with an actual problem right now. In this case, the solution is to stay calm and exterminate ... I mean figure out how LibreOffice works and then trick it into thinking everything is fine. Lock files, permissions. That's the essence of it. Hopefully, this little guide will have saved some of you some bacon. That's all.