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

Commit 044a4012 authored by Suchi Amalapurapu's avatar Suchi Amalapurapu Committed by Android (Google) Code Review
Browse files

Merge "Move mount service wrapper calls to PackageHelper"

parents bb9a5176 679bba33
Loading
Loading
Loading
Loading
+154 −0
Original line number Diff line number Diff line
@@ -16,13 +16,167 @@

package com.android.internal.content;

import android.os.storage.IMountService;

import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.storage.StorageResultCode;
import android.util.Log;

import java.io.File;

/**
 * Constants used internally between the PackageManager
 * and media container service transports.
 * Some utility methods to invoke MountService api.
 */
public class PackageHelper {
    public static final int RECOMMEND_INSTALL_INTERNAL = 1;
    public static final int RECOMMEND_INSTALL_EXTERNAL = 2;
    public static final int RECOMMEND_FAILED_INSUFFICIENT_STORAGE = -1;
    public static final int RECOMMEND_FAILED_INVALID_APK = -2;
    private static final boolean DEBUG_SD_INSTALL = true;
    private static final String TAG = "PackageHelper";

    public static IMountService getMountService() {
        IBinder service = ServiceManager.getService("mount");
        if (service != null) {
            return IMountService.Stub.asInterface(service);
        } else {
            Log.e(TAG, "Can't get mount service");
        }
        return null;
    }

    public static String createSdDir(File tmpPackageFile, String cid,
            String sdEncKey, int uid) {
        // Create mount point via MountService
        IMountService mountService = getMountService();
        long len = tmpPackageFile.length();
        int mbLen = (int) (len/(1024*1024));
        if ((len - (mbLen * 1024 * 1024)) > 0) {
            mbLen++;
        }
        if (DEBUG_SD_INSTALL) Log.i(TAG, "Size of resource " + mbLen);

        try {
            int rc = mountService.createSecureContainer(
                    cid, mbLen, "vfat", sdEncKey, uid);
            if (rc != StorageResultCode.OperationSucceeded) {
                Log.e(TAG, "Failed to create secure container " + cid);
                return null;
            }
            String cachePath = mountService.getSecureContainerPath(cid);
            if (DEBUG_SD_INSTALL) Log.i(TAG, "Created secure container " + cid +
                    " at " + cachePath);
                return cachePath;
        } catch (RemoteException e) {
            Log.e(TAG, "MountService running?");
        }
        return null;
    }

   public static String mountSdDir(String cid, String key, int ownerUid) {
    try {
        int rc = getMountService().mountSecureContainer(cid, key, ownerUid);
        if (rc != StorageResultCode.OperationSucceeded) {
            Log.i(TAG, "Failed to mount container " + cid + " rc : " + rc);
            return null;
        }
        return getMountService().getSecureContainerPath(cid);
    } catch (RemoteException e) {
        Log.e(TAG, "MountService running?");
    }
    return null;
   }

   public static boolean unMountSdDir(String cid) {
    try {
        int rc = getMountService().unmountSecureContainer(cid);
        if (rc != StorageResultCode.OperationSucceeded) {
            Log.e(TAG, "Failed to unmount " + cid + " with rc " + rc);
            return false;
        }
        return true;
    } catch (RemoteException e) {
        Log.e(TAG, "MountService running?");
    }
        return false;
   }

   public static boolean renameSdDir(String oldId, String newId) {
       try {
           int rc = getMountService().renameSecureContainer(oldId, newId);
           if (rc != StorageResultCode.OperationSucceeded) {
               Log.e(TAG, "Failed to rename " + oldId + " to " +
                       newId + "with rc " + rc);
               return false;
           }
           return true;
       } catch (RemoteException e) {
           Log.i(TAG, "Failed ot rename  " + oldId + " to " + newId +
                   " with exception : " + e);
       }
       return false;
   }

