Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7e7f368
Fix wrong drive format on Unix
MojtabaTajik Sep 4, 2024
9fe1985
Merge branch 'main' into FixWrongDriveFormatOnUnix
MojtabaTajik Sep 4, 2024
fec07ce
Add support for reading filesystem type from /proc/self/mountinfo on …
MojtabaTajik Sep 5, 2024
080825d
Fix whitespaces and replace tabs with 4 spaces
MojtabaTajik Sep 5, 2024
1171819
Remove pre-processor directives indention
MojtabaTajik Sep 5, 2024
d43dd7b
Fix whitespaces
MojtabaTajik Sep 5, 2024
3542fd8
Update src/libraries/Common/src/Interop/Unix/System.Native/Interop.Mo…
MojtabaTajik Sep 5, 2024
1607833
fix indention
MojtabaTajik Sep 5, 2024
2fd5836
Update src/libraries/Common/src/Interop/Unix/System.Native/Interop.Mo…
MojtabaTajik Sep 6, 2024
49ac57e
Update src/libraries/Common/src/Interop/Unix/System.Native/Interop.Mo…
MojtabaTajik Sep 6, 2024
eafc91b
Use actual type instead of var
MojtabaTajik Sep 6, 2024
4a17349
Update src/libraries/Common/src/Interop/Unix/System.Native/Interop.Mo…
MojtabaTajik Sep 6, 2024
3505c12
Update src/libraries/Common/src/Interop/Unix/System.Native/Interop.Mo…
MojtabaTajik Sep 6, 2024
c8c0a84
Simplify checks
MojtabaTajik Sep 6, 2024
eba6e78
add new line
MojtabaTajik Sep 6, 2024
5da14ac
fix spaces
MojtabaTajik Sep 6, 2024
ab1e2f4
Revert "fix spaces"
MojtabaTajik Sep 6, 2024
d52fe86
Revert "add new line"
MojtabaTajik Sep 6, 2024
4bbceac
Revert "Simplify checks"
MojtabaTajik Sep 6, 2024
459d1a6
Revert "Update src/libraries/Common/src/Interop/Unix/System.Native/In…
MojtabaTajik Sep 6, 2024
b5e2873
Update src/libraries/Common/src/Interop/Unix/System.Native/Interop.Mo…
MojtabaTajik Sep 6, 2024
542c244
Skip to mount point
MojtabaTajik Sep 6, 2024
171c068
Update comment
MojtabaTajik Sep 6, 2024
1ea7097
Use MemoryExtensions.Equals
MojtabaTajik Sep 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static unsafe partial int GetFormatInfoForMountPoint(
byte* formatNameBuffer,
int bufferLength,
long* formatType);

internal static int GetFormatInfoForMountPoint(string name, out string format)
{
return GetFormatInfoForMountPoint(name, out format, out _);
Expand All @@ -49,9 +49,69 @@ internal static int GetFormatInfoForMountPoint(string name, out DriveType type)
{
return GetFormatInfoForMountPoint(name, out _, out type);
}

/// <summary>
/// Retrieves format information for the specified mount point.
/// </summary>
/// <remarks>
/// This method uses a two-step approach to retrieve filesystem information:
/// 1. On Linux systems, it first attempts to read from `/proc/self/mountinfo`.
/// 2. If step 1 fails or on non-Linux systems, it falls back to a P/Invoke call.
///
/// The `/proc/self/mountinfo` approach is preferred on Linux because
/// it's more reliable when procfs is available and functioning correctly.
///
/// For other systems:
/// - SunOS and similar: procfs provides a binary interface, not suitable for this method.
/// - macOS: procfs is not available.
/// - FreeBSD: procfs is optional and not enabled by default.
///
/// The method uses try/catch blocks to ensure robustness, even though `/proc/self/mountinfo`
/// should be specific to each process according to kernel documentation.
/// </remarks>
/// <param name="name">The mount point name to query.</param>
/// <param name="format">Output parameter for the filesystem format.</param>
/// <param name="type">Output parameter for the drive type.</param>
/// <returns>0 if successful, otherwise an error code.</returns>
private static unsafe int GetFormatInfoForMountPoint(string name, out string format, out DriveType type)
{
#if TARGET_LINUX
try
{
const string mountInfoFilePath = "/proc/self/mountinfo";
var mountInfoFileContent = File.ReadAllLines(mountInfoFilePath);
foreach (var line in mountInfoFileContent)
{
var parser = new StringParser(line, ' ');

// Skip fields we don't care about (Fields 1-4)
parser.MoveNext(); // Skip Mount ID
parser.MoveNext(); // Skip Parent ID
parser.MoveNext(); // Skip Major:Minor
parser.MoveNext(); // Skip Root

// Get the mount point (Field 5)
string mountPoint = parser.MoveAndExtractNext();

// Skip to the separator which is end of optional fields (Field 8)
while (parser.MoveAndExtractNext() != "-")
{
}

// Get filesystem type (Field 9)
string filesystemType = parser.MoveAndExtractNext();

if (mountPoint.Equals(name, StringComparison.Ordinal))
{
format = filesystemType;
type = GetDriveType(filesystemType);
return 0;
}
}
}
catch { /* ignored */ }
#endif

private static unsafe int GetFormatInfoForMountPoint(string name, out string format, out DriveType type)
{
byte* formatBuffer = stackalloc byte[MountPointFormatBufferSizeInBytes]; // format names should be small
long numericFormat;
int result = GetFormatInfoForMountPoint(name, formatBuffer, MountPointFormatBufferSizeInBytes, &numericFormat);
Expand All @@ -70,7 +130,8 @@ private static unsafe int GetFormatInfoForMountPoint(string name, out string for
}

return result;
}
}


/// <summary>Categorizes a file system name into a drive type.</summary>
/// <param name="fileSystemName">The name to categorize.</param>
Expand Down
2 changes: 1 addition & 1 deletion src/native/libs/System.Native/entrypoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static const Entry s_sysNative[] =
DllImportEntry(SystemNative_Malloc)
DllImportEntry(SystemNative_Realloc)
DllImportEntry(SystemNative_GetSpaceInfoForMountPoint)
DllImportEntry(SystemNative_GetFormatInfoForMountPoint)
DllImportEntry(SystemNative_GetFormatInfoForMountPoint)
DllImportEntry(SystemNative_GetAllMountPoints)
DllImportEntry(SystemNative_ReadEvents)
DllImportEntry(SystemNative_CreateNetworkChangeListenerSocket)
Expand Down
2 changes: 1 addition & 1 deletion src/native/libs/System.Native/pal_mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ SystemNative_GetFormatInfoForMountPoint(const char* name, char* formatNameBuffer
}

return result;
}
}
2 changes: 1 addition & 1 deletion src/native/libs/System.Native/pal_mount_wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ SystemNative_GetFormatInfoForMountPoint(const char* name, char* formatNameBuffer
assert((formatNameBuffer != NULL) && (formatType != NULL));
assert(bufferLength > 0);
return -1;
}
}