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
17 #define LOG_TAG "NStorage"
18
19 #include <android/storage_manager.h>
20 #include <storage/IMountService.h>
21 #include <storage/ObbInfo.h>
22
23 #include <androidfw/ObbFile.h>
24 #include <binder/Binder.h>
25 #include <binder/IServiceManager.h>
26 #include <cutils/atomic.h>
27 #include <utils/Log.h>
28 #include <utils/RefBase.h>
29 #include <utils/String8.h>
30 #include <utils/String16.h>
31 #include <utils/Vector.h>
32 #include <utils/threads.h>
33
34 using namespace android;
35
36 struct ObbActionListener : public BnObbActionListener {
37 private:
38 sp<AStorageManager> mStorageManager;
39
40 public:
ObbActionListenerObbActionListener41 explicit ObbActionListener(AStorageManager* mgr) :
42 mStorageManager(mgr)
43 {}
44
45 virtual void onObbResult(const android::String16& filename, const int32_t nonce,
46 const int32_t state);
47 };
48
49 class ObbCallback {
50 public:
ObbCallback(int32_t _nonce,AStorageManager_obbCallbackFunc _cb,void * _data)51 ObbCallback(int32_t _nonce, AStorageManager_obbCallbackFunc _cb, void* _data)
52 : nonce(_nonce)
53 , cb(_cb)
54 , data(_data)
55 {}
56
57 int32_t nonce;
58 AStorageManager_obbCallbackFunc cb;
59 void* data;
60 };
61
62 struct AStorageManager : public RefBase {
63 protected:
64 Mutex mCallbackLock;
65 Vector<ObbCallback*> mCallbacks;
66 volatile int32_t mNextNonce;
67 sp<ObbActionListener> mObbActionListener;
68 sp<IMountService> mMountService;
69
getNextNonceAStorageManager70 int32_t getNextNonce() {
71 return android_atomic_inc(&mNextNonce);
72 }
73
registerObbCallbackAStorageManager74 ObbCallback* registerObbCallback(AStorageManager_obbCallbackFunc func, void* data) {
75 ObbCallback* cb = new ObbCallback(getNextNonce(), func, data);
76 {
77 AutoMutex _l(mCallbackLock);
78 mCallbacks.push(cb);
79 }
80 return cb;
81 }
82
getObbInfoAStorageManager83 ObbInfo* getObbInfo(char* canonicalPath) {
84 sp<ObbFile> obbFile = new ObbFile();
85 if (!obbFile->readFrom(canonicalPath)) {
86 return nullptr;
87 }
88
89 String16 fileName(obbFile->getFileName());
90 String16 packageName(obbFile->getPackageName());
91 size_t length;
92 const unsigned char* salt = obbFile->getSalt(&length);
93 return new ObbInfo(fileName, packageName,
94 obbFile->getVersion(), obbFile->getFlags(), length, salt);
95 }
96
97 public:
AStorageManagerAStorageManager98 AStorageManager()
99 {
100 }
101
initializeAStorageManager102 bool initialize() {
103 sp<IServiceManager> sm = defaultServiceManager();
104 if (sm == NULL) {
105 ALOGE("Couldn't get default ServiceManager\n");
106 return false;
107 }
108
109 mMountService = interface_cast<IMountService>(sm->getService(String16("mount")));
110 if (mMountService == NULL) {
111 ALOGE("Couldn't get connection to MountService\n");
112 return false;
113 }
114
115 mObbActionListener = new ObbActionListener(this);
116
117 return true;
118 }
119
fireCallbackAStorageManager120 void fireCallback(const char* filename, const int32_t nonce, const int32_t state) {
121 ObbCallback* target = NULL;
122 {
123 AutoMutex _l(mCallbackLock);
124 int N = mCallbacks.size();
125 for (int i = 0; i < N; i++) {
126 ObbCallback* cb = mCallbacks.editItemAt(i);
127 if (cb->nonce == nonce) {
128 target = cb;
129 mCallbacks.removeAt(i);
130 break;
131 }
132 }
133 }
134
135 if (target != NULL) {
136 target->cb(filename, state, target->data);
137 delete target;
138 } else {
139 ALOGI("Didn't find the callback handler for: %s\n", filename);
140 }
141 }
142
mountObbAStorageManager143 void mountObb(const char* rawPath, AStorageManager_obbCallbackFunc func, void* data) {
144 // Resolve path before sending to MountService
145 char canonicalPath[PATH_MAX];
146 if (realpath(rawPath, canonicalPath) == NULL) {
147 ALOGE("mountObb failed to resolve path %s: %s", rawPath, strerror(errno));
148 return;
149 }
150
151 sp<ObbInfo> obbInfo = getObbInfo(canonicalPath);
152 if (obbInfo == nullptr) {
153 ALOGE("Couldn't get obb info for %s: %s", canonicalPath, strerror(errno));
154 return;
155 }
156
157 ObbCallback* cb = registerObbCallback(func, data);
158 String16 rawPath16(rawPath);
159 String16 canonicalPath16(canonicalPath);
160 mMountService->mountObb(rawPath16, canonicalPath16, mObbActionListener, cb->nonce, obbInfo);
161 }
162
unmountObbAStorageManager163 void unmountObb(const char* filename, const bool force, AStorageManager_obbCallbackFunc func, void* data) {
164 ObbCallback* cb = registerObbCallback(func, data);
165 String16 filename16(filename);
166 mMountService->unmountObb(filename16, force, mObbActionListener, cb->nonce);
167 }
168
isObbMountedAStorageManager169 int isObbMounted(const char* filename) {
170 String16 filename16(filename);
171 return mMountService->isObbMounted(filename16);
172 }
173
getMountedObbPathAStorageManager174 const char* getMountedObbPath(const char* filename) {
175 String16 filename16(filename);
176 String16 path16;
177 if (mMountService->getMountedObbPath(filename16, path16)) {
178 return String8(path16).string();
179 } else {
180 return NULL;
181 }
182 }
183 };
184
onObbResult(const android::String16 & filename,const int32_t nonce,const int32_t state)185 void ObbActionListener::onObbResult(const android::String16& filename, const int32_t nonce, const int32_t state) {
186 mStorageManager->fireCallback(String8(filename).string(), nonce, state);
187 }
188
189
AStorageManager_new()190 AStorageManager* AStorageManager_new() {
191 sp<AStorageManager> mgr = new AStorageManager();
192 if (mgr == NULL || !mgr->initialize()) {
193 return NULL;
194 }
195 mgr->incStrong((void*)AStorageManager_new);
196 return static_cast<AStorageManager*>(mgr.get());
197 }
198
AStorageManager_delete(AStorageManager * mgr)199 void AStorageManager_delete(AStorageManager* mgr) {
200 if (mgr) {
201 mgr->decStrong((void*)AStorageManager_new);
202 }
203 }
204
AStorageManager_mountObb(AStorageManager * mgr,const char * filename,const char * key,AStorageManager_obbCallbackFunc cb,void * data)205 void AStorageManager_mountObb(AStorageManager* mgr, const char* filename, const char* key,
206 AStorageManager_obbCallbackFunc cb, void* data) {
207 if (key != nullptr && key[0] != '\0') {
208 ALOGE("mounting encrypted OBBs is no longer supported");
209 return;
210 }
211 mgr->mountObb(filename, cb, data);
212 }
213
AStorageManager_unmountObb(AStorageManager * mgr,const char * filename,const int force,AStorageManager_obbCallbackFunc cb,void * data)214 void AStorageManager_unmountObb(AStorageManager* mgr, const char* filename, const int force,
215 AStorageManager_obbCallbackFunc cb, void* data) {
216 mgr->unmountObb(filename, force != 0, cb, data);
217 }
218
AStorageManager_isObbMounted(AStorageManager * mgr,const char * filename)219 int AStorageManager_isObbMounted(AStorageManager* mgr, const char* filename) {
220 return mgr->isObbMounted(filename) != 0;
221 }
222
AStorageManager_getMountedObbPath(AStorageManager * mgr,const char * filename)223 const char* AStorageManager_getMountedObbPath(AStorageManager* mgr, const char* filename) {
224 return mgr->getMountedObbPath(filename);
225 }
226