   public static String getSdDir(String cid) {
       try {
            return getMountService().getSecureContainerPath(cid);
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get container path for " + cid +
                " with exception " + e);
        }
        return null;
   }

    public static boolean finalizeSdDir(String cid) {
        try {
            int rc = getMountService().finalizeSecureContainer(cid);
            if (rc != StorageResultCode.OperationSucceeded) {
                Log.i(TAG, "Failed to finalize container " + cid);
                return false;
            }
            return true;
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to finalize container " + cid +
                    " with exception " + e);
        }
        return false;
    }

    public static boolean destroySdDir(String cid) {
        try {
            int rc = getMountService().destroySecureContainer(cid);
            if (rc != StorageResultCode.OperationSucceeded) {
                Log.i(TAG, "Failed to destroy container " + cid);
                return false;
            }
            return true;
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to destroy container " + cid +
                    " with exception " + e);
        }
        return false;
    }

    public static String[] getSecureContainerList() {
        try {
            return getMountService().getSecureContainerList();
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to get secure container list with exception" +
                    e);
        }
        return null;
    }

   public static boolean isContainerMounted(String cid) {
       try {
           return getMountService().isSecureContainerMounted(cid);
       } catch (RemoteException e) {
           Log.e(TAG, "Failed to find out if container " + cid + " mounted");
       }
       return false;
   }
}
+22 −121
Original line number Diff line number Diff line
@@ -9,18 +9,14 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageParser;
import android.content.pm.PackageParser.Package;
import android.net.Uri;
import android.os.Debug;
import android.os.Environment;
import android.os.IBinder;
import android.os.storage.IMountService;
import android.os.storage.StorageResultCode;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.StatFs;
import android.app.IntentService;
import android.app.Service;
import android.util.DisplayMetrics;
import android.util.Log;

