LCOV - code coverage report
Current view: top level - src/plugins/gpgme - keylist.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 35 40 87.5 %
Date: 2022-05-21 16:19:22 Functions: 5 5 100.0 %

          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           4 : void elektraGpgmeKeylistInit (keylist_t * list)
      18             : {
      19           4 :         list->head = NULL;
      20           4 :         list->iterator = NULL;
      21           4 :         list->size = 0;
      22           4 : }
      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           2 : int elektraGpgmeKeylistAdd (keylist_t * list, gpgme_key_t key)
      32             : {
      33           2 :         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           2 :                 list->head = elektraMalloc (sizeof (struct internal_keylist));
      49           2 :                 if (!list->head)
      50             :                 {
      51             :                         return 0; // not enough memory
      52             :                 }
      53           2 :                 list->head->key = key;
      54           2 :                 list->head->next = NULL;
      55           2 :                 list->iterator = list->head;
      56             :         }
      57           2 :         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           2 :         gpgme_key_ref (key);
      61           2 :         return 1; // success
      62             : }
      63             : 
      64             : /**
      65             :  * @brief reset the iterator of the list to the head of the list.
      66             :  */
      67           2 : void elektraGpgmeKeylistRewind (keylist_t * list)
      68             : {
      69           2 :         list->iterator = list->head;
      70           2 : }
      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           4 : gpgme_key_t elektraGpgmeKeylistNext (keylist_t * list)
      77             : {
      78           4 :         if (!list->iterator)
      79             :         {
      80             :                 return NULL;
      81             :         }
      82           2 :         gpgme_key_t key = list->iterator->key;
      83           2 :         list->iterator = list->iterator->next;
      84           2 :         return key;
      85             : }
      86             : 
      87             : /**
      88             :  * @brief release the key list
      89             :  * @param list the list to be released
      90             :  */
      91           4 : void elektraGpgmeKeylistFree (keylist_t * list)
      92             : {
      93           4 :         struct internal_keylist * iterator = list->head;
      94           4 :         struct internal_keylist * next;
      95             : 
      96           6 :         while (iterator)
      97             :         {
      98           2 :                 next = iterator->next;
      99           2 :                 gpgme_key_unref (iterator->key);
     100           2 :                 elektraFree (iterator);
     101           2 :                 iterator = next;
     102             :         }
     103           4 :         list->head = NULL;
     104           4 :         list->iterator = NULL;
     105           4 :         list->size = 0;
     106           4 : }

Generated by: LCOV version 1.13