From 6025aa2d86498a9d2e97ed3d83c538a60fff31d5 Mon Sep 17 00:00:00 2001 From: Ian Hays Date: Thu, 23 Feb 2017 14:46:15 -0800 Subject: [PATCH] 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. Commit migrated from https://github.com/dotnet/coreclr/commit/0daa63e9ed40323b6f248ded8959530ea94f19aa --- .../src/mscorlib/System.Private.CoreLib.csproj | 6 +++++ .../mscorlib/corefx/System/IO/FileStream.Linux.cs | 30 ++++++++++++++++++++++ .../mscorlib/corefx/System/IO/FileStream.OSX.cs | 19 ++++++++++++++ .../mscorlib/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/coreclr/src/mscorlib/corefx/System/IO/FileStream.Linux.cs create mode 100644 src/coreclr/src/mscorlib/corefx/System/IO/FileStream.OSX.cs diff --git a/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj b/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj index 8162956..415d04b 100644 --- a/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj @@ -858,6 +858,12 @@ + + + + + + diff --git a/src/coreclr/src/mscorlib/corefx/System/IO/FileStream.Linux.cs b/src/coreclr/src/mscorlib/corefx/System/IO/FileStream.Linux.cs new file mode 100644 index 0000000..873c4eb --- /dev/null +++ b/src/coreclr/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/coreclr/src/mscorlib/corefx/System/IO/FileStream.OSX.cs b/src/coreclr/src/mscorlib/corefx/System/IO/FileStream.OSX.cs new file mode 100644 index 0000000..a1167bf --- /dev/null +++ b/src/coreclr/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/coreclr/src/mscorlib/corefx/System/IO/FileStream.Unix.cs b/src/coreclr/src/mscorlib/corefx/System/IO/FileStream.Unix.cs index f83fc84..87bcc6c1 100644 --- a/src/coreclr/src/mscorlib/corefx/System/IO/FileStream.Unix.cs +++ b/src/coreclr/src/mscorlib/corefx/System/IO/FileStream.Unix.cs @@ -796,22 +796,6 @@ namespace System.IO } } - /// 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/coreclr/src/mscorlib/src/System.Private.CoreLib.txt b/src/coreclr/src/mscorlib/src/System.Private.CoreLib.txt index e45de2d..15fcf57 100644 --- a/src/coreclr/src/mscorlib/src/System.Private.CoreLib.txt +++ b/src/coreclr/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 ; -- 2.7.4