LCOV - code coverage report
Current view: top level - src/tools/kdb - metaset.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 28 35 80.0 %
Date: 2019-09-12 12:28:41 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <metaset.hpp>
      10             : 
      11             : #include <iostream>
      12             : #include <string>
      13             : 
      14             : #include <cmdline.hpp>
      15             : #include <kdb.hpp>
      16             : 
      17             : using namespace std;
      18             : using namespace kdb;
      19             : 
      20         163 : MetaSetCommand::MetaSetCommand ()
      21             : {
      22         163 : }
      23             : 
      24          85 : int MetaSetCommand::execute (Cmdline const & cl)
      25             : {
      26         170 :         if (cl.arguments.size () < 2 || cl.arguments.size () > 3)
      27             :         {
      28           0 :                 throw invalid_argument ("Need 2 or 3 arguments");
      29             :         }
      30         255 :         string metaname = cl.arguments[1];
      31             : 
      32         170 :         Key parentKey = cl.createKey (0);
      33         170 :         string keyname = parentKey.getName ();
      34          85 :         if (keyname[0] == '/')
      35             :         {
      36             :                 // fix name for lookup
      37          12 :                 keyname = "spec" + keyname;
      38          18 :                 if (!cl.quiet) std::cout << "Using keyname " << keyname << std::endl;
      39             : 
      40             :                 // fix k for kdb.set later
      41           6 :                 parentKey.setName (keyname);
      42             :         }
      43             : 
      44         170 :         KeySet conf;
      45          85 :         kdb.get (conf, parentKey);
      46         170 :         Key k = conf.lookup (parentKey);
      47             : 
      48          85 :         if (!k)
      49             :         {
      50          88 :                 k = Key (keyname, KEY_END);
      51             :                 // k.setBinary(0, 0); // conceptually maybe better, but would have confusing "binary" metadata
      52          22 :                 conf.append (k);
      53          22 :                 if (cl.verbose) cout << "Creating key " << keyname << endl;
      54             :         }
      55          85 :         if (!k.isValid ())
      56             :         {
      57           0 :                 cerr << "Could not create key " << keyname << endl;
      58             :                 return 1;
      59             :         }
      60             : 
      61         170 :         if (cl.arguments.size () == 2)
      62             :         {
      63           0 :                 if (!cl.quiet) cout << "Only two arguments, thus deleting metaname " << metaname << endl;
      64             :                 k.delMeta (metaname);
      65             :         }
      66             :         else
      67             :         {
      68         255 :                 std::string metavalue = cl.arguments[2];
      69         255 :                 if (metaname == "atime" || metaname == "mtime" || metaname == "ctime")
      70             :                 {
      71           0 :                         stringstream str (metavalue);
      72             :                         time_t t;
      73           0 :                         str >> t;
      74           0 :                         if (!str.good ()) throw "conversion failure";
      75           0 :                         k.setMeta<time_t> (metaname, t);
      76             :                 }
      77             :                 else
      78             :                 {
      79         170 :                         k.setMeta<string> (metaname, metavalue);
      80             :                 }
      81             :         }
      82             : 
      83          85 :         kdb.set (conf, parentKey);
      84          83 :         printWarnings (cerr, parentKey, cl.verbose, cl.debug);
      85          83 :         printError (cerr, k, cl.verbose, cl.debug);
      86             : 
      87             :         return 0;
      88             : }
      89             : 
      90         326 : MetaSetCommand::~MetaSetCommand ()
      91             : {
      92        7327 : }

Generated by: LCOV version 1.13