1 /*
2 * Copyright (c) 2023 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 "data_publisher_sys_event_callback.h"
17
18 #include <unistd.h>
19
20 #include "errors.h"
21 #include "hilog/log.h"
22
23 #include "data_share_common.h"
24 #include "data_share_util.h"
25 #include "file_util.h"
26 #include "string_util.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30
31 namespace {
32 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D10, "HiView-DataPublisherSysEventCallback" };
33 } // namespace
34
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)35 void DataPublisherSysEventCallback::OnQuery(const std::vector<std::u16string>& sysEvent,
36 const std::vector<int64_t>& seq)
37 {
38 lastDestFilePath_ = destPath_;
39 lastDestFilePath_.append("-")
40 .append(SUCCESS_CODE)
41 .append("-")
42 .append(std::to_string(fileIndex_))
43 .append(FILE_SUFFIX);
44 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
45 for (auto iter: sysEvent) {
46 int32_t eventJsonSize = static_cast<int32_t>((iter.size() + 1) * sizeof(std::u16string));
47 if (eventJsonSize + totalJsonSize_ > MAXIMUM_FILE_SIZE) {
48 HandleEventFile(srcPath_, lastDestFilePath_);
49 fileIndex_++;
50 lastDestFilePath_ = destPath_;
51 lastDestFilePath_.append("-")
52 .append(SUCCESS_CODE)
53 .append("-")
54 .append(std::to_string(fileIndex_))
55 .append(FILE_SUFFIX);
56 totalJsonSize_ = 0;
57 }
58 std::string str = convert.to_bytes(iter);
59 if (!FileUtil::SaveStringToFile(srcPath_, str + ",", false)) {
60 HiLog::Error(LABEL, "failed to persist iter to file");
61 }
62 totalJsonSize_ += eventJsonSize;
63 }
64 }
65
OnComplete(int32_t reason,int32_t total,int64_t seq)66 void DataPublisherSysEventCallback::OnComplete(int32_t reason, int32_t total, int64_t seq)
67 {
68 if (totalJsonSize_ != 0) {
69 HandleEventFile(srcPath_, lastDestFilePath_);
70 }
71 }
72
HandleEventFile(const std::string & srcPath,const std::string & desPath)73 void DataPublisherSysEventCallback::HandleEventFile(const std::string &srcPath, const std::string &desPath)
74 {
75 auto res = OHOS::HiviewDFX::DataShareUtil::CopyFile(srcPath.c_str(), desPath.c_str());
76 if (res == -1) {
77 HiLog::Error(LABEL, "failed to move file to desPath.");
78 }
79 if (!FileUtil::RemoveFile(srcPath)) {
80 HiLog::Error(LABEL, "failed to remove resourceFile.");
81 }
82 if (chmod(desPath.c_str(), FileUtil::FILE_PERM_666)) {
83 HiLog::Error(LABEL, "Failed to chmod socket.");
84 }
85 }
86 } // namespace HiviewDFX
87 } // namespace OHOS
88
89