diff --git a/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.GetFileSystemTypeForPath.cs b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.GetFileSystemTypeForPath.cs new file mode 100644 index 00000000000000..070f62e3a5619c --- /dev/null +++ b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.GetFileSystemTypeForPath.cs @@ -0,0 +1,66 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.Globalization; + +internal static partial class Interop +{ + internal static partial class @procfs + { + internal static string? GetFileSystemTypeForPath(string path, IEnumerable lines) + { + Dictionary childMounts = new(); + (ulong MountId, int MountPointLength, string FileSystemType) currentMount = (0, int.MaxValue, ""); + + foreach (string line in lines) + { + if (!TryParseMountInfoLine(line, out ParsedMount mount) || + !path.AsSpan().StartsWith(mount.MountPoint, StringComparison.Ordinal)) + { + continue; + } + + int mountPointLength = mount.MountPoint.Length; + if (mountPointLength > 1 && mountPointLength < path.Length && path[mountPointLength] != '/') + { + continue; + } + + if (!ulong.TryParse(mount.MountId, NumberStyles.None, CultureInfo.InvariantCulture, out ulong mountId) || + !ulong.TryParse(mount.ParentId, NumberStyles.None, CultureInfo.InvariantCulture, out ulong parentId)) + { + continue; + } + + var candidate = (MountId: mountId, MountPointLength: mountPointLength, FileSystemType: mount.FileSystemType.ToString()); + if (mountPointLength < currentMount.MountPointLength) + { + currentMount = candidate; + } + + // A shorter matching child hides longer matching siblings of the same parent. + // https://man7.org/linux/man-pages/man5/proc_pid_mountinfo.5.html + if (mountId != parentId && + (!childMounts.TryGetValue(parentId, out var childMount) || mountPointLength < childMount.MountPointLength)) + { + childMounts[parentId] = candidate; + } + } + + // Bound traversal in case the mount table changes while it is read. + for (int remaining = childMounts.Count; remaining >= 0; remaining--) + { + if (!childMounts.TryGetValue(currentMount.MountId, out var childMount)) + { + return currentMount.FileSystemType.Length == 0 ? null : currentMount.FileSystemType; + } + + currentMount = childMount; + } + + return null; + } + } +} diff --git a/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.TryParseMountInfoLine.cs b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.TryParseMountInfoLine.cs index 540540eef55913..3bc5ed87f11000 100644 --- a/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.TryParseMountInfoLine.cs +++ b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.TryParseMountInfoLine.cs @@ -11,6 +11,8 @@ internal static partial class @procfs { internal ref struct ParsedMount { + public required ReadOnlySpan MountId { get; init; } + public required ReadOnlySpan ParentId { get; init; } public required ReadOnlySpan Root { get; init; } public required ReadOnlySpan MountPoint { get; init; } public required ReadOnlySpan FileSystemType { get; init; } @@ -30,9 +32,21 @@ internal static bool TryParseMountInfoLine(ReadOnlySpan line, out ParsedMo MemoryExtensions.SpanSplitEnumerator fields = line.Split(' '); // (1) mount ID + if (!fields.MoveNext()) + { + return false; + } + ReadOnlySpan mountId = line[fields.Current]; + // (2) parent ID + if (!fields.MoveNext()) + { + return false; + } + ReadOnlySpan parentId = line[fields.Current]; + // (3) major:minor - if (!fields.MoveNext() || !fields.MoveNext() || !fields.MoveNext()) + if (!fields.MoveNext()) { return false; } @@ -83,6 +97,8 @@ internal static bool TryParseMountInfoLine(ReadOnlySpan line, out ParsedMo result = new ParsedMount() { + MountId = mountId, + ParentId = parentId, Root = root.Contains('\\') ? UnescapePath(root) : root, MountPoint = mountPoint.Contains('\\') ? UnescapePath(mountPoint) : mountPoint, FileSystemType = fileSystemType, diff --git a/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.cs b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.cs index de68fe04013a03..e880eecdde92e6 100644 --- a/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.cs +++ b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcMountInfo.cs @@ -19,45 +19,10 @@ internal static Error GetFileSystemTypeForRealPath(string path, out string forma { try { - ReadOnlySpan currentFormat = default; - int currentBestLength = 0; - - using StreamReader reader = new(ProcMountInfoFilePath); - - string? line; - while ((line = reader.ReadLine()) is not null) - { - if (TryParseMountInfoLine(line, out ParsedMount mount)) - { - if (mount.MountPoint.Length < currentBestLength) - { - continue; - } - - if (!path.StartsWith(mount.MountPoint)) - { - continue; - } - - if (mount.MountPoint.Length == path.Length) - { - currentFormat = mount.FileSystemType; - break; - } - - if (mount.MountPoint.Length > 1 && path[mount.MountPoint.Length] != '/') - { - continue; - } - - currentBestLength = mount.MountPoint.Length; - currentFormat = mount.FileSystemType; - } - } - - if (currentFormat.Length > 0) + string? fileSystemType = GetFileSystemTypeForPath(path, File.ReadLines(ProcMountInfoFilePath)); + if (fileSystemType is not null) { - format = currentFormat.ToString(); + format = fileSystemType; return Error.SUCCESS; } return Error.ENOENT; diff --git a/src/libraries/Common/tests/Common.Tests.csproj b/src/libraries/Common/tests/Common.Tests.csproj index dd184b098635f5..102e1fd89594dd 100644 --- a/src/libraries/Common/tests/Common.Tests.csproj +++ b/src/libraries/Common/tests/Common.Tests.csproj @@ -16,6 +16,8 @@ Link="Common\Interop\Linux\Interop.ProcFsStat.TryReadStatusFile.cs" /> + FileSystemTypeForPath_TestData() + { + const string RootMount = "1 1 8:1 / / rw - ext4 /dev/root rw"; + + yield return ["/", Array.Empty(), null]; + yield return ["/", new[] { RootMount }, "ext4"]; + yield return ["/home/file", new[] { RootMount }, "ext4"]; + + string[] mounts = + [ + RootMount, + "20 1 0:20 / /mnt rw - tmpfs tmpfs rw", + "3 20 11:0 / /mnt/child ro - iso9660 /dev/sr0 ro" + ]; + yield return ["/mnt", mounts, "tmpfs"]; + yield return ["/mnt/child", mounts, "iso9660"]; + yield return ["/mnt/child/file", mounts, "iso9660"]; + yield return ["/mnt/childish", mounts, "tmpfs"]; + yield return ["/mnt2/file", mounts, "ext4"]; + + mounts = + [ + RootMount, + "200 1 0:20 / /media/disc rw - autofs systemd-1 rw", + "3 200 11:0 / /media/disc ro - iso9660 /dev/sr0 ro" + ]; + yield return ["/media/disc", mounts, "iso9660"]; + yield return ["/media/disc/file", mounts, "iso9660"]; + string[] stackedMounts = [.. mounts, "150 3 0:21 / /media/disc rw - nfs server:/share rw"]; + yield return ["/media/disc", stackedMounts, "nfs"]; + yield return ["/media/disc/file", stackedMounts, "nfs"]; + + mounts = + [ + RootMount, + "25 1 0:4 / /proc rw - proc proc rw", + "39 25 0:20 / /proc/sys/fs/binfmt_misc rw - autofs systemd-1 rw", + "102 39 0:21 / /proc/sys/fs/binfmt_misc rw - binfmt_misc binfmt_misc rw" + ]; + yield return ["/proc/sys/fs/binfmt_misc", mounts, "binfmt_misc"]; + yield return ["/proc/sys/fs/binfmt_misc/status", mounts, "binfmt_misc"]; + + mounts = + [ + RootMount, + "200 1 0:20 / /sys/fs/cgroup rw - tmpfs tmpfs rw", + "3 200 0:21 / /sys/fs/cgroup rw - cgroup2 cgroup2 rw" + ]; + yield return ["/sys/fs/cgroup", mounts, "cgroup2"]; + yield return ["/sys/fs/cgroup/memory.max", mounts, "cgroup2"]; + + mounts = + [ + RootMount, + "200 1 8:2 / /mnt rw - ext4 /dev/lower rw", + "30 200 0:21 / /mnt/child rw - nfs server:/share rw", + "3 200 0:22 / /mnt rw - tmpfs tmpfs rw" + ]; + yield return ["/mnt", mounts, "tmpfs"]; + yield return ["/mnt/child", mounts, "tmpfs"]; + yield return ["/mnt/child/file", mounts, "tmpfs"]; + string[] visibleChildMounts = [.. mounts, "4 3 11:0 / /mnt/child ro - iso9660 /dev/sr0 ro"]; + yield return ["/mnt/child/file", visibleChildMounts, "iso9660"]; + + mounts = + [ + "200 999 0:1 / / rw - rootfs rootfs rw", + "30 200 8:1 / /hidden rw - ext4 /dev/lower rw", + "3 200 0:2 / / rw - overlay overlay rw", + "4 3 0:3 / /data rw - tmpfs tmpfs rw" + ]; + yield return ["/", mounts, "overlay"]; + yield return ["/hidden/file", mounts, "overlay"]; + yield return ["/data/file", mounts, "tmpfs"]; + yield return ["/", new[] { RootMount, "3 1 0:2 / / rw - overlay overlay rw" }, "overlay"]; + + mounts = + [ + "200 999 8:2 / /mnt rw - exfat /dev/lower rw", + "3 200 0:21 / /mnt rw - cifs //server/share rw" + ]; + yield return ["/mnt", mounts, "cifs"]; + yield return ["/mnt/file", mounts, "cifs"]; + yield return ["/mnt2", mounts, null]; + + mounts = + [ + RootMount, + "200 1 0:20 / /mnt/disc\\040space rw - autofs systemd-1 rw", + "3 200 11:0 / /mnt/disc\\040space ro - iso9660 /dev/sr0 ro" + ]; + yield return ["/mnt/disc space", mounts, "iso9660"]; + yield return ["/mnt/disc space/file", mounts, "iso9660"]; + yield return ["/mnt/disc", mounts, "ext4"]; + + mounts = + [ + RootMount, + "3 1 0:2 / /mnt/a\\134b rw - tmpfs tmpfs rw", + "4 1 0:3 / /mnt/r\u00E9sum\u00E9 rw - exfat /dev/data rw", + "5 1 0:4 / /etc/hosts rw - tmpfs tmpfs rw" + ]; + yield return ["/mnt/a\\b/file", mounts, "tmpfs"]; + yield return ["/mnt/r\u00E9sum\u00E9/file", mounts, "exfat"]; + yield return ["/etc/hosts", mounts, "tmpfs"]; + yield return ["/MNT/a\\b/file", mounts, "ext4"]; + + yield return ["/mnt/file", new[] { RootMount, "invalid", "x 1 0:2 / /mnt rw - tmpfs tmpfs rw" }, "ext4"]; + yield return ["/mnt/file", new[] { "2 3 0:2 / /mnt rw - ext4 /dev/lower rw", "3 2 0:3 / /mnt rw - tmpfs tmpfs rw" }, null]; + + mounts = + [ + RootMount, + "4294967296 1 0:20 / /mnt rw - autofs systemd-1 rw", + "18446744073709551615 4294967296 11:0 / /mnt ro - iso9660 /dev/sr0 ro" + ]; + yield return ["/mnt", mounts, "iso9660"]; + yield return ["/mnt/file", mounts, "iso9660"]; + + mounts = + [ + "1 1 0:1 / / rw - rootfs root rw", + "2 1 0:2 / /mnt rw - ext4 root rw", + "3 2 0:3 / /mnt/sub rw - tmpfs tmpfs rw", + "4 1 0:4 / /other rw - nfs server rw" + ]; + yield return ["/", mounts, "rootfs"]; + yield return ["/file", mounts, "rootfs"]; + yield return ["/mnt", mounts, "ext4"]; + yield return ["/mnt/file", mounts, "ext4"]; + yield return ["/mnt2", mounts, "rootfs"]; + yield return ["/mnt/sub", mounts, "tmpfs"]; + yield return ["/mnt/sub/file", mounts, "tmpfs"]; + yield return ["/mnt/subway", mounts, "ext4"]; + yield return ["/Mnt/sub", mounts, "rootfs"]; + + mounts = + [ + "1 99 0:1 / / rw - rootfs root rw", + "5 1 0:2 / / rw - overlay overlay rw", + "6 5 0:3 / /mnt\\040path rw - autofs systemd rw", + "2 6 0:4 / /mnt\\040path rw - ext4 root rw", + "3 6 0:5 / /mnt\\040path/nested rw - tmpfs tmpfs rw", + "4 2 0:6 / /mnt\\040path/visible rw - nfs server rw", + "7 1 0:7 / /mnt\\040path/root-hidden rw - xfs root rw", + "8 5 0:8 / /mnt\\040path/visible/hidden rw - btrfs root rw" + ]; + yield return ["/", mounts, "overlay"]; + yield return ["/file", mounts, "overlay"]; + yield return ["/mnt path", mounts, "ext4"]; + yield return ["/mnt path/file", mounts, "ext4"]; + yield return ["/mnt path/nested", mounts, "ext4"]; + yield return ["/mnt path/nested/file", mounts, "ext4"]; + yield return ["/mnt path/visible", mounts, "nfs"]; + yield return ["/mnt path/visible/file", mounts, "nfs"]; + yield return ["/mnt path/visible/hidden", mounts, "nfs"]; + yield return ["/mnt path/root-hidden", mounts, "ext4"]; + yield return ["/mnt pathway", mounts, "overlay"]; + + mounts = + [ + "3 99 0:3 / /mnt/sub rw - tmpfs tmpfs rw", + "2 99 0:2 / /mnt rw - ext4 root rw", + "4 2 0:4 / /mnt/sub/visible rw - nfs server rw" + ]; + yield return ["/mnt/sub/file", mounts, "ext4"]; + yield return ["/mnt/sub/visible", mounts, "nfs"]; + yield return ["/mnt/sub/visible/file", mounts, "nfs"]; + + yield return ["/mnt", new[] { "" }, null]; + yield return ["/mnt", new[] { "invalid" }, null]; + yield return ["/mnt", new[] { "1 99 0:1 / /different rw - ext4 root rw" }, null]; + } + + [Theory] + [MemberData(nameof(FileSystemTypeForPath_TestData))] + public void GetFileSystemTypeForPath_SelectsVisibleMount(string path, string[] mounts, string? expectedFormat) + { + for (int first = 0; first < Math.Max(1, mounts.Length); first++) + { + for (int direction = -1; direction <= 1; direction += 2) + { + string[] orderedMounts = new string[mounts.Length]; + for (int i = 0; i < mounts.Length; i++) + { + orderedMounts[i] = mounts[(first + mounts.Length + i * direction) % mounts.Length]; + } + + Assert.Equal(expectedFormat, Interop.procfs.GetFileSystemTypeForPath(path, orderedMounts)); + } + } + } } } diff --git a/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj b/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj index c8075367005d3a..ee4619be1e3614 100644 --- a/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj +++ b/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj @@ -347,6 +347,8 @@ Link="Common\Interop\Unix\Interop.RealPath.cs" /> + + Common\Interop\Unix\System.Native\Interop.MountPoints.cs + Common\Interop\Unix\System.Native\Interop.Open.cs diff --git a/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj index 5c14dc81d331de..ce4f96db884376 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj @@ -19,6 +19,8 @@ Link="Common\Interop\Linux\Interop.cgroups.cs" /> +