From 0daa63e9ed40323b6f248ded8959530ea94f19aa Mon Sep 17 00:00:00 2001 From: Ian Hays Date: Thu, 23 Feb 2017 14:46:15 -0800 Subject: [PATCH 1/2] Remove OSX FileStream Lock/Unlock OSX doesn't support usage of both fcntl and flock. Since we're already using one in FileShare for the entire file, we cannot enable partial file locking like we do on other Unix platforms. The alternative is to throw a PNSE and suggest using FileShare on the whole file instead. --- src/mscorlib/System.Private.CoreLib.csproj | 6 ++++ .../corefx/System/IO/FileStream.Linux.cs | 30 +++++++++++++++++++ .../corefx/System/IO/FileStream.OSX.cs | 19 ++++++++++++ .../corefx/System/IO/FileStream.Unix.cs | 16 ---------- src/mscorlib/src/System.Private.CoreLib.txt | 2 ++ 5 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 src/mscorlib/corefx/System/IO/FileStream.Linux.cs create mode 100644 src/mscorlib/corefx/System/IO/FileStream.OSX.cs diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 816295636b2e..415d04bd0427 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -858,6 +858,12 @@ + + + + + + diff --git a/src/mscorlib/corefx/System/IO/FileStream.Linux.cs b/src/mscorlib/corefx/System/IO/FileStream.Linux.cs new file mode 100644 index 000000000000..873c4eb5599d --- /dev/null +++ b/src/mscorlib/corefx/System/IO/FileStream.Linux.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace System.IO +{ + public partial class FileStream : Stream + { + /// Prevents other processes from reading from or writing to the FileStream. + /// The beginning of the range to lock. + /// The range to be locked. + private void LockInternal(long position, long length) + { + CheckFileCall(Interop.Sys.LockFileRegion(_fileHandle, position, length, Interop.Sys.LockType.F_WRLCK)); + } + + /// Allows access by other processes to all or part of a file that was previously locked. + /// The beginning of the range to unlock. + /// The range to be unlocked. + private void UnlockInternal(long position, long length) + { + CheckFileCall(Interop.Sys.LockFileRegion(_fileHandle, position, length, Interop.Sys.LockType.F_UNLCK)); + } + } +} diff --git a/src/mscorlib/corefx/System/IO/FileStream.OSX.cs b/src/mscorlib/corefx/System/IO/FileStream.OSX.cs new file mode 100644 index 000000000000..a1167bfca01f --- /dev/null +++ b/src/mscorlib/corefx/System/IO/FileStream.OSX.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.IO +{ + public partial class FileStream : Stream + { + private void LockInternal(long position, long length) + { + throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_OSXFileLocking")); + } + + private void UnlockInternal(long position, long length) + { + throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_OSXFileLocking")); + } + } +} diff --git a/src/mscorlib/corefx/System/IO/FileStream.Unix.cs b/src/mscorlib/corefx/System/IO/FileStream.Unix.cs index f83fc8425900..87bcc6c15ead 100644 --- a/src/mscorlib/corefx/System/IO/FileStream.Unix.cs +++ b/src/mscorlib/corefx/System/IO/FileStream.Unix.cs @@ -796,22 +796,6 @@ public override void WriteByte(byte value) // avoids an array allocation in the } } - /// Prevents other processes from reading from or writing to the FileStream. - /// The beginning of the range to lock. - /// The range to be locked. - private void LockInternal(long position, long length) - { - CheckFileCall(Interop.Sys.LockFileRegion(_fileHandle, position, length, Interop.Sys.LockType.F_WRLCK)); - } - - /// Allows access by other processes to all or part of a file that was previously locked. - /// The beginning of the range to unlock. - /// The range to be unlocked. - private void UnlockInternal(long position, long length) - { - CheckFileCall(Interop.Sys.LockFileRegion(_fileHandle, position, length, Interop.Sys.LockType.F_UNLCK)); - } - /// Sets the current position of this stream to the given value. /// The point relative to origin from which to begin seeking. /// diff --git a/src/mscorlib/src/System.Private.CoreLib.txt b/src/mscorlib/src/System.Private.CoreLib.txt index e45de2dbbef8..15fcf5766581 100644 --- a/src/mscorlib/src/System.Private.CoreLib.txt +++ b/src/mscorlib/src/System.Private.CoreLib.txt @@ -1719,6 +1719,8 @@ event_Barrier_PhaseFinished=Barrier finishing phase {1}. ; Unix threading PlatformNotSupported_NamedSynchronizationPrimitives=The named version of this synchronization primitive is not supported on this platform. PlatformNotSupported_NamedSyncObjectWaitAnyWaitAll=Wait operations on multiple wait handles including a named synchronization primitive are not supported on this platform. +; OSX File locking +PlatformNotSupported_OSXFileLocking=Locking/unlocking file regions is not supported. Use FileShare on the entire file instead. #endif ; From 10c6689e4cff982b49300428ce7d6627bbbbbdf7 Mon Sep 17 00:00:00 2001 From: Ian Hays Date: Fri, 24 Feb 2017 11:46:25 -0800 Subject: [PATCH 2/2] Update FileLock/Unlock PNSE message for OSX --- src/mscorlib/src/System.Private.CoreLib.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mscorlib/src/System.Private.CoreLib.txt b/src/mscorlib/src/System.Private.CoreLib.txt index 15fcf5766581..cb4a62c1c61c 100644 --- a/src/mscorlib/src/System.Private.CoreLib.txt +++ b/src/mscorlib/src/System.Private.CoreLib.txt @@ -1720,7 +1720,7 @@ event_Barrier_PhaseFinished=Barrier finishing phase {1}. PlatformNotSupported_NamedSynchronizationPrimitives=The named version of this synchronization primitive is not supported on this platform. PlatformNotSupported_NamedSyncObjectWaitAnyWaitAll=Wait operations on multiple wait handles including a named synchronization primitive are not supported on this platform. ; OSX File locking -PlatformNotSupported_OSXFileLocking=Locking/unlocking file regions is not supported. Use FileShare on the entire file instead. +PlatformNotSupported_OSXFileLocking=Locking/unlocking file regions is not supported on this platform. Use FileShare on the entire file instead. #endif ;