4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
26 #include "wgtpkg-permissions.h"
31 unsigned requested: 1;
35 static const char prefix_of_permissions[] = FWK_PREFIX_PERMISSION;
37 static unsigned int nrpermissions = 0;
38 static struct permission *permissions = NULL;
39 static unsigned int indexiter = 0;
41 /* check is the name has the correct prefix for permissions */
42 int is_standard_permission(const char *name)
44 return 0 == memcmp(name, prefix_of_permissions, sizeof(prefix_of_permissions) - 1);
47 /* retrieves the permission of name */
48 static struct permission *get_permission(const char *name)
52 for (i = 0 ; i < nrpermissions ; i++)
53 if (0 == strcmp(permissions[i].name, name))
58 /* add a permission of name */
59 static struct permission *add_permission(const char *name)
61 struct permission *p = get_permission(name);
63 p = realloc(permissions,
64 ((nrpermissions + 8) & ~(unsigned)7) * sizeof(*p));
67 p = permissions + nrpermissions;
68 memset(p, 0, sizeof(*p));
69 p->name = strdup(name);
79 /* remove any granting */
80 void reset_permissions()
83 for (i = 0 ; i < nrpermissions ; i++)
84 permissions[i].granted = 0;
87 /* remove any granting */
88 void crop_permissions(unsigned level)
91 for (i = 0 ; i < nrpermissions ; i++)
92 if (permissions[i].level < level)
93 permissions[i].granted = 0;
96 /* add permissions granted for installation */
97 void grant_permission_list(const char *list)
102 static const char separators[] = " \t\n\r,";
104 iter = strdupa(list);
105 iter += strspn(iter, separators);
107 n = (unsigned)strcspn(iter, separators);
110 p = add_permission(iter);
112 ERROR("Can't allocate permission");
118 iter += strspn(iter, separators);
122 /* checks if the permission 'name' is recorded */
123 int permission_exists(const char *name)
125 return !!get_permission(name);
128 /* request the permission, returns 1 if granted or 0 otherwise */
129 int request_permission(const char *name)
131 struct permission *p = get_permission(name);
140 /* iteration over granted and requested permissions */
141 const char *first_usable_permission()
144 return next_usable_permission();
147 const char *next_usable_permission()
149 while(indexiter < nrpermissions) {
150 struct permission *p = &permissions[indexiter++];
151 if (p->granted && p->requested)