LCOV - code coverage report
Current view: top level - src/tools/kdb - mountbase.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 31 64 48.4 %
Date: 2019-09-12 12:28:41 Functions: 6 10 60.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 <mountbase.hpp>
      10             : 
      11             : #include <backend.hpp>
      12             : #include <backends.hpp>
      13             : #include <cmdline.hpp>
      14             : 
      15             : #include <algorithm>
      16             : #include <iostream>
      17             : #include <iterator>
      18             : #include <string>
      19             : #include <vector>
      20             : 
      21             : 
      22             : using namespace std;
      23             : using namespace kdb;
      24             : using namespace kdb::tools;
      25             : 
      26             : /**
      27             :  * @brief Read in configuration and print warnings
      28             :  *
      29             :  * @post will update mountConf
      30             :  */
      31         324 : void MountBaseCommand::readMountConf (Cmdline const & cl)
      32             : {
      33        1296 :         Key parentKey (mountpointsPath, KEY_END);
      34             : 
      35         324 :         kdb.get (mountConf, parentKey);
      36             : 
      37         324 :         if (!cl.null && cl.first && cl.second && cl.third)
      38             :         {
      39         324 :                 printWarnings (cerr, parentKey, cl.verbose, cl.debug);
      40             :         }
      41         324 : }
      42             : 
      43         242 : void MountBaseCommand::outputMissingRecommends (std::vector<std::string> missingRecommends)
      44             : {
      45         242 :         if (!missingRecommends.empty ())
      46             :         {
      47           0 :                 std::cout << "Missing recommended plugins: ";
      48           0 :                 for (auto const & p : missingRecommends)
      49             :                 {
      50           0 :                         std::cout << p << " ";
      51             :                 }
      52             :                 std::cout << std::endl;
      53             :         }
      54         242 : }
      55             : 
      56             : 
      57             : /**
      58             :  * @brief set mp (interactive or by commandline)
      59             :  *
      60             :  * @see getName()
      61             :  */
      62         234 : void MountBaseCommand::getMountpoint (Cmdline const & cl)
      63             : {
      64         468 :         Key cur;
      65         468 :         std::vector<std::string> mountpoints;
      66        1170 :         mountpoints.push_back ("system/elektra");
      67         234 :         mountConf.rewind ();
      68        6620 :         while ((cur = mountConf.next ()))
      69             :         {
      70        3270 :                 if (cur.getBaseName () == "mountpoint")
      71             :                 {
      72         156 :                         mountpoints.push_back (cur.getString ());
      73             :                 };
      74             :         }
      75             : 
      76         234 :         if (cl.interactive)
      77             :         {
      78           0 :                 cout << "Already used are: ";
      79           0 :                 std::copy (mountpoints.begin (), mountpoints.end (), ostream_iterator<std::string> (cout, " "));
      80           0 :                 cout << endl;
      81           0 :                 cout << "Please start with / for a cascading backend" << endl;
      82           0 :                 cout << "Enter the mountpoint: ";
      83           0 :                 cin >> mp;
      84             :         }
      85             :         else
      86             :         {
      87         702 :                 mp = cl.createKey (1).getName ();
      88             :         }
      89         234 : }
      90             : 
      91         229 : void MountBaseCommand::askForConfirmation (Cmdline const & cl)
      92             : {
      93         229 :         if (cl.interactive)
      94             :         {
      95           0 :                 cout << endl;
      96           0 :                 cout << "Ready to mount with following configuration:" << endl;
      97           0 :                 cout << "Mountpoint: " << mp << endl;
      98           0 :                 cout << "Path:       " << path << endl;
      99             :         }
     100             : 
     101         229 :         if (cl.debug)
     102             :         {
     103           0 :                 cout << "The configuration which will be set is:" << endl;
     104           0 :                 mountConf.rewind ();
     105           0 :                 while (Key k = mountConf.next ())
     106             :                 {
     107           0 :                         cout << k.getName () << " " << k.getString () << endl;
     108             :                 }
     109             :         }
     110             : 
     111         229 :         if (cl.interactive)
     112             :         {
     113           0 :                 cout << "Are you sure you want to do that (y/N): ";
     114           0 :                 std::string answer;
     115           0 :                 cin >> answer;
     116           0 :                 if (answer != "y") throw CommandAbortException ();
     117             :         }
     118             : 
     119         229 :         if (cl.debug)
     120             :         {
     121           0 :                 cout << "Now writing the mountpoint configuration";
     122             :         }
     123         229 : }
     124             : 
     125           0 : class KDBMountException : public KDBException
     126             : {
     127             :         std::string msg;
     128             : 
     129             : public:
     130           0 :         explicit KDBMountException (std::string const & e) : KDBException (Key ()), msg (e)
     131             :         {
     132           0 :         }
     133             : 
     134           0 :         virtual const char * what () const noexcept override
     135             :         {
     136           0 :                 return msg.c_str ();
     137             :         }
     138             : };
     139             : 
     140             : /**
     141             :  * @brief Really write out config
     142             :  */
     143         242 : void MountBaseCommand::doIt ()
     144             : {
     145         968 :         Key parentKey (mountpointsPath, KEY_END);
     146             : 
     147             :         try
     148             :         {
     149         242 :                 kdb.set (mountConf, parentKey);
     150             :         }
     151           0 :         catch (KDBException const & e)
     152             :         {
     153           0 :                 throw KDBMountException (std::string (e.what ()) +
     154             :                                          "\n\n"
     155             :                                          "IMPORTANT: Sorry, I am unable to write your requested mountpoint to system/elektra/mountpoints.\n"
     156           0 :                                          "           Please make sure that you can write to `Configfile:` as mentioned above.\n" +
     157           0 :                                          getErrorColor (ANSI_COLOR::BOLD) + getErrorColor (ANSI_COLOR::YELLOW) +
     158           0 :                                          "           Usually you need to be root for this operation (try `sudo !!`)." +
     159           0 :                                          getErrorColor (ANSI_COLOR::RESET));
     160             :         }
     161             : 
     162         242 :         printWarnings (cerr, parentKey, true, true);
     163        7406 : }

Generated by: LCOV version 1.13