diff options
| author | Po Lu | 2023-08-06 21:45:29 +0800 |
|---|---|---|
| committer | Po Lu | 2023-08-06 21:45:29 +0800 |
| commit | 7873369338ee0159ca285153fd4592cbcff65d7a (patch) | |
| tree | bd4fb3b1c8b19836d8d78b3ff4173470cfefd687 /java | |
| parent | 669a4b96c374801faa137f43b4497b2ed6511104 (diff) | |
| download | emacs-7873369338ee0159ca285153fd4592cbcff65d7a.tar.gz emacs-7873369338ee0159ca285153fd4592cbcff65d7a.zip | |
Update Android port
* java/org/gnu/emacs/EmacsNative.java: Declare ftruncate.
* java/org/gnu/emacs/EmacsSafThread.java (openDocument1): If
initially opening with rwt, verify the file descriptor is really
writable; if not, resort to rw and truncating the file descriptor
by hand instead.
* src/androidvfs.c (NATIVE_NAME (ftruncate)): New function.
Truncate file descriptor and return whether that was successful.
Diffstat (limited to 'java')
| -rw-r--r-- | java/org/gnu/emacs/EmacsNative.java | 4 | ||||
| -rw-r--r-- | java/org/gnu/emacs/EmacsSafThread.java | 37 |
2 files changed, 41 insertions, 0 deletions
diff --git a/java/org/gnu/emacs/EmacsNative.java b/java/org/gnu/emacs/EmacsNative.java index 7d72a9f192e..fae0ba98f86 100644 --- a/java/org/gnu/emacs/EmacsNative.java +++ b/java/org/gnu/emacs/EmacsNative.java | |||
| @@ -274,6 +274,10 @@ public final class EmacsNative | |||
| 274 | operations. */ | 274 | operations. */ |
| 275 | public static native void safPostRequest (); | 275 | public static native void safPostRequest (); |
| 276 | 276 | ||
| 277 | /* Detect and return FD is writable. FD may be truncated to 0 bytes | ||
| 278 | in the process. */ | ||
| 279 | public static native boolean ftruncate (int fd); | ||
| 280 | |||
| 277 | static | 281 | static |
| 278 | { | 282 | { |
| 279 | /* Older versions of Android cannot link correctly with shared | 283 | /* Older versions of Android cannot link correctly with shared |
diff --git a/java/org/gnu/emacs/EmacsSafThread.java b/java/org/gnu/emacs/EmacsSafThread.java index 29cd3fa6bc7..3ae3c0839ce 100644 --- a/java/org/gnu/emacs/EmacsSafThread.java +++ b/java/org/gnu/emacs/EmacsSafThread.java | |||
| @@ -24,6 +24,7 @@ import java.util.HashMap; | |||
| 24 | import java.util.Iterator; | 24 | import java.util.Iterator; |
| 25 | 25 | ||
| 26 | import java.io.FileNotFoundException; | 26 | import java.io.FileNotFoundException; |
| 27 | import java.io.IOException; | ||
| 27 | 28 | ||
| 28 | import android.content.ContentResolver; | 29 | import android.content.ContentResolver; |
| 29 | import android.database.Cursor; | 30 | import android.database.Cursor; |
| @@ -1597,6 +1598,42 @@ public final class EmacsSafThread extends HandlerThread | |||
| 1597 | = resolver.openFileDescriptor (documentUri, mode, | 1598 | = resolver.openFileDescriptor (documentUri, mode, |
| 1598 | signal); | 1599 | signal); |
| 1599 | 1600 | ||
| 1601 | /* If a writable file descriptor is requested and TRUNCATE is set, | ||
| 1602 | then probe the file descriptor to detect if it is actually | ||
| 1603 | readable. If not, close this file descriptor and reopen it | ||
| 1604 | with MODE set to rw; some document providers granting access to | ||
| 1605 | Samba shares don't implement rwt, but these document providers | ||
| 1606 | invariably truncate the file opened even when the mode is | ||
| 1607 | merely rw. | ||
| 1608 | |||
| 1609 | This may be ascribed to a mix-up in Android's documentation | ||
| 1610 | regardin DocumentsProvider: the `openDocument' function is only | ||
| 1611 | documented to accept r or rw, whereas the default | ||
| 1612 | implementation of the `openFile' function (which documents rwt) | ||
| 1613 | delegates to `openDocument'. */ | ||
| 1614 | |||
| 1615 | if (write && truncate && fileDescriptor != null | ||
| 1616 | && !EmacsNative.ftruncate (fileDescriptor.getFd ())) | ||
| 1617 | { | ||
| 1618 | try | ||
| 1619 | { | ||
| 1620 | fileDescriptor.closeWithError ("File descriptor requested" | ||
| 1621 | + " is not writable"); | ||
| 1622 | } | ||
| 1623 | catch (IOException e) | ||
| 1624 | { | ||
| 1625 | Log.w (TAG, "Leaking unclosed file descriptor " + e); | ||
| 1626 | } | ||
| 1627 | |||
| 1628 | fileDescriptor | ||
| 1629 | = resolver.openFileDescriptor (documentUri, "rw", signal); | ||
| 1630 | |||
| 1631 | /* Try to truncate fileDescriptor just to stay on the safe | ||
| 1632 | side. */ | ||
| 1633 | if (fileDescriptor != null) | ||
| 1634 | EmacsNative.ftruncate (fileDescriptor.getFd ()); | ||
| 1635 | } | ||
| 1636 | |||
| 1600 | /* Every time a document is opened, remove it from the file status | 1637 | /* Every time a document is opened, remove it from the file status |
| 1601 | cache. */ | 1638 | cache. */ |
| 1602 | toplevel = getCache (treeUri); | 1639 | toplevel = getCache (treeUri); |