|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.Win32.SafeHandles; |
| 5 | +using System.Diagnostics; |
| 6 | + |
| 7 | +namespace System.IO |
| 8 | +{ |
| 9 | + internal static partial class FileSystem |
| 10 | + { |
| 11 | + static partial void TryCloneFile(string sourceFullPath, string destFullPath, bool overwrite, ref bool cloned) |
| 12 | + { |
| 13 | + // This helper function calls out to clonefile, and returns the error. |
| 14 | + static bool TryCloneFile(string sourceFullPath, string destFullPath, int flags, out Interop.Error error) |
| 15 | + { |
| 16 | + if (Interop.@libc.clonefile(sourceFullPath, destFullPath, flags) == 0) |
| 17 | + { |
| 18 | + // Success. |
| 19 | + error = Interop.Error.SUCCESS; |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + error = Interop.Sys.GetLastError(); |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + // Try to clone the file immediately, this will only succeed if the |
| 28 | + // destination doesn't exist, so we don't worry about locking for this one. |
| 29 | + int flags = Interop.@libc.CLONE_ACL; |
| 30 | + Interop.Error error; |
| 31 | + if (TryCloneFile(sourceFullPath, destFullPath, flags, out error)) |
| 32 | + { |
| 33 | + cloned = true; |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Some filesystems don't support ACLs, so may fail due to trying to copy ACLs. |
| 38 | + // This will disable them and allow trying again (a maximum of 1 time). |
| 39 | + if (error == Interop.Error.EINVAL) |
| 40 | + { |
| 41 | + flags = 0; |
| 42 | + if (TryCloneFile(sourceFullPath, destFullPath, flags, out error)) |
| 43 | + { |
| 44 | + cloned = true; |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Try to delete the destination file if we're overwriting. |
| 50 | + if (error == Interop.Error.EEXIST && overwrite) |
| 51 | + { |
| 52 | + // Delete the destination. This should fail on directories. Get a lock to the dest file to ensure we don't copy onto it when |
| 53 | + // it's locked by something else, and then delete it. It should also fail if destination == source since it's already locked. |
| 54 | + try |
| 55 | + { |
| 56 | + using SafeFileHandle? dstHandle = SafeFileHandle.Open(destFullPath, FileMode.Open, FileAccess.ReadWrite, |
| 57 | + FileShare.None, FileOptions.None, preallocationSize: 0, createOpenException: CreateOpenExceptionForCopyFile); |
| 58 | + if (Interop.Sys.Unlink(destFullPath) < 0 && |
| 59 | + Interop.Sys.GetLastError() != Interop.Error.ENOENT) |
| 60 | + { |
| 61 | + // Fall back to standard copy as an unexpected error has occurred. |
| 62 | + return; |
| 63 | + } |
| 64 | + } |
| 65 | + catch (FileNotFoundException) |
| 66 | + { |
| 67 | + // We don't want to throw if it's just the file not existing, since we're trying to delete it. |
| 68 | + } |
| 69 | + |
| 70 | + // Try clonefile now we've deleted the destination file. |
| 71 | + if (TryCloneFile(sourceFullPath, destFullPath, flags, out error)) |
| 72 | + { |
| 73 | + cloned = true; |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (error is Interop.Error.ENOTSUP // Check if it's not supported, |
| 79 | + or Interop.Error.EXDEV // if files are on different filesystems, |
| 80 | + or Interop.Error.EEXIST) // or if the destination file still exists. |
| 81 | + { |
| 82 | + // Fall back to normal copy. |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Throw the appropriate exception. |
| 87 | + Debug.Assert(error != Interop.Error.EINVAL); // We shouldn't fail due to an invalid parameter. |
| 88 | + Debug.Assert(error != Interop.Error.SUCCESS); // We shouldn't fail with success. |
| 89 | + throw Interop.GetExceptionForIoErrno(error.Info(), destFullPath); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments