Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageVersion Include="FluentAssertions" Version="8.9.0" />
<PackageVersion Include="Gress" Version="2.1.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
<PackageVersion Include="PolyShim" Version="2.9.0" />
<PackageVersion Include="PolyShim" Version="2.10.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.5.61" />
Expand Down
119 changes: 119 additions & 0 deletions PowerKit.Tests/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.Versioning;
using FluentAssertions;
using PowerKit;
using PowerKit.Extensions;
Expand All @@ -9,6 +10,124 @@ namespace PowerKit.Tests;

public class DirectoryExtensionsTests
{
[Fact]
public void Copy_Test()
{
// Arrange
using var sourceDirectory = TempDirectory.Create();
using var destinationDirectory = TempDirectory.Create();

File.WriteAllText(Path.Combine(sourceDirectory.Path, "file.txt"), "hello");

// Act
Directory.Copy(sourceDirectory.Path, destinationDirectory.Path);

// Assert
File.ReadAllText(Path.Combine(destinationDirectory.Path, "file.txt")).Should().Be("hello");
}

[Fact]
public void Copy_Nested_Test()
{
// Arrange
using var sourceDirectory = TempDirectory.Create();
using var destinationDirectory = TempDirectory.Create();

Directory.CreateDirectory(Path.Combine(sourceDirectory.Path, "sub"));
File.WriteAllText(Path.Combine(sourceDirectory.Path, "sub", "file.txt"), "nested");

// Act
Directory.Copy(sourceDirectory.Path, destinationDirectory.Path);

// Assert
File.ReadAllText(Path.Combine(destinationDirectory.Path, "sub", "file.txt"))
.Should()
.Be("nested");
}

[Fact]
public void Copy_Overwrite_Test()
{
// Arrange
using var sourceDirectory = TempDirectory.Create();
using var destinationDirectory = TempDirectory.Create();

File.WriteAllText(Path.Combine(sourceDirectory.Path, "file.txt"), "new");
File.WriteAllText(Path.Combine(destinationDirectory.Path, "file.txt"), "old");

// Act
Directory.Copy(sourceDirectory.Path, destinationDirectory.Path, true);

// Assert
File.ReadAllText(Path.Combine(destinationDirectory.Path, "file.txt")).Should().Be("new");
}

[Fact]
public void Copy_NoOverwrite_Test()
{
// Arrange
using var sourceDirectory = TempDirectory.Create();
using var destinationDirectory = TempDirectory.Create();

File.WriteAllText(Path.Combine(sourceDirectory.Path, "file.txt"), "source");
File.WriteAllText(Path.Combine(destinationDirectory.Path, "file.txt"), "existing");

// Act
var act = () => Directory.Copy(sourceDirectory.Path, destinationDirectory.Path, false);

// Assert
act.Should().Throw<IOException>();
}

[Fact]
public void Copy_Truncates_Test()
{
// Arrange
using var sourceDirectory = TempDirectory.Create();
using var destinationDirectory = TempDirectory.Create();

File.WriteAllText(Path.Combine(sourceDirectory.Path, "file.txt"), "hi");
File.WriteAllText(Path.Combine(destinationDirectory.Path, "file.txt"), "longer content");

// Act
Directory.Copy(sourceDirectory.Path, destinationDirectory.Path, true);

// Assert
File.ReadAllText(Path.Combine(destinationDirectory.Path, "file.txt")).Should().Be("hi");
}

[SkippableFact]
[UnsupportedOSPlatform("windows")]
public void Copy_UnixFileMode_Test()
{
Skip.If(OperatingSystem.IsWindows());

// Arrange
using var sourceDirectory = TempDirectory.Create();
using var destinationDirectory = TempDirectory.Create();

var sourceFilePath = Path.Combine(sourceDirectory.Path, "file.sh");
File.WriteAllText(sourceFilePath, "#!/bin/sh");
File.SetUnixFileMode(
sourceFilePath,
UnixFileMode.UserRead
| UnixFileMode.UserWrite
| UnixFileMode.UserExecute
| UnixFileMode.GroupRead
| UnixFileMode.GroupExecute
| UnixFileMode.OtherRead
| UnixFileMode.OtherExecute
);

// Act
Directory.Copy(sourceDirectory.Path, destinationDirectory.Path);

// Assert
File.GetUnixFileMode(Path.Combine(destinationDirectory.Path, "file.sh"))
.Should()
.Be(File.GetUnixFileMode(sourceFilePath));
}

[Fact]
public void CheckWriteAccess_Test()
{
Expand Down
90 changes: 90 additions & 0 deletions PowerKit/Extensions/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;

namespace PowerKit.Extensions;

Expand Down Expand Up @@ -43,6 +45,94 @@ public static bool TryDelete(string path, bool recursive = false)
}
}

/// <summary>
/// Recursively copies all files from <paramref name="sourcePath" /> to <paramref name="destinationPath" />.
/// Destination files are opened with exclusive locks before any data is written.
/// Concurrent readers may be blocked or fail with a sharing violation while a file is being updated,
/// and this method does not guarantee atomic old-or-new visibility to readers.
Comment thread
Tyrrrz marked this conversation as resolved.
/// </summary>
public static void Copy(string sourcePath, string destinationPath, bool overwrite = true)
{
var sourceStreams = new List<FileStream>();
var destinationStreams = new List<FileStream>();

try
{
// Create all destination directories
Directory.CreateDirectory(destinationPath);
foreach (
var sourceDirectoryPath in Directory.GetDirectories(
sourcePath,
"*",
SearchOption.AllDirectories
)
)
{
Directory.CreateDirectory(
Path.Combine(
destinationPath,
Path.GetRelativePath(sourcePath, sourceDirectoryPath)
)
);
Comment thread
Tyrrrz marked this conversation as resolved.
}

// Create file stream pairs
foreach (
var sourceFilePath in Directory.GetFiles(
sourcePath,
"*",
SearchOption.AllDirectories
)
)
{
sourceStreams.Add(File.OpenRead(sourceFilePath));

var destinationFilePath = Path.Combine(
destinationPath,
Path.GetRelativePath(sourcePath, sourceFilePath)
);
Comment thread
Tyrrrz marked this conversation as resolved.

destinationStreams.Add(
overwrite
? File.OpenWrite(destinationFilePath)
: File.Open(
destinationFilePath,
FileMode.CreateNew,
FileAccess.Write,
FileShare.None
)
);
}

// Copy the file contents
foreach (
var (sourceStream, destinationStream) in sourceStreams.Zip(
destinationStreams,
(s, d) => (s, d)
)
)
{
sourceStream.CopyTo(destinationStream);

// Truncate the destination file if the source file is shorter
destinationStream.SetLength(sourceStream.Length);

// Preserve Unix file permissions on non-Windows platforms
if (!OperatingSystem.IsWindows())
{
File.SetUnixFileMode(
destinationStream.Name,
File.GetUnixFileMode(sourceStream.Name)
);
}
}
}
finally
{
Disposable.Merge([.. sourceStreams, .. destinationStreams]).Dispose();
}
}

/// <summary>
/// Checks if it's possible to write to the specified directory.
/// </summary>
Expand Down