int CpCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 2) { throw invalid_argument ("wrong number of arguments, 2 needed"); } KeySet conf; Key sourceKey = cl.createKey (0); if (!sourceKey.isValid ()) { throw invalid_argument ("Source given is not a valid keyname"); } Key destKey = cl.createKey (1); if (!destKey.isValid ()) { throw invalid_argument ("Destination given is not a valid keyname"); } string newDirName = destKey.getName (); kdb.get (conf, sourceKey); kdb.get (conf, destKey); KeySet tmpConf = conf; KeySet oldConf; oldConf.append (tmpConf.cut (sourceKey)); KeySet newConf; oldConf.rewind (); std::string sourceName = sourceKey.getName (); if (cl.verbose) cout << "common name: " << sourceName << endl; if (cl.recursive) { // copy all keys with new name Key k; while ((k = oldConf.next ())) { Key rk = rename_key (k, sourceName, newDirName, cl.verbose); copySingleKey (cl, rk, tmpConf, newConf); } } else { // just copy one key Key k = oldConf.next (); Key rk = rename_key (k, sourceName, newDirName, cl.verbose); copySingleKey (cl, rk, tmpConf, newConf); } newConf.append (tmpConf); // these are unrelated keys newConf.append (oldConf); // these are the original keys newConf.rewind (); kdb.set (newConf, destKey); return 0; }
int FstabCommand::execute (Cmdline const & cl) { int argc = cl.arguments.size (); if (argc != 5 && argc != 6 && argc != 7) { throw invalid_argument ("number of arguments not correct, need 5, 6 or 7"); } KeySet conf; Key parentKey = cl.createKey (0); kdb.get (conf, parentKey); printWarnings (cerr, parentKey); Key k = conf.lookup (parentKey); if (!k) { k = cl.createKey (0); conf.append (k); } std::string keyname = k.getName (); string dumpfreq = "0"; if (argc >= 6) { dumpfreq = cl.arguments[5].c_str (); } string passno = "0"; if (argc >= 7) { passno = cl.arguments[6].c_str (); } kdb::KeySet config (20, *kdb::Key (keyname + "/ZZZNewFstabName", KEY_END), *kdb::Key (keyname + "/ZZZNewFstabName/device", KEY_VALUE, cl.arguments[1].c_str (), KEY_END), *kdb::Key (keyname + "/ZZZNewFstabName/mpoint", KEY_VALUE, cl.arguments[2].c_str (), KEY_END), *kdb::Key (keyname + "/ZZZNewFstabName/type", KEY_VALUE, cl.arguments[3].c_str (), KEY_END), *kdb::Key (keyname + "/ZZZNewFstabName/options", KEY_VALUE, cl.arguments[4].c_str (), KEY_END), *kdb::Key (keyname + "/ZZZNewFstabName/dumpfreq", KEY_VALUE, dumpfreq.c_str (), KEY_END), *kdb::Key (keyname + "/ZZZNewFstabName/passno", KEY_VALUE, passno.c_str (), KEY_END), KS_END); conf.append (config); if (cl.verbose) { cout << conf; } kdb.set (conf, parentKey); printWarnings (cerr, parentKey); return 0; }
int LsCommand::execute (Cmdline const & cl) { checkArguments (cl); printWarnings (cerr, root); root = cl.createKey (0); kdb.get (ks, root); if (cl.verbose) cout << "size of all keys in mountpoint: " << ks.size () << endl; KeySet part (ks.cut (root)); if (cl.verbose) cout << "size of requested keys: " << part.size () << endl; cout.setf (std::ios_base::unitbuf); if (cl.null) { cout.unsetf (std::ios_base::skipws); } printResults (part, getDepth (root), cl); printWarnings (cerr, root); return 0; }
int FileCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument"); KeySet conf; Key x = cl.createKey (0); if (x.getName ()[0] == '/') { x.setName (cl.ns + x.getName ()); std::cerr << "Using name " << x.getName () << std::endl; } if (!x.isValid ()) { throw invalid_argument (cl.arguments[0] + " is not a valid keyname"); } kdb.get (conf, x); cout << x.getString (); if (!cl.noNewline) { cout << endl; } return 0; }
int GetCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument"); KeySet conf; kdb::Key root = cl.createKey (0); kdb::KDB kdb (root); std::string n; if (cl.all) { n = root.getName (); root.setName ("/"); } kdb.get (conf, root); if (cl.all) { root.setName (n); } // do a lookup without tracer to warm up default cache conf.lookup (root); root.setCallback (warnOnMeta); if (cl.verbose) { cout << "got " << conf.size () << " keys" << std::endl; root.setCallback (printTrace); } Key k = conf.lookup (root); int ret = 0; if (k) { if (cl.verbose) { cout << "The resulting keyname is " << k.getName () << std::endl; } cout << k.getString (); } else { cerr << "Did not find key"; ret = 1; } if (!cl.noNewline) { cout << endl; } printWarnings (cerr, root); printError (cerr, root); return ret; }
int LsCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 1) { throw invalid_argument ("1 argument required"); } printWarnings (cerr, root); root = cl.createKey (0); kdb.get (ks, root); if (cl.verbose) cout << "size of all keys in mountpoint: " << ks.size () << endl; KeySet part (ks.cut (root)); if (cl.verbose) cout << "size of requested keys: " << part.size () << endl; cout.setf (std::ios_base::unitbuf); if (cl.null) { cout.unsetf (std::ios_base::skipws); } cout << part; printWarnings (cerr, root); return 0; }
int MetaRemoveCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 2) { throw invalid_argument ("Need 2 arguments"); } Key parentKey = cl.createKey (0); string metaname = cl.arguments[1]; KeySet conf; kdb.get (conf, parentKey); printWarnings (cerr, parentKey); Key k = conf.lookup (parentKey); if (!k) { cerr << "Key not found" << endl; return 1; } k.delMeta (metaname); kdb.set (conf, parentKey); return 0; }
int MetaSetCommand::execute (Cmdline const & cl) { if (cl.arguments.size () < 2 || cl.arguments.size () > 3) { throw invalid_argument ("Need 2 or 3 arguments"); } string metaname = cl.arguments[1]; Key parentKey = cl.createKey (0); string keyname = parentKey.getName (); if (keyname[0] == '/') { // fix name for lookup keyname = "spec" + keyname; if (!cl.quiet) std::cout << "Using keyname " << keyname << std::endl; // fix k for kdb.set later parentKey.setName (keyname); } KeySet conf; kdb.get (conf, parentKey); Key k = conf.lookup (parentKey); if (!k) { k = Key (keyname, KEY_END); // k.setBinary(0, 0); // conceptually maybe better, but would have confusing "binary" metadata conf.append (k); if (cl.verbose) cout << "Creating key " << keyname << endl; } if (!k.isValid ()) { cerr << "Could not create key " << keyname << endl; return 1; } if (cl.arguments.size () == 2) { if (!cl.quiet) cout << "Only two arguments, thus deleting metaname " << metaname << endl; k.delMeta (metaname); } else { std::string metavalue = cl.arguments[2]; if (metaname == "atime" || metaname == "mtime" || metaname == "ctime") { stringstream str (metavalue); time_t t; str >> t; if (!str.good ()) throw "conversion failure"; k.setMeta<time_t> (metaname, t); } else {
void SpecMountCommand::setMountpoint (Cmdline const & cl) { if (cl.arguments.empty ()) { throw invalid_argument ("you need to provide one argument: spec-mountpoint"); } mp = cl.createKey (0).getName (); if (mp.at (0) != '/') { throw invalid_argument (mp + " is not a cascading mountpoint"); } }
int MetaLsCommand::execute (Cmdline const & cl) { int ret = 0; if (cl.arguments.size () != 1) { throw invalid_argument ("1 argument required"); } Key root = cl.createKey (0); kdb.get (ks, root); Key k = ks.lookup (root); if (k) { if (cl.verbose) { std::cout << "Got key " << k.getName () << std::endl; } k.rewindMeta (); while (const Key meta = k.nextMeta ()) { cout << meta.getName (); if (cl.null) { cout << '\0' << std::flush; } else { cout << endl; } } } else { std::cerr << "Did not find key" << std::endl; ret = 1; } printWarnings (cerr, root); return ret; }
int ExportCommand::execute (Cmdline const & cl) { size_t argc = cl.arguments.size (); if (argc != 1 && argc != 2 && argc != 3) { throw invalid_argument ("need 1 to 3 arguments"); } Key root = cl.createKey (0); kdb.get (ks, root); printWarnings (cerr, root); KeySet part (ks.cut (root)); if (cl.withoutElektra) { Key systemElektra ("system/elektra", KEY_END); part.cut (systemElektra); } string format = cl.format; if (argc > 1) format = cl.arguments[1]; #ifdef _WIN32 string file = "CON"; #else string file = "/dev/stdout"; #endif if (argc > 2 && cl.arguments[2] != "-") file = cl.arguments[2]; Modules modules; PluginPtr plugin = modules.load (format, cl.getPluginsConfig ()); Key errorKey (root); errorKey.setString (file); plugin->set (part, errorKey); printWarnings (cerr, errorKey); printError (cerr, errorKey); return 0; }
int TestCommand::execute(Cmdline const& cl) { if (cl.arguments.size() < 1) { throw invalid_argument ("need at least one argument"); } // do a basic check on every argument for (size_t i=1; i<cl.arguments.size(); ++i) { string name = " "; name += cl.arguments[i]; name += " "; if (testNames.find(name) == std::string::npos) { throw invalid_argument ("test name " + cl.arguments[i] + " does not exist in:" + testNames); } } printWarnings(cerr, root); root = cl.createKey(0); KDB kdb; KeySet original; kdb.get(original, root); original.rewind(); doTests(cl.arguments); cerr << "We got " << nrError << " errors in " << nrTest << " testcases." << endl; cout << "Test suite is now finished." << endl; cout << "Now restoring the original keyset." << endl; kdb.set(original, root); printWarnings(cerr, root); return nrError; }
int ValidationCommand::execute(Cmdline const& cl) { size_t argc = cl.arguments.size(); if (argc != 3 && argc != 4) { throw invalid_argument("need 3 or 4 arguments"); } KeySet conf; Key parentKey = cl.createKey(0); string keyname = parentKey.getName(); kdb.get(conf, parentKey); Key k = conf.lookup(keyname); if (!k) { k = Key(keyname, KEY_END); conf.append (k); } if (!k.isValid()) { throw invalid_argument("keyname not valid"); } string value = cl.arguments[1]; string validationregex = cl.arguments[2]; string validationmessage; if (argc == 4) validationmessage = cl.arguments[3]; else validationmessage = "Regular expression " + validationregex + " does not match the supplied value"; k.setString (value); k.setMeta<string> ("validation/regex", validationregex); k.setMeta<string> ("validation/message", validationmessage); kdb.set(conf,parentKey); return 0; }
int UmountCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 1) throw invalid_argument ("1 argument required"); KeySet conf; Key parentKey (Backends::mountpointsPath, KEY_END); kdb.get (conf, parentKey); printWarnings (cerr, parentKey); std::string name = cl.createKey (0).getName (); if (cl.verbose) Backends::findBackend (name, conf, true); if (Backends::umount (name, conf) == 0) { cerr << "Mountpoint " << name << " does not exist" << endl; return 1; } kdb.set (conf, parentKey); return 0; }
int MetaGetCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 2) { throw invalid_argument ("Need 2 arguments"); } Key parentKey = cl.createKey (0); string metaname = cl.arguments[1]; KeySet conf; kdb.get (conf, parentKey); printWarnings (cerr, parentKey); Key k = conf.lookup (parentKey); if (!k) { cerr << "Key not found" << endl; return 1; } if (!k.getMeta<const Key> (metaname)) { cerr << "Metakey not found" << endl; return 2; } cout << k.getMeta<string> (metaname); if (!cl.noNewline) { cout << endl; } return 0; }
int ImportCommand::execute (Cmdline const & cl) { size_t argc = cl.arguments.size (); if (argc != 1 && argc != 2 && argc != 3) { throw invalid_argument ("need 1 to 3 arguments"); } Key root = cl.createKey (0); if (!root.isValid ()) { throw invalid_argument ("root key \"" + cl.arguments[0] + "\" is not a valid key name"); } KeySet originalKeys; kdb.get (originalKeys, root); KeySet base = originalKeys.cut (root); printWarnings (cerr, root); string format = cl.format; if (argc > 1) format = cl.arguments[1]; string file = "/dev/stdin"; if (argc > 2 && cl.arguments[2] != "-") file = cl.arguments[2]; Modules modules; PluginPtr plugin = modules.load (format, cl.getPluginsConfig ()); Key errorKey (root); errorKey.setString (file); KeySet importedKeys; plugin->get (importedKeys, errorKey); importedKeys = importedKeys.cut (root); printWarnings (cerr, errorKey); printError (cerr, errorKey); ThreeWayMerge merger; MergeHelper helper; helper.configureMerger (cl, merger); MergeResult result = merger.mergeKeySet ( MergeTask (BaseMergeKeys (base, root), OurMergeKeys (base, root), TheirMergeKeys (importedKeys, root), root)); helper.reportResult (cl, result, cout, cerr); int ret = -1; if (!result.hasConflicts ()) { if (cl.verbose) { cout << "The merged keyset with strategy " << cl.strategy << " is:" << endl; cout << result.getMergedKeys (); } KeySet resultKeys = result.getMergedKeys (); originalKeys.append (resultKeys); kdb.set (originalKeys, root); ret = 0; } return ret; }
int MvCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 2) { throw invalid_argument ("wrong number of arguments, 2 needed"); } KeySet conf; Key sourceKey = cl.createKey (0, false); Key destKey = cl.createKey (1, false); string newDirName = destKey.getName (); Key root = tools::helper::commonKeyName (sourceKey, destKey); if (cl.verbose) std::cout << "using common basename: " << root.getName () << std::endl; kdb.get (conf, root); KeySet tmpConf = conf; KeySet oldConf; oldConf.append (tmpConf.cut (sourceKey)); std::string sourceName = sourceKey.getName (); if (!oldConf.size ()) { std::cerr << "No key to copy found below '" << sourceName << "'" << std::endl; return 11; } KeySet newConf; Key k; oldConf.rewind (); if (cl.recursive) { while ((k = oldConf.next ())) { newConf.append (rename_key (k, sourceName, newDirName, cl.verbose)); } } else { // just rename one key k = oldConf.next (); if (k != sourceKey) { cerr << "First key found " << k.getName () << " does not exactly match given key " << sourceKey.getName () << ", aborting (use -r to move hierarchy)\n"; return 11; } newConf.append (rename_key (k, sourceName, newDirName, cl.verbose)); } newConf.append (tmpConf); // these are unrelated keys // drop the original configuration newConf.rewind (); if (cl.verbose) { cout << "Will write out:" << endl; cout << newConf; } kdb.set (newConf, root); printWarnings (cerr, root); return 0; }
int GetCommand::execute (Cmdline const & cl) { if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument"); KeySet conf; kdb::Key root = cl.createKey (0); kdb::KDB kdb (root); std::string n; if (cl.all) { n = root.getName (); root.setName ("/"); } kdb.get (conf, root); if (cl.all) { root.setName (n); } // do a lookup without tracer to warm up default cache conf.lookup (root); root.setCallback (warnOnMeta); if (cl.verbose) { cout << "got " << conf.size () << " keys" << std::endl; root.setCallback (printTrace); } Key k = conf.lookup (root); int ret = 0; if (k) { if (cl.verbose) { cout << "The resulting keyname is " << k.getName () << std::endl; cout << "The resulting value size is " << k.getStringSize () << std::endl; } if (k.isBinary ()) { if (cl.verbose) { if (k.getBinarySize () == 0) { cout << "The key is null." << std::endl; } else { cout << "The key is binary." << std::endl; } } cout << std::hex; const uint8_t * data = static_cast<const uint8_t *> (k.getValue ()); for (auto position = 0; position < k.getBinarySize (); position++) { cout << "\\x" << unsigned(data[position]); } cout << std::dec; } else { cout << k.getString (); } } else { cerr << "Did not find key '" << root.getName () << "'"; ret = 11; } if (!cl.noNewline) { cout << endl; } printWarnings (cerr, root); printError (cerr, root); return ret; }
int MergeCommand::execute(Cmdline const& cl) { if (cl.arguments.size () < 4) { throw invalid_argument ("wrong number of arguments, 4 needed"); } Key oursRoot = cl.createKey(0); Key theirsRoot = cl.createKey(1); Key baseRoot = cl.createKey(2); Key resultRoot = cl.createKey(3); KeySet ours; KeySet theirs; KeySet base; { KDB lkdb; lkdb.get (ours, oursRoot); ours = ours.cut (oursRoot); ours.lookup(oursRoot, KDB_O_POP); if (cl.verbose) std::cout << "we got ours: " << oursRoot << " with keys " << ours << std::endl; } { KDB lkdb; lkdb.get (theirs, theirsRoot); theirs = theirs.cut (theirsRoot); ours.lookup(oursRoot, KDB_O_POP); if (cl.verbose) std::cout << "we got theirs: " << theirsRoot << " with keys " << theirs << std::endl; } { KDB lkdb; lkdb.get (base, baseRoot); base = base.cut (baseRoot); ours.lookup(oursRoot, KDB_O_POP); if (cl.verbose) std::cout << "we got base: " << baseRoot << " with keys " << base << std::endl; } KeySet resultKeys; kdb.get (resultKeys, resultRoot); KeySet discard = resultKeys.cut (resultRoot); if (discard.size () != 0) { if (cl.force) { if (cl.verbose) { std::cout << "will remove " << discard.size () << " keys, because -f was given" << std::endl; } } else { std::cerr << discard.size () << " keys exist in merge resultroot, will quit. Use -f to override the keys there." << std::endl; } } MergeHelper helper; ThreeWayMerge merger; helper.configureMerger (cl, merger); MergeResult result = merger.mergeKeySet ( MergeTask (BaseMergeKeys (base, baseRoot), OurMergeKeys (ours, oursRoot), TheirMergeKeys (theirs, theirsRoot), resultRoot)); helper.reportResult (cl, result, cout, cerr); int ret = 0; if (!result.hasConflicts ()) { resultKeys.append(result.getMergedKeys()); kdb.set (resultKeys, resultRoot); } else { ret = -1; } return ret; }
int EditorCommand::execute(Cmdline const& cl) { #ifdef _WIN32 throw EditorNotAvailable(); #endif int argc = cl.arguments.size (); if (argc < 1) { throw invalid_argument ("wrong number of arguments, 1 needed"); } Key root = cl.createKey(0); KeySet ours; KDB kdb; kdb.get (ours, root); KeySet oursToEdit = ours.cut (root); // export it to file string format = cl.format; if (argc > 1) format = cl.arguments[1]; Modules modules; PluginPtr plugin = modules.load(format); tmpFile(); if (cl.verbose) std::cout << "filename set to " << filename << std::endl; Key errorKey(root); errorKey.setString(filename); if (plugin->set(oursToEdit, errorKey) == -1) { printWarnings(cerr, errorKey); printError(cerr, errorKey); return 11; } printWarnings(cerr, errorKey); // start editor if (cl.verbose) std::cout << "running editor with " << filename << std::endl; if (!cl.editor.empty()) { if (!runEditor (cl.editor, filename)) { std::cerr << "Could not run editor " << cl.editor << std::endl; return 12; } } else { if (!runAllEditors(filename)) { std::cerr << "Could not run any editor, please change /sw/elektra/kdb/#0/current/editor" << std::endl; return 12; } } // import from the file KeySet importedKeys; plugin->get(importedKeys, errorKey); importedKeys = importedKeys.cut(root); printWarnings (cerr, errorKey); printError (cerr, errorKey); ThreeWayMerge merger; MergeHelper helper; helper.configureMerger (cl, merger); MergeResult result = merger.mergeKeySet ( MergeTask (BaseMergeKeys (oursToEdit, root), OurMergeKeys (oursToEdit, root), TheirMergeKeys (importedKeys, root), root)); helper.reportResult (cl, result, cout, cerr); int ret = 13; if (!result.hasConflicts ()) { if (cl.verbose) { cout << "The merged keyset with strategy " << cl.strategy << " is:" << endl; cout << result.getMergedKeys(); } KeySet resultKeys = result.getMergedKeys(); if (cl.verbose) std::cout << "about to write result keys " << resultKeys << std::endl; ours.append(resultKeys); kdb.set (ours, root); if (cl.verbose) std::cout << "successful, cleaning up " << filename << std::endl; unlink(filename.c_str()); ret = 0; } else { std::cout << "Import not successful, please import and remove \"" << filename << '"' << std::endl; } return ret; }