Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
| 17 | package com.android.internal.content; |
| 18 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 19 | import android.os.FileUtils; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 20 | import android.os.IBinder; |
| 21 | import android.os.RemoteException; |
| 22 | import android.os.ServiceManager; |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 23 | import android.os.storage.IMountService; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 24 | import android.os.storage.StorageResultCode; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | import java.io.File; |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 28 | import java.io.FileOutputStream; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.util.Collections; |
| 32 | import java.util.zip.ZipEntry; |
| 33 | import java.util.zip.ZipFile; |
| 34 | import java.util.zip.ZipOutputStream; |
| 35 | |
| 36 | import libcore.io.IoUtils; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 37 | |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Constants used internally between the PackageManager |
| 40 | * and media container service transports. |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 41 | * Some utility methods to invoke MountService api. |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 42 | */ |
| 43 | public class PackageHelper { |
| 44 | public static final int RECOMMEND_INSTALL_INTERNAL = 1; |
| 45 | public static final int RECOMMEND_INSTALL_EXTERNAL = 2; |
| 46 | public static final int RECOMMEND_FAILED_INSUFFICIENT_STORAGE = -1; |
| 47 | public static final int RECOMMEND_FAILED_INVALID_APK = -2; |
Suchi Amalapurapu | a2b6c37 | 2010-03-05 17:40:11 -0800 | [diff] [blame] | 48 | public static final int RECOMMEND_FAILED_INVALID_LOCATION = -3; |
| 49 | public static final int RECOMMEND_FAILED_ALREADY_EXISTS = -4; |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 50 | public static final int RECOMMEND_MEDIA_UNAVAILABLE = -5; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 51 | public static final int RECOMMEND_FAILED_INVALID_URI = -6; |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 52 | public static final int RECOMMEND_FAILED_VERSION_DOWNGRADE = -7; |
Kenny Root | 1ebd74a | 2011-08-03 15:09:44 -0700 | [diff] [blame] | 53 | |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 54 | private static final boolean localLOGV = true; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 55 | private static final String TAG = "PackageHelper"; |
Suchi Amalapurapu | 089262d | 2010-03-10 14:19:21 -0800 | [diff] [blame] | 56 | // App installation location settings values |
| 57 | public static final int APP_INSTALL_AUTO = 0; |
| 58 | public static final int APP_INSTALL_INTERNAL = 1; |
| 59 | public static final int APP_INSTALL_EXTERNAL = 2; |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 60 | |
Kenny Root | 07ba2ae | 2012-05-09 09:14:28 -0700 | [diff] [blame] | 61 | public static IMountService getMountService() throws RemoteException { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 62 | IBinder service = ServiceManager.getService("mount"); |
| 63 | if (service != null) { |
| 64 | return IMountService.Stub.asInterface(service); |
| 65 | } else { |
| 66 | Log.e(TAG, "Can't get mount service"); |
Kenny Root | 07ba2ae | 2012-05-09 09:14:28 -0700 | [diff] [blame] | 67 | throw new RemoteException("Could not contact mount service"); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 68 | } |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 71 | public static String createSdDir(int sizeMb, String cid, String sdEncKey, int uid, |
| 72 | boolean isExternal) { |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 73 | // Create mount point via MountService |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 74 | try { |
Kenny Root | 07ba2ae | 2012-05-09 09:14:28 -0700 | [diff] [blame] | 75 | IMountService mountService = getMountService(); |
| 76 | |
| 77 | if (localLOGV) |
| 78 | Log.i(TAG, "Size of container " + sizeMb + " MB"); |
| 79 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 80 | int rc = mountService.createSecureContainer(cid, sizeMb, "ext4", sdEncKey, uid, |
| 81 | isExternal); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 82 | if (rc != StorageResultCode.OperationSucceeded) { |
| 83 | Log.e(TAG, "Failed to create secure container " + cid); |
| 84 | return null; |
| 85 | } |
| 86 | String cachePath = mountService.getSecureContainerPath(cid); |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 87 | if (localLOGV) Log.i(TAG, "Created secure container " + cid + |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 88 | " at " + cachePath); |
| 89 | return cachePath; |
| 90 | } catch (RemoteException e) { |
| 91 | Log.e(TAG, "MountService running?"); |
| 92 | } |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | public static String mountSdDir(String cid, String key, int ownerUid) { |
| 97 | try { |
| 98 | int rc = getMountService().mountSecureContainer(cid, key, ownerUid); |
| 99 | if (rc != StorageResultCode.OperationSucceeded) { |
| 100 | Log.i(TAG, "Failed to mount container " + cid + " rc : " + rc); |
| 101 | return null; |
| 102 | } |
| 103 | return getMountService().getSecureContainerPath(cid); |
| 104 | } catch (RemoteException e) { |
| 105 | Log.e(TAG, "MountService running?"); |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | public static boolean unMountSdDir(String cid) { |
| 111 | try { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 112 | int rc = getMountService().unmountSecureContainer(cid, true); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 113 | if (rc != StorageResultCode.OperationSucceeded) { |
| 114 | Log.e(TAG, "Failed to unmount " + cid + " with rc " + rc); |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } catch (RemoteException e) { |
| 119 | Log.e(TAG, "MountService running?"); |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | public static boolean renameSdDir(String oldId, String newId) { |
| 125 | try { |
| 126 | int rc = getMountService().renameSecureContainer(oldId, newId); |
| 127 | if (rc != StorageResultCode.OperationSucceeded) { |
| 128 | Log.e(TAG, "Failed to rename " + oldId + " to " + |
| 129 | newId + "with rc " + rc); |
| 130 | return false; |
| 131 | } |
| 132 | return true; |
| 133 | } catch (RemoteException e) { |
| 134 | Log.i(TAG, "Failed ot rename " + oldId + " to " + newId + |
| 135 | " with exception : " + e); |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | public static String getSdDir(String cid) { |
| 141 | try { |
| 142 | return getMountService().getSecureContainerPath(cid); |
| 143 | } catch (RemoteException e) { |
| 144 | Log.e(TAG, "Failed to get container path for " + cid + |
| 145 | " with exception " + e); |
| 146 | } |
| 147 | return null; |
| 148 | } |
| 149 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 150 | public static String getSdFilesystem(String cid) { |
| 151 | try { |
| 152 | return getMountService().getSecureContainerFilesystemPath(cid); |
| 153 | } catch (RemoteException e) { |
| 154 | Log.e(TAG, "Failed to get container path for " + cid + |
| 155 | " with exception " + e); |
| 156 | } |
| 157 | return null; |
| 158 | } |
| 159 | |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 160 | public static boolean finalizeSdDir(String cid) { |
| 161 | try { |
| 162 | int rc = getMountService().finalizeSecureContainer(cid); |
| 163 | if (rc != StorageResultCode.OperationSucceeded) { |
| 164 | Log.i(TAG, "Failed to finalize container " + cid); |
| 165 | return false; |
| 166 | } |
| 167 | return true; |
| 168 | } catch (RemoteException e) { |
| 169 | Log.e(TAG, "Failed to finalize container " + cid + |
| 170 | " with exception " + e); |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | public static boolean destroySdDir(String cid) { |
| 176 | try { |
Suchi Amalapurapu | cf6eaea | 2010-02-23 19:37:45 -0800 | [diff] [blame] | 177 | if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid); |
| 178 | int rc = getMountService().destroySecureContainer(cid, true); |
Suchi Amalapurapu | 679bba3 | 2010-02-16 11:52:44 -0800 | [diff] [blame] | 179 | if (rc != StorageResultCode.OperationSucceeded) { |
| 180 | Log.i(TAG, "Failed to destroy container " + cid); |
| 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | } catch (RemoteException e) { |
| 185 | Log.e(TAG, "Failed to destroy container " + cid + |
| 186 | " with exception " + e); |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | public static String[] getSecureContainerList() { |
| 192 | try { |
| 193 | return getMountService().getSecureContainerList(); |
| 194 | } catch (RemoteException e) { |
| 195 | Log.e(TAG, "Failed to get secure container list with exception" + |
| 196 | e); |
| 197 | } |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | public static boolean isContainerMounted(String cid) { |
| 202 | try { |
| 203 | return getMountService().isSecureContainerMounted(cid); |
| 204 | } catch (RemoteException e) { |
| 205 | Log.e(TAG, "Failed to find out if container " + cid + " mounted"); |
| 206 | } |
| 207 | return false; |
| 208 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 209 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 210 | public static int extractPublicFiles(String packagePath, File publicZipFile) |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 211 | throws IOException { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 212 | final FileOutputStream fstr; |
| 213 | final ZipOutputStream publicZipOutStream; |
| 214 | |
| 215 | if (publicZipFile == null) { |
| 216 | fstr = null; |
| 217 | publicZipOutStream = null; |
| 218 | } else { |
| 219 | fstr = new FileOutputStream(publicZipFile); |
| 220 | publicZipOutStream = new ZipOutputStream(fstr); |
| 221 | } |
| 222 | |
| 223 | int size = 0; |
| 224 | |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 225 | try { |
| 226 | final ZipFile privateZip = new ZipFile(packagePath); |
| 227 | try { |
| 228 | // Copy manifest, resources.arsc and res directory to public zip |
| 229 | for (final ZipEntry zipEntry : Collections.list(privateZip.entries())) { |
| 230 | final String zipEntryName = zipEntry.getName(); |
| 231 | if ("AndroidManifest.xml".equals(zipEntryName) |
| 232 | || "resources.arsc".equals(zipEntryName) |
| 233 | || zipEntryName.startsWith("res/")) { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 234 | size += zipEntry.getSize(); |
| 235 | if (publicZipFile != null) { |
| 236 | copyZipEntry(zipEntry, privateZip, publicZipOutStream); |
| 237 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } finally { |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 241 | try { privateZip.close(); } catch (IOException e) {} |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 244 | if (publicZipFile != null) { |
| 245 | publicZipOutStream.finish(); |
| 246 | publicZipOutStream.flush(); |
| 247 | FileUtils.sync(fstr); |
| 248 | publicZipOutStream.close(); |
| 249 | FileUtils.setPermissions(publicZipFile.getAbsolutePath(), FileUtils.S_IRUSR |
| 250 | | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IROTH, -1, -1); |
| 251 | } |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 252 | } finally { |
| 253 | IoUtils.closeQuietly(publicZipOutStream); |
| 254 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 255 | |
| 256 | return size; |
Kenny Root | 2782a47 | 2012-04-14 21:48:21 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | private static void copyZipEntry(ZipEntry zipEntry, ZipFile inZipFile, |
| 260 | ZipOutputStream outZipStream) throws IOException { |
| 261 | byte[] buffer = new byte[4096]; |
| 262 | int num; |
| 263 | |
| 264 | ZipEntry newEntry; |
| 265 | if (zipEntry.getMethod() == ZipEntry.STORED) { |
| 266 | // Preserve the STORED method of the input entry. |
| 267 | newEntry = new ZipEntry(zipEntry); |
| 268 | } else { |
| 269 | // Create a new entry so that the compressed len is recomputed. |
| 270 | newEntry = new ZipEntry(zipEntry.getName()); |
| 271 | } |
| 272 | outZipStream.putNextEntry(newEntry); |
| 273 | |
| 274 | final InputStream data = inZipFile.getInputStream(zipEntry); |
| 275 | try { |
| 276 | while ((num = data.read(buffer)) > 0) { |
| 277 | outZipStream.write(buffer, 0, num); |
| 278 | } |
| 279 | outZipStream.flush(); |
| 280 | } finally { |
| 281 | IoUtils.closeQuietly(data); |
| 282 | } |
| 283 | } |
Kenny Root | 6dceb88 | 2012-04-12 14:23:49 -0700 | [diff] [blame] | 284 | |
| 285 | public static boolean fixSdPermissions(String cid, int gid, String filename) { |
| 286 | try { |
| 287 | int rc = getMountService().fixPermissionsSecureContainer(cid, gid, filename); |
| 288 | if (rc != StorageResultCode.OperationSucceeded) { |
| 289 | Log.i(TAG, "Failed to fixperms container " + cid); |
| 290 | return false; |
| 291 | } |
| 292 | return true; |
| 293 | } catch (RemoteException e) { |
| 294 | Log.e(TAG, "Failed to fixperms container " + cid + " with exception " + e); |
| 295 | } |
| 296 | return false; |
| 297 | } |
Suchi Amalapurapu | 5b993ce | 2010-02-12 09:43:29 -0800 | [diff] [blame] | 298 | } |