From 8047ae45bb969ccd678d133168294cf5a919777d Mon Sep 17 00:00:00 2001 From: "C. Augusto Proiete" Date: Tue, 14 Nov 2023 17:14:43 -0400 Subject: [PATCH] GH-4132: Add File APIs for setting timestamps (creation time, last write time, last access time) (#4133) (GH-4132) Add File APIs for setting timestamps (creation time, last write time, last access time) --- src/Cake.Core/IO/File.cs | 42 +++++++++++++++++++++++++++++++++ src/Cake.Core/IO/IFile.cs | 45 +++++++++++++++++++++++++++++++++++- src/Cake.Testing/FakeFile.cs | 38 +++++++++++++++++++++++++++++- 3 files changed, 123 insertions(+), 2 deletions(-) diff --git a/src/Cake.Core/IO/File.cs b/src/Cake.Core/IO/File.cs index d100381ffc..3ee422e20a 100644 --- a/src/Cake.Core/IO/File.cs +++ b/src/Cake.Core/IO/File.cs @@ -62,5 +62,47 @@ public Stream Open(FileMode fileMode, FileAccess fileAccess, FileShare fileShare { return _file.Open(fileMode, fileAccess, fileShare); } + + /// + public IFile SetCreationTime(DateTime creationTime) + { + System.IO.File.SetCreationTime(Path.FullPath, creationTime); + return this; + } + + /// + public IFile SetCreationTimeUtc(DateTime creationTimeUtc) + { + System.IO.File.SetCreationTimeUtc(Path.FullPath, creationTimeUtc); + return this; + } + + /// + public IFile SetLastAccessTime(DateTime lastAccessTime) + { + System.IO.File.SetLastAccessTime(Path.FullPath, lastAccessTime); + return this; + } + + /// + public IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc) + { + System.IO.File.SetLastAccessTimeUtc(Path.FullPath, lastAccessTimeUtc); + return this; + } + + /// + public IFile SetLastWriteTime(DateTime lastWriteTime) + { + System.IO.File.SetLastWriteTime(Path.FullPath, lastWriteTime); + return this; + } + + /// + public IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc) + { + System.IO.File.SetLastWriteTimeUtc(Path.FullPath, lastWriteTimeUtc); + return this; + } } } \ No newline at end of file diff --git a/src/Cake.Core/IO/IFile.cs b/src/Cake.Core/IO/IFile.cs index 102eb8a06a..0bb6851b6e 100644 --- a/src/Cake.Core/IO/IFile.cs +++ b/src/Cake.Core/IO/IFile.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.IO; namespace Cake.Core.IO @@ -55,5 +56,47 @@ public interface IFile : IFileSystemInfo /// The file share. /// A to the file. Stream Open(FileMode fileMode, FileAccess fileAccess, FileShare fileShare); + + /// + /// Sets the date and time that the file was created. + /// + /// A containing the value to set for the creation date and time of path. This value is expressed in local time. + /// A instance representing the specified path. + IFile SetCreationTime(DateTime creationTime) => this; + + /// + /// Sets the date and time, in Coordinated Universal Time (UTC), that the file was created. + /// + /// A containing the value to set for the creation date and time of path. This value is expressed in UTC time. + /// A instance representing the specified path. + IFile SetCreationTimeUtc(DateTime creationTimeUtc) => this; + + /// + /// Sets the date and time that the specified file or directory was last accessed. + /// + /// A containing the value to set for the last access date and time of path. This value is expressed in local time. + /// A instance representing the specified path. + IFile SetLastAccessTime(DateTime lastAccessTime) => this; + + /// + /// Sets the date and time, in Coordinated Universal Time (UTC), that the specified file or directory was last accessed. + /// + /// A containing the value to set for the last access date and time of path. This value is expressed in local time. + /// A instance representing the specified path. + IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc) => this; + + /// + /// Sets the date and time that the specified file or directory was last written to. + /// + /// A containing the value to set for the last access date and time of path. This value is expressed in local time. + /// A instance representing the specified path. + IFile SetLastWriteTime(DateTime lastWriteTime) => this; + + /// + /// Sets the date and time, in Coordinated Universal Time (UTC), that the specified file or directory was last written to. + /// + /// A containing the value to set for the last access date and time of path. This value is expressed in local time. + /// A instance representing the specified path. + IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc) => this; } -} \ No newline at end of file +} diff --git a/src/Cake.Testing/FakeFile.cs b/src/Cake.Testing/FakeFile.cs index 5cc67671b2..e8705f1cf7 100644 --- a/src/Cake.Testing/FakeFile.cs +++ b/src/Cake.Testing/FakeFile.cs @@ -157,5 +157,41 @@ private long GetPosition(FileMode fileMode, out bool fileWasCreated) } throw new NotSupportedException(); } + + /// + public IFile SetCreationTime(DateTime creationTime) + { + return this; + } + + /// + public IFile SetCreationTimeUtc(DateTime creationTimeUtc) + { + return this; + } + + /// + public IFile SetLastAccessTime(DateTime lastAccessTime) + { + return this; + } + + /// + public IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc) + { + return this; + } + + /// + public IFile SetLastWriteTime(DateTime lastWriteTime) + { + return this; + } + + /// + public IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc) + { + return this; + } } -} \ No newline at end of file +}