Skip to content

Commit

Permalink
there is no need to perform the extra checks on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Jan 12, 2022
1 parent 896245d commit af1cd9b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ public void MoveTo(string destDirName)
string destination = Path.GetFullPath(destDirName);
string destinationWithSeparator = PathInternal.EnsureTrailingSeparator(destination);

FileSystem.MoveDirectory(FullPath, destination, destinationWithSeparator, Exists);
FileSystem.MoveDirectory(FullPath, destination, destinationWithSeparator,
OperatingSystem.IsWindows() ? null : Exists); // on Windows we don't need to perform the extra check

Init(originalPath: destDirName,
fullPath: destinationWithSeparator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,22 @@ private static void CreateParentsAndDirectory(string fullPath)
}
}

private static void MoveDirectory(string sourceFullPath, string destFullPath)
private static void MoveDirectory(string sourceFullPath, string destFullPath, bool sameDirectoryDifferentCase, bool? sourceDirectoryExists)
{
if (sourceDirectoryExists is null)
{
sourceDirectoryExists = DirectoryExists(sourceFullPath);
}

// Windows will throw if the source file/directory doesn't exist, we preemptively check
// to make sure our cross platform behavior matches .NET Framework behavior.
if (!sourceDirectoryExists.Value && !FileExists(sourceFullPath))
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceFullPath));

if (!sameDirectoryDifferentCase // This check is to allow renaming of directories
&& DirectoryExists(destFullPath))
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath));

// Windows doesn't care if you try and copy a file via "MoveDirectory"...
if (FileExists(sourceFullPath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static DateTimeOffset GetLastWriteTime(string fullPath)
return data.ftLastWriteTime.ToDateTimeOffset();
}

private static void MoveDirectory(string sourceFullPath, string destFullPath)
private static void MoveDirectory(string sourceFullPath, string destFullPath, bool sameDirectoryDifferentCase, bool? sourceDirectoryExists)
{
if (!Interop.Kernel32.MoveFile(sourceFullPath, destFullPath, overwrite: false))
{
Expand Down
16 changes: 1 addition & 15 deletions src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,7 @@ internal static void MoveDirectory(string sourceFullPath, string destFullPath, s
if (!sourceRoot.Equals(destinationRoot, StringComparison.OrdinalIgnoreCase))
throw new IOException(SR.IO_SourceDestMustHaveSameRoot);

if (sourceDirectoryExists is null)
{
sourceDirectoryExists = DirectoryExists(sourceFullPath);
}

// Windows will throw if the source file/directory doesn't exist, we preemptively check
// to make sure our cross platform behavior matches .NET Framework behavior.
if (!sourceDirectoryExists.Value && !FileExists(sourceFullPath))
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceFullPath));

if (!sameDirectoryDifferentCase // This check is to allow renaming of directories
&& DirectoryExists(destFullPath))
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath));

MoveDirectory(sourceFullPath, destFullPath);
MoveDirectory(sourceFullPath, destFullPath, sameDirectoryDifferentCase, sourceDirectoryExists);
}
}
}

0 comments on commit af1cd9b

Please sign in to comment.