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_share_util.h"
17
18 #include "bundle_mgr_client.h"
19 #include <cstdio>
20 #include <iostream>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/sendfile.h>
25
26 #include "file_util.h"
27 #include "hiview_logger.h"
28
29 namespace OHOS {
30 namespace HiviewDFX {
31 DEFINE_LOG_TAG("HiView-DataShareUtil");
32 namespace {
33 constexpr int VALUE_MOD = 200000;
34 } // namespace
35
GetSandBoxPathByUid(int32_t uid)36 std::string DataShareUtil::GetSandBoxPathByUid(int32_t uid)
37 {
38 int userId = uid / VALUE_MOD;
39 std::string bundleName = OHOS::HiviewDFX::DataShareUtil::GetBundleNameById(uid);
40 std::string path;
41 path.append("/data/app/el2/")
42 .append(std::to_string(userId))
43 .append("/base/")
44 .append(bundleName)
45 .append("/cache/hiview/event");
46 return path;
47 }
48
CopyFile(const char * src,const char * des)49 int DataShareUtil::CopyFile(const char *src, const char *des)
50 {
51 int src_fd = open(src, O_RDONLY);
52 if (src_fd == -1) {
53 HIVIEW_LOGE("failed to open source file, src=%{public}s", src);
54 return -1;
55 }
56
57 int dest_fd = open(des, O_WRONLY | O_CREAT, S_IWUSR);
58 if (dest_fd == -1) {
59 perror("open");
60 HIVIEW_LOGE("failed to open destination file, des=%{public}s", des);
61 close(src_fd);
62 return -1;
63 }
64
65 struct stat st;
66 if (fstat(src_fd, &st) == -1) {
67 HIVIEW_LOGE("failed to get source file size");
68 close(dest_fd);
69 close(src_fd);
70 return -1;
71 }
72
73 off_t offset = 0;
74 ssize_t ret = sendfile(dest_fd, src_fd, &offset, st.st_size);
75 if (ret == -1) {
76 HIVIEW_LOGE("failed to sendfile");
77 close(dest_fd);
78 close(src_fd);
79 return -1;
80 }
81 close(src_fd);
82 close(dest_fd);
83 return 0;
84 }
85
GetBundleNameById(int32_t uid)86 std::string DataShareUtil::GetBundleNameById(int32_t uid)
87 {
88 std::string bundleName;
89 AppExecFwk::BundleMgrClient client;
90 if (client.GetNameForUid(uid, bundleName) != ERR_OK) {
91 HIVIEW_LOGE("Failed to query bundleName from bms, uid:%{public}d.", uid);
92 } else {
93 HIVIEW_LOGE("bundleName of uid:%{public}d, bundleName is %{public}s", uid, bundleName.c_str());
94 }
95 return bundleName;
96 }
97
GetUidByBundleName(const std::string & bundleName)98 int32_t DataShareUtil::GetUidByBundleName(const std::string& bundleName)
99 {
100 AppExecFwk::BundleInfo info;
101 AppExecFwk::BundleMgrClient client;
102 if (!client.GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_DEFAULT, info,
103 AppExecFwk::Constants::ALL_USERID)) {
104 HIVIEW_LOGE("Failed to query uid from bms, bundleName=%{public}s.", bundleName.c_str());
105 } else {
106 HIVIEW_LOGD("bundleName of uid=%{public}d, bundleName=%{public}s", info.uid, bundleName.c_str());
107 }
108 return info.uid;
109 }
110 } // namespace HiviewDFX
111 } // namespace OHOS