Fix truncation for the Android internal storage provider

* java/org/gnu/emacs/EmacsSafThread.java (openDocument1): If
truncate is specified while resorting to `w', try truncating the
file by hand.
This commit is contained in:
Po Lu 2023-08-08 14:04:36 +08:00
parent 8cd1681c32
commit 5e7e7304c3

View file

@ -1598,7 +1598,7 @@ In addition, arbitrary runtime exceptions (such as
/* Set mode to w when WRITE && !READ, disregarding TRUNCATE.
In contradiction with the ContentResolver documentation,
document providers seem to truncate files whenever w is
specified, at least superficially. */
specified, at least superficially. (But see below.) */
mode = "w";
}
else
@ -1607,7 +1607,6 @@ In addition, arbitrary runtime exceptions (such as
fileDescriptor
= resolver.openFileDescriptor (documentUri, mode,
signal);
Log.d (TAG, "openDocument1: " + mode + " " + fileDescriptor);
/* If a writable on-disk file descriptor is requested and TRUNCATE
is set, then probe the file descriptor to detect if it is
@ -1644,6 +1643,12 @@ In addition, arbitrary runtime exceptions (such as
if (fileDescriptor != null)
EmacsNative.ftruncate (fileDescriptor.getFd ());
}
else if (!read && write && truncate && fileDescriptor != null)
/* Moreover, document providers that return actual seekable
files characteristically neglect to truncate the file
returned when the access mode is merely w, so attempt to
truncate it by hand. */
EmacsNative.ftruncate (fileDescriptor.getFd ());
/* Every time a document is opened, remove it from the file status
cache. */