Skip to content

Commit

Permalink
GH-4132: Add File APIs for setting timestamps (creation time, last wr…
Browse files Browse the repository at this point in the history
…ite time, last access time) (#4133)

(GH-4132) Add File APIs for setting timestamps (creation time, last write time, last access time)
  • Loading branch information
augustoproiete committed Nov 14, 2023
1 parent 444f759 commit 8047ae4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/Cake.Core/IO/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,47 @@ public Stream Open(FileMode fileMode, FileAccess fileAccess, FileShare fileShare
{
return _file.Open(fileMode, fileAccess, fileShare);
}

/// <inheritdoc/>
public IFile SetCreationTime(DateTime creationTime)
{
System.IO.File.SetCreationTime(Path.FullPath, creationTime);
return this;
}

/// <inheritdoc/>
public IFile SetCreationTimeUtc(DateTime creationTimeUtc)
{
System.IO.File.SetCreationTimeUtc(Path.FullPath, creationTimeUtc);
return this;
}

/// <inheritdoc/>
public IFile SetLastAccessTime(DateTime lastAccessTime)
{
System.IO.File.SetLastAccessTime(Path.FullPath, lastAccessTime);
return this;
}

/// <inheritdoc/>
public IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc)
{
System.IO.File.SetLastAccessTimeUtc(Path.FullPath, lastAccessTimeUtc);
return this;
}

/// <inheritdoc/>
public IFile SetLastWriteTime(DateTime lastWriteTime)
{
System.IO.File.SetLastWriteTime(Path.FullPath, lastWriteTime);
return this;
}

/// <inheritdoc/>
public IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc)
{
System.IO.File.SetLastWriteTimeUtc(Path.FullPath, lastWriteTimeUtc);
return this;
}
}
}
45 changes: 44 additions & 1 deletion src/Cake.Core/IO/IFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,5 +56,47 @@ public interface IFile : IFileSystemInfo
/// <param name="fileShare">The file share.</param>
/// <returns>A <see cref="Stream"/> to the file.</returns>
Stream Open(FileMode fileMode, FileAccess fileAccess, FileShare fileShare);

/// <summary>
/// Sets the date and time that the file was created.
/// </summary>
/// <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>
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
IFile SetCreationTime(DateTime creationTime) => this;

/// <summary>
/// Sets the date and time, in Coordinated Universal Time (UTC), that the file was created.
/// </summary>
/// <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>
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
IFile SetCreationTimeUtc(DateTime creationTimeUtc) => this;

/// <summary>
/// Sets the date and time that the specified file or directory was last accessed.
/// </summary>
/// <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>
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
IFile SetLastAccessTime(DateTime lastAccessTime) => this;

/// <summary>
/// Sets the date and time, in Coordinated Universal Time (UTC), that the specified file or directory was last accessed.
/// </summary>
/// <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>
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc) => this;

/// <summary>
/// Sets the date and time that the specified file or directory was last written to.
/// </summary>
/// <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>
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
IFile SetLastWriteTime(DateTime lastWriteTime) => this;

/// <summary>
/// Sets the date and time, in Coordinated Universal Time (UTC), that the specified file or directory was last written to.
/// </summary>
/// <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>
/// <returns>A <see cref="IFile"/> instance representing the specified path.</returns>
IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc) => this;
}
}
}
38 changes: 37 additions & 1 deletion src/Cake.Testing/FakeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,41 @@ private long GetPosition(FileMode fileMode, out bool fileWasCreated)
}
throw new NotSupportedException();
}

/// <inheritdoc/>
public IFile SetCreationTime(DateTime creationTime)
{
return this;
}

/// <inheritdoc/>
public IFile SetCreationTimeUtc(DateTime creationTimeUtc)
{
return this;
}

/// <inheritdoc/>
public IFile SetLastAccessTime(DateTime lastAccessTime)
{
return this;
}

/// <inheritdoc/>
public IFile SetLastAccessTimeUtc(DateTime lastAccessTimeUtc)
{
return this;
}

/// <inheritdoc/>
public IFile SetLastWriteTime(DateTime lastWriteTime)
{
return this;
}

/// <inheritdoc/>
public IFile SetLastWriteTimeUtc(DateTime lastWriteTimeUtc)
{
return this;
}
}
}
}

0 comments on commit 8047ae4

Please sign in to comment.