• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "write_zip_file_strategy.h"
17 
18 #include <chrono>
19 #include <cstdio>
20 
21 #include "event_export_util.h"
22 #include "file_util.h"
23 #include "hiview_global.h"
24 #include "hiview_logger.h"
25 #include "hiview_zip_util.h"
26 #include "parameter.h"
27 #include "parameter_ex.h"
28 #include "string_util.h"
29 #include "time_util.h"
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 DEFINE_LOG_TAG("HiView-EventExportFlow");
34 namespace {
35 constexpr char EXPORT_JSON_FILE_NAME[] = "HiSysEvent.json";
36 constexpr char SYSEVENT_EXPORT_TMP_DIR[] = "tmp";
37 constexpr char SYSEVENT_EXPORT_DIR[] = "sys_event_export";
38 constexpr char ZIP_FILE_DELIM[] = "_";
39 constexpr mode_t EVENT_EXPORT_DIR_MODE = S_IRWXU | S_IRWXG; // rwxrwx---
40 constexpr mode_t EVENT_EXPORT_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; // rw-rw----
41 constexpr uint32_t LOG_GID = 1007;
42 
AppendZipFile(std::string & dir,int32_t uid)43 void AppendZipFile(std::string& dir, int32_t uid)
44 {
45     dir.append("HSE").append(ZIP_FILE_DELIM)
46         .append(Parameter::GetBrandStr()).append(std::string(ZIP_FILE_DELIM))
47         .append(Parameter::GetProductModelStr()).append(std::string(ZIP_FILE_DELIM))
48         .append(Parameter::GetSysVersionStr()).append(std::string(ZIP_FILE_DELIM))
49         .append(EventExportUtil::GetDeviceId()).append(std::string(ZIP_FILE_DELIM))
50         .append(TimeUtil::GetFormattedTimestampEndWithMilli()).append(std::string(ZIP_FILE_DELIM))
51         .append("U").append(std::to_string(uid)).append(std::string(".zip"));
52 }
53 
GetWroteTempDir(const std::string & moduleName,const EventVersion & version)54 std::string GetWroteTempDir(const std::string& moduleName, const EventVersion& version)
55 {
56     auto& context = HiviewGlobal::GetInstance();
57     if (context == nullptr) {
58         return "";
59     }
60     std::string tmpDir = context->GetHiViewDirectory(HiviewContext::DirectoryType::WORK_DIRECTORY);
61     tmpDir = FileUtil::IncludeTrailingPathDelimiter(tmpDir.append(SYSEVENT_EXPORT_DIR));
62     tmpDir = FileUtil::IncludeTrailingPathDelimiter(tmpDir.append(SYSEVENT_EXPORT_TMP_DIR));
63     tmpDir = FileUtil::IncludeTrailingPathDelimiter(tmpDir.append(moduleName));
64     tmpDir = FileUtil::IncludeTrailingPathDelimiter(tmpDir.append(version.systemVersion));
65     if (!FileUtil::IsDirectory(tmpDir) && !FileUtil::ForceCreateDirectory(tmpDir)) {
66         HIVIEW_LOGE("failed to init directory %{public}s.", tmpDir.c_str());
67         return "";
68     }
69     return tmpDir;
70 }
71 
GetTmpZipFile(const std::string & exportDir,const std::string & moduleName,const EventVersion & version,int32_t uid)72 std::string GetTmpZipFile(const std::string& exportDir, const std::string& moduleName,
73     const EventVersion& version, int32_t uid)
74 {
75     std::string dir = FileUtil::IncludeTrailingPathDelimiter(exportDir);
76     dir = FileUtil::IncludeTrailingPathDelimiter(dir.append(SYSEVENT_EXPORT_TMP_DIR));
77     dir = FileUtil::IncludeTrailingPathDelimiter(dir.append(moduleName));
78     dir = FileUtil::IncludeTrailingPathDelimiter(dir.append(version.systemVersion));
79     if (!FileUtil::IsDirectory(dir) && !FileUtil::ForceCreateDirectory(dir)) {
80         HIVIEW_LOGE("failed to init directory %{public}s.", dir.c_str());
81         return "";
82     }
83     AppendZipFile(dir, uid);
84     return dir;
85 }
86 
ChangeFileModeAndGid(const std::string & file,mode_t mode,uint32_t gid)87 bool ChangeFileModeAndGid(const std::string& file, mode_t mode, uint32_t gid)
88 {
89     if (!FileUtil::ChangeModeFile(file, mode)) {
90         HIVIEW_LOGE("failed to change file mode of %{public}s.", StringUtil::HideDeviceIdInfo(file).c_str());
91         return false;
92     }
93     if (chown(file.c_str(), -1, gid) != 0) {
94         HIVIEW_LOGE("failed to change file owner of %{public}s.", StringUtil::HideDeviceIdInfo(file).c_str());
95         return false;
96     }
97     return true;
98 }
99 
GetZipFile(const std::string & exportDir,int32_t uid)100 std::string GetZipFile(const std::string& exportDir, int32_t uid)
101 {
102     std::string dir = FileUtil::IncludeTrailingPathDelimiter(exportDir);
103     if (!FileUtil::IsDirectory(dir) && !FileUtil::ForceCreateDirectory(dir)) {
104         HIVIEW_LOGE("failed to init directory %{public}s.", dir.c_str());
105         return "";
106     }
107     if (!ChangeFileModeAndGid(dir, EVENT_EXPORT_DIR_MODE, LOG_GID)) {
108         return "";
109     }
110     AppendZipFile(dir, uid);
111     return dir;
112 }
113 
ZipExportFile(const std::string & src,const std::string & dest)114 bool ZipExportFile(const std::string& src, const std::string& dest)
115 {
116     HiviewZipUnit zipUnit(dest);
117     if (int32_t ret = zipUnit.AddFileInZip(src, ZipFileLevel::KEEP_NONE_PARENT_PATH); ret != 0) {
118         HIVIEW_LOGW("zip db failed, ret: %{public}d.", ret);
119         return false;
120     }
121     if (!ChangeFileModeAndGid(dest, EVENT_EXPORT_FILE_MODE, LOG_GID)) {
122         return false;
123     }
124     // delete json file
125     FileUtil::RemoveFile(src);
126     return true;
127 }
128 
WriteContentToFile(std::string & content,const std::string & localFile)129 void WriteContentToFile(std::string& content, const std::string& localFile)
130 {
131     FILE* file = fopen(localFile.c_str(), "w+");
132     if (file == nullptr) {
133         HIVIEW_LOGE("failed to open file: %{public}s.", localFile.c_str());
134         return;
135     }
136     (void)fprintf(file, "%s", content.c_str());
137     (void)fclose(file);
138 }
139 }
140 
GetPackagerKey(std::shared_ptr<CachedEvent> event)141 std::string WriteZipFileStrategy::GetPackagerKey(std::shared_ptr<CachedEvent> event)
142 {
143     if (event == nullptr) {
144         return "";
145     }
146     std::string packagerKey;
147     packagerKey.append(event->version.systemVersion).append("_");
148     packagerKey.append(event->version.patchVersion).append("_");
149     packagerKey.append(std::to_string(event->uid));
150     return packagerKey;
151 }
152 
Write(std::string & exportContent,WroteCallback callback)153 bool WriteZipFileStrategy::Write(std::string& exportContent, WroteCallback callback)
154 {
155     if (callback == nullptr) {
156         HIVIEW_LOGE("wrote call back is invalid");
157         return false;
158     }
159     auto wroteFileName = GetWroteTempDir(param_.moduleName, param_.version).append(EXPORT_JSON_FILE_NAME);
160     WriteContentToFile(exportContent, wroteFileName);
161     // zip json file into a temporary zip file
162     auto tmpZipFile = GetTmpZipFile(param_.exportDir, param_.moduleName, param_.version, param_.uid);
163     if (!ZipExportFile(wroteFileName, tmpZipFile)) {
164         HIVIEW_LOGE("failed to zip %{public}s to %{public}s", wroteFileName.c_str(),
165             StringUtil::HideDeviceIdInfo(tmpZipFile).c_str());
166         return false;
167     }
168     auto zipFile = GetZipFile(param_.exportDir, param_.uid);
169     HIVIEW_LOGD("dest file: %{public}s", StringUtil::HideDeviceIdInfo(zipFile).c_str());
170     callback(tmpZipFile, zipFile);
171     return true;
172 }
173 } // namespace HiviewDFX
174 } // namespace OHOS