Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 17 | package com.android.defcontainer; |
| 18 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 19 | import android.app.IntentService; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 20 | import android.content.Intent; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 21 | import android.content.pm.ContainerEncryptionParams; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 22 | import android.content.pm.IPackageManager; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 23 | import android.content.pm.LimitedLengthInputStream; |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 24 | import android.content.pm.MacAuthenticatedInputStream; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageCleanItem; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 26 | import android.content.pm.PackageInfo; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageInfoLite; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageParser; |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 30 | import android.content.res.ObbInfo; |
| 31 | import android.content.res.ObbScanner; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 32 | import android.net.Uri; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 33 | import android.os.Environment; |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 34 | import android.os.Environment.UserEnvironment; |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 35 | import android.os.FileUtils; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 36 | import android.os.IBinder; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 37 | import android.os.ParcelFileDescriptor; |
| 38 | import android.os.Process; |
| 39 | import android.os.RemoteException; |
| 40 | import android.os.ServiceManager; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 41 | import android.os.StatFs; |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 42 | import android.provider.Settings; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 43 | import android.util.DisplayMetrics; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 44 | import android.util.Slog; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 45 | |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 46 | import com.android.internal.app.IMediaContainerService; |
| 47 | import com.android.internal.content.NativeLibraryHelper; |
| 48 | import com.android.internal.content.PackageHelper; |
| 49 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 50 | import java.io.BufferedInputStream; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 51 | import java.io.File; |
| 52 | import java.io.FileInputStream; |
| 53 | import java.io.FileNotFoundException; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 54 | import java.io.IOException; |
| 55 | import java.io.InputStream; |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 56 | import java.io.OutputStream; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 57 | import java.security.DigestException; |
| 58 | import java.security.GeneralSecurityException; |
| 59 | import java.security.InvalidAlgorithmParameterException; |
| 60 | import java.security.InvalidKeyException; |
| 61 | import java.security.NoSuchAlgorithmException; |
| 62 | |
| 63 | import javax.crypto.Cipher; |
| 64 | import javax.crypto.CipherInputStream; |
| 65 | import javax.crypto.Mac; |
| 66 | import javax.crypto.NoSuchPaddingException; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 67 | |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 68 | import libcore.io.ErrnoException; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 69 | import libcore.io.IoUtils; |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 70 | import libcore.io.Libcore; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 71 | import libcore.io.Streams; |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 72 | import libcore.io.StructStatVfs; |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 73 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 74 | /* |
| 75 | * This service copies a downloaded apk to a file passed in as |
| 76 | * a ParcelFileDescriptor or to a newly created container specified |
| 77 | * by parameters. The DownloadManager gives access to this process |
| 78 | * based on its uid. This process also needs the ACCESS_DOWNLOAD_MANAGER |
| 79 | * permission to access apks downloaded via the download manager. |
| 80 | */ |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 81 | public class DefaultContainerService extends IntentService { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 82 | private static final String TAG = "DefContainer"; |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 83 | private static final boolean localLOGV = false; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 84 | |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 85 | private static final String LIB_DIR_NAME = "lib"; |
| 86 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 87 | private IMediaContainerService.Stub mBinder = new IMediaContainerService.Stub() { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 88 | /** |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 89 | * Creates a new container and copies resource there. |
| 90 | * @param paackageURI the uri of resource to be copied. Can be either |
| 91 | * a content uri or a file uri |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 92 | * @param cid the id of the secure container that should |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 93 | * be used for creating a secure container into which the resource |
| 94 | * will be copied. |
| 95 | * @param key Refers to key used for encrypting the secure container |
| 96 | * @param resFileName Name of the target resource file(relative to newly |
| 97 | * created secure container) |
| 98 | * @return Returns the new cache path where the resource has been copied into |
| 99 | * |
| 100 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 101 | public String copyResourceToContainer(final Uri packageURI, final String cid, |
| 102 | final String key, final String resFileName, final String publicResFileName, |
| 103 | boolean isExternal, boolean isForwardLocked) { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 104 | if (packageURI == null || cid == null) { |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 105 | return null; |
| 106 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 107 | |
| 108 | return copyResourceInner(packageURI, cid, key, resFileName, publicResFileName, |
| 109 | isExternal, isForwardLocked); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 112 | /** |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 113 | * Copy specified resource to output stream |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 114 | * |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 115 | * @param packageURI the uri of resource to be copied. Should be a file |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 116 | * uri |
| 117 | * @param encryptionParams parameters describing the encryption used for |
| 118 | * this file |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 119 | * @param outStream Remote file descriptor to be used for copying |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 120 | * @return returns status code according to those in |
| 121 | * {@link PackageManager} |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 122 | */ |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 123 | public int copyResource(final Uri packageURI, ContainerEncryptionParams encryptionParams, |
| 124 | ParcelFileDescriptor outStream) { |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 125 | if (packageURI == null || outStream == null) { |
| 126 | return PackageManager.INSTALL_FAILED_INVALID_URI; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 127 | } |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 128 | |
| 129 | ParcelFileDescriptor.AutoCloseOutputStream autoOut |
| 130 | = new ParcelFileDescriptor.AutoCloseOutputStream(outStream); |
| 131 | |
| 132 | try { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 133 | copyFile(packageURI, autoOut, encryptionParams); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 134 | return PackageManager.INSTALL_SUCCEEDED; |
| 135 | } catch (FileNotFoundException e) { |
| 136 | Slog.e(TAG, "Could not copy URI " + packageURI.toString() + " FNF: " |
| 137 | + e.getMessage()); |
| 138 | return PackageManager.INSTALL_FAILED_INVALID_URI; |
| 139 | } catch (IOException e) { |
| 140 | Slog.e(TAG, "Could not copy URI " + packageURI.toString() + " IO: " |
| 141 | + e.getMessage()); |
| 142 | return PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 143 | } catch (DigestException e) { |
| 144 | Slog.e(TAG, "Could not copy URI " + packageURI.toString() + " Security: " |
| 145 | + e.getMessage()); |
| 146 | return PackageManager.INSTALL_FAILED_INVALID_APK; |
Jeff Sharkey | 5adb34f | 2013-04-25 11:35:12 -0700 | [diff] [blame] | 147 | } finally { |
| 148 | IoUtils.closeQuietly(autoOut); |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 149 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 150 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 151 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 152 | /** |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 153 | * Determine the recommended install location for package |
| 154 | * specified by file uri location. |
| 155 | * @param fileUri the uri of resource to be copied. Should be a |
| 156 | * file uri |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 157 | * @return Returns PackageInfoLite object containing |
| 158 | * the package info and recommended app location. |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 159 | */ |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 160 | public PackageInfoLite getMinimalPackageInfo(final String packagePath, int flags, |
| 161 | long threshold) { |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 162 | PackageInfoLite ret = new PackageInfoLite(); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 163 | |
| 164 | if (packagePath == null) { |
| 165 | Slog.i(TAG, "Invalid package file " + packagePath); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 166 | ret.recommendedInstallLocation = PackageHelper.RECOMMEND_FAILED_INVALID_APK; |
| 167 | return ret; |
Suchi Amalapurapu | 3602f76 | 2010-03-03 17:29:33 -0800 | [diff] [blame] | 168 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 169 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 170 | DisplayMetrics metrics = new DisplayMetrics(); |
| 171 | metrics.setToDefaults(); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 172 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 173 | PackageParser.PackageLite pkg = PackageParser.parsePackageLite(packagePath, 0); |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 174 | if (pkg == null) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 175 | Slog.w(TAG, "Failed to parse package"); |
| 176 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 177 | final File apkFile = new File(packagePath); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 178 | if (!apkFile.exists()) { |
| 179 | ret.recommendedInstallLocation = PackageHelper.RECOMMEND_FAILED_INVALID_URI; |
| 180 | } else { |
| 181 | ret.recommendedInstallLocation = PackageHelper.RECOMMEND_FAILED_INVALID_APK; |
| 182 | } |
| 183 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 184 | return ret; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 185 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 186 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 187 | ret.packageName = pkg.packageName; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 188 | ret.versionCode = pkg.versionCode; |
Kenny Root | 930d3af | 2010-07-30 16:52:29 -0700 | [diff] [blame] | 189 | ret.installLocation = pkg.installLocation; |
Kenny Root | 05ca4c9 | 2011-09-15 10:36:25 -0700 | [diff] [blame] | 190 | ret.verifiers = pkg.verifiers; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 191 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 192 | ret.recommendedInstallLocation = recommendAppInstallLocation(pkg.installLocation, |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 193 | packagePath, flags, threshold); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 194 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 195 | return ret; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 196 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 197 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 198 | @Override |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 199 | public boolean checkInternalFreeStorage(Uri packageUri, boolean isForwardLocked, |
| 200 | long threshold) throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 201 | final File apkFile = new File(packageUri.getPath()); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 202 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 203 | return isUnderInternalThreshold(apkFile, isForwardLocked, threshold); |
| 204 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 205 | return true; |
| 206 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 210 | public boolean checkExternalFreeStorage(Uri packageUri, boolean isForwardLocked) |
| 211 | throws RemoteException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 212 | final File apkFile = new File(packageUri.getPath()); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 213 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 214 | return isUnderExternalThreshold(apkFile, isForwardLocked); |
| 215 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 216 | return true; |
| 217 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 218 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 219 | |
| 220 | public ObbInfo getObbInfo(String filename) { |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 221 | try { |
| 222 | return ObbScanner.getObbInfo(filename); |
| 223 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 224 | Slog.d(TAG, "Couldn't get OBB info for " + filename); |
Kenny Root | 05105f7 | 2010-09-22 17:29:43 -0700 | [diff] [blame] | 225 | return null; |
| 226 | } |
Kenny Root | a02b8b0 | 2010-08-05 16:14:17 -0700 | [diff] [blame] | 227 | } |
Kenny Root | aa183e2 | 2010-12-02 18:00:38 -0800 | [diff] [blame] | 228 | |
| 229 | @Override |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 230 | public long calculateDirectorySize(String path) throws RemoteException { |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 231 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 232 | |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 233 | final File dir = Environment.maybeTranslateEmulatedPathToInternal(new File(path)); |
| 234 | if (dir.exists() && dir.isDirectory()) { |
| 235 | final String targetPath = dir.getAbsolutePath(); |
| 236 | return MeasurementUtils.measureDirectory(targetPath); |
Kenny Root | 366949c | 2011-01-14 17:18:14 -0800 | [diff] [blame] | 237 | } else { |
| 238 | return 0L; |
| 239 | } |
Kenny Root | aa183e2 | 2010-12-02 18:00:38 -0800 | [diff] [blame] | 240 | } |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 241 | |
| 242 | @Override |
| 243 | public long[] getFileSystemStats(String path) { |
Kenny Root | a69b7eb | 2012-05-14 14:47:06 -0700 | [diff] [blame] | 244 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 245 | |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 246 | try { |
Elliott Hughes | befd0b1 | 2013-07-09 14:46:18 -0700 | [diff] [blame] | 247 | final StructStatVfs stat = Libcore.os.statvfs(path); |
Jeff Sharkey | 9cbe986a | 2012-04-22 18:56:43 -0700 | [diff] [blame] | 248 | final long totalSize = stat.f_blocks * stat.f_bsize; |
| 249 | final long availSize = stat.f_bavail * stat.f_bsize; |
| 250 | return new long[] { totalSize, availSize }; |
| 251 | } catch (ErrnoException e) { |
| 252 | throw new IllegalStateException(e); |
| 253 | } |
| 254 | } |
Dianne Hackborn | 183ce02 | 2012-06-29 15:00:21 -0700 | [diff] [blame] | 255 | |
| 256 | @Override |
| 257 | public void clearDirectory(String path) throws RemoteException { |
| 258 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 259 | |
| 260 | final File directory = new File(path); |
| 261 | if (directory.exists() && directory.isDirectory()) { |
| 262 | eraseFiles(directory); |
| 263 | } |
| 264 | } |
Kenny Root | cea3743 | 2012-10-18 14:57:33 -0700 | [diff] [blame] | 265 | |
| 266 | @Override |
| 267 | public long calculateInstalledSize(String packagePath, boolean isForwardLocked) |
| 268 | throws RemoteException { |
| 269 | final File packageFile = new File(packagePath); |
| 270 | try { |
| 271 | return calculateContainerSize(packageFile, isForwardLocked) * 1024 * 1024; |
| 272 | } catch (IOException e) { |
| 273 | /* |
| 274 | * Okay, something failed, so let's just estimate it to be 2x |
| 275 | * the file size. Note this will be 0 if the file doesn't exist. |
| 276 | */ |
| 277 | return packageFile.length() * 2; |
| 278 | } |
| 279 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 280 | }; |
| 281 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 282 | public DefaultContainerService() { |
| 283 | super("DefaultContainerService"); |
| 284 | setIntentRedelivery(true); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | protected void onHandleIntent(Intent intent) { |
| 289 | if (PackageManager.ACTION_CLEAN_EXTERNAL_STORAGE.equals(intent.getAction())) { |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 290 | final IPackageManager pm = IPackageManager.Stub.asInterface( |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 291 | ServiceManager.getService("package")); |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 292 | PackageCleanItem item = null; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 293 | try { |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 294 | while ((item = pm.nextPackageToClean(item)) != null) { |
| 295 | final UserEnvironment userEnv = new UserEnvironment(item.userId); |
Jeff Sharkey | 7f392de | 2013-08-11 17:42:17 -0700 | [diff] [blame] | 296 | eraseFiles(userEnv.buildExternalStorageAppDataDirs(item.packageName)); |
| 297 | eraseFiles(userEnv.buildExternalStorageAppMediaDirs(item.packageName)); |
Jeff Sharkey | 752cd92 | 2012-09-23 16:25:12 -0700 | [diff] [blame] | 298 | if (item.andCode) { |
Jeff Sharkey | 7f392de | 2013-08-11 17:42:17 -0700 | [diff] [blame] | 299 | eraseFiles(userEnv.buildExternalStorageAppObbDirs(item.packageName)); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 300 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 301 | } |
| 302 | } catch (RemoteException e) { |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
Jeff Sharkey | 7f392de | 2013-08-11 17:42:17 -0700 | [diff] [blame] | 307 | void eraseFiles(File[] paths) { |
| 308 | for (File path : paths) { |
| 309 | eraseFiles(path); |
| 310 | } |
| 311 | } |
| 312 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 313 | void eraseFiles(File path) { |
| 314 | if (path.isDirectory()) { |
| 315 | String[] files = path.list(); |
| 316 | if (files != null) { |
| 317 | for (String file : files) { |
| 318 | eraseFiles(new File(path, file)); |
| 319 | } |
| 320 | } |
| 321 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 322 | path.delete(); |
| 323 | } |
| 324 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 325 | public IBinder onBind(Intent intent) { |
| 326 | return mBinder; |
| 327 | } |
| 328 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 329 | private String copyResourceInner(Uri packageURI, String newCid, String key, String resFileName, |
| 330 | String publicResFileName, boolean isExternal, boolean isForwardLocked) { |
| 331 | |
| 332 | if (isExternal) { |
| 333 | // Make sure the sdcard is mounted. |
| 334 | String status = Environment.getExternalStorageState(); |
| 335 | if (!status.equals(Environment.MEDIA_MOUNTED)) { |
| 336 | Slog.w(TAG, "Make sure sdcard is mounted."); |
| 337 | return null; |
| 338 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 339 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 340 | |
| 341 | // The .apk file |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 342 | String codePath = packageURI.getPath(); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 343 | File codeFile = new File(codePath); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 344 | |
| 345 | // Calculate size of container needed to hold base APK. |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 346 | final int sizeMb; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 347 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 348 | sizeMb = calculateContainerSize(codeFile, isForwardLocked); |
| 349 | } catch (IOException e) { |
| 350 | Slog.w(TAG, "Problem when trying to copy " + codeFile.getPath()); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 351 | return null; |
| 352 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 353 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 354 | // Create new container |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 355 | final String newCachePath = PackageHelper.createSdDir(sizeMb, newCid, key, Process.myUid(), |
| 356 | isExternal); |
| 357 | if (newCachePath == null) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 358 | Slog.e(TAG, "Failed to create container " + newCid); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 359 | return null; |
| 360 | } |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 361 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 362 | if (localLOGV) { |
| 363 | Slog.i(TAG, "Created container for " + newCid + " at path : " + newCachePath); |
| 364 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 365 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 366 | final File resFile = new File(newCachePath, resFileName); |
| 367 | if (FileUtils.copyFile(new File(codePath), resFile)) { |
| 368 | if (localLOGV) { |
| 369 | Slog.i(TAG, "Copied " + codePath + " to " + resFile); |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 370 | } |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 371 | } else { |
| 372 | Slog.e(TAG, "Failed to copy " + codePath + " to " + resFile); |
| 373 | // Clean up container |
Kenny Root | 85387d7 | 2010-08-26 10:13:11 -0700 | [diff] [blame] | 374 | PackageHelper.destroySdDir(newCid); |
| 375 | return null; |
| 376 | } |
| 377 | |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 378 | try { |
| 379 | Libcore.os.chmod(resFile.getAbsolutePath(), 0640); |
| 380 | } catch (ErrnoException e) { |
| 381 | Slog.e(TAG, "Could not chown APK: " + e.getMessage()); |
| 382 | PackageHelper.destroySdDir(newCid); |
| 383 | return null; |
| 384 | } |
| 385 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 386 | if (isForwardLocked) { |
| 387 | File publicZipFile = new File(newCachePath, publicResFileName); |
| 388 | try { |
| 389 | PackageHelper.extractPublicFiles(resFile.getAbsolutePath(), publicZipFile); |
| 390 | if (localLOGV) { |
| 391 | Slog.i(TAG, "Copied resources to " + publicZipFile); |
| 392 | } |
| 393 | } catch (IOException e) { |
| 394 | Slog.e(TAG, "Could not chown public APK " + publicZipFile.getAbsolutePath() + ": " |
| 395 | + e.getMessage()); |
| 396 | PackageHelper.destroySdDir(newCid); |
| 397 | return null; |
| 398 | } |
| 399 | |
| 400 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 401 | Libcore.os.chmod(publicZipFile.getAbsolutePath(), 0644); |
| 402 | } catch (ErrnoException e) { |
Kenny Root | bf02358 | 2012-05-02 16:56:15 -0700 | [diff] [blame] | 403 | Slog.e(TAG, "Could not chown public resource file: " + e.getMessage()); |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 404 | PackageHelper.destroySdDir(newCid); |
| 405 | return null; |
| 406 | } |
| 407 | } |
| 408 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 409 | final File sharedLibraryDir = new File(newCachePath, LIB_DIR_NAME); |
| 410 | if (sharedLibraryDir.mkdir()) { |
| 411 | int ret = NativeLibraryHelper.copyNativeBinariesIfNeededLI(codeFile, sharedLibraryDir); |
| 412 | if (ret != PackageManager.INSTALL_SUCCEEDED) { |
| 413 | Slog.e(TAG, "Could not copy native libraries to " + sharedLibraryDir.getPath()); |
| 414 | PackageHelper.destroySdDir(newCid); |
| 415 | return null; |
| 416 | } |
| 417 | } else { |
| 418 | Slog.e(TAG, "Could not create native lib directory: " + sharedLibraryDir.getPath()); |
| 419 | PackageHelper.destroySdDir(newCid); |
| 420 | return null; |
| 421 | } |
| 422 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 423 | if (!PackageHelper.finalizeSdDir(newCid)) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 424 | Slog.e(TAG, "Failed to finalize " + newCid + " at path " + newCachePath); |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 425 | // Clean up container |
| 426 | PackageHelper.destroySdDir(newCid); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 427 | return null; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 428 | } |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 429 | |
| 430 | if (localLOGV) { |
| 431 | Slog.i(TAG, "Finalized container " + newCid); |
| 432 | } |
| 433 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 434 | if (PackageHelper.isContainerMounted(newCid)) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 435 | if (localLOGV) { |
| 436 | Slog.i(TAG, "Unmounting " + newCid + " at path " + newCachePath); |
| 437 | } |
| 438 | |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 439 | // Force a gc to avoid being killed. |
| 440 | Runtime.getRuntime().gc(); |
| 441 | PackageHelper.unMountSdDir(newCid); |
| 442 | } else { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 443 | if (localLOGV) { |
| 444 | Slog.i(TAG, "Container " + newCid + " not mounted"); |
| 445 | } |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 446 | } |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 447 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 448 | return newCachePath; |
| 449 | } |
| 450 | |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 451 | private static void copyToFile(InputStream inputStream, OutputStream out) throws IOException { |
| 452 | byte[] buffer = new byte[16384]; |
| 453 | int bytesRead; |
| 454 | while ((bytesRead = inputStream.read(buffer)) >= 0) { |
| 455 | out.write(buffer, 0, bytesRead); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 459 | private void copyFile(Uri pPackageURI, OutputStream outStream, |
| 460 | ContainerEncryptionParams encryptionParams) throws FileNotFoundException, IOException, |
| 461 | DigestException { |
| 462 | String scheme = pPackageURI.getScheme(); |
| 463 | InputStream inStream = null; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 464 | try { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 465 | if (scheme == null || scheme.equals("file")) { |
| 466 | final InputStream is = new FileInputStream(new File(pPackageURI.getPath())); |
| 467 | inStream = new BufferedInputStream(is); |
| 468 | } else if (scheme.equals("content")) { |
| 469 | final ParcelFileDescriptor fd; |
| 470 | try { |
| 471 | fd = getContentResolver().openFileDescriptor(pPackageURI, "r"); |
| 472 | } catch (FileNotFoundException e) { |
| 473 | Slog.e(TAG, "Couldn't open file descriptor from download service. " |
| 474 | + "Failed with exception " + e); |
| 475 | throw e; |
| 476 | } |
| 477 | |
| 478 | if (fd == null) { |
| 479 | Slog.e(TAG, "Provider returned no file descriptor for " + |
| 480 | pPackageURI.toString()); |
| 481 | throw new FileNotFoundException("provider returned no file descriptor"); |
| 482 | } else { |
| 483 | if (localLOGV) { |
| 484 | Slog.i(TAG, "Opened file descriptor from download service."); |
| 485 | } |
| 486 | inStream = new ParcelFileDescriptor.AutoCloseInputStream(fd); |
| 487 | } |
| 488 | } else { |
| 489 | Slog.e(TAG, "Package URI is not 'file:' or 'content:' - " + pPackageURI); |
| 490 | throw new FileNotFoundException("Package URI is not 'file:' or 'content:'"); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * If this resource is encrypted, get the decrypted stream version |
| 495 | * of it. |
| 496 | */ |
| 497 | ApkContainer container = new ApkContainer(inStream, encryptionParams); |
| 498 | |
| 499 | try { |
| 500 | /* |
| 501 | * We copy the source package file to a temp file and then |
| 502 | * rename it to the destination file in order to eliminate a |
| 503 | * window where the package directory scanner notices the new |
| 504 | * package file but it's not completely copied yet. |
| 505 | */ |
| 506 | copyToFile(container.getInputStream(), outStream); |
| 507 | |
| 508 | if (!container.isAuthenticated()) { |
| 509 | throw new DigestException(); |
| 510 | } |
| 511 | } catch (GeneralSecurityException e) { |
| 512 | throw new DigestException("A problem occured copying the file."); |
| 513 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 514 | } finally { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 515 | IoUtils.closeQuietly(inStream); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 519 | private static class ApkContainer { |
Kenny Root | 103d530 | 2012-05-10 10:21:06 -0700 | [diff] [blame] | 520 | private static final int MAX_AUTHENTICATED_DATA_SIZE = 16384; |
| 521 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 522 | private final InputStream mInStream; |
Kenny Root | f5121a9 | 2011-08-10 16:23:32 -0700 | [diff] [blame] | 523 | |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 524 | private MacAuthenticatedInputStream mAuthenticatedStream; |
| 525 | |
| 526 | private byte[] mTag; |
| 527 | |
| 528 | public ApkContainer(InputStream inStream, ContainerEncryptionParams encryptionParams) |
| 529 | throws IOException { |
| 530 | if (encryptionParams == null) { |
| 531 | mInStream = inStream; |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 532 | } else { |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 533 | mInStream = getDecryptedStream(inStream, encryptionParams); |
| 534 | mTag = encryptionParams.getMacTag(); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 535 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 536 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 537 | |
| 538 | public boolean isAuthenticated() { |
| 539 | if (mAuthenticatedStream == null) { |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | return mAuthenticatedStream.isTagEqual(mTag); |
| 544 | } |
| 545 | |
| 546 | private Mac getMacInstance(ContainerEncryptionParams encryptionParams) throws IOException { |
| 547 | final Mac m; |
| 548 | try { |
| 549 | final String macAlgo = encryptionParams.getMacAlgorithm(); |
| 550 | |
| 551 | if (macAlgo != null) { |
| 552 | m = Mac.getInstance(macAlgo); |
| 553 | m.init(encryptionParams.getMacKey(), encryptionParams.getMacSpec()); |
| 554 | } else { |
| 555 | m = null; |
| 556 | } |
| 557 | |
| 558 | return m; |
| 559 | } catch (NoSuchAlgorithmException e) { |
| 560 | throw new IOException(e); |
| 561 | } catch (InvalidKeyException e) { |
| 562 | throw new IOException(e); |
| 563 | } catch (InvalidAlgorithmParameterException e) { |
| 564 | throw new IOException(e); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | public InputStream getInputStream() { |
| 569 | return mInStream; |
| 570 | } |
| 571 | |
| 572 | private InputStream getDecryptedStream(InputStream inStream, |
| 573 | ContainerEncryptionParams encryptionParams) throws IOException { |
| 574 | final Cipher c; |
| 575 | try { |
| 576 | c = Cipher.getInstance(encryptionParams.getEncryptionAlgorithm()); |
| 577 | c.init(Cipher.DECRYPT_MODE, encryptionParams.getEncryptionKey(), |
| 578 | encryptionParams.getEncryptionSpec()); |
| 579 | } catch (NoSuchAlgorithmException e) { |
| 580 | throw new IOException(e); |
| 581 | } catch (NoSuchPaddingException e) { |
| 582 | throw new IOException(e); |
| 583 | } catch (InvalidKeyException e) { |
| 584 | throw new IOException(e); |
| 585 | } catch (InvalidAlgorithmParameterException e) { |
| 586 | throw new IOException(e); |
| 587 | } |
| 588 | |
Kenny Root | 103d530 | 2012-05-10 10:21:06 -0700 | [diff] [blame] | 589 | final long encStart = encryptionParams.getEncryptedDataStart(); |
| 590 | final long end = encryptionParams.getDataEnd(); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 591 | if (end < encStart) { |
| 592 | throw new IOException("end <= encStart"); |
| 593 | } |
| 594 | |
| 595 | final Mac mac = getMacInstance(encryptionParams); |
| 596 | if (mac != null) { |
Kenny Root | 103d530 | 2012-05-10 10:21:06 -0700 | [diff] [blame] | 597 | final long macStart = encryptionParams.getAuthenticatedDataStart(); |
| 598 | if (macStart >= Integer.MAX_VALUE) { |
| 599 | throw new IOException("macStart >= Integer.MAX_VALUE"); |
| 600 | } |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 601 | |
Kenny Root | 103d530 | 2012-05-10 10:21:06 -0700 | [diff] [blame] | 602 | final long furtherOffset; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 603 | if (macStart >= 0 && encStart >= 0 && macStart < encStart) { |
| 604 | /* |
| 605 | * If there is authenticated data at the beginning, read |
| 606 | * that into our MAC first. |
| 607 | */ |
Kenny Root | 103d530 | 2012-05-10 10:21:06 -0700 | [diff] [blame] | 608 | final long authenticatedLengthLong = encStart - macStart; |
| 609 | if (authenticatedLengthLong > MAX_AUTHENTICATED_DATA_SIZE) { |
| 610 | throw new IOException("authenticated data is too long"); |
| 611 | } |
| 612 | final int authenticatedLength = (int) authenticatedLengthLong; |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 613 | |
Kenny Root | 103d530 | 2012-05-10 10:21:06 -0700 | [diff] [blame] | 614 | final byte[] authenticatedData = new byte[(int) authenticatedLength]; |
| 615 | |
| 616 | Streams.readFully(inStream, authenticatedData, (int) macStart, |
| 617 | authenticatedLength); |
Anonymous Coward | ceb1b0b | 2012-04-24 10:35:16 -0700 | [diff] [blame] | 618 | mac.update(authenticatedData, 0, authenticatedLength); |
| 619 | |
| 620 | furtherOffset = 0; |
| 621 | } else { |
| 622 | /* |
| 623 | * No authenticated data at the beginning. Just skip the |
| 624 | * required number of bytes to the beginning of the stream. |
| 625 | */ |
| 626 | if (encStart > 0) { |
| 627 | furtherOffset = encStart; |
| 628 | } else { |
| 629 | furtherOffset = 0; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * If there is data at the end of the stream we want to ignore, |
| 635 | * wrap this in a LimitedLengthInputStream. |
| 636 | */ |
| 637 | if (furtherOffset >= 0 && end > furtherOffset) { |
| 638 | inStream = new LimitedLengthInputStream(inStream, furtherOffset, end - encStart); |
| 639 | } else if (furtherOffset > 0) { |
| 640 | inStream.skip(furtherOffset); |
| 641 | } |
| 642 | |
| 643 | mAuthenticatedStream = new MacAuthenticatedInputStream(inStream, mac); |
| 644 | |
| 645 | inStream = mAuthenticatedStream; |
| 646 | } else { |
| 647 | if (encStart >= 0) { |
| 648 | if (end > encStart) { |
| 649 | inStream = new LimitedLengthInputStream(inStream, encStart, end - encStart); |
| 650 | } else { |
| 651 | inStream.skip(encStart); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | return new CipherInputStream(inStream, c); |
| 657 | } |
| 658 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 659 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 660 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 661 | private static final int PREFER_INTERNAL = 1; |
| 662 | private static final int PREFER_EXTERNAL = 2; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 663 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 664 | private int recommendAppInstallLocation(int installLocation, String archiveFilePath, int flags, |
| 665 | long threshold) { |
| 666 | int prefer; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 667 | boolean checkBoth = false; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 668 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 669 | final boolean isForwardLocked = (flags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 670 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 671 | check_inner : { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 672 | /* |
| 673 | * Explicit install flags should override the manifest settings. |
| 674 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 675 | if ((flags & PackageManager.INSTALL_INTERNAL) != 0) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 676 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 677 | break check_inner; |
| 678 | } else if ((flags & PackageManager.INSTALL_EXTERNAL) != 0) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 679 | prefer = PREFER_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 680 | break check_inner; |
| 681 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 682 | |
| 683 | /* No install flags. Check for manifest option. */ |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 684 | if (installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 685 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 686 | break check_inner; |
| 687 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 688 | prefer = PREFER_EXTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 689 | checkBoth = true; |
| 690 | break check_inner; |
| 691 | } else if (installLocation == PackageInfo.INSTALL_LOCATION_AUTO) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 692 | // We default to preferring internal storage. |
| 693 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 694 | checkBoth = true; |
| 695 | break check_inner; |
| 696 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 697 | |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 698 | // Pick user preference |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 699 | int installPreference = Settings.Global.getInt(getApplicationContext() |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 700 | .getContentResolver(), |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 701 | Settings.Global.DEFAULT_INSTALL_LOCATION, |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 702 | PackageHelper.APP_INSTALL_AUTO); |
| 703 | if (installPreference == PackageHelper.APP_INSTALL_INTERNAL) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 704 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 705 | break check_inner; |
| 706 | } else if (installPreference == PackageHelper.APP_INSTALL_EXTERNAL) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 707 | prefer = PREFER_EXTERNAL; |
Suchi Amalapurapu | 40e4725 | 2010-04-07 16:15:50 -0700 | [diff] [blame] | 708 | break check_inner; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 709 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 710 | |
| 711 | /* |
| 712 | * Fall back to default policy of internal-only if nothing else is |
| 713 | * specified. |
| 714 | */ |
| 715 | prefer = PREFER_INTERNAL; |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 718 | final boolean emulated = Environment.isExternalStorageEmulated(); |
| 719 | |
| 720 | final File apkFile = new File(archiveFilePath); |
| 721 | |
| 722 | boolean fitsOnInternal = false; |
| 723 | if (checkBoth || prefer == PREFER_INTERNAL) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 724 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 725 | fitsOnInternal = isUnderInternalThreshold(apkFile, isForwardLocked, threshold); |
| 726 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 727 | return PackageHelper.RECOMMEND_FAILED_INVALID_URI; |
| 728 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 731 | boolean fitsOnSd = false; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 732 | if (!emulated && (checkBoth || prefer == PREFER_EXTERNAL)) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 733 | try { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 734 | fitsOnSd = isUnderExternalThreshold(apkFile, isForwardLocked); |
| 735 | } catch (IOException e) { |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 736 | return PackageHelper.RECOMMEND_FAILED_INVALID_URI; |
| 737 | } |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 738 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 739 | |
| 740 | if (prefer == PREFER_INTERNAL) { |
| 741 | if (fitsOnInternal) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 742 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
| 743 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 744 | } else if (!emulated && prefer == PREFER_EXTERNAL) { |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 745 | if (fitsOnSd) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 746 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 747 | } |
| 748 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 749 | |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 750 | if (checkBoth) { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 751 | if (fitsOnInternal) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 752 | return PackageHelper.RECOMMEND_INSTALL_INTERNAL; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 753 | } else if (!emulated && fitsOnSd) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 754 | return PackageHelper.RECOMMEND_INSTALL_EXTERNAL; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 757 | |
| 758 | /* |
| 759 | * If they requested to be on the external media by default, return that |
| 760 | * the media was unavailable. Otherwise, indicate there was insufficient |
| 761 | * storage space available. |
| 762 | */ |
| 763 | if (!emulated && (checkBoth || prefer == PREFER_EXTERNAL) |
| 764 | && !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { |
Suchi Amalapurapu | 14b6abd | 2010-03-17 08:37:04 -0700 | [diff] [blame] | 765 | return PackageHelper.RECOMMEND_MEDIA_UNAVAILABLE; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 766 | } else { |
| 767 | return PackageHelper.RECOMMEND_FAILED_INSUFFICIENT_STORAGE; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 768 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 771 | /** |
| 772 | * Measure a file to see if it fits within the free space threshold. |
| 773 | * |
| 774 | * @param apkFile file to check |
| 775 | * @param threshold byte threshold to compare against |
| 776 | * @return true if file fits under threshold |
| 777 | * @throws FileNotFoundException when APK does not exist |
| 778 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 779 | private boolean isUnderInternalThreshold(File apkFile, boolean isForwardLocked, long threshold) |
| 780 | throws IOException { |
| 781 | long size = apkFile.length(); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 782 | if (size == 0 && !apkFile.exists()) { |
| 783 | throw new FileNotFoundException(); |
| 784 | } |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 785 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 786 | if (isForwardLocked) { |
| 787 | size += PackageHelper.extractPublicFiles(apkFile.getAbsolutePath(), null); |
| 788 | } |
| 789 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 790 | final StatFs internalStats = new StatFs(Environment.getDataDirectory().getPath()); |
| 791 | final long availInternalSize = (long) internalStats.getAvailableBlocks() |
| 792 | * (long) internalStats.getBlockSize(); |
| 793 | |
| 794 | return (availInternalSize - size) > threshold; |
| 795 | } |
| 796 | |
| 797 | |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 798 | /** |
| 799 | * Measure a file to see if it fits in the external free space. |
| 800 | * |
| 801 | * @param apkFile file to check |
| 802 | * @return true if file fits |
| 803 | * @throws IOException when file does not exist |
| 804 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 805 | private boolean isUnderExternalThreshold(File apkFile, boolean isForwardLocked) |
| 806 | throws IOException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 807 | if (Environment.isExternalStorageEmulated()) { |
| 808 | return false; |
| 809 | } |
| 810 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 811 | final int sizeMb = calculateContainerSize(apkFile, isForwardLocked); |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 812 | |
| 813 | final int availSdMb; |
| 814 | if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { |
Kenny Root | 61942c5 | 2011-08-15 12:46:04 -0700 | [diff] [blame] | 815 | final StatFs sdStats = new StatFs(Environment.getExternalStorageDirectory().getPath()); |
| 816 | final int blocksToMb = (1 << 20) / sdStats.getBlockSize(); |
| 817 | availSdMb = sdStats.getAvailableBlocks() * blocksToMb; |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 818 | } else { |
| 819 | availSdMb = -1; |
| 820 | } |
| 821 | |
| 822 | return availSdMb > sizeMb; |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Calculate the container size for an APK. Takes into account the |
| 827 | * |
| 828 | * @param apkFile file from which to calculate size |
| 829 | * @return size in megabytes (2^20 bytes) |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 830 | * @throws IOException when there is a problem reading the file |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 831 | */ |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 832 | private int calculateContainerSize(File apkFile, boolean forwardLocked) throws IOException { |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 833 | // Calculate size of container needed to hold base APK. |
| 834 | long sizeBytes = apkFile.length(); |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 835 | if (sizeBytes == 0 && !apkFile.exists()) { |
| 836 | throw new FileNotFoundException(); |
| 837 | } |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 838 | |
| 839 | // Check all the native files that need to be copied and add that to the |
| 840 | // container size. |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 841 | sizeBytes += NativeLibraryHelper.sumNativeBinariesLI(apkFile); |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 842 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 843 | if (forwardLocked) { |
| 844 | sizeBytes += PackageHelper.extractPublicFiles(apkFile.getPath(), null); |
| 845 | } |
| 846 | |
Kenny Root | 62e1b4e | 2011-03-14 17:13:39 -0700 | [diff] [blame] | 847 | int sizeMb = (int) (sizeBytes >> 20); |
| 848 | if ((sizeBytes - (sizeMb * 1024 * 1024)) > 0) { |
| 849 | sizeMb++; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Add buffer size because we don't have a good way to determine the |
| 854 | * real FAT size. Your FAT size varies with how many directory entries |
| 855 | * you need, how big the whole filesystem is, and other such headaches. |
| 856 | */ |
| 857 | sizeMb++; |
| 858 | |
| 859 | return sizeMb; |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 860 | } |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 861 | } |