1 /*
2 * Copyright (c) 2023-2024 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 "zip_file_reader.h"
17
18 #include <memory>
19 #include <sys/stat.h>
20
21 #include "ability_base_log_wrapper.h"
22 #include "constants.h"
23 #include "hitrace_meter.h"
24 #include "zip_file_reader_io.h"
25 #include "zip_file_reader_mem.h"
26
27 namespace OHOS {
28 namespace AbilityBase {
29 constexpr size_t MEM_MAX_FILE_SIZE = 1u;
30
CreateZipFileReader(const std::string & filePath)31 std::shared_ptr<ZipFileReader> ZipFileReader::CreateZipFileReader(const std::string &filePath)
32 {
33 size_t fileSize = GetFileLen(filePath);
34 if (fileSize == 0) {
35 ABILITYBASE_LOGE("fileSize error: %{public}s", filePath.c_str());
36 return nullptr;
37 }
38
39 std::shared_ptr<ZipFileReader> result;
40 if (fileSize <= MEM_MAX_FILE_SIZE) {
41 result = std::make_shared<ZipFileReaderMem>(filePath);
42 } else {
43 result = std::make_shared<ZipFileReaderIo>(filePath);
44 }
45
46 result->fileLen_ = fileSize;
47 if (result->init()) {
48 return result;
49 }
50 return nullptr;
51 }
52
~ZipFileReader()53 ZipFileReader::~ZipFileReader()
54 {
55 if (fd_ >= 0 && closable_) {
56 close(fd_);
57 fd_ = -1;
58 }
59 }
60
GetFileLen(const std::string & filePath)61 size_t ZipFileReader::GetFileLen(const std::string &filePath)
62 {
63 if (filePath.empty()) {
64 return 0;
65 }
66
67 struct stat fileStat{};
68 if (stat(filePath.c_str(), &fileStat) == 0) {
69 return fileStat.st_size;
70 }
71
72 ABILITYBASE_LOGE("error: %{public}s : %{public}d", filePath.c_str(), errno);
73 return 0;
74 }
75
init()76 bool ZipFileReader::init()
77 {
78 if (filePath_.empty()) {
79 return false;
80 }
81 std::string resolvePath;
82 resolvePath.reserve(PATH_MAX);
83 resolvePath.resize(PATH_MAX - 1);
84 if (filePath_.substr(0, Constants::GetProcPrefix().size()) == Constants::GetProcPrefix()) {
85 resolvePath = filePath_;
86 } else {
87 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "realpath_file");
88 if (realpath(filePath_.c_str(), &(resolvePath[0])) == nullptr) {
89 ABILITYBASE_LOGE("realpath error: %{public}s : %{public}d", resolvePath.c_str(), errno);
90 return false;
91 }
92 }
93 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "open_file");
94 fd_ = open(resolvePath.c_str(), O_RDONLY);
95 if (fd_ < 0) {
96 ABILITYBASE_LOGE("open file error: %{public}s : %{public}d", resolvePath.c_str(), errno);
97 return false;
98 }
99
100 return true;
101 }
102 }
103 }