Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate DocumentFile before writing to it #402

Merged
merged 1 commit into from
Nov 8, 2024
Merged

Conversation

amberin
Copy link
Member

@amberin amberin commented Nov 8, 2024

Before 43d88fd, this was not needed, because the file was always deleted and re-created first.

This should resolve issues #385 and #395.

Before 43d88fd, this was not needed, because the file was always deleted
and re-created first.

This should resolve issues #385 and #395.
@amberin amberin marked this pull request as ready for review November 8, 2024 01:03
@amberin
Copy link
Member Author

amberin commented Nov 8, 2024

Big thanks to @toridnyc and @Sergey-Makarov for reporting this serious issue. 🙏

@amberin amberin merged commit 3aa99c5 into master Nov 8, 2024
8 of 10 checks passed
@amberin amberin deleted the fix-issue-385 branch November 8, 2024 13:53
@doak
Copy link

doak commented Nov 14, 2024

I've found it "by accident" because I followed the changelog out of curiosity. Indeed my files were already broken. Perhaps it would make sense to ask people to check their files explicitly.

@decadent
Copy link

@amberin Do I understand it right that the app opens the synced file directly? This still can lead to loss of on-disk data, e.g. if the system runs out of disk space right before the file is written, or if the phone is unexpectedly turned off. In desktop programming, this is usually circumvented by writing to a temporary file (on the same disk volume) and then renaming the file in place of the original — which is an atomic operation. If this can be done on Android, perhaps it's better to do it this way.

(Android might already provide a readymade solution for this, though — but idk if it does.)

@decadent
Copy link

Also, I second doak's comment that the changelog should be more explicit about the fact that data might've been lost — especially since the changelog is shown in the app itself. At least people could then check their backup copies or any copies preserved by their sync methods. Seeing as people using this app are likely somewhat versed in sync workflows, can fiddle with files, and are concerned about their notes.

@amberin
Copy link
Member Author

amberin commented Nov 14, 2024

@decadent No, Orgzly stores notebooks in its on-disk database, and they are exported as text files during sync.

@doak Orgzly's view of the file stays correct until you make remote changes in a corrupted file and sync them. That's of course pretty likely to happen, but if it hasn't, the next edit from Orgzly will remove the corruption.

Thankfully, the bug only meant that content at the end of a file was left in, when it should have been deleted. It looks scary, but nobody has actually lost information in a strict sense.

@decadent
Copy link

Orgzly stores notebooks in its on-disk database, and they are exported as text files during sync.

I understand that, I'm talking about when the app exports the file in syncing. The best practice is to not write directly into the existing file, since this operation can fail midway. Instead one should write a new file, ensure that no errors occurred, and then rename it in place of the original — this way only the directory's pointer to the file needs to be overwritten to finish the operation. (Though one issue with this is that the app should also copy the creation time from the original file, to preserve that metadata.)

@amberin
Copy link
Member Author

amberin commented Nov 14, 2024

@decadent PRs with improvements are always welcome. Yes, writing the file can fail midway but I consider it a pretty academic problem compared to this serious bug. But moving the new file in place should be best, I don't see anything preventing that off the top of my head. (When I introduced this bug, I replaced the previous logic which started by deleting the existing file...)

@decadent
Copy link

the bug only meant that content at the end of a file was left in, when it should have been deleted. It looks scary, but nobody has actually lost information in a strict sense.

I know I'm being somewhat annoying here, but: another argument for writing more explicitly about such issues in the changelog and the in-app changelog dialog is that currently, when people discover junk at the end of the file, left over from the untruncated writing, they won't know where it came from. Whereas explicit instructions could tell them that it's indeed junk and they're free to delete it.

Also btw, I just discovered that in the case of non-ascii utf-8 characters, they could be chopped off mid-sequence when overwritten, so that the remaining bytes aren't valid utf-8. Emacs itself falls back to loading such a file in an ascii mode with a binary representation of non-ascii characters, which looks kinda scary. And external tools can have some trouble with such files too.

@amberin
Copy link
Member Author

amberin commented Nov 14, 2024

another argument for writing more explicitly about such issues in the changelog and the in-app changelog dialog is that currently, when people discover junk at the end of the file, left over from the untruncated writing, they won't know where it came from. Whereas explicit instructions could tell them that it's indeed junk and they're free to delete it.

I completely agree. I might have added something like that had I fully grasped the consequences before I released the fix. I'll add a few words in the next release.

@decadent
Copy link

Perhaps a pinned issue could be made, explaining the happenings — and linked from the changelog dialog, if it allows links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants