Skip to content

Commit 4a9b024

Browse files
(GH-4132) Add File APIs for setting timestamps (creation time, last write time, last access time)
1 parent b764038 commit 4a9b024

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

src/Cake.Core/IO/File.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,47 @@ public Stream Open(FileMode fileMode, FileAccess fileAccess, FileShare fileShare
6262
{
6363
return _file.Open(fileMode, fileAccess, fileShare);
6464
}
65+
66+
/// <inheritdoc/>
67+
public IFile SetCreationTime(DateTime creationTime)
68+
{
69+
System.IO.File.SetCreationTime(Path.FullPath, creationTime);
70+
return this;
71+
}
72+
73+
/// <inheritdoc/>
74+
public IFile SetCreationTimeUtc(DateTime creationTimeUtc)
75+
{
76+
System.IO.File.SetCreationTimeUtc(Path.FullPath, creationTimeUtc);
77+
return this;
78+
}
79+
80+
/// <inheritdoc/>
81+
public IFile SetLastAccessTime(DateTime lastAccessTime)
82+
{
83+
System.IO.File.SetLastAccessTime(Path.FullPath, lastAccessTime);
84+
return this;
85+
}
86+
87+
/// <inheritdoc/>
88+
public IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc)
89+
{
90+
System.IO.File.SetLastAccessTimeUtc(Path.FullPath, lastAccessTimeUtc);
91+
return this;
92+
}
93+
94+
/// <inheritdoc/>
95+
public IFile SetLastWriteTime(DateTime lastWriteTime)
96+
{
97+
System.IO.File.SetLastWriteTime(Path.FullPath, lastWriteTime);
98+
return this;
99+
}
100+
101+
/// <inheritdoc/>
102+
public IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc)
103+
{
104+
System.IO.File.SetLastWriteTimeUtc(Path.FullPath, lastWriteTimeUtc);
105+
return this;
106+
}
65107
}
66108
}

src/Cake.Core/IO/IFile.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.IO;
67

78
namespace Cake.Core.IO
@@ -55,5 +56,47 @@ public interface IFile : IFileSystemInfo
5556
/// <param name="fileShare">The file share.</param>
5657
/// <returns>A <see cref="Stream"/> to the file.</returns>
5758
Stream Open(FileMode fileMode, FileAccess fileAccess, FileShare fileShare);
59+
60+
/// <summary>
61+
/// Sets the date and time that the file was created.
62+
/// </summary>
63+
/// <param name="creationTime">A <see cref="DateTime"/> containing the value to set for the creation date and time of path. This value is expressed in local time.</param>
64+
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
65+
IFile SetCreationTime(DateTime creationTime) => this;
66+
67+
/// <summary>
68+
/// Sets the date and time, in Coordinated Universal Time (UTC), that the file was created.
69+
/// </summary>
70+
/// <param name="creationTimeUtc">A <see cref="DateTime"/> containing the value to set for the creation date and time of path. This value is expressed in UTC time.</param>
71+
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
72+
IFile SetCreationTimeUtc(DateTime creationTimeUtc) => this;
73+
74+
/// <summary>
75+
/// Sets the date and time that the specified file or directory was last accessed.
76+
/// </summary>
77+
/// <param name="lastAccessTime">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
78+
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
79+
IFile SetLastAccessTime(DateTime lastAccessTime) => this;
80+
81+
/// <summary>
82+
/// Sets the date and time, in Coordinated Universal Time (UTC), that the specified file or directory was last accessed.
83+
/// </summary>
84+
/// <param name="lastAccessTimeUtc">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
85+
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
86+
IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc) => this;
87+
88+
/// <summary>
89+
/// Sets the date and time that the specified file or directory was last written to.
90+
/// </summary>
91+
/// <param name="lastWriteTime">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
92+
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
93+
IFile SetLastWriteTime(DateTime lastWriteTime) => this;
94+
95+
/// <summary>
96+
/// Sets the date and time, in Coordinated Universal Time (UTC), that the specified file or directory was last written to.
97+
/// </summary>
98+
/// <param name="lastWriteTimeUtc">A <see cref="DateTime"/> containing the value to set for the last access date and time of path. This value is expressed in local time.</param>
99+
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
100+
IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc) => this;
58101
}
59-
}
102+
}

src/Cake.Testing/FakeFile.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,41 @@ private long GetPosition(FileMode fileMode, out bool fileWasCreated)
157157
}
158158
throw new NotSupportedException();
159159
}
160+
161+
/// <inheritdoc/>
162+
public IFile SetCreationTime(DateTime creationTime)
163+
{
164+
return this;
165+
}
166+
167+
/// <inheritdoc/>
168+
public IFile SetCreationTimeUtc(DateTime creationTimeUtc)
169+
{
170+
return this;
171+
}
172+
173+
/// <inheritdoc/>
174+
public IFile SetLastAccessTime(DateTime lastAccessTime)
175+
{
176+
return this;
177+
}
178+
179+
/// <inheritdoc/>
180+
public IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc)
181+
{
182+
return this;
183+
}
184+
185+
/// <inheritdoc/>
186+
public IFile SetLastWriteTime(DateTime lastWriteTime)
187+
{
188+
return this;
189+
}
190+
191+
/// <inheritdoc/>
192+
public IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc)
193+
{
194+
return this;
195+
}
160196
}
161-
}
197+
}

0 commit comments

Comments
 (0)