Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief provides linked list of GPGME's gpgme_key_t for the gpgme plugin.
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include "keylist.h"
11 : #include <kdbhelper.h>
12 :
13 : /**
14 : * @brief initializes the internal key list
15 : * @param list the list
16 : */
17 0 : void elektraGpgmeKeylistInit (keylist_t * list)
18 : {
19 0 : list->head = NULL;
20 0 : list->iterator = NULL;
21 0 : list->size = 0;
22 0 : }
23 :
24 : /**
25 : * @brief add key to the list
26 : * @param list the key list
27 : * @param key the element to be added to the list
28 : * @retval 0 in case of insufficient memory (malloc failed)
29 : * @retval 1 in case of success
30 : */
31 0 : int elektraGpgmeKeylistAdd (keylist_t * list, gpgme_key_t key)
32 : {
33 0 : if (list->iterator)
34 : {
35 : // append key to existing list
36 0 : list->iterator->next = elektraMalloc (sizeof (struct internal_keylist));
37 0 : if (!list->iterator->next)
38 : {
39 : return 0; // not enough memory
40 : }
41 0 : list->iterator->next->key = key;
42 0 : list->iterator->next->next = NULL;
43 0 : list->iterator = list->iterator->next;
44 : }
45 : else
46 : {
47 : // first element in empty list
48 0 : list->head = elektraMalloc (sizeof (struct internal_keylist));
49 0 : if (!list->head)
50 : {
51 : return 0; // not enough memory
52 : }
53 0 : list->head->key = key;
54 0 : list->head->next = NULL;
55 0 : list->iterator = list->head;
56 : }
57 0 : list->size++;
58 : // instruct gpgme to not release the key handle until we are done with it
59 : // NOTE do not forget to invoke gpgme_key_unref when releasing the key list
60 0 : gpgme_key_ref (key);
61 0 : return 1; // success
62 : }
63 :
64 : /**
65 : * @brief reset the iterator of the list to the head of the list.
66 : */
67 0 : void elektraGpgmeKeylistRewind (keylist_t * list)
68 : {
69 0 : list->iterator = list->head;
70 0 : }
71 :
72 : /**
73 : * @brief get the next gpgme_key_t of the list.
74 : * @return the next element or NULL if no such element exists.
75 : */
76 0 : gpgme_key_t elektraGpgmeKeylistNext (keylist_t * list)
77 : {
78 0 : if (!list->iterator)
79 : {
80 : return NULL;
81 : }
82 0 : gpgme_key_t key = list->iterator->key;
83 0 : list->iterator = list->iterator->next;
84 0 : return key;
85 : }
86 :
87 : /**
88 : * @brief release the key list
89 : * @param list the list to be released
90 : */
91 0 : void elektraGpgmeKeylistFree (keylist_t * list)
92 : {
93 0 : struct internal_keylist * iterator = list->head;
94 : struct internal_keylist * next;
95 :
96 0 : while (iterator)
97 : {
98 0 : next = iterator->next;
99 0 : gpgme_key_unref (iterator->key);
100 0 : elektraFree (iterator);
101 0 : iterator = next;
102 : }
103 0 : list->head = NULL;
104 0 : list->iterator = NULL;
105 0 : list->size = 0;
106 0 : }
|