Skip to content

Commit

Permalink
check for source file existience only once
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Jan 12, 2022
1 parent af1cd9b commit 373a4a2
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,23 +389,18 @@ private static void CreateParentsAndDirectory(string fullPath)

private static void MoveDirectory(string sourceFullPath, string destFullPath, bool sameDirectoryDifferentCase, bool? sourceDirectoryExists)
{
if (sourceDirectoryExists is null)
{
sourceDirectoryExists = DirectoryExists(sourceFullPath);
}
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))
if (!sourceDirectoryExists.Value)
{
if (!FileExists(sourceFullPath))
{
throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceFullPath));
}

// Windows doesn't care if you try and copy a file via "MoveDirectory"...
// ... but it doesn't like the source to have a trailing slash ...

// On Windows we end up with ERROR_INVALID_NAME, which is
Expand All @@ -415,17 +410,26 @@ private static void MoveDirectory(string sourceFullPath, string destFullPath, bo
// give DirectoryNotFound.

if (Path.EndsInDirectorySeparator(sourceFullPath))
{
throw new IOException(SR.Format(SR.IO_PathNotFound_Path, sourceFullPath));
}

// ... but it doesn't care if the destination has a trailing separator.
destFullPath = Path.TrimEndingDirectorySeparator(destFullPath);
}

if (FileExists(destFullPath))
if (!sameDirectoryDifferentCase) // This check is to allow renaming of directories
{
// Some Unix distros will overwrite the destination file if it already exists.
// Throwing IOException to match Windows behavior.
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath));
if (DirectoryExists(destFullPath))
{
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath));
}
else if (FileExists(destFullPath))
{
// Some Unix distros will overwrite the destination file if it already exists.
// Throwing IOException to match Windows behavior.
throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath));
}
}

if (Interop.Sys.Rename(sourceFullPath, destFullPath) < 0)
Expand Down

0 comments on commit 373a4a2

Please sign in to comment.