1 /*
2 * Copyright (c) 2022 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 #include "work_sched_utils.h"
16
17 #include "errors.h"
18 #include "ohos_account_kits.h"
19 #include "os_account_manager.h"
20 #include "work_sched_hilog.h"
21
22 namespace OHOS {
23 namespace WorkScheduler {
GetCurrentAccountId()24 int WorkSchedUtils::GetCurrentAccountId()
25 {
26 std::vector<int> osAccountIds;
27 ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
28 if (ret != ERR_OK) {
29 WS_HILOGE("QueryActiveOsAccountIds failed.");
30 return -1;
31 }
32
33 if (osAccountIds.empty()) {
34 WS_HILOGE("osAccountInfos is empty, no accounts.");
35 return -1;
36 }
37
38 for (const auto& accountId : osAccountIds) {
39 if (accountId >= 0) {
40 return accountId;
41 }
42 }
43 WS_HILOGE("GetCurrentAccountId failed, no osAccountIds now.");
44 return -1;
45 }
46
IsIdActive(int id)47 bool WorkSchedUtils::IsIdActive(int id)
48 {
49 std::vector<int> osAccountIds;
50 ErrCode ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
51 if (ret != ERR_OK) {
52 WS_HILOGE("QueryActiveOsAccountIds failed.");
53 return false;
54 }
55
56 if (osAccountIds.empty()) {
57 WS_HILOGE("osAccountIds is empty, no accounts.");
58 return false;
59 }
60
61 for (const auto& accountId : osAccountIds) {
62 if (accountId == id) {
63 return true;
64 }
65 }
66 WS_HILOGE("IsIdActive failed, osAccountIds now.");
67 return false;
68 }
69
GetUserIdByUid(int32_t uid)70 int32_t WorkSchedUtils::GetUserIdByUid(int32_t uid)
71 {
72 if (uid <= INVALID_DATA) {
73 WS_HILOGE("uid is illegal: %{public}d", uid);
74 return INVALID_DATA;
75 }
76 const int BASE_USER_RANGE = 200000;
77 return uid / BASE_USER_RANGE;
78 }
79
ConvertFullPath(const std::string & partialPath,std::string & fullPath)80 bool WorkSchedUtils::ConvertFullPath(const std::string& partialPath, std::string& fullPath)
81 {
82 if (partialPath.empty() || partialPath.length() >= PATH_MAX) {
83 return false;
84 }
85 char tmpPath[PATH_MAX] = {0};
86 if (realpath(partialPath.c_str(), tmpPath) == nullptr) {
87 return false;
88 }
89 fullPath = tmpPath;
90 return true;
91 }
92 }
93 }