runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[System.IO]: File.Copy does not retain the timestamp of the original file

Open Sridhar-MS opened this issue 10 years ago • 16 comments

Copied file has the current timestamp instead of the source file's timestamp.

Sridhar-MS avatar Feb 12 '16 22:02 Sridhar-MS

On what platform?

stephentoub avatar Feb 12 '16 22:02 stephentoub

Oops Sorry missed it. Ubuntu.

Sridhar-MS avatar Feb 12 '16 22:02 Sridhar-MS

The attributes are fine and consistent between platforms, but we don't do anything for the timing info.

Windows: LastAccessTime: Set to DateTime.Now for copy LastWriteTime: Set to source LastWriteTime CreationTime: Set to DateTime.Now for copy

Linux: LastAccessTime: Set to source LastWriteTime LastWriteTime: Set to DateTime.Now for copy CreationTime: Set to source LastWriteTime

So we're essentially doing the opposite of what we do on Windows.

        [Fact]
        public void CopyPreservesCreationTime()
        {
            string testFileSource = GetTestFilePath();
            string testFileDest = GetTestFilePath();
            File.Create(testFileSource).Dispose();
            File.SetCreationTimeUtc(testFileSource, new DateTime(634878298376583000, DateTimeKind.Utc));
            Copy(testFileSource, testFileDest);
            Assert.NotEqual(File.GetCreationTimeUtc(testFileSource), File.GetCreationTimeUtc(testFileDest));
        }

        [Fact]
        public void CopyPreservesLastAccessTime()
        {
            string testFileSource = GetTestFilePath();
            string testFileDest = GetTestFilePath();
            File.Create(testFileSource).Dispose();
            File.SetLastAccessTimeUtc(testFileSource, new DateTime(634878298378287000, DateTimeKind.Utc));
            Copy(testFileSource, testFileDest);
            Assert.NotEqual(File.GetLastAccessTimeUtc(testFileSource), File.GetLastAccessTimeUtc(testFileDest));
        }

        [Fact]
        public void CopyPreservesLastWriteTime()
        {
            string testFileSource = GetTestFilePath();
            string testFileDest = GetTestFilePath();
            File.Create(testFileSource).Dispose();
            File.SetLastWriteTimeUtc(testFileSource, new DateTime(634170636680000000, DateTimeKind.Utc));
            Copy(testFileSource, testFileDest);
            Assert.Equal(File.GetLastWriteTimeUtc(testFileSource), File.GetLastWriteTimeUtc(testFileDest));
        }

ianhays avatar Feb 12 '16 22:02 ianhays

@Sridhar-MS, you say "the timestamp"... what timestamp are you referring to? And what behavior is it that you're looking for / expecting?

As @ianhays highlighted, there are three relevant times: last access, last modification time, and creation time. On Linux we don't have any good control or even access to creation time. And copying a source to a destination will change the last access time of the source (it was accessed to copy it). So, are you looking for the last modification time to be preserved? That's likely doable, but to what end? The copy was just written, so we'd be changing it to some older value.

stephentoub avatar Feb 13 '16 00:02 stephentoub

I took a look at what cp does. From what I can tell, by default it essentially does what we're doing with regards to time stamps, simply letting the natural thing happen. But you can specify the preserve option which appears to copy the times to match after the copy has completed. We could potentially do a similar thing, but since there's no option here around behavior, we'd have to decide what the "right" one is. I don't have a strong opinion.

@Sridhar-MS, can you share more about your requirements that led you to open this issue?

stephentoub avatar Feb 13 '16 15:02 stephentoub

@stephentoub I am specifically talking about the 'LastWrittenTime'. Well this issue interferes with the incremental compilation in dotnet CLI. dotet build command uses the last modified time (of the sources and binaries) to decide whether to rebuild a project or not. So when copying a project to a different folder all the source files and binaries do not retain the original timestamp and they they are always rebuild even when the project is up to date.

Sridhar-MS avatar Feb 14 '16 20:02 Sridhar-MS

Also there is this discrepancy in the behaviour of File.Copy between Windows and Ubuntu.

Sridhar-MS avatar Feb 14 '16 20:02 Sridhar-MS

