LCOV - code coverage report
Current view: top level - src/plugins/dpkg - dpkg.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 9 67 13.4 %
Date: 2019-09-12 12:28:41 Functions: 2 7 28.6 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Source for dpkg plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include "dpkg.h"
      11             : #include <kdb.h>
      12             : #include <kdbhelper.h>
      13             : #include <stdio.h>
      14             : #include <stdlib.h>
      15             : #include <string.h>
      16             : 
      17             : #define DPKG_LINE_MAX 512
      18             : 
      19           0 : static void appendToKey (Key * key, const char * line)
      20             : {
      21             :         char * buffer;
      22           0 :         size_t len = keyGetValueSize (key) + elektraStrLen (line) - 1;
      23           0 :         buffer = elektraMalloc (len);
      24           0 :         snprintf (buffer, len, "%s%s", keyString (key), line);
      25           0 :         keySetString (key, buffer);
      26           0 :         elektraFree (buffer);
      27           0 : }
      28           0 : static KeySet * nextPackage (FILE * fp, Key * parentKey)
      29             : {
      30           0 :         char * line = elektraMalloc (DPKG_LINE_MAX);
      31           0 :         KeySet * package = ksNew (500, KS_END);
      32           0 :         Key * lastKey = NULL;
      33           0 :         Key * baseKey = NULL;
      34           0 :         int notDone = 0;
      35           0 :         while (fgets (line, DPKG_LINE_MAX, fp) != NULL)
      36             :         {
      37           0 :                 if (*line == '\n') break;
      38           0 :                 if (*line == ' ')
      39             :                 {
      40           0 :                         if (strchr (line, '\n'))
      41             :                                 notDone = 0;
      42             :                         else
      43           0 :                                 notDone = 1;
      44           0 :                         appendToKey (lastKey, line);
      45             :                 }
      46           0 :                 else if (notDone)
      47             :                 {
      48           0 :                         if (strchr (line, '\n')) notDone = 0;
      49           0 :                         appendToKey (lastKey, line);
      50             :                 }
      51             :                 else
      52             :                 {
      53           0 :                         if (!strchr (line, '\n')) notDone = 1;
      54           0 :                         char * section = line;
      55           0 :                         char * data = strchr (line, ':');
      56           0 :                         if (data) *data = '\0';
      57           0 :                         ++data;              // skip :
      58           0 :                         ++data;              // skip whitespace
      59           0 :                         strtok (data, "\n"); // remove newline
      60           0 :                         if (!strcmp (section, "Package"))
      61             :                         {
      62           0 :                                 baseKey = keyDup (parentKey);
      63           0 :                                 keyAddBaseName (baseKey, data);
      64           0 :                                 lastKey = baseKey;
      65           0 :                                 ksAppendKey (package, baseKey);
      66             :                         }
      67             :                         else
      68             :                         {
      69           0 :                                 Key * key = keyDup (baseKey);
      70           0 :                                 keyAddBaseName (key, section);
      71           0 :                                 keySetString (key, data);
      72           0 :                                 lastKey = key;
      73           0 :                                 ksAppendKey (package, key);
      74             :                         }
      75             :                 }
      76           0 :                 memset (line, 0, DPKG_LINE_MAX);
      77             :         }
      78           0 :         elektraFree (line);
      79           0 :         return package;
      80             : }
      81           0 : static KeySet * readFile (Key * parentKey)
      82             : {
      83           0 :         FILE * fp = fopen (keyString (parentKey), "r");
      84           0 :         KeySet * result = ksNew (0, KS_END);
      85           0 :         if (!fp) return result;
      86           0 :         while (!feof (fp))
      87             :         {
      88           0 :                 KeySet * package = nextPackage (fp, parentKey);
      89           0 :                 ksAppend (result, package);
      90           0 :                 ksDel (package);
      91             :         }
      92           0 :         fclose (fp);
      93           0 :         return result;
      94             : }
      95          20 : int elektraDpkgGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
      96             : {
      97          20 :         if (!elektraStrCmp (keyName (parentKey), "system/elektra/modules/dpkg"))
      98             :         {
      99          20 :                 KeySet * contract =
     100          20 :                         ksNew (30, keyNew ("system/elektra/modules/dpkg", KEY_VALUE, "dpkg plugin waits for your orders", KEY_END),
     101             :                                keyNew ("system/elektra/modules/dpkg/exports", KEY_END),
     102             :                                keyNew ("system/elektra/modules/dpkg/exports/get", KEY_FUNC, elektraDpkgGet, KEY_END),
     103             :                                keyNew ("system/elektra/modules/dpkg/exports/set", KEY_FUNC, elektraDpkgSet, KEY_END),
     104             : #include ELEKTRA_README
     105             :                                keyNew ("system/elektra/modules/dpkg/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
     106          20 :                 ksAppend (returned, contract);
     107          20 :                 ksDel (contract);
     108             : 
     109          20 :                 return 1; // success
     110             :         }
     111             :         // get all keys
     112           0 :         KeySet * ks = readFile (parentKey);
     113           0 :         ksAppend (returned, ks);
     114           0 :         ksDel (ks);
     115           0 :         return 1; // success
     116             : }
     117             : 
     118           0 : int elektraDpkgSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
     119             : {
     120             :         // get all keys
     121             : 
     122           0 :         return 1; // success
     123             : }
     124             : 
     125          20 : Plugin * ELEKTRA_PLUGIN_EXPORT
     126             : {
     127          20 :         return elektraPluginExport ("dpkg", ELEKTRA_PLUGIN_GET, &elektraDpkgGet, ELEKTRA_PLUGIN_SET, &elektraDpkgSet, ELEKTRA_PLUGIN_END);
     128             : }

Generated by: LCOV version 1.13