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