From 9fb00904f959a7e94cf992acb3a96e78a05e719c Mon Sep 17 00:00:00 2001 From: Po Lu Date: Mon, 14 Aug 2023 13:15:08 +0800 Subject: Improve efficiency of checking for access to authority documents * java/org/gnu/emacs/EmacsService.java (checkContentUri): Take a string instead of a byte array. Then, use checkCallingUriPermission, in lieu of opening the file. * src/android.c (android_check_content_access): Delete unused function. (android_init_emacs_service): Adjust for changes to checkContentUri's signature. * src/androidvfs.c (android_get_content_name): Return the file name in a new buffer. (android_check_content_access): Adjust correspondingly. (android_authority_name): Verify NAME is a valid JNI string. --- java/org/gnu/emacs/EmacsService.java | 46 +++++++++++++----------------------- 1 file changed, 16 insertions(+), 30 deletions(-) (limited to 'java/org/gnu') diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java index cee823abc83..19ce67c59b7 100644 --- a/java/org/gnu/emacs/EmacsService.java +++ b/java/org/gnu/emacs/EmacsService.java @@ -960,44 +960,30 @@ public final class EmacsService extends Service } } + /* Return whether Emacs is directly permitted to access the + content:// URI NAME. This is not a suitable test for files which + Emacs can access by virtue of their containing document + trees. */ + public boolean - checkContentUri (byte[] string, boolean readable, boolean writable) + checkContentUri (String name, boolean readable, boolean writable) { - String mode, name; + String mode; ParcelFileDescriptor fd; + Uri uri; + int rc, flags; - /* Decode this into a URI. */ - - try - { - /* The usual file name encoding question rears its ugly head - again. */ - name = new String (string, "UTF-8"); - } - catch (UnsupportedEncodingException exception) - { - name = null; - throw new RuntimeException (exception); - } + uri = Uri.parse (name); + flags = 0; - mode = "r"; + if (readable) + flags |= Intent.FLAG_GRANT_READ_URI_PERMISSION; if (writable) - mode += "w"; - - try - { - fd = resolver.openFileDescriptor (Uri.parse (name), mode); - fd.close (); + flags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION; - return true; - } - catch (Exception exception) - { - /* Fall through. */ - } - - return false; + rc = checkCallingUriPermission (uri, flags); + return rc == PackageManager.PERMISSION_GRANTED; } /* Build a content file name for URI. -- cgit v1.2.1