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

Commit 9b10ef5f authored by Suchi Amalapurapu's avatar Suchi Amalapurapu
Browse files

Rework the way PackageManager binds to default container service.

Clean up stale containers when enabling/disabling packages on sdcard.
Check the path of packages which are being enabled.
Make sure gc's are done prior to destroying containers when moving applicati
as well as enabling/disabling packages for sdcard mount status changes.

Some miscellaneous issues
   Remove hack to avoid renaming containers.
   Fix test with forward locked apps
   Remove adding container id to asec list when renaming
   Some cosmetic changes to DefaultContainerService
parent a034cd3e
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -54,11 +54,13 @@ public class PackageHelper {
        // Create mount point via MountService
        IMountService mountService = getMountService();
        long len = tmpPackageFile.length();
        int mbLen = (int) (len/(1024*1024));
        int mbLen = (int) (len >> 20);
        if ((len - (mbLen * 1024 * 1024)) > 0) {
            mbLen++;
        }
        if (localLOGV) Log.i(TAG, "Size of resource " + mbLen);
        // Add buffer size
        mbLen++;
        if (localLOGV) Log.i(TAG, "Size of container " + mbLen + " MB " + len + " bytes");

        try {
            int rc = mountService.createSecureContainer(
+29 −50
Original line number Diff line number Diff line
@@ -166,62 +166,41 @@ public class DefaultContainerService extends IntentService {
        String codePath = packageURI.getPath();
        File codeFile = new File(codePath);
        String newCachePath = null;
        final int CREATE_FAILED = 1;
        final int COPY_FAILED = 2;
        final int FINALIZE_FAILED = 3;
        final int PASS = 4;
        int errCode = CREATE_FAILED;
        // Create new container
        if ((newCachePath = PackageHelper.createSdDir(codeFile,
                newCid, key, Process.myUid())) != null) {
                newCid, key, Process.myUid())) == null) {
            Log.e(TAG, "Failed creating container " + newCid);
            return null;
        }
        if (localLOGV) Log.i(TAG, "Created container for " + newCid
                + " at path : " + newCachePath);
        File resFile = new File(newCachePath, resFileName);
            errCode = COPY_FAILED;
        // Copy file from codePath
            if (FileUtils.copyFile(new File(codePath), resFile)) {
                if (localLOGV) Log.i(TAG, "Copied " + codePath + " to " + resFile);
                errCode = FINALIZE_FAILED;
                if (PackageHelper.finalizeSdDir(newCid)) {
                    if (localLOGV) Log.i(TAG, "Finalized container " + newCid);
                    errCode = PASS;
                }
            }
        }
        // Print error based on errCode
        String errMsg = "";
        switch (errCode) {
            case CREATE_FAILED:
                errMsg = "CREATE_FAILED";
                break;
            case COPY_FAILED:
                errMsg = "COPY_FAILED";
                if (localLOGV) Log.i(TAG, "Destroying " + newCid +
                        " at path " + newCachePath + " after " + errMsg);
        if (!FileUtils.copyFile(new File(codePath), resFile)) {
            Log.e(TAG, "Failed to copy " + codePath + " to " + resFile);
            // Clean up created container
            PackageHelper.destroySdDir(newCid);
                break;
            case FINALIZE_FAILED:
                errMsg = "FINALIZE_FAILED";
                if (localLOGV) Log.i(TAG, "Destroying " + newCid +
                        " at path " + newCachePath + " after " + errMsg);
            return null;
        }
        if (localLOGV) Log.i(TAG, "Copied " + codePath + " to " + resFile);
        // Finalize container now
        if (!PackageHelper.finalizeSdDir(newCid)) {
            Log.e(TAG, "Failed to finalize " + newCid + " at cache path " + newCachePath);
            // Clean up created container
            PackageHelper.destroySdDir(newCid);
                break;
            default:
                errMsg = "PASS";
                if (PackageHelper.isContainerMounted(newCid)) {
                    if (localLOGV) Log.i(TAG, "Unmounting " + newCid +
                            " at path " + newCachePath + " after " + errMsg);
            return null;
        }
        if (localLOGV) Log.i(TAG, "Finalized container " + newCid);
        // Force a gc to avoid being killed.
        Runtime.getRuntime().gc();
        // Unmount container
        if (PackageHelper.isContainerMounted(newCid)) {
            if (localLOGV) Log.i(TAG, "Unmounting " + newCid +
                    " at path " + newCachePath);
            PackageHelper.unMountSdDir(newCid);
        } else {
            if (localLOGV) Log.i(TAG, "Container " + newCid + " not mounted");
        }
                break;
        }
        if (errCode != PASS) {
            return null;
        }
        return newCachePath;
    }

+0 −7
Original line number Diff line number Diff line
@@ -1176,13 +1176,6 @@ class MountService extends IMountService.Stub
        } catch (NativeDaemonConnectorException e) {
            rc = StorageResultCode.OperationFailedInternalError;
        }
        if (rc == StorageResultCode.OperationSucceeded) {
            synchronized (mAsecMountSet) {
                if (!mAsecMountSet.contains(newId)) {
                    mAsecMountSet.add(newId);
                }
            }
        }

        return rc;
    }
