1 /*
2 * Copyright (c) 2021 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 "bundle_util.h"
17
18 #include <sys/stat.h>
19 #include <unistd.h>
20
21 #include "string_ex.h"
22 #include "directory_ex.h"
23 #include "app_log_wrapper.h"
24 #include "bundle_constants.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28
CheckFilePath(const std::string & bundlePath,std::string & realPath)29 ErrCode BundleUtil::CheckFilePath(const std::string &bundlePath, std::string &realPath)
30 {
31 if (!CheckFileName(bundlePath)) {
32 APP_LOGE("bundle file path invalid");
33 return ERR_APPEXECFWK_INSTALL_FILE_PATH_INVALID;
34 }
35 if (!CheckFileType(bundlePath, Constants::INSTALL_FILE_SUFFIX)) {
36 APP_LOGE("file is not hap");
37 return ERR_APPEXECFWK_INSTALL_INVALID_HAP_NAME;
38 }
39 if (!PathToRealPath(bundlePath, realPath)) {
40 APP_LOGE("file is not real path");
41 return ERR_APPEXECFWK_INSTALL_FILE_PATH_INVALID;
42 }
43 if (access(realPath.c_str(), F_OK) != 0) {
44 APP_LOGE("can not access the bundle file path: %{private}s", realPath.c_str());
45 return ERR_APPEXECFWK_INSTALL_INVALID_BUNDLE_FILE;
46 }
47 if (!CheckFileSize(realPath, Constants::MAX_HAP_SIZE)) {
48 APP_LOGE("file size is larger than max size Max size is: %{public}d", Constants::MAX_HAP_SIZE);
49 return ERR_APPEXECFWK_INSTALL_INVALID_HAP_SIZE;
50 }
51 return ERR_OK;
52 }
53
CheckFileType(const std::string & fileName,const std::string & extensionName)54 bool BundleUtil::CheckFileType(const std::string &fileName, const std::string &extensionName)
55 {
56 APP_LOGD("path is %{public}s, support suffix is %{public}s", fileName.c_str(), extensionName.c_str());
57 if (!CheckFileName(fileName)) {
58 return false;
59 }
60
61 auto position = fileName.rfind('.');
62 if (position == std::string::npos) {
63 APP_LOGE("filename no extension name");
64 return false;
65 }
66
67 std::string suffixStr = fileName.substr(position);
68 return LowerStr(suffixStr) == extensionName;
69 }
70
CheckFileName(const std::string & fileName)71 bool BundleUtil::CheckFileName(const std::string &fileName)
72 {
73 if (fileName.empty()) {
74 APP_LOGE("the file name is empty");
75 return false;
76 }
77 if (fileName.size() > Constants::PATH_MAX_SIZE) {
78 APP_LOGE("bundle file path length %{public}zu too long", fileName.size());
79 return false;
80 }
81 return true;
82 }
83
CheckFileSize(const std::string & bundlePath,const int32_t fileSize)84 bool BundleUtil::CheckFileSize(const std::string &bundlePath, const int32_t fileSize)
85 {
86 struct stat fileInfo = { 0 };
87 if (stat(bundlePath.c_str(), &fileInfo) != 0) {
88 APP_LOGE("call stat error");
89 return false;
90 }
91 if (fileInfo.st_size > fileSize) {
92 return false;
93 }
94 return true;
95 }
96
97 } // namespace AppExecFwk
98 } // namespace OHOS
99