gsantner / markor

Text editor - Notes & ToDo (for Android) - Markdown, todo.txt, plaintext, math, ..
https://github.com/gsantner/markor/discussions/2269
Other
3.84k stars 368 forks source link

Truncate file on sdcard before re-writing it #1192

Closed oddleaf closed 3 years ago

oddleaf commented 3 years ago

General information

Description

Content deformed while editing file in SDCard. (Editing file in internal storage is just OK)

Seems to be the issue occurs after deleting content in a file then saving it. While adding content without removing anything seems to produce expected result.

I tested it on a Markdown file, but similar issue seems to occur also on another filetypes.

If it could be your reference... I experience this issue since v2.3.2 (but I didn't get enough time to report it. So I just reinstalled last 'stable' version I have, v2.2.8 from my backup).

How to reproduce

  1. Create new file and write:
hello
world
  1. Reopen, then edit to:
hello
foo
  1. Reopen, but I get:
hello
foo
d
  1. Edit it to:
hello
  1. Reopen, but I get:
hello
foo
d
  1. Edit it to:
a
  1. Reopen, then I get:
a
llo
foo
d
  1. Edit it to:
a
hello
foo
d
  1. Reopen, (it shows the same as in 8, which is an expected result) then continue editing to:
hello
foo
d
  1. Reopen, then I get:
hello
foo
d
d
gsantner commented 3 years ago

do you give it some time between edits? like wait 2 seconds before reopen?

SD Card&SAF I/O on Android usually is slow like snail.

oddleaf commented 3 years ago

Tried just now, I also clicked save and waited for some seconds before each operation to be sure. But it didn't fix.

oddleaf commented 3 years ago

I retested it by editing a file and keeping it open. To monitor any change, after I click save then I read the file through http server from another device.

I think I got some pattern:

  1. Create new file, write and save:
hello
world

Result:

hello
world\n
  1. Without reopen, edit to and save:
hello
foo

Result:

hello
foo
d\n

It seems like... world (5 chars) replaced by foo (3) resulting into foo\nd (5).

\n after foo seems to be caused by Markor added a newline to EOF if none of it, which is normal. But the problem is the remaining d, it looks like residue in a buffer?

Though, there's also another \n after d, it is a result of previous EOF termination in step 1.

Other test results with a \n in EOF ignored:

gsantner commented 3 years ago

not sure how exactly that could happen, we get the edited text as a string and write that to disk. No delta applied or something similar.

oddleaf commented 3 years ago

Hmmm, I see. Or probably SAF didn't truncate the file properly?

oddleaf commented 3 years ago

Seems to be mode "rw" in net.gsantner.opoc.util.ShareUtil.writeFile line 1151 is the cause:

pfd = _context.getContentResolver().openFileDescriptor(dof.getUri(), "rw");

It needs to be "rwt", "t" for truncate. Please give it a try (I don't have a supported environtment to try it).

gsantner commented 3 years ago

you can download test apk here when finished

https://github.com/gsantner/markor/actions/runs/484863211

oddleaf commented 3 years ago

Yes! It's working perfectly! Thank you! :D

Anyway, I looked at code in v2.2.8 (which is previously worked for me), the mode was w. But in current version is rw, if we just writing to the file without reading it (when saving), why would it also hold r instead of just w or wt?

PS: I didn't know there's such a great feature of Github, build on cloud(?).

gsantner commented 3 years ago

@oddleaf Please also try this ("w") version from here https://github.com/gsantner/markor/actions/runs/485307850 . If possible please open, close, edit, rewrite, close app, switch between apps ... whatever multiple files of different sizes - let me know if you notice something wrong (or i.e. wrongfully deleted).

Any notable difference for you between w and rwt build?

PS: I didn't know there's such a great feature of Github, build on cloud(?).

It's GitHub's own Continious Integration system, GitHub Actions. There are others like CircleCi and Travis CI too.

oddleaf commented 3 years ago

It's GitHub's own Continious Integration system, GitHub Actions. There are others like CircleCi and Travis CI too.

Oh, thank you!


I don't see any notable difference between the two, it seems to be alright.

I don't know but if there's any behavioral change in some case like this, then please consider using wt or rwt instead.


I just encountered another problem... Markor crashed when I try to create empty Zim Wiki on both SD Card and internal storage. It also happens on Google Play version, I think I need to file it into another issue.

oddleaf commented 3 years ago

@gsantner Thank you! :)