• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ipc/installd_host.h"
17 
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_constants.h"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
InstalldHost()26 InstalldHost::InstalldHost()
27 {
28     init();
29     APP_LOGI("installd host instance is created");
30 }
31 
~InstalldHost()32 InstalldHost::~InstalldHost()
33 {
34     APP_LOGI("installd host instance is destroyed");
35 }
36 
init()37 void InstalldHost::init()
38 {
39     funcMap_.emplace(IInstalld::Message::CREATE_BUNDLE_DIR, &InstalldHost::HandleCreateBundleDir);
40     funcMap_.emplace(IInstalld::Message::EXTRACT_MODULE_FILES, &InstalldHost::HandleExtractModuleFiles);
41     funcMap_.emplace(IInstalld::Message::RENAME_MODULE_DIR, &InstalldHost::HandleRenameModuleDir);
42     funcMap_.emplace(IInstalld::Message::CREATE_BUNDLE_DATA_DIR, &InstalldHost::HandleCreateBundleDataDir);
43     funcMap_.emplace(IInstalld::Message::REMOVE_BUNDLE_DATA_DIR, &InstalldHost::HandleRemoveBundleDataDir);
44     funcMap_.emplace(IInstalld::Message::REMOVE_MODULE_DATA_DIR, &InstalldHost::HandleRemoveModuleDataDir);
45     funcMap_.emplace(IInstalld::Message::CLEAN_BUNDLE_DATA_DIR, &InstalldHost::HandleCleanBundleDataDir);
46     funcMap_.emplace(IInstalld::Message::SET_DIR_APL, &InstalldHost::HandleSetDirApl);
47     funcMap_.emplace(IInstalld::Message::REMOVE_DIR, &InstalldHost::HandleRemoveDir);
48     funcMap_.emplace(IInstalld::Message::GET_BUNDLE_STATS, &InstalldHost::HandleGetBundleStats);
49     funcMap_.emplace(IInstalld::Message::GET_BUNDLE_CACHE_PATH, &InstalldHost::HandleGetBundleCachePath);
50     funcMap_.emplace(IInstalld::Message::SCAN_DIR, &InstalldHost::HandleScanDir);
51     funcMap_.emplace(IInstalld::Message::MOVE_FILE, &InstalldHost::HandleMoveFile);
52     funcMap_.emplace(IInstalld::Message::COPY_FILE, &InstalldHost::HandleCopyFile);
53     funcMap_.emplace(IInstalld::Message::MKDIR, &InstalldHost::HandleMkdir);
54     funcMap_.emplace(IInstalld::Message::GET_FILE_STAT, &InstalldHost::HandleGetFileStat);
55     funcMap_.emplace(IInstalld::Message::EXTRACT_DIFF_FILES, &InstalldHost::HandleExtractDiffFiles);
56     funcMap_.emplace(IInstalld::Message::APPLY_DIFF_PATCH, &InstalldHost::HandleApplyDiffPatch);
57     funcMap_.emplace(IInstalld::Message::IS_EXIST_DIR, &InstalldHost::HandleIsExistDir);
58     funcMap_.emplace(IInstalld::Message::IS_DIR_EMPTY, &InstalldHost::HandleIsDirEmpty);
59     funcMap_.emplace(IInstalld::Message::OBTAIN_QUICK_FIX_DIR, &InstalldHost::HandObtainQuickFixFileDir);
60     funcMap_.emplace(IInstalld::Message::COPY_FILES, &InstalldHost::HandCopyFiles);
61     funcMap_.emplace(IInstalld::Message::EXTRACT_FILES, &InstalldHost::HandleExtractFiles);
62 }
63 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)64 int InstalldHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
65 {
66     APP_LOGD(
67         "installd host receives message from client, code = %{public}d, flags = %{public}d", code, option.GetFlags());
68     std::u16string descripter = InstalldHost::GetDescriptor();
69     std::u16string remoteDescripter = data.ReadInterfaceToken();
70     if (descripter != remoteDescripter) {
71         APP_LOGE("installd host fail to write reply message due to the reply is nullptr");
72         return OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
73     }
74     bool result = true;
75     APP_LOGD("funcMap_ size is %{public}d", static_cast<int32_t>(funcMap_.size()));
76     if (funcMap_.find(code) != funcMap_.end() && funcMap_[code] != nullptr) {
77         result = (this->*funcMap_[code])(data, reply);
78     } else {
79         APP_LOGW("installd host receives unknown code, code = %{public}u", code);
80         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81     }
82     APP_LOGD("installd host finish to process message from client");
83     return result ? NO_ERROR : OHOS::ERR_APPEXECFWK_PARCEL_ERROR;
84 }
85 
HandleCreateBundleDir(MessageParcel & data,MessageParcel & reply)86 bool InstalldHost::HandleCreateBundleDir(MessageParcel &data, MessageParcel &reply)
87 {
88     std::string bundleDir = Str16ToStr8(data.ReadString16());
89     APP_LOGI("bundleName %{public}s", bundleDir.c_str());
90     ErrCode result = CreateBundleDir(bundleDir);
91     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
92     return true;
93 }
94 
HandleExtractModuleFiles(MessageParcel & data,MessageParcel & reply)95 bool InstalldHost::HandleExtractModuleFiles(MessageParcel &data, MessageParcel &reply)
96 {
97     std::string srcModulePath = Str16ToStr8(data.ReadString16());
98     std::string targetPath = Str16ToStr8(data.ReadString16());
99     std::string targetSoPath = Str16ToStr8(data.ReadString16());
100     std::string cpuAbi = Str16ToStr8(data.ReadString16());
101     APP_LOGI("extract module %{private}s", targetPath.c_str());
102     ErrCode result = ExtractModuleFiles(srcModulePath, targetPath, targetSoPath, cpuAbi);
103     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
104     return true;
105 }
106 
HandleExtractFiles(MessageParcel & data,MessageParcel & reply)107 bool InstalldHost::HandleExtractFiles(MessageParcel &data, MessageParcel &reply)
108 {
109     std::unique_ptr<ExtractParam> info(data.ReadParcelable<ExtractParam>());
110     if (info == nullptr) {
111         APP_LOGE("readParcelableInfo failed");
112         return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
113     }
114 
115     ErrCode result = ExtractFiles(*info);
116     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
117     return true;
118 }
119 
HandleRenameModuleDir(MessageParcel & data,MessageParcel & reply)120 bool InstalldHost::HandleRenameModuleDir(MessageParcel &data, MessageParcel &reply)
121 {
122     std::string oldPath = Str16ToStr8(data.ReadString16());
123     std::string newPath = Str16ToStr8(data.ReadString16());
124     APP_LOGI("rename moduleDir %{private}s", oldPath.c_str());
125     ErrCode result = RenameModuleDir(oldPath, newPath);
126     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
127     return true;
128 }
129 
HandleCreateBundleDataDir(MessageParcel & data,MessageParcel & reply)130 bool InstalldHost::HandleCreateBundleDataDir(MessageParcel &data, MessageParcel &reply)
131 {
132     std::string bundleName = Str16ToStr8(data.ReadString16());
133     int userid = data.ReadInt32();
134     int uid = data.ReadInt32();
135     int gid = data.ReadInt32();
136     std::string apl = Str16ToStr8(data.ReadString16());
137     ErrCode result = CreateBundleDataDir(bundleName, userid, uid, gid, apl);
138     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
139     return true;
140 }
141 
HandleRemoveBundleDataDir(MessageParcel & data,MessageParcel & reply)142 bool InstalldHost::HandleRemoveBundleDataDir(MessageParcel &data, MessageParcel &reply)
143 {
144     std::string bundleName = Str16ToStr8(data.ReadString16());
145     int userid = data.ReadInt32();
146     ErrCode result = RemoveBundleDataDir(bundleName, userid);
147     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
148     return true;
149 }
150 
HandleRemoveModuleDataDir(MessageParcel & data,MessageParcel & reply)151 bool InstalldHost::HandleRemoveModuleDataDir(MessageParcel &data, MessageParcel &reply)
152 {
153     std::string moduleNmae = Str16ToStr8(data.ReadString16());
154     int userid = data.ReadInt32();
155     ErrCode result = RemoveModuleDataDir(moduleNmae, userid);
156     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
157     return true;
158 }
159 
HandleRemoveDir(MessageParcel & data,MessageParcel & reply)160 bool InstalldHost::HandleRemoveDir(MessageParcel &data, MessageParcel &reply)
161 {
162     std::string removedDir = Str16ToStr8(data.ReadString16());
163     ErrCode result = RemoveDir(removedDir);
164     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
165     return true;
166 }
167 
HandleCleanBundleDataDir(MessageParcel & data,MessageParcel & reply)168 bool InstalldHost::HandleCleanBundleDataDir(MessageParcel &data, MessageParcel &reply)
169 {
170     std::string bundleDir = Str16ToStr8(data.ReadString16());
171     ErrCode result = CleanBundleDataDir(bundleDir);
172     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
173     return true;
174 }
175 
HandleGetBundleStats(MessageParcel & data,MessageParcel & reply)176 bool InstalldHost::HandleGetBundleStats(MessageParcel &data, MessageParcel &reply)
177 {
178     std::string bundleName = Str16ToStr8(data.ReadString16());
179     int32_t userId = data.ReadInt32();
180     std::vector<int64_t> bundleStats;
181     ErrCode result = GetBundleStats(bundleName, userId, bundleStats);
182     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
183     if (!reply.WriteInt64Vector(bundleStats)) {
184         APP_LOGE("HandleGetBundleStats write failed");
185         return false;
186     }
187     return true;
188 }
189 
HandleSetDirApl(MessageParcel & data,MessageParcel & reply)190 bool InstalldHost::HandleSetDirApl(MessageParcel &data, MessageParcel &reply)
191 {
192     std::string dataDir = Str16ToStr8(data.ReadString16());
193     std::string bundleName = Str16ToStr8(data.ReadString16());
194     std::string apl = Str16ToStr8(data.ReadString16());
195     ErrCode result = SetDirApl(dataDir, bundleName, apl);
196     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
197     return true;
198 }
199 
HandleGetBundleCachePath(MessageParcel & data,MessageParcel & reply)200 bool InstalldHost::HandleGetBundleCachePath(MessageParcel &data, MessageParcel &reply)
201 {
202     std::string dir = Str16ToStr8(data.ReadString16());
203     std::vector<std::string> cachePath;
204     ErrCode result = GetBundleCachePath(dir, cachePath);
205     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
206     if (!reply.WriteStringVector(cachePath)) {
207         APP_LOGE("fail to GetBundleCachePath from reply");
208         return false;
209     }
210     return true;
211 }
212 
HandleScanDir(MessageParcel & data,MessageParcel & reply)213 bool InstalldHost::HandleScanDir(MessageParcel &data, MessageParcel &reply)
214 {
215     std::string dir = Str16ToStr8(data.ReadString16());
216     ScanMode scanMode = static_cast<ScanMode>(data.ReadInt32());
217     ResultMode resultMode = static_cast<ResultMode>(data.ReadInt32());
218     std::vector<std::string> paths;
219     ErrCode result = ScanDir(dir, scanMode, resultMode, paths);
220     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
221     if (!reply.WriteStringVector(paths)) {
222         APP_LOGE("fail to Scan from reply");
223         return false;
224     }
225 
226     return true;
227 }
228 
HandleMoveFile(MessageParcel & data,MessageParcel & reply)229 bool InstalldHost::HandleMoveFile(MessageParcel &data, MessageParcel &reply)
230 {
231     std::string oldPath = Str16ToStr8(data.ReadString16());
232     std::string newPath = Str16ToStr8(data.ReadString16());
233     ErrCode result = MoveFile(oldPath, newPath);
234     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
235     return true;
236 }
237 
HandleCopyFile(MessageParcel & data,MessageParcel & reply)238 bool InstalldHost::HandleCopyFile(MessageParcel &data, MessageParcel &reply)
239 {
240     std::string oldPath = Str16ToStr8(data.ReadString16());
241     std::string newPath = Str16ToStr8(data.ReadString16());
242     ErrCode result = CopyFile(oldPath, newPath);
243     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
244     return true;
245 }
246 
HandleMkdir(MessageParcel & data,MessageParcel & reply)247 bool InstalldHost::HandleMkdir(MessageParcel &data, MessageParcel &reply)
248 {
249     std::string dir = Str16ToStr8(data.ReadString16());
250     int32_t mode = data.ReadInt32();
251     int32_t uid = data.ReadInt32();
252     int32_t gid = data.ReadInt32();
253     ErrCode result = Mkdir(dir, mode, uid, gid);
254     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
255     return true;
256 }
257 
HandleGetFileStat(MessageParcel & data,MessageParcel & reply)258 bool InstalldHost::HandleGetFileStat(MessageParcel &data, MessageParcel &reply)
259 {
260     std::string file = Str16ToStr8(data.ReadString16());
261     FileStat fileStat;
262     ErrCode result = GetFileStat(file, fileStat);
263     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
264     if (!reply.WriteParcelable(&fileStat)) {
265         APP_LOGE("fail to GetFileStat from reply");
266         return false;
267     }
268 
269     return true;
270 }
271 
HandleExtractDiffFiles(MessageParcel & data,MessageParcel & reply)272 bool InstalldHost::HandleExtractDiffFiles(MessageParcel &data, MessageParcel &reply)
273 {
274     std::string filePath = Str16ToStr8(data.ReadString16());
275     std::string targetPath = Str16ToStr8(data.ReadString16());
276     std::string cpuAbi = Str16ToStr8(data.ReadString16());
277     ErrCode result = ExtractDiffFiles(filePath, targetPath, cpuAbi);
278     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
279     return true;
280 }
281 
HandleApplyDiffPatch(MessageParcel & data,MessageParcel & reply)282 bool InstalldHost::HandleApplyDiffPatch(MessageParcel &data, MessageParcel &reply)
283 {
284     std::string oldSoPath = Str16ToStr8(data.ReadString16());
285     std::string diffFilePath = Str16ToStr8(data.ReadString16());
286     std::string newSoPath = Str16ToStr8(data.ReadString16());
287     ErrCode result = ApplyDiffPatch(oldSoPath, diffFilePath, newSoPath);
288     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
289     return true;
290 }
291 
HandleIsExistDir(MessageParcel & data,MessageParcel & reply)292 bool InstalldHost::HandleIsExistDir(MessageParcel &data, MessageParcel &reply)
293 {
294     std::string path = Str16ToStr8(data.ReadString16());
295     bool isExist = false;
296     ErrCode result = IsExistDir(path, isExist);
297     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
298     if (!reply.WriteBool(isExist)) {
299         APP_LOGE("fail to IsExistDir from reply");
300         return false;
301     }
302     return true;
303 }
304 
HandleIsDirEmpty(MessageParcel & data,MessageParcel & reply)305 bool InstalldHost::HandleIsDirEmpty(MessageParcel &data, MessageParcel &reply)
306 {
307     std::string dir = Str16ToStr8(data.ReadString16());
308     bool isDirEmpty = false;
309     ErrCode result = IsDirEmpty(dir, isDirEmpty);
310     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
311     if (!reply.WriteBool(isDirEmpty)) {
312         APP_LOGE("write isDirEmpty failed");
313         return false;
314     }
315     return true;
316 }
317 
HandObtainQuickFixFileDir(MessageParcel & data,MessageParcel & reply)318 bool InstalldHost::HandObtainQuickFixFileDir(MessageParcel &data, MessageParcel &reply)
319 {
320     std::string dir = Str16ToStr8(data.ReadString16());
321     std::vector<std::string> dirVec;
322     ErrCode result = ObtainQuickFixFileDir(dir, dirVec);
323     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
324     if ((result == ERR_OK) && !reply.WriteStringVector(dirVec)) {
325         APP_LOGE("fail to obtain quick fix file dir from reply");
326         return false;
327     }
328     return true;
329 }
330 
HandCopyFiles(MessageParcel & data,MessageParcel & reply)331 bool InstalldHost::HandCopyFiles(MessageParcel &data, MessageParcel &reply)
332 {
333     std::string sourceDir = Str16ToStr8(data.ReadString16());
334     std::string destinationDir = Str16ToStr8(data.ReadString16());
335 
336     ErrCode result = CopyFiles(sourceDir, destinationDir);
337     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, reply, result);
338     return true;
339 }
340 }  // namespace AppExecFwk
341 }  // namespace OHOS
342