Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4b85f98d authored by John Wu's avatar John Wu Committed by Android (Google) Code Review
Browse files

Merge "[Ravenwood] Enable PFD.getStatSize and cleanup implementation" into main

parents ac2d801f be940db4
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -54,7 +54,6 @@ import android.provider.DocumentsContract.Document;
import android.provider.MediaStore;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.system.StructStat;
import android.text.TextUtils;
import android.util.DataUnit;
@@ -1535,7 +1534,6 @@ public final class FileUtils {
    }

    /** {@hide} */
    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = OsConstants.class)
    public static int translateModeStringToPosix(String mode) {
        // Quick check for invalid chars
        for (int i = 0; i < mode.length(); i++) {
@@ -1570,7 +1568,6 @@ public final class FileUtils {
    }

    /** {@hide} */
    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = OsConstants.class)
    public static String translateModePosixToString(int mode) {
        String res = "";
        if ((mode & O_ACCMODE) == O_RDWR) {
@@ -1592,7 +1589,6 @@ public final class FileUtils {
    }

    /** {@hide} */
    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = OsConstants.class)
    public static int translateModePosixToPfd(int mode) {
        int res = 0;
        if ((mode & O_ACCMODE) == O_RDWR) {
@@ -1617,7 +1613,6 @@ public final class FileUtils {
    }

    /** {@hide} */
    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = OsConstants.class)
    public static int translateModePfdToPosix(int mode) {
        int res = 0;
        if ((mode & MODE_READ_WRITE) == MODE_READ_WRITE) {
@@ -1642,7 +1637,6 @@ public final class FileUtils {
    }

    /** {@hide} */
    @android.ravenwood.annotation.RavenwoodThrow(blockedBy = OsConstants.class)
    public static int translateModeAccessToPosix(int mode) {
        if (mode == F_OK) {
            // There's not an exact mapping, so we attempt a read-only open to
+6 −32
Original line number Diff line number Diff line
@@ -340,7 +340,6 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
        return pfd;
    }

    @RavenwoodReplace
    private static FileDescriptor openInternal(File file, int mode) throws FileNotFoundException {
        if ((mode & MODE_WRITE_ONLY) != 0 && (mode & MODE_APPEND) == 0
                && (mode & MODE_TRUNCATE) == 0 && ((mode & MODE_READ_ONLY) == 0)
@@ -364,26 +363,16 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
        }
    }

    private static FileDescriptor openInternal$ravenwood(File file, int mode)
            throws FileNotFoundException {
        try {
            return native_open$ravenwood(file, mode);
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw new FileNotFoundException(e.getMessage());
        }
    }

    @RavenwoodReplace
    private static void closeInternal(FileDescriptor fd) {
        IoUtils.closeQuietly(fd);
    }

    private static void closeInternal$ravenwood(FileDescriptor fd) {
        // Desktop JVM doesn't have FileDescriptor.close(), so we'll need to go to the ravenwood
        // side to close it.
        native_close$ravenwood(fd);
        try {
            Os.close(fd);
        } catch (ErrnoException ignored) {
        }
    }

    /**
@@ -743,7 +732,6 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
     * Return the total size of the file representing this fd, as determined by
     * {@code stat()}. Returns -1 if the fd is not a file.
     */
    @RavenwoodThrow(reason = "Os.readlink() and Os.stat()")
    public long getStatSize() {
        if (mWrapped != null) {
            return mWrapped.getStatSize();
@@ -1277,32 +1265,19 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
        }
    }

    // These native methods are currently only implemented by Ravenwood, as it's the only
    // mechanism we have to jump to our RavenwoodNativeSubstitutionClass
    private static native void native_setFdInt$ravenwood(FileDescriptor fd, int fdInt);
    private static native int native_getFdInt$ravenwood(FileDescriptor fd);
    private static native FileDescriptor native_open$ravenwood(File file, int pfdMode)
            throws IOException;
    private static native void native_close$ravenwood(FileDescriptor fd);
    private static native void setFdInt$ravenwood(FileDescriptor fd, int fdInt);
    private static native int getFdInt$ravenwood(FileDescriptor fd);

    @RavenwoodReplace
    private static void setFdInt(FileDescriptor fd, int fdInt) {
        fd.setInt$(fdInt);
    }

    private static void setFdInt$ravenwood(FileDescriptor fd, int fdInt) {
        native_setFdInt$ravenwood(fd, fdInt);
    }

    @RavenwoodReplace
    private static int getFdInt(FileDescriptor fd) {
        return fd.getInt$();
    }

    private static int getFdInt$ravenwood(FileDescriptor fd) {
        return native_getFdInt$ravenwood(fd);
    }

    @RavenwoodReplace
    private void setFdOwner(FileDescriptor fd) {
        IoUtils.setFdOwner(fd, this);
@@ -1320,7 +1295,6 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
    private int acquireRawFd$ravenwood(FileDescriptor fd) {
        // FD owners currently unsupported under Ravenwood; return FD directly
        return getFdInt(fd);

    }

    @RavenwoodReplace
+18 −23
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.os.FileUtils.MemoryPipe;
import android.platform.test.annotations.IgnoreUnderRavenwood;
import android.platform.test.annotations.DisabledOnRavenwood;
import android.platform.test.ravenwood.RavenwoodRule;
import android.provider.DocumentsContract.Document;
import android.system.Os;
@@ -156,7 +156,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = MemoryPipe.class)
    @DisabledOnRavenwood(blockedBy = MemoryPipe.class)
    public void testCopy_FileToPipe() throws Exception {
        for (int size : DATA_SIZES) {
            final File src = new File(mTarget, "src");
@@ -177,7 +177,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = MemoryPipe.class)
    @DisabledOnRavenwood(blockedBy = MemoryPipe.class)
    public void testCopy_PipeToFile() throws Exception {
        for (int size : DATA_SIZES) {
            final File dest = new File(mTarget, "dest");
@@ -197,7 +197,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = MemoryPipe.class)
    @DisabledOnRavenwood(blockedBy = MemoryPipe.class)
    public void testCopy_PipeToPipe() throws Exception {
        for (int size : DATA_SIZES) {
            byte[] expected = new byte[size];
@@ -215,7 +215,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = MemoryPipe.class)
    @DisabledOnRavenwood(blockedBy = MemoryPipe.class)
    public void testCopy_ShortPipeToFile() throws Exception {
        byte[] source = new byte[33_000_000];
        new Random().nextBytes(source);
@@ -257,9 +257,9 @@ public class FileUtilsTest {
        assertArrayEquals(expected, actual);
    }

    //TODO(ravenwood) Remove the _$noRavenwood suffix and add @RavenwoodIgnore instead
    @Test
    public void testCopy_SocketToFile_FileToSocket$noRavenwood() throws Exception {
    @DisabledOnRavenwood(reason = "Missing Os methods in Ravenwood")
    public void testCopy_SocketToFile_FileToSocket() throws Exception {
        for (int size : DATA_SIZES ) {
            final File src = new File(mTarget, "src");
            final File dest = new File(mTarget, "dest");
@@ -510,7 +510,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    @DisabledOnRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    public void testBuildUniqueFile_normal() throws Exception {
        assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test"));
        assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
@@ -530,7 +530,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    @DisabledOnRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    public void testBuildUniqueFile_unknown() throws Exception {
        assertNameEquals("test",
                FileUtils.buildUniqueFile(mTarget, "application/octet-stream", "test"));
@@ -544,7 +544,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    @DisabledOnRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    public void testBuildUniqueFile_dir() throws Exception {
        assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test"));
        new File(mTarget, "test").mkdir();
@@ -559,7 +559,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    @DisabledOnRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    public void testBuildUniqueFile_increment() throws Exception {
        assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
        new File(mTarget, "test.jpg").createNewFile();
@@ -579,7 +579,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    @DisabledOnRavenwood(blockedBy = android.webkit.MimeTypeMap.class)
    public void testBuildUniqueFile_mimeless() throws Exception {
        assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "test.jpg"));
        new File(mTarget, "test.jpg").createNewFile();
@@ -675,8 +675,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(reason = "Requires kernel support")
    public void testTranslateMode() throws Exception {
    public void testTranslateMode() {
        assertTranslate("r", O_RDONLY, MODE_READ_ONLY);

        assertTranslate("rw", O_RDWR | O_CREAT,
@@ -695,8 +694,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(reason = "Requires kernel support")
    public void testMalformedTransate_int() throws Exception {
    public void testMalformedTransate_int() {
        try {
            // The non-standard Linux access mode 3 should throw
            // an IllegalArgumentException.
@@ -707,8 +705,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(reason = "Requires kernel support")
    public void testMalformedTransate_string() throws Exception {
    public void testMalformedTransate_string() {
        try {
            // The non-standard Linux access mode 3 should throw
            // an IllegalArgumentException.
@@ -719,8 +716,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(reason = "Requires kernel support")
    public void testTranslateMode_Invalid() throws Exception {
    public void testTranslateMode_Invalid() {
        try {
            translateModeStringToPosix("rwx");
            fail();
@@ -734,8 +730,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(reason = "Requires kernel support")
    public void testTranslateMode_Access() throws Exception {
    public void testTranslateMode_Access() {
        assertEquals(O_RDONLY, translateModeAccessToPosix(F_OK));
        assertEquals(O_RDONLY, translateModeAccessToPosix(R_OK));
        assertEquals(O_WRONLY, translateModeAccessToPosix(W_OK));
@@ -744,7 +739,7 @@ public class FileUtilsTest {
    }

    @Test
    @IgnoreUnderRavenwood(reason = "Requires kernel support")
    @DisabledOnRavenwood(reason = "Requires kernel support")
    public void testConvertToModernFd() throws Exception {
        final String nonce = String.valueOf(System.nanoTime());

+11 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.ravenwood.common;

import java.io.FileDescriptor;
import java.io.IOException;

/**
 * Collection of methods to workaround limitation in the hostside JVM.
@@ -43,6 +44,11 @@ public abstract class JvmWorkaround {
     */
    public abstract int getFdInt(FileDescriptor fd);

    /**
     * Equivalent to Android's Os.close(fd).
     */
    public abstract void closeFd(FileDescriptor fd) throws IOException;

    /**
     * Placeholder implementation for the host side.
     *
@@ -64,5 +70,10 @@ public abstract class JvmWorkaround {
        public int getFdInt(FileDescriptor fd) {
            throw calledOnHostside();
        }

        @Override
        public void closeFd(FileDescriptor fd) {
            throw calledOnHostside();
        }
    }
}
+17 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@
package com.android.ravenwood.common;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

class OpenJdkWorkaround extends JvmWorkaround {
    @Override
@@ -43,4 +45,19 @@ class OpenJdkWorkaround extends JvmWorkaround {
                    + " perhaps JRE has changed?", e);
        }
    }

    @Override
    public void closeFd(FileDescriptor fd) throws IOException {
        try {
            final Object obj = Class.forName("jdk.internal.access.SharedSecrets").getMethod(
                    "getJavaIOFileDescriptorAccess").invoke(null);
            Class.forName("jdk.internal.access.JavaIOFileDescriptorAccess").getMethod(
                    "close", FileDescriptor.class).invoke(obj, fd);
        } catch (InvocationTargetException e) {
            SneakyThrow.sneakyThrow(e.getTargetException());
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException("Failed to interact with raw FileDescriptor internals;"
                    + " perhaps JRE has changed?", e);
        }
    }
}
Loading