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

Commit be16cb15 authored by San Mehat's avatar San Mehat
Browse files

PackageManagerService: Refactor MountService calls (new api)



Signed-off-by: default avatarSan Mehat <san@google.com>
parent ec7f7e63
Loading
Loading
Loading
Loading
+57 −72
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ import android.os.Environment;
import android.os.FileObserver;
import android.os.FileUtils;
import android.os.Handler;
import android.os.MountServiceResultCode;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.ServiceManager;
@@ -7940,98 +7941,82 @@ class PackageManagerService extends IPackageManager.Stub {
            Log.e(TAG, "Failed to create encryption keys with exception: " + nsae);
            return null;
        }
        try {
            cachePath = mountService.createSecureContainer(pkgName,
                mbLen,
                "vfat", sdEncKey, Process.SYSTEM_UID);
            if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to install " + pkgName + ", cachePath =" + cachePath);
            return cachePath;
        } catch(IllegalStateException e) {
            Log.e(TAG, "Failed to create storage on sdcard with exception: " + e);
        }
        // TODO just fail here and let the user delete later on.
        try {
            mountService.destroySecureContainer(pkgName);
            if (DEBUG_SD_INSTALL) Log.i(TAG, "Destroying cache for " + pkgName + ", cachePath =" + cachePath);
        } catch(IllegalStateException e) {
            Log.e(TAG, "Failed to destroy existing cache: " + e);

        int rc = mountService.createSecureContainer(
                pkgName, mbLen, "vfat", sdEncKey, Process.SYSTEM_UID);
        if (rc != MountServiceResultCode.OperationSucceeded) {
            Log.e(TAG, String.format("Failed to create container (%d)", rc));

            rc = mountService.destroySecureContainer(pkgName);
            if (rc != MountServiceResultCode.OperationSucceeded) {
                Log.e(TAG, String.format("Failed to cleanup container (%d)", rc));
                return null;
            }
       try {
            cachePath = mountService.createSecureContainer(pkgName,
                mbLen,
                "vfat", sdEncKey, Process.SYSTEM_UID);
            if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to install again " + pkgName + ", cachePath =" + cachePath);
            return cachePath;
        } catch(IllegalStateException e) {
            Log.e(TAG, "Failed to create storage on sdcard with exception: " + e);
            rc = mountService.createSecureContainer(
                    pkgName, mbLen, "vfat", sdEncKey, Process.SYSTEM_UID);
            if (rc != MountServiceResultCode.OperationSucceeded) {
                Log.e(TAG, String.format("Failed to create container (2nd try) (%d)", rc));
                return null;
            }
        }

        cachePath = mountService.getSecureContainerPath(pkgName);
        if (DEBUG_SD_INSTALL) Log.i(TAG, "Trying to install " + pkgName + ", cachePath =" + cachePath);
            return cachePath;
    }

   private String mountSdDir(String pkgName, int ownerUid) {
       String sdEncKey = SystemKeyStore.getInstance().retrieveKeyHexString(mSdEncryptKey);
       if (sdEncKey == null) {
           Log.e(TAG, "Failed to retrieve encryption keys to mount package code: " + pkgName + ".");
           return null;
       }
       try {
           return getMountService().mountSecureContainer(pkgName, sdEncKey, ownerUid);
       } catch (IllegalStateException e) {
           Log.i(TAG, "Failed to mount container for pkg : " + pkgName + " exception : " + e);
       }

       int rc = getMountService().mountSecureContainer(pkgName, sdEncKey, ownerUid);

       if (rc != MountServiceResultCode.OperationSucceeded) {
           Log.i(TAG, "Failed to mount container for pkg : " + pkgName + " rc : " + rc);
           return null;
       }

       return getMountService().getSecureContainerPath(pkgName);
   }

   private boolean unMountSdDir(String pkgName) {
       // STOPSHIP unmount directory
       try {
           getMountService().unmountSecureContainer(pkgName);
           return true;
       } catch (IllegalStateException e) {
           Log.e(TAG, "Failed to unmount : " + pkgName + " with exception " + e);
       }
       int rc = getMountService().unmountSecureContainer(pkgName);
       if (rc != MountServiceResultCode.OperationSucceeded) {
           Log.e(TAG, "Failed to unmount : " + pkgName + " with rc " + rc);
           return false;
       }
       return true;
   }

    private String getSdDir(String pkgName) {
       String cachePath = null;
       try {
           cachePath = getMountService().getSecureContainerPath(pkgName);
       } catch (IllegalStateException e) {
           Log.e(TAG, "Failed to retrieve secure container path for pkg : " + pkgName + " with exception " + e);
       }
       return cachePath;
        return getMountService().getSecureContainerPath(pkgName);
    }

    private boolean finalizeSdDir(String pkgName) {
       try {
           getMountService().finalizeSecureContainer(pkgName);
           return true;
       } catch (IllegalStateException e) {
           Log.i(TAG, "Failed to destroy container for pkg : " + pkgName);
        int rc = getMountService().finalizeSecureContainer(pkgName);
        if (rc != MountServiceResultCode.OperationSucceeded) {
            Log.i(TAG, "Failed to finalize container for pkg : " + pkgName);
            return false;
        }
        return true;
    }

    private boolean destroySdDir(String pkgName) {
       try {
           // We need to destroy right away
           getMountService().destroySecureContainer(pkgName);
           return true;
       } catch (IllegalStateException e) {
        int rc = getMountService().destroySecureContainer(pkgName);
        if (rc != MountServiceResultCode.OperationSucceeded) {
            Log.i(TAG, "Failed to destroy container for pkg : " + pkgName);
            return false;
        }
        return true;
    }

    static String[] getSecureContainerList() {
       try {
           return getMountService().getSecureContainerList();
       } catch (IllegalStateException e) {
           Log.i(TAG, "Failed to getSecureContainerList");
       }
       return null;
        String[] list = getMountService().getSecureContainerList();
        return list.length == 0 ? null : list;
    }

   static String getTempContainerId() {