+359 −223

File changed.

Preview size limit exceeded, changes collapsed.

+26 −4
Original line number Diff line number Diff line
@@ -263,6 +263,9 @@ public class PackageManagerTests extends AndroidTestCase {
        if (expInstallLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) {
            return true;
        }
        if (expInstallLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) {
            return false;
        }
        // TODO Out of memory checks here.
        boolean checkSd = false;
        int setLoc = 0;
@@ -403,7 +406,7 @@ public class PackageManagerTests extends AndroidTestCase {
            return ip;
        } finally {
            if (cleanUp) {
                //cleanUpInstall(ip);
                cleanUpInstall(ip);
            }
        }
    }
@@ -931,9 +934,9 @@ public class PackageManagerTests extends AndroidTestCase {

    public void testManifestInstallLocationFwdLockedSdcard() {
        installFromRawResource("install.apk", R.raw.install_loc_sdcard,
                PackageManager.INSTALL_FORWARD_LOCK, true, true,
                PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION,
                PackageInfo.INSTALL_LOCATION_AUTO);
                PackageManager.INSTALL_FORWARD_LOCK, true, false,
                -1,
                PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY);
    }

    public void xxxtestClearAllSecureContainers() {
@@ -1050,6 +1053,21 @@ public class PackageManagerTests extends AndroidTestCase {
        }
    }

    private int getInstallLoc() {
        boolean userSetting = false;
        int origDefaultLoc = PackageInfo.INSTALL_LOCATION_AUTO;
        try {
            userSetting = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SET_INSTALL_LOCATION) != 0;
            origDefaultLoc = Settings.System.getInt(mContext.getContentResolver(), Settings.System.DEFAULT_INSTALL_LOCATION);
        } catch (SettingNotFoundException e1) {
        }
        return origDefaultLoc;
    }

    private void setInstallLoc(int loc) {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.DEFAULT_INSTALL_LOCATION, loc);
    }
    /*
     * Utility function that reads a apk bundled as a raw resource
     * copies it into own data directory and invokes
@@ -1058,6 +1076,8 @@ public class PackageManagerTests extends AndroidTestCase {
     */
    public void moveFromRawResource(int installFlags, int moveFlags,
            int expRetCode) {
        int origDefaultLoc = getInstallLoc();
        setInstallLoc(PackageInfo.INSTALL_LOCATION_AUTO);
        // Install first
        InstallParams ip = sampleInstallFromRawResource(installFlags, false);
        ApplicationInfo oldAppInfo = null;
@@ -1091,6 +1111,8 @@ public class PackageManagerTests extends AndroidTestCase {
            failStr("Failed with exception : " + e);
        } finally {
            cleanUpInstall(ip);
            // Restore default install location
            setInstallLoc(origDefaultLoc);
        }
    }