summaryrefslogtreecommitdiff
path: root/proxy/permissions.c
blob: 320112683683bb3300890ccf5449e755335b7c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright © 2016 Canonical Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Allison Lortie <desrt@desrt.ca>
 */

#include "permissions.h"

void
permission_list_add (PermissionList *self,
                     const gchar    *string)
{
  gsize current;

  current = (gsize) g_hash_table_lookup (self->hash_table, string);
  g_hash_table_insert (self->hash_table, g_strdup (string), (gpointer) (current + 1));
}

void
permission_list_remove (PermissionList *self,
                        const gchar    *string)
{
  gsize current;

  current = (gsize) g_hash_table_lookup (self->hash_table, string);
  g_assert (current != 0);

  if (current > 1)
    g_hash_table_insert (self->hash_table, g_strdup (string), (gpointer) (current - 1));
  else
    g_hash_table_remove (self->hash_table, string);
}

void
permission_list_merge (PermissionList *self,
                       PermissionList *to_merge)
{
  GHashTableIter iter;
  gpointer key;

  g_hash_table_iter_init (&iter, to_merge->hash_table);
  while (g_hash_table_iter_next (&iter, &key, NULL))
    permission_list_add (self, key);
}

void
permission_list_unmerge (PermissionList *self,
                         PermissionList *to_unmerge)
{
  GHashTableIter iter;
  gpointer key;

  g_hash_table_iter_init (&iter, to_unmerge->hash_table);
  while (g_hash_table_iter_next (&iter, &key, NULL))
    permission_list_remove (self, key);
}

void
permission_list_init (PermissionList  *self,
                      gchar          **contents)
{
  self->hash_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

  if (contents != NULL)
    {
      gint i;

      for (i = 0; contents[i]; i++)
        g_hash_table_insert (self->hash_table, contents[i], (gpointer) 1u);

      g_free (contents);
    }
}

void
permission_list_clear (PermissionList *self)
{
  g_hash_table_unref (self->hash_table);
  self->hash_table = NULL;
}

void
permissions_init (Permissions *permissions)
{
  permission_list_init (&permissions->readable, NULL);
  permission_list_init (&permissions->writable, NULL);
}

void
permissions_clear (Permissions *permissions)
{
  permission_list_clear (&permissions->readable);
  permission_list_clear (&permissions->writable);
}

static void
merge_string (gchar       **dest,
              const gchar  *src)
{
  if (*dest == NULL)
    *dest = g_strdup (src);

  g_assert_cmpstr (*dest, ==, src);
}

void
permissions_merge (Permissions *permissions,
                   Permissions *to_merge)
{
  merge_string (&permissions->app_id, to_merge->app_id);
  merge_string (&permissions->ipc_dir, to_merge->ipc_dir);

  permission_list_merge (&permissions->readable, &to_merge->readable);
  permission_list_merge (&permissions->writable, &to_merge->writable);
}

void
permissions_unmerge (Permissions *permissions,
                     Permissions *to_unmerge)
{
  permission_list_unmerge (&permissions->readable, &to_unmerge->readable);
  permission_list_unmerge (&permissions->writable, &to_unmerge->writable);
}