Added mountinfo_root_resolver and removed...

mount_parser.
This commit is contained in:
snake-4
2024-04-28 14:09:15 +02:00
parent 2a31d654a6
commit 7cbb41bbb6
6 changed files with 98 additions and 158 deletions

View File

@@ -3,19 +3,39 @@
#include <fstream>
#include <vector>
#include <unordered_map>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include "mountinfo_parser.hpp"
#include "mount_parser.hpp"
#include "logging.hpp"
using namespace Parsers;
mountinfo_entry_t::mountinfo_entry_t(int mount_id, int parent_id, int major, int minor,
static std::unordered_map<std::string, std::string> parseMountOptions(const std::string &input)
{
std::unordered_map<std::string, std::string> ret;
std::istringstream iss(input);
std::string token;
while (std::getline(iss, token, ','))
{
std::istringstream tokenStream(token);
std::string key, value;
if (std::getline(tokenStream, key, '='))
{
std::getline(tokenStream, value); // Put what's left in the stream to value, could be empty
ret[key] = value;
}
}
return ret;
}
mountinfo_entry_t::mountinfo_entry_t(int mount_id, int parent_id, dev_t device,
const std::string &root, const std::string &mount_point,
const std::string &mount_options, const std::string &optional_fields,
const std::string &filesystem_type, const std::string &mount_source,
const std::string &super_options)
: mount_id(mount_id), parent_id(parent_id), major(major), minor(minor),
: mount_id(mount_id), parent_id(parent_id), device(device),
root(root), mount_point(mount_point),
optional_fields(optional_fields), filesystem_type(filesystem_type),
mount_source(mount_source)
@@ -26,8 +46,7 @@ mountinfo_entry_t::mountinfo_entry_t(int mount_id, int parent_id, int major, int
int mountinfo_entry_t::getMountId() const { return mount_id; }
int mountinfo_entry_t::getParentId() const { return parent_id; }
int mountinfo_entry_t::getMajor() const { return major; }
int mountinfo_entry_t::getMinor() const { return minor; }
dev_t mountinfo_entry_t::getDevice() const { return device; }
const std::string &mountinfo_entry_t::getRoot() const { return root; }
const std::string &mountinfo_entry_t::getMountPoint() const { return mount_point; }
const std::unordered_map<std::string, std::string> &mountinfo_entry_t::getMountOptions() const { return mount_options; }
@@ -56,12 +75,12 @@ const std::vector<mountinfo_entry_t> &Parsers::parseSelfMountinfo(bool cached)
{
std::istringstream iss(line);
int mount_id, parent_id, major, minor;
int mount_id, parent_id, _major, _minor;
std::string root, mount_point, mount_options, optional_fields, filesystem_type, mount_source, super_options;
char colon;
// Read the first 6 fields (major, colon and minor are the same field)
iss >> mount_id >> parent_id >> major >> colon >> minor >> root >> mount_point >> mount_options;
iss >> mount_id >> parent_id >> _major >> colon >> _minor >> root >> mount_point >> mount_options;
if (iss.fail())
{
LOGE("parseSelfMountinfo failed to parse the first 6 fields of line: %s", line.c_str());
@@ -87,7 +106,7 @@ const std::vector<mountinfo_entry_t> &Parsers::parseSelfMountinfo(bool cached)
continue;
}
parser_cache.emplace_back(mountinfo_entry_t(mount_id, parent_id, major, minor,
parser_cache.emplace_back(mountinfo_entry_t(mount_id, parent_id, makedev(_major, _minor),
root, mount_point, mount_options,
optional_fields, filesystem_type, mount_source,
super_options));
@@ -95,3 +114,29 @@ const std::vector<mountinfo_entry_t> &Parsers::parseSelfMountinfo(bool cached)
return parser_cache;
}
mountinfo_root_resolver::mountinfo_root_resolver(const std::vector<mountinfo_entry_t> &mount_infos)
{
for (const auto &mount_info : mount_infos)
{
if (mount_info.getRoot() == "/")
{
device_mount_map[mount_info.getDevice()] = mount_info.getMountPoint();
}
}
}
std::string mountinfo_root_resolver::resolveRootOf(const mountinfo_entry_t &mount_info) const
{
auto dev = mount_info.getDevice();
if (device_mount_map.contains(dev))
{
const auto &mount_root = device_mount_map.at(dev);
// If mount root is /, mount_info root will already be the true root
if (mount_root != "/")
return mount_root + mount_info.getRoot();
}
return mount_info.getRoot();
}