1 /*
2 * Copyright (c) 2020-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
16 #include "perm_operate.h"
17 #include <string.h>
18 #include "hal_pms.h"
19
20 #define RET_OK 0
21 #define RET_NOK (-1)
22
PermissionIsGranted(const TList * list,int uid,const char * permission)23 int PermissionIsGranted(const TList *list, int uid, const char *permission)
24 {
25 TNode *cur = list->head;
26 while (cur != NULL) {
27 if (cur->uid != uid) {
28 cur = cur->next;
29 continue;
30 }
31 for (int i = 0; i < cur->permNum; i++) {
32 if (strcmp(cur->permList[i].name, permission) == 0) {
33 return (int)cur->permList[i].granted;
34 }
35 }
36 return RET_NOK;
37 }
38 return RET_NOK;
39 }
40
ModifyPermission(TNode * node,const char * permission,const enum IsGranted granted)41 int ModifyPermission(TNode *node, const char *permission, const enum IsGranted granted)
42 {
43 if (node == NULL || permission == NULL) {
44 return RET_NOK;
45 }
46
47 for (int i = 0; i < node->permNum; i++) {
48 if (strcmp(node->permList[i].name, permission) == 0) {
49 node->permList[i].granted = granted;
50 return RET_OK;
51 }
52 }
53 return RET_NOK;
54 }
55
AddTask(TList * list,TNode * node)56 void AddTask(TList *list, TNode *node)
57 {
58 if (list->head == NULL) {
59 list->head = node;
60 } else {
61 node->next = list->head;
62 list->head = node;
63 }
64 }
65
DeleteTask(TList * list,int uid)66 void DeleteTask(TList *list, int uid)
67 {
68 TNode *cur = list->head;
69 TNode *pre = NULL;
70 while (cur != NULL) {
71 if (cur->uid == uid) {
72 if (pre == NULL) {
73 list->head = cur->next;
74 } else {
75 pre->next = cur->next;
76 }
77 HalFree(cur->permList);
78 HalFree(cur);
79 return;
80 }
81 pre = cur;
82 cur = cur->next;
83 }
84 }
85
GetTaskWithUid(TList * list,int uid)86 TNode *GetTaskWithUid(TList *list, int uid)
87 {
88 TNode *cur = list->head;
89 while (cur != NULL) {
90 if (cur->uid == uid) {
91 return cur;
92 }
93 cur = cur->next;
94 }
95 return NULL;
96 }
97
GetTaskWithPkgName(TList * list,const char * pkgName)98 TNode *GetTaskWithPkgName(TList *list, const char *pkgName)
99 {
100 TNode *cur = list->head;
101 while (cur != NULL) {
102 if (strcmp(cur->pkgName, pkgName) == 0) {
103 return cur;
104 }
105 cur = cur->next;
106 }
107 return NULL;
108 }
109