aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorPo Lu2023-08-08 13:39:16 +0800
committerPo Lu2023-08-08 13:39:16 +0800
commitd35ead5bd8a2630c34a38e02d4689e682b0e1ba9 (patch)
tree9eb098f0d3842c765326d43dcf71d5b06808436e /java
parent3fb2c174d3a73ee5a2670b438538a5c32ad9d7ac (diff)
downloademacs-d35ead5bd8a2630c34a38e02d4689e682b0e1ba9.tar.gz
emacs-d35ead5bd8a2630c34a38e02d4689e682b0e1ba9.zip
Utilize more frequently supported file access modes
* java/org/gnu/emacs/EmacsSafThread.java (openDocument1): Use plain r or w where possible, as the fileio stuff is now better prepared for FIFOs. (openDocument): New argument READ. * java/org/gnu/emacs/EmacsService.java (openDocument): New argument READ. * src/android.c (android_init_emacs_service): Adjust correspondingly. * src/androidvfs.c (android_saf_file_open): Don't support O_APPEND. Pass read as well as trunc and write.
Diffstat (limited to 'java')
-rw-r--r--java/org/gnu/emacs/EmacsSafThread.java58
-rw-r--r--java/org/gnu/emacs/EmacsService.java20
2 files changed, 43 insertions, 35 deletions
diff --git a/java/org/gnu/emacs/EmacsSafThread.java b/java/org/gnu/emacs/EmacsSafThread.java
index 3ae3c0839ce..9f5d7f3d0cf 100644
--- a/java/org/gnu/emacs/EmacsSafThread.java
+++ b/java/org/gnu/emacs/EmacsSafThread.java
@@ -1565,8 +1565,9 @@ public final class EmacsSafThread extends HandlerThread
1565 signal. */ 1565 signal. */
1566 1566
1567 public ParcelFileDescriptor 1567 public ParcelFileDescriptor
1568 openDocument1 (String uri, String documentId, boolean write, 1568 openDocument1 (String uri, String documentId, boolean read,
1569 boolean truncate, CancellationSignal signal) 1569 boolean write, boolean truncate,
1570 CancellationSignal signal)
1570 throws Throwable 1571 throws Throwable
1571 { 1572 {
1572 Uri treeUri, documentUri; 1573 Uri treeUri, documentUri;
@@ -1586,10 +1587,19 @@ public final class EmacsSafThread extends HandlerThread
1586 1587
1587 if (write) 1588 if (write)
1588 { 1589 {
1589 if (truncate) 1590 if (read)
1590 mode = "rwt"; 1591 {
1592 if (truncate)
1593 mode = "rwt";
1594 else
1595 mode = "rw";
1596 }
1591 else 1597 else
1592 mode = "rw"; 1598 /* Set mode to w when WRITE && !READ, disregarding TRUNCATE.
1599 In contradiction with the ContentResolver documentation,
1600 document providers seem to truncate files whenever w is
1601 specified, at least superficially. */
1602 mode = "w";
1593 } 1603 }
1594 else 1604 else
1595 mode = "r"; 1605 mode = "r";
@@ -1597,14 +1607,15 @@ public final class EmacsSafThread extends HandlerThread
1597 fileDescriptor 1607 fileDescriptor
1598 = resolver.openFileDescriptor (documentUri, mode, 1608 = resolver.openFileDescriptor (documentUri, mode,
1599 signal); 1609 signal);
1610 Log.d (TAG, "openDocument1: " + mode + " " + fileDescriptor);
1600 1611
1601 /* If a writable file descriptor is requested and TRUNCATE is set, 1612 /* If a writable on-disk file descriptor is requested and TRUNCATE
1602 then probe the file descriptor to detect if it is actually 1613 is set, then probe the file descriptor to detect if it is
1603 readable. If not, close this file descriptor and reopen it 1614 actually readable. If not, close this file descriptor and
1604 with MODE set to rw; some document providers granting access to 1615 reopen it with MODE set to rw; some document providers granting
1605 Samba shares don't implement rwt, but these document providers 1616 access to Samba shares don't implement rwt, but these document
1606 invariably truncate the file opened even when the mode is 1617 providers invariably truncate the file opened even when the
1607 merely rw. 1618 mode is merely w.
1608 1619
1609 This may be ascribed to a mix-up in Android's documentation 1620 This may be ascribed to a mix-up in Android's documentation
1610 regardin DocumentsProvider: the `openDocument' function is only 1621 regardin DocumentsProvider: the `openDocument' function is only
@@ -1612,7 +1623,7 @@ public final class EmacsSafThread extends HandlerThread
1612 implementation of the `openFile' function (which documents rwt) 1623 implementation of the `openFile' function (which documents rwt)
1613 delegates to `openDocument'. */ 1624 delegates to `openDocument'. */
1614 1625
1615 if (write && truncate && fileDescriptor != null 1626 if (read && write && truncate && fileDescriptor != null
1616 && !EmacsNative.ftruncate (fileDescriptor.getFd ())) 1627 && !EmacsNative.ftruncate (fileDescriptor.getFd ()))
1617 { 1628 {
1618 try 1629 try
@@ -1647,15 +1658,13 @@ public final class EmacsSafThread extends HandlerThread
1647 TRUNCATE and the document already exists, truncate its contents 1658 TRUNCATE and the document already exists, truncate its contents
1648 before returning. 1659 before returning.
1649 1660
1650 On Android 9.0 and earlier, always open the document in 1661 If READ && WRITE, open the file under either the `rw' or `rwt'
1651 ``read-write'' mode; this instructs the document provider to 1662 access mode, which implies that the value must be a seekable
1652 return a seekable file that is stored on disk and returns correct 1663 on-disk file. If WRITE && !READ or TRUNC && WRITE, also truncate
1653 file status. 1664 the file after it is opened.
1654 1665
1655 Under newer versions of Android, open the document in a 1666 If only READ or WRITE is set, value may be a non-seekable FIFO or
1656 non-writable mode if WRITE is false. This is possible because 1667 one end of a socket pair.
1657 these versions allow Emacs to explicitly request a seekable
1658 on-disk file.
1659 1668
1660 Value is NULL upon failure or a parcel file descriptor upon 1669 Value is NULL upon failure or a parcel file descriptor upon
1661 success. Call `ParcelFileDescriptor.close' on this file 1670 success. Call `ParcelFileDescriptor.close' on this file
@@ -1667,7 +1676,8 @@ public final class EmacsSafThread extends HandlerThread
1667 1676
1668 public ParcelFileDescriptor 1677 public ParcelFileDescriptor
1669 openDocument (final String uri, final String documentId, 1678 openDocument (final String uri, final String documentId,
1670 final boolean write, final boolean truncate) 1679 final boolean read, final boolean write,
1680 final boolean truncate)
1671 { 1681 {
1672 Object tem; 1682 Object tem;
1673 1683
@@ -1677,8 +1687,8 @@ public final class EmacsSafThread extends HandlerThread
1677 runObject (CancellationSignal signal) 1687 runObject (CancellationSignal signal)
1678 throws Throwable 1688 throws Throwable
1679 { 1689 {
1680 return openDocument1 (uri, documentId, write, truncate, 1690 return openDocument1 (uri, documentId, read,
1681 signal); 1691 write, truncate, signal);
1682 } 1692 }
1683 }); 1693 });
1684 1694
diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java
index d91d8f66009..14ff2cce98f 100644
--- a/java/org/gnu/emacs/EmacsService.java
+++ b/java/org/gnu/emacs/EmacsService.java
@@ -1537,15 +1537,13 @@ public final class EmacsService extends Service
1537 TRUNCATE and the document already exists, truncate its contents 1537 TRUNCATE and the document already exists, truncate its contents
1538 before returning. 1538 before returning.
1539 1539
1540 On Android 9.0 and earlier, always open the document in 1540 If READ && WRITE, open the file under either the `rw' or `rwt'
1541 ``read-write'' mode; this instructs the document provider to 1541 access mode, which implies that the value must be a seekable
1542 return a seekable file that is stored on disk and returns correct 1542 on-disk file. If TRUNC && WRITE, also truncate the file after it
1543 file status. 1543 is opened.
1544 1544
1545 Under newer versions of Android, open the document in a 1545 If only READ or WRITE is set, value may be a non-seekable FIFO or
1546 non-writable mode if WRITE is false. This is possible because 1546 one end of a socket pair.
1547 these versions allow Emacs to explicitly request a seekable
1548 on-disk file.
1549 1547
1550 Value is NULL upon failure or a parcel file descriptor upon 1548 Value is NULL upon failure or a parcel file descriptor upon
1551 success. Call `ParcelFileDescriptor.close' on this file 1549 success. Call `ParcelFileDescriptor.close' on this file
@@ -1555,8 +1553,8 @@ public final class EmacsService extends Service
1555 UnsupportedOperationException may be thrown upon failure. */ 1553 UnsupportedOperationException may be thrown upon failure. */
1556 1554
1557 public ParcelFileDescriptor 1555 public ParcelFileDescriptor
1558 openDocument (String uri, String documentId, boolean write, 1556 openDocument (String uri, String documentId,
1559 boolean truncate) 1557 boolean read, boolean write, boolean truncate)
1560 { 1558 {
1561 /* Start the thread used to run SAF requests if it isn't already 1559 /* Start the thread used to run SAF requests if it isn't already
1562 running. */ 1560 running. */
@@ -1567,7 +1565,7 @@ public final class EmacsService extends Service
1567 storageThread.start (); 1565 storageThread.start ();
1568 } 1566 }
1569 1567
1570 return storageThread.openDocument (uri, documentId, write, 1568 return storageThread.openDocument (uri, documentId, read, write,
1571 truncate); 1569 truncate);
1572 } 1570 }
1573 1571