-
Notifications
You must be signed in to change notification settings - Fork 5k
FileSystem.Unix: improve CopyFile. #59695
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
9 commits
Select commit
Hold shift + click to select a range
e2d3577
FileSystem.Unix: improve CopyFile.
tmds 712db40
Don't FICLONE for zero sourceLength
tmds 3fd621d
PR feedback
tmds b7ab817
When using sendfile, don't loop when source file gets truncated.
tmds 9be3b2f
Fall through when FICLONE fails.
tmds 4e70e52
Don't stop CopyFile_ReadWrite until read returns zero.
tmds 3c95290
Revert all changes to CopyFile_ReadWrite
tmds 5d7cd45
Move comment a few lines up.
tmds b67b262
Fix unused error.
tmds 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,25 +11,30 @@ namespace System.IO | |
/// <summary>Provides an implementation of FileSystem for Unix systems.</summary> | ||
internal static partial class FileSystem | ||
{ | ||
internal const int DefaultBufferSize = 4096; | ||
|
||
// On Linux, the maximum number of symbolic links that are followed while resolving a pathname is 40. | ||
// See: https://man7.org/linux/man-pages/man7/path_resolution.7.html | ||
private const int MaxFollowedLinks = 40; | ||
|
||
public static void CopyFile(string sourceFullPath, string destFullPath, bool overwrite) | ||
{ | ||
// If the destination path points to a directory, we throw to match Windows behaviour | ||
if (DirectoryExists(destFullPath)) | ||
{ | ||
throw new IOException(SR.Format(SR.Arg_FileIsDirectory_Name, destFullPath)); | ||
} | ||
long fileLength; | ||
Interop.Sys.Permissions filePermissions; | ||
using SafeFileHandle src = SafeFileHandle.OpenReadOnly(sourceFullPath, FileOptions.None, out fileLength, out filePermissions); | ||
jeffhandley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using SafeFileHandle dst = SafeFileHandle.Open(destFullPath, overwrite ? FileMode.Create : FileMode.CreateNew, | ||
FileAccess.ReadWrite, FileShare.None, FileOptions.None, preallocationSize: 0, openPermissions: filePermissions, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether it would be beneficial to provide |
||
(Interop.ErrorInfo error, Interop.Sys.OpenFlags flags, string path) => CreateOpenException(error, flags, path)); | ||
|
||
Interop.CheckIo(Interop.Sys.CopyFile(src, dst, fileLength)); | ||
|
||
// Copy the contents of the file from the source to the destination, creating the destination in the process | ||
using (SafeFileHandle src = File.OpenHandle(sourceFullPath, FileMode.Open, FileAccess.Read, FileShare.Read, FileOptions.None)) | ||
using (SafeFileHandle dst = File.OpenHandle(destFullPath, overwrite ? FileMode.Create : FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, FileOptions.None)) | ||
static Exception? CreateOpenException(Interop.ErrorInfo error, Interop.Sys.OpenFlags flags, string path) | ||
jeffhandley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Interop.CheckIo(Interop.Sys.CopyFile(src, dst)); | ||
// If the destination path points to a directory, we throw to match Windows behaviour. | ||
if (error.Error == Interop.Error.EEXIST && DirectoryExists(path)) | ||
{ | ||
return new IOException(SR.Format(SR.Arg_FileIsDirectory_Name, path)); | ||
} | ||
|
||
return null; // Let SafeFileHandle create the exception for this error. | ||
} | ||
} | ||
|
||
|
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.