1f5921b11Sopenharmony_ci/*
2f5921b11Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3f5921b11Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f5921b11Sopenharmony_ci * you may not use this file except in compliance with the License.
5f5921b11Sopenharmony_ci * You may obtain a copy of the License at
6f5921b11Sopenharmony_ci *
7f5921b11Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
8f5921b11Sopenharmony_ci *
9f5921b11Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f5921b11Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f5921b11Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f5921b11Sopenharmony_ci * See the License for the specific language governing permissions and
13f5921b11Sopenharmony_ci * limitations under the License.
14f5921b11Sopenharmony_ci */
15f5921b11Sopenharmony_ci
16f5921b11Sopenharmony_ci#include "perm_operate.h"
17f5921b11Sopenharmony_ci#include <string.h>
18f5921b11Sopenharmony_ci#include "hal_pms.h"
19f5921b11Sopenharmony_ci
20f5921b11Sopenharmony_ci#define RET_OK 0
21f5921b11Sopenharmony_ci#define RET_NOK (-1)
22f5921b11Sopenharmony_ci
23f5921b11Sopenharmony_ciint PermissionIsGranted(const TList *list, int uid, const char *permission)
24f5921b11Sopenharmony_ci{
25f5921b11Sopenharmony_ci    TNode *cur = list->head;
26f5921b11Sopenharmony_ci    while (cur != NULL) {
27f5921b11Sopenharmony_ci        if (cur->uid != uid) {
28f5921b11Sopenharmony_ci            cur = cur->next;
29f5921b11Sopenharmony_ci            continue;
30f5921b11Sopenharmony_ci        }
31f5921b11Sopenharmony_ci        for (int i = 0; i < cur->permNum; i++) {
32f5921b11Sopenharmony_ci            if (strcmp(cur->permList[i].name, permission) == 0) {
33f5921b11Sopenharmony_ci                return (int)cur->permList[i].granted;
34f5921b11Sopenharmony_ci            }
35f5921b11Sopenharmony_ci        }
36f5921b11Sopenharmony_ci        return RET_NOK;
37f5921b11Sopenharmony_ci    }
38f5921b11Sopenharmony_ci    return RET_NOK;
39f5921b11Sopenharmony_ci}
40f5921b11Sopenharmony_ci
41f5921b11Sopenharmony_ciint ModifyPermission(TNode *node, const char *permission, const enum IsGranted granted)
42f5921b11Sopenharmony_ci{
43f5921b11Sopenharmony_ci    if (node == NULL || permission == NULL) {
44f5921b11Sopenharmony_ci        return RET_NOK;
45f5921b11Sopenharmony_ci    }
46f5921b11Sopenharmony_ci
47f5921b11Sopenharmony_ci    for (int i = 0; i < node->permNum; i++) {
48f5921b11Sopenharmony_ci        if (strcmp(node->permList[i].name, permission) == 0) {
49f5921b11Sopenharmony_ci            node->permList[i].granted = granted;
50f5921b11Sopenharmony_ci            return RET_OK;
51f5921b11Sopenharmony_ci        }
52f5921b11Sopenharmony_ci    }
53f5921b11Sopenharmony_ci    return RET_NOK;
54f5921b11Sopenharmony_ci}
55f5921b11Sopenharmony_ci
56f5921b11Sopenharmony_civoid AddTask(TList *list, TNode *node)
57f5921b11Sopenharmony_ci{
58f5921b11Sopenharmony_ci    if (list->head == NULL) {
59f5921b11Sopenharmony_ci        list->head = node;
60f5921b11Sopenharmony_ci    } else {
61f5921b11Sopenharmony_ci        node->next = list->head;
62f5921b11Sopenharmony_ci        list->head = node;
63f5921b11Sopenharmony_ci    }
64f5921b11Sopenharmony_ci}
65f5921b11Sopenharmony_ci
66f5921b11Sopenharmony_civoid DeleteTask(TList *list, int uid)
67f5921b11Sopenharmony_ci{
68f5921b11Sopenharmony_ci    TNode *cur = list->head;
69f5921b11Sopenharmony_ci    TNode *pre = NULL;
70f5921b11Sopenharmony_ci    while (cur != NULL) {
71f5921b11Sopenharmony_ci        if (cur->uid == uid) {
72f5921b11Sopenharmony_ci            if (pre == NULL) {
73f5921b11Sopenharmony_ci                list->head = cur->next;
74f5921b11Sopenharmony_ci            } else {
75f5921b11Sopenharmony_ci                pre->next = cur->next;
76f5921b11Sopenharmony_ci            }
77f5921b11Sopenharmony_ci            HalFree(cur->permList);
78f5921b11Sopenharmony_ci            HalFree(cur);
79f5921b11Sopenharmony_ci            return;
80f5921b11Sopenharmony_ci        }
81f5921b11Sopenharmony_ci        pre = cur;
82f5921b11Sopenharmony_ci        cur = cur->next;
83f5921b11Sopenharmony_ci    }
84f5921b11Sopenharmony_ci}
85f5921b11Sopenharmony_ci
86f5921b11Sopenharmony_ciTNode *GetTaskWithUid(TList *list, int uid)
87f5921b11Sopenharmony_ci{
88f5921b11Sopenharmony_ci    TNode *cur = list->head;
89f5921b11Sopenharmony_ci    while (cur != NULL) {
90f5921b11Sopenharmony_ci        if (cur->uid == uid) {
91f5921b11Sopenharmony_ci            return cur;
92f5921b11Sopenharmony_ci        }
93f5921b11Sopenharmony_ci        cur = cur->next;
94f5921b11Sopenharmony_ci    }
95f5921b11Sopenharmony_ci    return NULL;
96f5921b11Sopenharmony_ci}
97f5921b11Sopenharmony_ci
98f5921b11Sopenharmony_ciTNode *GetTaskWithPkgName(TList *list, const char *pkgName)
99f5921b11Sopenharmony_ci{
100f5921b11Sopenharmony_ci    TNode *cur = list->head;
101f5921b11Sopenharmony_ci    while (cur != NULL) {
102f5921b11Sopenharmony_ci        if (strcmp(cur->pkgName, pkgName) == 0) {
103f5921b11Sopenharmony_ci            return cur;
104f5921b11Sopenharmony_ci        }
105f5921b11Sopenharmony_ci        cur = cur->next;
106f5921b11Sopenharmony_ci    }
107f5921b11Sopenharmony_ci    return NULL;
108f5921b11Sopenharmony_ci}
109