runtime icon indicating copy to clipboard operation
runtime copied to clipboard

File.Copy(source, dest, overwrite) on Unix has opposite overwrite functionality of Windows

Open ianhays opened this issue 11 years ago • 1 comments

On Windows, the following test code passes the intended way without issue:

string testFileSource = GetTestFilePath();
string testFileDest = GetTestFilePath();
using (StreamWriter sourceStream = new StreamWriter(File.Create(testFileSource)))
using (StreamWriter destStream = new StreamWriter(File.Create(testFileDest)))
{
            sourceStream.Write(sourceData, 0, sourceData.Length);
            sourceStream.Flush();
            destStream.Write(destData, 0, destData.Length);
            destStream.Flush();
}
Assert.Throws<IOException>(() => Copy(testFileSource, testFileDest, false)); //Throws an error since testFileDest already exists
Copy(testFileSource, testFileDest, true); //overwrite testFileDest with data from testFileSource

The same code on Ubuntu fails, however. To pass on Ubuntu the intended way, 'overwrite' has to be set to the opposite of what it should be on Windows, like so:

string testFileSource = GetTestFilePath();
string testFileDest = GetTestFilePath();
using (StreamWriter sourceStream = new StreamWriter(File.Create(testFileSource)))
using (StreamWriter destStream = new StreamWriter(File.Create(testFileDest)))
{
            sourceStream.Write(sourceData, 0, sourceData.Length);
            sourceStream.Flush();
            destStream.Write(destData, 0, destData.Length);
            destStream.Flush();
}
Assert.Throws<IOException>(() => Copy(testFileSource, testFileDest, true));
Copy(testFileSource, testFileDest, false); 

ianhays avatar Jul 08 '15 18:07 ianhays

Line 41 of "UnixFileSystem.cs" is:

using (Stream dst = new FileStream(destFullPath, overwrite ? FileMode.CreateNew : FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize, useAsync))

when it should be:

using (Stream dst = new FileStream(destFullPath, overwrite ? FileMode.Create : FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, bufferSize, useAsync))

Will include the change in my next PR.

ianhays avatar Jul 08 '15 19:07 ianhays