@@ -30,7 +26,6 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.FileUtils;
import android.provider.Settings;
@@ -51,7 +46,7 @@ public class DefaultContainerService extends IntentService {
         * Creates a new container and copies resource there.
         * @param paackageURI the uri of resource to be copied. Can be either
         * a content uri or a file uri
         * @param containerId the id of the secure container that should
         * @param cid the id of the secure container that should
         * be used for creating a secure container into which the resource
         * will be copied.
         * @param key Refers to key used for encrypting the secure container
@@ -61,12 +56,12 @@ public class DefaultContainerService extends IntentService {
         *
         */
        public String copyResourceToContainer(final Uri packageURI,
                final String containerId,
                final String cid,
                final String key, final String resFileName) {
            if (packageURI == null || containerId == null) {
            if (packageURI == null || cid == null) {
                return null;
            }
            return copyResourceInner(packageURI, containerId, key, resFileName);
            return copyResourceInner(packageURI, cid, key, resFileName);
        }

        /*
@@ -162,13 +157,10 @@ public class DefaultContainerService extends IntentService {
        return mBinder;
    }

    private IMountService getMountService() {
        return IMountService.Stub.asInterface(ServiceManager.getService("mount"));
    }

    private String copyResourceInner(Uri packageURI, String newCacheId, String key, String resFileName) {
    private String copyResourceInner(Uri packageURI, String newCid, String key, String resFileName) {
        // Create new container at newCachePath
        String codePath = packageURI.getPath();
        File codeFile = new File(codePath);
        String newCachePath = null;
        final int CREATE_FAILED = 1;
        final int COPY_FAILED = 2;
@@ -176,8 +168,9 @@ public class DefaultContainerService extends IntentService {
        final int PASS = 4;
        int errCode = CREATE_FAILED;
        // Create new container
        if ((newCachePath = createSdDir(packageURI, newCacheId, key)) != null) {
            if (localLOGV) Log.i(TAG, "Created container for " + newCacheId
        if ((newCachePath = PackageHelper.createSdDir(codeFile,
                newCid, key, Process.myUid())) != null) {
            if (localLOGV) Log.i(TAG, "Created container for " + newCid
                    + " at path : " + newCachePath);
            File resFile = new File(newCachePath, resFileName);
            errCode = COPY_FAILED;
@@ -185,7 +178,8 @@ public class DefaultContainerService extends IntentService {
            if (FileUtils.copyFile(new File(codePath), resFile)) {
                if (localLOGV) Log.i(TAG, "Copied " + codePath + " to " + resFile);
                errCode = FINALIZE_FAILED;
                if (finalizeSdDir(newCacheId)) {
                if (PackageHelper.finalizeSdDir(newCid)) {
                    if (localLOGV) Log.i(TAG, "Finalized container " + newCid);
                    errCode = PASS;
                }
            }
@@ -198,21 +192,25 @@ public class DefaultContainerService extends IntentService {
                break;
            case COPY_FAILED:
                errMsg = "COPY_FAILED";
                if (localLOGV) Log.i(TAG, "Destroying " + newCacheId +
                if (localLOGV) Log.i(TAG, "Destroying " + newCid +
                        " at path " + newCachePath + " after " + errMsg);
                destroySdDir(newCacheId);
                PackageHelper.destroySdDir(newCid);
                break;
            case FINALIZE_FAILED:
                errMsg = "FINALIZE_FAILED";
                if (localLOGV) Log.i(TAG, "Destroying " + newCacheId +
                if (localLOGV) Log.i(TAG, "Destroying " + newCid +
                        " at path " + newCachePath + " after " + errMsg);
                destroySdDir(newCacheId);
                PackageHelper.destroySdDir(newCid);
                break;
            default:
                errMsg = "PASS";
            if (localLOGV) Log.i(TAG, "Unmounting " + newCacheId +
                if (PackageHelper.isContainerMounted(newCid)) {
                    if (localLOGV) Log.i(TAG, "Unmounting " + newCid +
                            " at path " + newCachePath + " after " + errMsg);
                unMountSdDir(newCacheId);
                    PackageHelper.unMountSdDir(newCid);
                } else {
                    if (localLOGV) Log.i(TAG, "Container " + newCid + " not mounted");
                }
                break;
        }
        if (errCode != PASS) {
@@ -221,102 +219,6 @@ public class DefaultContainerService extends IntentService {
        return newCachePath;
    }

    private String createSdDir(final Uri packageURI,
            String containerId, String sdEncKey) {
        File tmpPackageFile = new File(packageURI.getPath());
        // Create mount point via MountService
        IMountService mountService = getMountService();
        long len = tmpPackageFile.length();
        int mbLen = (int) (len/(1024*1024));
        if ((len - (mbLen * 1024 * 1024)) > 0) {
            mbLen++;
        }
        if (localLOGV) Log.i(TAG, "mbLen=" + mbLen);
        String cachePath = null;
        int ownerUid = Process.myUid();
        try {
            int rc = mountService.createSecureContainer(
                    containerId, mbLen, "vfat", sdEncKey, ownerUid);

            if (rc != StorageResultCode.OperationSucceeded) {
                Log.e(TAG, String.format("Container creation failed (%d)", rc));

                // XXX: This destroy should not be necessary
                rc = mountService.destroySecureContainer(containerId);
                if (rc != StorageResultCode.OperationSucceeded) {
                    Log.e(TAG, String.format("Container creation-cleanup failed (%d)", rc));
                    return null;
                }

                // XXX: Does this ever actually succeed?
                rc = mountService.createSecureContainer(
                        containerId, mbLen, "vfat", sdEncKey, ownerUid);
                if (rc != StorageResultCode.OperationSucceeded) {
                    Log.e(TAG, String.format("Container creation retry failed (%d)", rc));
                }
            }

            cachePath = mountService.getSecureContainerPath(containerId);
            if (localLOGV) Log.i(TAG, "Trying to create secure container for  "
                    + containerId + ", cachePath =" + cachePath);
            return cachePath;
        } catch(RemoteException e) {
            Log.e(TAG, "MountService not running?");
            return null;
        }
    }

    private boolean destroySdDir(String containerId) {
        try {
            // We need to destroy right away
            getMountService().destroySecureContainer(containerId);
            return true;
        } catch (IllegalStateException e) {
            Log.i(TAG, "Failed to destroy container : " + containerId);
        } catch(RemoteException e) {
            Log.e(TAG, "MountService not running?");
        }
        return false;
    }

    private boolean finalizeSdDir(String containerId){
        try {
            getMountService().finalizeSecureContainer(containerId);
            return true;
        } catch (IllegalStateException e) {
            Log.i(TAG, "Failed to finalize container for pkg : " + containerId);
        } catch(RemoteException e) {
            Log.e(TAG, "MountService not running?");
        }
        return false;
    }

    private boolean unMountSdDir(String containerId) {
        try {
            getMountService().unmountSecureContainer(containerId);
            return true;
        } catch (IllegalStateException e) {
            Log.e(TAG, "Failed to unmount id:  " + containerId + " with exception " + e);
        } catch(RemoteException e) {
            Log.e(TAG, "MountService not running?");
        }
        return false;
    }

    private String mountSdDir(String containerId, String key) {
        try {
            int rc = getMountService().mountSecureContainer(containerId, key, Process.myUid());
            if (rc == StorageResultCode.OperationSucceeded) {
                return getMountService().getSecureContainerPath(containerId);
            } else {
                Log.e(TAG, String.format("Failed to mount id %s with rc %d ", containerId, rc));
            }
        } catch(RemoteException e) {
            Log.e(TAG, "MountService not running?");
        }
        return null;
    }

    public static boolean copyToFile(InputStream inputStream, FileOutputStream out) {
        try {
            byte[] buffer = new byte[4096];
@@ -483,5 +385,4 @@ public class DefaultContainerService extends IntentService {
        // Return error code
        return ERR_LOC;
    }

}
+33 −215

File changed.

Preview size limit exceeded, changes collapsed.