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 <remount.hpp>
10 :
11 : #include <backend.hpp>
12 : #include <backends.hpp>
13 : #include <cmdline.hpp>
14 : #include <helper/keyhelper.hpp>
15 : #include <kdb.hpp>
16 :
17 : using namespace std;
18 : using namespace kdb;
19 : using namespace kdb::tools;
20 : using namespace kdb::tools::helper;
21 :
22 158 : RemountCommand::RemountCommand ()
23 : {
24 79 : }
25 :
26 :
27 1 : void RemountCommand::getExistingMountpoint (Cmdline const & cl)
28 : {
29 3 : std::string search = cl.arguments[2];
30 4 : BackendInfo bi = Backends::findBackend (search, mountConf);
31 :
32 1 : if (bi.name.empty ())
33 : {
34 0 : throw invalid_argument ("could not find the mountpoint \"" + search + "\"");
35 : }
36 :
37 2 : existingName = bi.name;
38 1 : }
39 :
40 1 : void RemountCommand::cloneMountpoint (Cmdline const & cl)
41 : {
42 5 : Key existingParent (Backends::getBasePath (existingName), KEY_END);
43 5 : Key newParent (Backends::getBasePath (mp), KEY_END);
44 :
45 4 : KeySet existingBackend = mountConf.cut (existingParent);
46 2 : mountConf.append (existingBackend);
47 2 : KeySet newBackend (existingBackend.size (), KS_END);
48 3 : string configPath = newParent.getName () + "/config/path";
49 3 : string mpPath = newParent.getName () + "/mountpoint";
50 : existingBackend.rewind ();
51 89 : while (Key current = existingBackend.next ())
52 : {
53 58 : Key newKey = rebaseKey (current, existingParent, newParent);
54 29 : newBackend.append (newKey);
55 :
56 58 : if (newKey.getName () == mpPath)
57 : {
58 4 : newKey.setString (mp);
59 : }
60 :
61 58 : if (newKey.getName () == configPath)
62 : {
63 4 : newKey.setString (cl.arguments[0]);
64 : }
65 : }
66 :
67 2 : mountConf.append (newBackend);
68 1 : }
69 :
70 1 : int RemountCommand::execute (Cmdline const & cl)
71 : {
72 2 : if (cl.arguments.size () != 3) throw invalid_argument ("3 argument required");
73 :
74 1 : readMountConf (cl);
75 1 : getExistingMountpoint (cl);
76 1 : getMountpoint (cl);
77 1 : cloneMountpoint (cl);
78 1 : askForConfirmation (cl);
79 1 : doIt ();
80 :
81 1 : return 0;
82 : }
83 :
84 237 : RemountCommand::~RemountCommand ()
85 : {
86 158 : }
|