/** * @brief give info about current mounted backends * * @param mountConf a keyset that contains everything below * Backends::mountpointsPath * * @return an vector of information about mounted backends */ Backends::BackendInfoVector Backends::getBackendInfo (KeySet mountConf) { std::vector<BackendInfo> ret; Key rootKey (Backends::mountpointsPath, KEY_END); Key cur; mountConf.rewind (); while ((cur = mountConf.next ())) { if (cur.isDirectBelow (rootKey)) { BackendInfo bi; Key path = mountConf.lookup (cur.getName () + "/config/path"); if (path) { bi.path = path.getString (); } Key mp = mountConf.lookup (cur.getName () + "/mountpoint"); if (mp) { bi.mountpoint = mp.getString (); } bi.name = cur.getBaseName (); ret.push_back (bi); } } return ret; }
/** * @brief set mp (interactive or by commandline) * * @see getName() */ void MountBaseCommand::getMountpoint (Cmdline const & cl) { Key cur; std::vector<std::string> mountpoints; mountpoints.push_back ("system/elektra"); mountConf.rewind (); while ((cur = mountConf.next ())) { if (cur.getBaseName () == "mountpoint") { if (cur.getString ().at (0) == '/') { mountpoints.push_back (Key ("user" + cur.getString (), KEY_END).getName ()); mountpoints.push_back (Key ("system" + cur.getString (), KEY_END).getName ()); } else { mountpoints.push_back (cur.getString ()); } }; } if (cl.interactive) { cout << "Already used are: "; std::copy (mountpoints.begin (), mountpoints.end (), ostream_iterator<std::string> (cout, " ")); cout << endl; cout << "Please start with / for a cascading backend" << endl; cout << "Enter the mountpoint: "; cin >> mp; }
/** * @brief (Recursively) add or update a key to nodes * * If the key exists, it will be updated * * If the key does not exist, node(s) will be created * * @param k the key to add * @param depth current depth of recursion */ void add(Key k, unsigned long depth) { assert(k); assert(m_self ? m_self.isBelow(k) : true); depth++; if (m_self.isDirectBelow(k) || depth == name_depth(k)) { for (KeyNodeIterator it = m_subnodes.begin(); it != m_subnodes.end(); ++it) { if (it->first == k.getBaseName()) { // found node, update it it->second.m_self = k; return; } } // will add new subnode (direct below+not found) m_subnodes.insert(std::make_pair(k.getBaseName(), KeyNode(depth, k))); return; } for (KeyNodeIterator it = m_subnodes.begin(); it != m_subnodes.end(); ++it) { if (k.isBelow(it->second.m_self)) { // found subnode, call recursively it->second.add(k, depth); return; } } // create a structure key (without key, only name) std::string name = nth_level_of_name(k, depth); std::pair<KeyNodeIterator, bool> p = m_subnodes.insert( std::make_pair(name, KeyNode(depth))); // structure keys get a null key KeyNodeIterator it = p.first; it->second.add(k, depth); }
/** * @brief Find a backend in the given name * * @param mountPath the given backend name to find * * For backwards compatibility old-style names containing _ instead of escaped / * are accepted if no modern-style mountpoint is found. * * @param mountConf the configuration to search (should contain keys * below mountpointsPath to find something) * * @return the found backend or an empty BackendInfo if nothing found * (with empty strings) */ BackendInfo Backends::findBackend (std::string const & mountPath, KeySet mountConf, bool verbose) { BackendInfo ret; if (mountPath.empty ()) return ret; Backends::BackendInfoVector mtab = Backends::getBackendInfo (mountConf); Key kmp (Backends::getBasePath (mountPath), KEY_END); // search for proper mountname: for (Backends::BackendInfoVector::const_iterator it = mtab.begin (); it != mtab.end (); ++it) { if (verbose) std::cout << "compare: " << it->mountpoint << " with " << kmp.getBaseName () << std::endl; if (it->mountpoint == kmp.getBaseName ()) { return *it; } }; // fall back to compatibility pre 0.8.11 mountnames // May umount wrong things, so its done as extra step so that // it will never happen if something desired is present. std::string soldMountpoint = mountPath; std::replace (soldMountpoint.begin (), soldMountpoint.end (), '_', '/'); Key koldMountpoint ("user/" + soldMountpoint, KEY_END); std::string omp = koldMountpoint.getName (); std::string oldMountpoint (omp.begin () + 4, omp.end ()); if (soldMountpoint.at (0) != '/') oldMountpoint.erase (0, 1); // fix non-cascading if (koldMountpoint.getName () == "user") oldMountpoint = "/"; // fix root for (Backends::BackendInfoVector::const_iterator it = mtab.begin (); it != mtab.end (); ++it) { if (verbose) std::cout << "fallback compare: " << it->mountpoint << " with " << oldMountpoint << std::endl; if (it->mountpoint == oldMountpoint) { return *it; } } return ret; }
int InfoCommand::execute (Cmdline const & cl) { std::string subkey; if (cl.arguments.size () == 1) { } else if (cl.arguments.size () == 2) { subkey = cl.arguments[1]; } else { throw invalid_argument ("Need at 1 or 2 argument(s)"); } std::string name = cl.arguments[0]; KeySet conf; Key parentKey (std::string ("system/elektra/modules/") + name, KEY_END); if (!cl.load) { kdb.get (conf, parentKey); } if (!conf.lookup (parentKey)) { if (!cl.load) { cerr << "Module does not seem to be loaded." << endl; cerr << "Now in fallback code. Will directly load config from plugin." << endl; } Modules modules; KeySet ks = cl.getPluginsConfig (); PluginPtr plugin; if (ks.size () == 0) { plugin = modules.load (name); } else { plugin = modules.load (name, ks); } conf.append (plugin->getInfo ()); } Key root (std::string ("system/elektra/modules/") + name + "/exports", KEY_END); if (!subkey.empty ()) { root.setName (std::string ("system/elektra/modules/") + name + "/infos/" + subkey); Key k = conf.lookup (root); if (k) { cout << k.getString () << std::endl; return 0; } else { cerr << "clause not found" << std::endl; return 1; } } root.setName (std::string ("system/elektra/modules/") + name + "/exports"); Key k = conf.lookup (root); if (k) { cout << "Exported symbols: "; while ((k = conf.next ()) && k.isBelow (root)) { cout << k.getBaseName () << " "; } cout << endl; } else cout << "no exported symbols found" << endl; root.setName (std::string ("system/elektra/modules/") + name + "/infos"); k = conf.lookup (root); if (k) { while ((k = conf.next ()) && k.isBelow (root)) { cout << k.getBaseName () << ": " << k.getString () << endl; } } else cout << "no information found" << endl; return 0; }