blob: c5e7d9de37164d6564435e35ded83df9f2082203 [file] [log] [blame]
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -08001/*
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
17package com.android.internal.content;
18
Kenny Root2782a472012-04-14 21:48:21 -070019import android.os.FileUtils;
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080020import android.os.IBinder;
21import android.os.RemoteException;
22import android.os.ServiceManager;
Kenny Root2782a472012-04-14 21:48:21 -070023import android.os.storage.IMountService;
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080024import android.os.storage.StorageResultCode;
25import android.util.Log;
26
27import java.io.File;
Kenny Root2782a472012-04-14 21:48:21 -070028import java.io.FileOutputStream;
29import java.io.IOException;
30import java.io.InputStream;
31import java.util.Collections;
32import java.util.zip.ZipEntry;
33import java.util.zip.ZipFile;
34import java.util.zip.ZipOutputStream;
35
36import libcore.io.IoUtils;
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080037
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -080038/**
39 * Constants used internally between the PackageManager
40 * and media container service transports.
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080041 * Some utility methods to invoke MountService api.
Suchi Amalapurapu5b993ce2010-02-12 09:43:29 -080042 */
43public 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 Amalapurapua2b6c372010-03-05 17:40:11 -080048 public static final int RECOMMEND_FAILED_INVALID_LOCATION = -3;
49 public static final int RECOMMEND_FAILED_ALREADY_EXISTS = -4;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080050 public static final int RECOMMEND_MEDIA_UNAVAILABLE = -5;
Kenny Root1ebd74a2011-08-03 15:09:44 -070051 public static final int RECOMMEND_FAILED_INVALID_URI = -6;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070052 public static final int RECOMMEND_FAILED_VERSION_DOWNGRADE = -7;
Kenny Root1ebd74a2011-08-03 15:09:44 -070053
Suchi Amalapurapucf6eaea2010-02-23 19:37:45 -080054 private static final boolean localLOGV = true;
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080055 private static final String TAG = "PackageHelper";
Suchi Amalapurapu089262d2010-03-10 14:19:21 -080056 // 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 Amalapurapu679bba32010-02-16 11:52:44 -080060
Kenny Root07ba2ae2012-05-09 09:14:28 -070061 public static IMountService getMountService() throws RemoteException {
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080062 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 Root07ba2ae2012-05-09 09:14:28 -070067 throw new RemoteException("Could not contact mount service");
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080068 }
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080069 }
70
Kenny Root6dceb882012-04-12 14:23:49 -070071 public static String createSdDir(int sizeMb, String cid, String sdEncKey, int uid,
72 boolean isExternal) {
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080073 // Create mount point via MountService
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080074 try {
Kenny Root07ba2ae2012-05-09 09:14:28 -070075 IMountService mountService = getMountService();
76
77 if (localLOGV)
78 Log.i(TAG, "Size of container " + sizeMb + " MB");
79
Kenny Root6dceb882012-04-12 14:23:49 -070080 int rc = mountService.createSecureContainer(cid, sizeMb, "ext4", sdEncKey, uid,
81 isExternal);
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080082 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 Amalapurapucf6eaea2010-02-23 19:37:45 -080087 if (localLOGV) Log.i(TAG, "Created secure container " + cid +
Suchi Amalapurapu679bba32010-02-16 11:52:44 -080088 " 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 Amalapurapucf6eaea2010-02-23 19:37:45 -0800112 int rc = getMountService().unmountSecureContainer(cid, true);
Suchi Amalapurapu679bba32010-02-16 11:52:44 -0800113 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 Hackborn292f8bc2011-06-27 16:27:41 -0700150 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 Amalapurapu679bba32010-02-16 11:52:44 -0800160 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 Amalapurapucf6eaea2010-02-23 19:37:45 -0800177 if (localLOGV) Log.i(TAG, "Forcibly destroying container " + cid);
178 int rc = getMountService().destroySecureContainer(cid, true);
Suchi Amalapurapu679bba32010-02-16 11:52:44 -0800179 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 Root2782a472012-04-14 21:48:21 -0700209
Kenny Root6dceb882012-04-12 14:23:49 -0700210 public static int extractPublicFiles(String packagePath, File publicZipFile)
Kenny Root2782a472012-04-14 21:48:21 -0700211 throws IOException {
Kenny Root6dceb882012-04-12 14:23:49 -0700212 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 Root2782a472012-04-14 21:48:21 -0700225 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 Root6dceb882012-04-12 14:23:49 -0700234 size += zipEntry.getSize();
235 if (publicZipFile != null) {
236 copyZipEntry(zipEntry, privateZip, publicZipOutStream);
237 }
Kenny Root2782a472012-04-14 21:48:21 -0700238 }
239 }
240 } finally {
Kenny Root6dceb882012-04-12 14:23:49 -0700241 try { privateZip.close(); } catch (IOException e) {}
Kenny Root2782a472012-04-14 21:48:21 -0700242 }
243
Kenny Root6dceb882012-04-12 14:23:49 -0700244 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 Root2782a472012-04-14 21:48:21 -0700252 } finally {
253 IoUtils.closeQuietly(publicZipOutStream);
254 }
Kenny Root6dceb882012-04-12 14:23:49 -0700255
256 return size;
Kenny Root2782a472012-04-14 21:48:21 -0700257 }
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 Root6dceb882012-04-12 14:23:49 -0700284
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 Amalapurapu5b993ce2010-02-12 09:43:29 -0800298}