use shell to open v4 signature file

BUG: 133435829
Test: atest PackageManagerShellCommandIncrementalTest
Change-Id: I8e57b00f042f8b6c85d5f905460dd2da6199accc
(cherry picked from commit cdbcb34c2babcd2523acbe3d359855fe1155e7c3)
diff --git a/core/java/android/os/incremental/V4Signature.java b/core/java/android/os/incremental/V4Signature.java
index 17adfc8..6d334f5 100644
--- a/core/java/android/os/incremental/V4Signature.java
+++ b/core/java/android/os/incremental/V4Signature.java
@@ -16,12 +16,12 @@
 
 package android.os.incremental;
 
+import android.os.ParcelFileDescriptor;
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 
 /**
@@ -41,11 +41,12 @@
     /**
      * Construct a V4Signature from .idsig file.
      */
-    public static V4Signature readFrom(File file) {
-        try (DataInputStream stream = new DataInputStream(new FileInputStream(file))) {
+    public static V4Signature readFrom(ParcelFileDescriptor pfd) throws IOException {
+        final ParcelFileDescriptor dupedFd = pfd.dup();
+        final ParcelFileDescriptor.AutoCloseInputStream fdInputStream =
+                new ParcelFileDescriptor.AutoCloseInputStream(dupedFd);
+        try (DataInputStream stream = new DataInputStream(fdInputStream)) {
             return readFrom(stream);
-        } catch (IOException e) {
-            return null;
         }
     }
 
diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
index 1dda3ab..0f06c18 100644
--- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
+++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
@@ -97,6 +97,7 @@
 import android.text.format.DateUtils;
 import android.util.ArraySet;
 import android.util.PrintWriterPrinter;
+import android.util.Slog;
 import android.util.SparseArray;
 
 import com.android.internal.content.PackageHelper;
@@ -139,6 +140,7 @@
     /** Path where ART profiles snapshots are dumped for the shell user */
     private final static String ART_PROFILE_SNAPSHOT_DEBUG_LOCATION = "/data/misc/profman/";
     private static final int DEFAULT_WAIT_MS = 60 * 1000;
+    private static final String TAG = "PackageManagerShellCommand";
 
     final IPackageManager mInterface;
     final IPermissionManager mPermissionManager;
@@ -3077,11 +3079,20 @@
         final long size = file.length();
         final byte[] metadata = inPath.getBytes(StandardCharsets.UTF_8);
 
-        // Try to load a v4 signature for the APK.
-        final V4Signature v4signature = V4Signature.readFrom(
-                new File(inPath + V4Signature.EXT));
-        final byte[] v4signatureBytes =
-                (v4signature != null) ? v4signature.toByteArray() : null;
+        byte[] v4signatureBytes = null;
+        // Try to load the v4 signature file for the APK; it might not exist.
+        final String v4SignaturePath = inPath + V4Signature.EXT;
+        final ParcelFileDescriptor pfd = openFileForSystem(v4SignaturePath, "r");
+        if (pfd != null) {
+            try {
+                final V4Signature v4signature = V4Signature.readFrom(pfd);
+                v4signatureBytes = v4signature.toByteArray();
+            } catch (IOException ex) {
+                Slog.e(TAG, "V4 signature file exists but failed to be parsed.", ex);
+            } finally {
+                IoUtils.closeQuietly(pfd);
+            }
+        }
 
         session.addFile(LOCATION_DATA_APP, name, size, metadata, v4signatureBytes);
     }