Files
Febe/src/Febe.Core/Library/MountInfo.cs
T
Alby96andClaude Opus 5.5 36ad5c6f9d
verifica / controlli (push) Canceled after 0s
verifica / prove (push) Canceled after 0s
verifica / installatore (push) Canceled after 0s
verifica / immagine (push) Canceled after 0s
E2: correzioni della revisione delle copie esterne
- Nome libero scelto prima di scrivere, libero sul disco e nel database;
  una riga orfana sul nome diventa «Tolto» (guardando il suo percorso,
  anche se differisce solo per le maiuscole): prima la copia finiva
  «Non riuscito» con il file già scritto, o toglieva la copia buona.
- «Controlla le copie» rilegge la radice e riguarda i file mancanti;
  con il disco staccato si ferma e mette in pausa senza segnare nulla.
- Su Linux lo spostamento sul nome finale non sostituisce mai un file
  (renameat2 RENAME_NOREPLACE, poi link e unlink).
- Le copie in coda di un lavoro interrotto diventano «Interrotta» e si
  riprendono; il permesso dei lavori lunghi si tiene già nel piano.
- «Superato» con il file di nuovo in libreria: filtro, piano e «Copia di
  nuovo» d'accordo; l'adozione ravviva la riga invece di fallire.
- «Una copia sola» con una regola sola in filtro, piano, visualizzatore
  e copiatore (solo copie di un altro account in cartelle in uso).
- Controllo della cartella per identità vera (percorso finale, realpath,
  dispositivo di mountinfo): riconosce collegamenti e bind mount di una
  sottocartella della libreria.
- Sfoglia delle cartelle con tempi massimi; «Nessuna cartella» solo se
  non ce n'è; «In copia…» nel visualizzatore; numero e fuoco stabili in
  «Copia di nuovo anche…»; una sola lettura per volta nella Dashboard.
- Prove dell'interfaccia indipendenti dall'ora e dal file scelto; prove
  nuove in ExportReviewTests, ExportFolderCheckTests, ExportRootTests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 17:02:06 +01:00

118 lines
5.0 KiB
C#

namespace Febe.Core.Library;
/// <summary>
/// One line of <c>/proc/self/mountinfo</c> (proc(5)): unlike <c>/proc/self/mounts</c> it says which folder of its file
/// system a mount shows (<see cref="Root"/>: <c>/</c> when the mount is the root of that file system) and gives the exact
/// mount point, which the export needs for <c>/esporta</c> (spec §4.10, §11).
/// </summary>
/// <param name="Id">Mount id.</param>
/// <param name="ParentId">Id of the parent mount.</param>
/// <param name="Root">Folder of the file system shown at the mount point (<c>/</c>: its root).</param>
/// <param name="MountPoint">Where it is mounted, unescaped.</param>
/// <param name="Options">Per-mount options (<c>rw</c>, <c>ro</c>, <c>noatime</c>…).</param>
/// <param name="FileSystemType">File system type (<c>ext4</c>, <c>vfat</c>, <c>cifs</c>…).</param>
/// <param name="Source">Mount source (<c>/dev/sdb1</c>, <c>//server/share</c>…), unescaped.</param>
/// <param name="Device">The device of the file system, <c>major:minor</c> (field 3): two mounts with the same device show
/// folders of one file system, so <see cref="Root"/> tells whether one is inside the other.</param>
public sealed record MountInfoEntry(int Id, int ParentId, string Root, string MountPoint, IReadOnlyList<string> Options, string FileSystemType, string Source, string? Device = null)
{
/// <summary>The mount is read-only (<c>ro</c>, as for a Docker volume mounted with <c>:ro</c>).</summary>
public bool ReadOnly => Options.Contains("ro", StringComparer.Ordinal);
}
/// <summary>Reads <c>/proc/self/mountinfo</c>.</summary>
public static class MountInfoTable
{
private const string MountInfoPath = "/proc/self/mountinfo";
/// <summary>
/// Parses the content of <c>/proc/self/mountinfo</c>: fields 0-5, then optional fields up to the separator «-», then
/// file system type and source. Malformed lines are skipped; nothing throws.
/// </summary>
public static IReadOnlyList<MountInfoEntry> Parse(string content)
{
ArgumentNullException.ThrowIfNull(content);
var entries = new List<MountInfoEntry>();
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;
}
/// <summary>
/// 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.
/// </summary>
public static MountInfoEntry? FindContaining(IReadOnlyList<MountInfoEntry> 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;
}
/// <summary>The content of <c>/proc/self/mountinfo</c> on Linux; null elsewhere or when it cannot be read.</summary>
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;
}
}
}