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