Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
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
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetFileSystemType")]
internal static extern long GetFileSystemType(SafeFileHandle fd);
}
}
2 changes: 2 additions & 0 deletions src/libraries/Native/Unix/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#cmakedefine01 HAVE_MMAP64
#cmakedefine01 HAVE_FTRUNCATE64
#cmakedefine01 HAVE_POSIX_FADVISE64
#cmakedefine01 HAVE_STATFS_VFS
#cmakedefine01 HAVE_STATFS_MOUNT
#cmakedefine01 HAVE_FLOCK64
#cmakedefine01 HAVE_F_DUPFD_CLOEXEC
#cmakedefine01 HAVE_F_FULLFSYNC
Expand Down
1 change: 1 addition & 0 deletions src/libraries/Native/Unix/System.Native/entrypoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ static const Entry s_sysNative[] =
DllImportEntry(SystemNative_INotifyRemoveWatch)
DllImportEntry(SystemNative_RealPath)
DllImportEntry(SystemNative_GetPeerID)
DllImportEntry(SystemNative_GetFileSystemType)
DllImportEntry(SystemNative_LockFileRegion)
DllImportEntry(SystemNative_LChflags)
DllImportEntry(SystemNative_LChflagsCanSetHiddenFlag)
Expand Down
19 changes: 19 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#if HAVE_INOTIFY
#include <sys/inotify.h>
#endif
#if HAVE_STATFS_VFS // Linux
#include <sys/vfs.h>
#elif HAVE_STATFS_MOUNT // BSD
#include <sys/mount.h>
#endif

#ifdef _AIX
#include <alloca.h>
Expand Down Expand Up @@ -1379,6 +1384,20 @@ static int16_t ConvertLockType(int16_t managedLockType)
}
}

int64_t SystemNative_GetFileSystemType(intptr_t fd)
{
#if HAVE_STATFS_VFS || HAVE_STATFS_MOUNT
int statfsRes;
struct statfs statfsArgs;
// for our needs (get file system type) statfs is always enough and there is no need to use statfs64
// which got deprecated in macOS 10.6, in favor of statfs
while ((statfsRes = fstatfs(ToFileDescriptor(fd), &statfsArgs)) == -1 && errno == EINTR) ;
return statfsRes == -1 ? (int64_t)-1 : (int64_t)statfsArgs.f_type;
#else
#error "Platform doesn't support fstatfs"
#endif
}

int32_t SystemNative_LockFileRegion(intptr_t fd, int64_t offset, int64_t length, int16_t lockType)
{
int16_t unixLockType = ConvertLockType(lockType);
Expand Down
5 changes: 5 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,11 @@ PALEXPORT char* SystemNative_RealPath(const char* path);
*/
PALEXPORT int32_t SystemNative_GetPeerID(intptr_t socket, uid_t* euid);

/**
* Returns file system type on success, or -1 on error.
*/
PALEXPORT int64_t SystemNative_GetFileSystemType(intptr_t fd);

/**
* Attempts to lock/unlock the region of the file "fd" specified by the offset and length. lockType
* can be set to F_UNLCK (2) for unlock or F_WRLCK (3) for lock.
Expand Down
22 changes: 22 additions & 0 deletions src/libraries/Native/Unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ check_c_source_compiles(

# /in_pktinfo

check_c_source_compiles(
"
#include <sys/vfs.h>
int main(void)
{
struct statfs s;
return 0;
}
"
HAVE_STATFS_VFS)

check_c_source_compiles(
"
#include <sys/mount.h>
int main(void)
{
struct statfs s;
return 0;
}
"
HAVE_STATFS_MOUNT)

check_c_source_compiles(
"
#include <fcntl.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace Microsoft.Win32.SafeHandles
{
public sealed partial class SafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
{
internal static bool DisableFileLocking { get; } = OperatingSystem.IsBrowser() // #40065: Emscripten does not support file locking
|| AppContextConfigHelper.GetBooleanConfig("System.IO.DisableFileLocking", "DOTNET_SYSTEM_IO_DISABLEFILELOCKING", defaultValue: false);

// not using bool? as it's not thread safe
private volatile NullableBool _canSeek = NullableBool.Undefined;
private bool _deleteOnClose;
Expand Down Expand Up @@ -123,7 +126,10 @@ protected override bool ReleaseHandle()
// which could prevent subsequent usage of the file until this process dies. To avoid that, we proactively
// try to release the lock before we close the handle. (If it's not locked, there's no behavioral
// problem trying to unlock it.)
Interop.Sys.FLock(handle, Interop.Sys.LockOperations.LOCK_UN); // ignore any errors
if (!DisableFileLocking)
{
Interop.Sys.FLock(handle, Interop.Sys.LockOperations.LOCK_UN); // ignore any errors
}

// If DeleteOnClose was requested when constructed, delete the file now.
// (Unix doesn't directly support DeleteOnClose, so we mimic it here.)
Expand Down Expand Up @@ -259,7 +265,7 @@ private void Init(string path, FileMode mode, FileAccess access, FileShare share
// lock on the file and all other modes use a shared lock. While this is not as granular as Windows, not mandatory,
// and not atomic with file opening, it's better than nothing.
Interop.Sys.LockOperations lockOperation = (share == FileShare.None) ? Interop.Sys.LockOperations.LOCK_EX : Interop.Sys.LockOperations.LOCK_SH;
if (Interop.Sys.FLock(this, lockOperation | Interop.Sys.LockOperations.LOCK_NB) < 0)
if (CanLockTheFile(lockOperation, access) && Interop.Sys.FLock(this, lockOperation | Interop.Sys.LockOperations.LOCK_NB) < 0)
{
// The only error we care about is EWOULDBLOCK, which indicates that the file is currently locked by someone
// else and we would block trying to access it. Other errors, such as ENOTSUP (locking isn't supported) or
Expand Down Expand Up @@ -320,6 +326,37 @@ private void Init(string path, FileMode mode, FileAccess access, FileShare share
}
}

private bool CanLockTheFile(Interop.Sys.LockOperations lockOperation, FileAccess access)
{
Debug.Assert(lockOperation == Interop.Sys.LockOperations.LOCK_EX || lockOperation == Interop.Sys.LockOperations.LOCK_SH);

if (DisableFileLocking)
{
return false;
}
else if (lockOperation == Interop.Sys.LockOperations.LOCK_EX)
{
return true; // LOCK_EX is always OK
}
else if ((access & FileAccess.Write) == 0)
{
return true; // LOCK_SH is always OK when reading
}

switch (Interop.Sys.GetFileSystemType(this))
{
case 0x6969: // NFS_SUPER_MAGIC
case 0xFF534D42: // CIFS_MAGIC_NUMBER
case 0x517B: // SMB_SUPER_MAGIC
case 0xFE534D42: // SMB2_SUPER_MAGIC (#53182)
return false; // LOCK_SH is not OK when writing to NFS, CIFS or SMB
case -1: // error
return false; // assume we should not acquire the lock if we don't know the File System
default:
return true; // in all other situations it should be OK
}
}

private bool GetCanSeek()
{
Debug.Assert(!IsClosed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,9 @@
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ErrNo.cs">
<Link>Common\Interop\Unix\System.Native\Interop.ErrNo.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Fstatfs.cs">
<Link>Common\Interop\Unix\System.Native\Interop.Fstatfs.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.FLock.cs">
<Link>Common\Interop\Unix\System.Native\Interop.FLock.cs</Link>
</Compile>
Expand Down