I am specifically talking about the 'LastWrittenTime'

Ok, thanks for the details.

Also there is this discrepancy in the behaviour of File.Copy between Windows and Ubuntu?

Sure, but that's the case of file copying in general, e.g. the defaults I mentioned with cp, the lack of an equivalent standardized CopyFile method, etc. In other words, you're just seeing a general difference in platform-related behavior between two different operating systems.

I'm not against the idea of always manually copying over the file times, but it would then result in differences in other areas, so we'd need to think it through. Note that in general the File.Copy implementation would have to simply copy over the write time, basically exactly what you could do on your own:

var lastWriteTime = File.GetLastWriteTimeUtc(src);
File.Copy(src, dst);
File.SetLastWriteTimeUtc(dst, lastWriteTime);

Again, I'm not saying we shouldn't build this in, but I'm not convinced it's a slam dunk.

stephentoub avatar Feb 14 '16 20:02 stephentoub

That what I do in my code as a workaround. Its not too bad and I am ok with this.

But as a user I am slightly inconvenienced and wish the C# code behaves similarly between platforms and the platform differences are abstracted from me.

Sridhar-MS avatar Feb 14 '16 21:02 Sridhar-MS

Just to highlight what I mean, if we were to do this in File.Copy, I think it would look like this https://github.com/dotnet/corefx/commit/f941881ee33fdba7b4fc547bd1892e4647870b54. In other words, the same as what a caller would do.

@ianhays, thoughts on this? If you think we should copy this over, I'll leave this commit in the PR. Otherwise, I'll remove it from the PR.

stephentoub avatar Feb 14 '16 21:02 stephentoub

wish the C# code behaves similarly between platforms

It does; it copies the data from one file to another, and it behaves according to the MSDN documentation for the method (which says nothing about time stamps).

My concern with doing workarounds like this is that we end up trying to put a Windows veneer on top of Unix... in other words, the behavior you're seeing isn't .NET behavior, it's Windows behavior that leaks through, and each time we try to emulate Windows behavior on Unix, we run several risks, like breaking with conventions elsewhere on Unix (a program written in C# to do a file copy may now behave differently from one written in another language that doesn't try to simulate Windows), like not completely emulating the Windows behavior (what else might be copied over on Windows that we're not copying on Unix), like doing something automatically that might not be expected by someone not from a Windows background, etc. I don't want us taking a Windows-centric view here. (As an example, you say "the platform differences are abstracted from me", but what if we decided to do that by always having the file have the last write time be, well, the last write time for the new file and not from the old file? That'd be consistent with the default cp behavior on unix, and we could certainly make that the behavior on Windows File.Copy, but that's not what you're hoping for.)

All that said, I see the value of maintaining the last write time, so if @ianhays thinks we should transfer it, we can, as I did in the most recent commit in that PR.

stephentoub avatar Feb 14 '16 21:02 stephentoub

I've been thinking about this some more. The docs do say that "attributes", and as @ianhays said, they are consistent between platforms. However, "attributes" in the general sense could potentially include any metadata, even though technically it means something more specific. That combined with the existing behavior on Windows being to maintain the time stamps, it does seem like there's a reasonable expectation that they be maintained across the copy, and obviously some people have such expectations :wink: And further, at least on OS X, the copy method does support the ability to copy metadata, as on Windows. Given all that, I think it's reasonable that we do the extra step to copy the timestamps in the copy method.

stephentoub avatar Feb 14 '16 23:02 stephentoub

I for one have working code using .Net 4.5 that depends on timestamps being copied over. I doubt I'm the only one.

JonHanna avatar Feb 14 '16 23:02 JonHanna

As an additional data point: Mono does preserve the timestamps as well with File.Copy().

akoeplinger avatar Feb 15 '16 00:02 akoeplinger

I should have mentioned that, since the code I was thinking of worked in both Windows with .NET an Linux in Mono.

JonHanna avatar Feb 15 '16 00:02 JonHanna

Ok, thanks. I've updated dotnet/corefx#6098 to handle this.

stephentoub avatar Feb 15 '16 04:02 stephentoub