-
Notifications
You must be signed in to change notification settings - Fork 5k
Clone files on OSX-like platforms when possible, instead of copying the whole file #79243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
85 commits
Select commit
Hold shift + click to select a range
a373c59
Initial implementation of fix for #77835 - cloning files on macOS
hamarb123 f21a5ae
Fix some build issues from previous commit for #77835
hamarb123 1890115
Fix some build issues from previous commit for #77835 again
hamarb123 e490d5e
Fix some build issues from previous commit for #77835 again (2)
hamarb123 1e43a91
Fix some build issues from previous commit for #77835 again (3)
hamarb123 4c5a995
Fix some build issues from previous commit for #77835 again (4)
hamarb123 c65e229
Fix some build issues from previous commit for #77835 again (5)
hamarb123 5377efb
Fix copying a file onto itself logic for #77835
hamarb123 8d8328a
Fix some nullability issues from previous commit for #77835
hamarb123 835fc2f
Fix some nullability issues from previous commit for #77835 (again)
hamarb123 05f9d26
Fix bug introduced in 'Fix copying a file onto itself logic for #77835'
hamarb123 5fe4cd2
Merge remote-tracking branch 'upstream'
hamarb123 28ef104
Merge branch 'main' of https://github.com/dotnet/runtime
hamarb123 21144e0
Add extra test
hamarb123 049627f
Use correct comment formatting
hamarb123 ab18c05
Use clonefile API instead
hamarb123 776f500
Remove redundant equality
hamarb123 dcddd8f
Clone ACLs and make CopyOntoLockedFile test conditional
hamarb123 ac27788
Remove retry logic
hamarb123 f320a70
Address feedback
hamarb123 3d4a962
Implement feedback to remove StartedCopyFileState
hamarb123 a908a79
Fix compilation issues by me thinking I was smart
hamarb123 16cedc6
Fix nullability
hamarb123 d15fed3
Implement feedback
hamarb123 617afbf
Merge remote-tracking branch 'upstream/main'
hamarb123 eb97585
Fix compilation issues
hamarb123 70c6e8b
Fix missed line in FileSystem.CopyFile.OSX.cs
hamarb123 f866a3d
Update src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.…
hamarb123 bf67649
Update src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.…
hamarb123 ff108db
Update src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.…
hamarb123 69071eb
Update src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.…
hamarb123 20c42f9
Replace var with actual type as per feedback
hamarb123 1280a8a
Update comment for clarity
hamarb123 f33ae9c
Implement feedback to remove OpenCopyFileDstHandle
hamarb123 068a8aa
Fix compilation errors
hamarb123 2c48aad
Remove some comments as per feedback
hamarb123 ef84010
Changes from feedback
hamarb123 8924afe
Remove redundant line as per feedback
hamarb123 1649d43
Remove old comment
hamarb123 a82767b
Implement some feedback
hamarb123 c0c7e33
Implement suggestion that cuts down on duplication
hamarb123 18b8c2e
Remove unnecessary using
hamarb123 00b68ed
Remove some other unnecessary usings
hamarb123 afa73ca
Fix filePermissions missing as per feedback
hamarb123 a684785
Fix compilation issues from files having wrong name in projitems
hamarb123 61c0576
Make the CopyFile implementation more similar to how it was before
hamarb123 7c6c80b
Fix copied code compilation issue
hamarb123 9cdb4b3
Remove filePermissions parameter as per feedback
hamarb123 2dc08e4
Remove isReadOnly as per feedback
hamarb123 02cfaf6
Fix whitespace
hamarb123 1fc02f7
Fix compilation errors
hamarb123 71bc96e
Fix more compilation issues
hamarb123 aa76f24
Try clonefile immediately as per feedback
hamarb123 3945a9f
Add error handling to it, instead of always falling back
hamarb123 3457dcc
Add missing cast
hamarb123 788216e
Remove unneeded filePermissions specification
hamarb123 233217a
Change indentation of delete section
hamarb123 20caacd
Temporarily comment out the stat section
hamarb123 6d40467
Remove some unneeded indentation
hamarb123 705aa4f
Remove special casing of EINVAL
hamarb123 a5de060
Remove section which does stat as it's unneeded
hamarb123 7221560
Improve some of the comments.
hamarb123 1db28fd
Add handler for case of EEXIST still
hamarb123 d360ae4
Remove partial method definitions
hamarb123 323cf67
Add some code to deal with EINVAL
hamarb123 44ef8f3
Remove srcStat parameter as it's no longer needed
hamarb123 50195af
Revert now-unnecessary changes to OpenReadOnly
hamarb123 53f52bc
Use Unlink instead of File.Delete
hamarb123 aee3ea8
Remove unnecessary diffs
hamarb123 c6174e6
Fix compilation errors
hamarb123 0838d9f
Update src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.…
hamarb123 013c775
Reorder flags check as per feedback
hamarb123 487aee9
Fix test failures
hamarb123 37b5183
Extract attempting clonefile to a separate function
hamarb123 bc99ec9
Use CreateOpenException when deleting the destination file
hamarb123 e1c7739
Use a debug statement to check EINVAL instead
hamarb123 aec1eac
Improve & fix comment
hamarb123 483858d
Fix compilation errors
hamarb123 f67bd18
Implement feedback
hamarb123 5cf74af
Move the Debug.Assert calls together
hamarb123 10011a5
Implement feedback to use partial functions
hamarb123 a793bb7
Implement feedback to use pattern matching, and move comments
hamarb123 550a76a
Implement other feedback
hamarb123 32c5463
Fix visibility of partial method
hamarb123 de964d0
Merge branch 'main' of https://github.com/dotnet/runtime
hamarb123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.TryCloneFile.OSX.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Win32.SafeHandles; | ||
using System.Diagnostics; | ||
|
||
namespace System.IO | ||
{ | ||
internal static partial class FileSystem | ||
{ | ||
static partial void TryCloneFile(string sourceFullPath, string destFullPath, bool overwrite, ref bool cloned) | ||
{ | ||
// This helper function calls out to clonefile, and returns the error. | ||
static bool TryCloneFile(string sourceFullPath, string destFullPath, int flags, out Interop.Error error) | ||
{ | ||
if (Interop.@libc.clonefile(sourceFullPath, destFullPath, flags) == 0) | ||
{ | ||
// Success. | ||
error = Interop.Error.SUCCESS; | ||
return true; | ||
} | ||
hamarb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
error = Interop.Sys.GetLastError(); | ||
return false; | ||
} | ||
|
||
// Try to clone the file immediately, this will only succeed if the | ||
// destination doesn't exist, so we don't worry about locking for this one. | ||
int flags = Interop.@libc.CLONE_ACL; | ||
Interop.Error error; | ||
if (TryCloneFile(sourceFullPath, destFullPath, flags, out error)) | ||
{ | ||
cloned = true; | ||
return; | ||
} | ||
|
||
// Some filesystems don't support ACLs, so may fail due to trying to copy ACLs. | ||
// This will disable them and allow trying again (a maximum of 1 time). | ||
if (error == Interop.Error.EINVAL) | ||
{ | ||
flags = 0; | ||
if (TryCloneFile(sourceFullPath, destFullPath, flags, out error)) | ||
{ | ||
cloned = true; | ||
return; | ||
} | ||
} | ||
|
||
// Try to delete the destination file if we're overwriting. | ||
if (error == Interop.Error.EEXIST && overwrite) | ||
hamarb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// Delete the destination. This should fail on directories. Get a lock to the dest file to ensure we don't copy onto it when | ||
// it's locked by something else, and then delete it. It should also fail if destination == source since it's already locked. | ||
danmoseley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try | ||
{ | ||
using SafeFileHandle? dstHandle = SafeFileHandle.Open(destFullPath, FileMode.Open, FileAccess.ReadWrite, | ||
hamarb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FileShare.None, FileOptions.None, preallocationSize: 0, createOpenException: CreateOpenExceptionForCopyFile); | ||
hamarb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (Interop.Sys.Unlink(destFullPath) < 0 && | ||
Interop.Sys.GetLastError() != Interop.Error.ENOENT) | ||
{ | ||
// Fall back to standard copy as an unexpected error has occurred. | ||
return; | ||
} | ||
} | ||
catch (FileNotFoundException) | ||
hamarb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
hamarb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// We don't want to throw if it's just the file not existing, since we're trying to delete it. | ||
} | ||
|
||
// Try clonefile now we've deleted the destination file. | ||
if (TryCloneFile(sourceFullPath, destFullPath, flags, out error)) | ||
{ | ||
cloned = true; | ||
return; | ||
} | ||
} | ||
|
||
if (error is Interop.Error.ENOTSUP // Check if it's not supported, | ||
or Interop.Error.EXDEV // if files are on different filesystems, | ||
or Interop.Error.EEXIST) // or if the destination file still exists. | ||
{ | ||
// Fall back to normal copy. | ||
return; | ||
} | ||
|
||
// Throw the appropriate exception. | ||
Debug.Assert(error != Interop.Error.EINVAL); // We shouldn't fail due to an invalid parameter. | ||
Debug.Assert(error != Interop.Error.SUCCESS); // We shouldn't fail with success. | ||
throw Interop.GetExceptionForIoErrno(error.Info(), destFullPath); | ||
hamarb123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.