namespace Febe.Core.Library;
///
/// One line of /proc/self/mountinfo (proc(5)): unlike /proc/self/mounts it says which folder of its file
/// system a mount shows (: / when the mount is the root of that file system) and gives the exact
/// mount point, which the export needs for /esporta (spec §4.10, §11).
///
/// Mount id.
/// Id of the parent mount.
/// Folder of the file system shown at the mount point (/: its root).
/// Where it is mounted, unescaped.
/// Per-mount options (rw, ro, noatime…).
/// File system type (ext4, vfat, cifs…).
/// Mount source (/dev/sdb1, //server/share…), unescaped.
/// The device of the file system, major:minor (field 3): two mounts with the same device show
/// folders of one file system, so tells whether one is inside the other.
public sealed record MountInfoEntry(int Id, int ParentId, string Root, string MountPoint, IReadOnlyList Options, string FileSystemType, string Source, string? Device = null)
{
/// The mount is read-only (ro, as for a Docker volume mounted with :ro).
public bool ReadOnly => Options.Contains("ro", StringComparer.Ordinal);
}
/// Reads /proc/self/mountinfo.
public static class MountInfoTable
{
private const string MountInfoPath = "/proc/self/mountinfo";
///
/// Parses the content of /proc/self/mountinfo: fields 0-5, then optional fields up to the separator «-», then
/// file system type and source. Malformed lines are skipped; nothing throws.
///
public static IReadOnlyList Parse(string content)
{
ArgumentNullException.ThrowIfNull(content);
var entries = new List();
foreach (var raw in content.Split('\n'))
{
var fields = raw.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (fields.Length < 9
|| !int.TryParse(fields[0], System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out var id)
|| !int.TryParse(fields[1], System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out var parent))
{
continue;
}
var separator = Array.IndexOf(fields, "-", 6);
if (separator < 0 || separator + 2 >= fields.Length)
{
continue;
}
var root = MountTable.Unescape(fields[3]);
var point = MountTable.Unescape(fields[4]);
if (!root.StartsWith('/') || !point.StartsWith('/'))
{
continue;
}
entries.Add(new MountInfoEntry(
id,
parent,
root,
point,
fields[5].Split(',', StringSplitOptions.RemoveEmptyEntries),
fields[separator + 1],
MountTable.Unescape(fields[separator + 2]),
fields[2]));
}
return entries;
}
///
/// The mount that holds a path: the longest mount point that is the path itself or one of its parents (ordinal
/// comparison; a later mount over the same point wins, as the kernel shows the last one). Null when none matches.
///
public static MountInfoEntry? FindContaining(IReadOnlyList mounts, string fullPath)
{
ArgumentNullException.ThrowIfNull(mounts);
ArgumentNullException.ThrowIfNull(fullPath);
var path = fullPath.Length > 1 ? fullPath.TrimEnd('/') : fullPath;
MountInfoEntry? best = null;
var bestLength = -1;
foreach (var mount in mounts)
{
var point = mount.MountPoint.Length > 1 ? mount.MountPoint.TrimEnd('/') : mount.MountPoint;
var contains = point == "/"
|| string.Equals(path, point, StringComparison.Ordinal)
|| path.StartsWith(point + "/", StringComparison.Ordinal);
if (contains && point.Length >= bestLength)
{
best = mount;
bestLength = point.Length;
}
}
return best;
}
/// The content of /proc/self/mountinfo on Linux; null elsewhere or when it cannot be read.
public static string? ReadProcSelf()
{
if (!OperatingSystem.IsLinux())
{
return null;
}
try
{
return File.Exists(MountInfoPath) ? File.ReadAllText(MountInfoPath) : null;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
return null;
}
}
}