Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
84 changes: 84 additions & 0 deletions PowerKit.Tests/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,90 @@ namespace PowerKit.Tests;

public class DirectoryExtensionsTests
{
[Fact]
public void Copy_Test()
{
// Arrange
using var sourceDir = TempDirectory.Create();
using var destDir = TempDirectory.Create();
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated

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

// Act
Directory.Copy(sourceDir.Path, destDir.Path);

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

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

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

// Act
Directory.Copy(sourceDir.Path, destDir.Path);

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

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

File.WriteAllText(Path.Combine(sourceDir.Path, "file.txt"), "new");
File.WriteAllText(Path.Combine(destDir.Path, "file.txt"), "old");

// Act
Directory.Copy(sourceDir.Path, destDir.Path, overwrite: true);

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

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

File.WriteAllText(Path.Combine(sourceDir.Path, "file.txt"), "source");
File.WriteAllText(Path.Combine(destDir.Path, "file.txt"), "existing");

// Act
var act = () => Directory.Copy(sourceDir.Path, destDir.Path, overwrite: false);

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

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

File.WriteAllText(Path.Combine(sourceDir.Path, "file.txt"), "hi");
File.WriteAllText(Path.Combine(destDir.Path, "file.txt"), "longer content");

// Act
Directory.Copy(sourceDir.Path, destDir.Path, overwrite: true);

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

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

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

/// <summary>
/// Recursively copies all files from <paramref name="sourceDirPath" /> to <paramref name="destDirPath" />.
/// File locks are acquired on every destination file before any data is written,
/// so concurrent readers will observe either the old content or the fully updated content.
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
/// </summary>
public static void Copy(string sourceDirPath, string destDirPath, bool overwrite = true)
{
var sourceStreams = new List<FileStream>();
var destStreams = new List<FileStream>();
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated

try
{
var normalizedSourceDir = sourceDirPath.TrimEnd(
Path.DirectorySeparatorChar,
Path.AltDirectorySeparatorChar
);

foreach (
var sourceFilePath in Directory.GetFiles(
sourceDirPath,
"*",
SearchOption.AllDirectories
)
)
{
sourceStreams.Add(
File.Open(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
);
Comment thread
Tyrrrz marked this conversation as resolved.

var relativePath = sourceFilePath.Substring(normalizedSourceDir.Length + 1);
var destFilePath = Path.Combine(destDirPath, relativePath);

// destFilePath is always a full path under destDirPath, so GetDirectoryName is never null
Directory.CreateDirectory(Path.GetDirectoryName(destFilePath)!);

destStreams.Add(
File.Open(
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
destFilePath,
overwrite ? FileMode.OpenOrCreate : FileMode.CreateNew,
FileAccess.ReadWrite,
FileShare.None
)
);
}

for (var i = 0; i < sourceStreams.Count; i++)
{
sourceStreams[i].CopyTo(destStreams[i]);

// Truncate the destination file if the source file is shorter
destStreams[i].SetLength(sourceStreams[i].Length);
}
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
}
finally
{
foreach (var stream in sourceStreams)
{
try
{
stream.Dispose();
}
catch { }
}

foreach (var stream in destStreams)
{
try
{
stream.Dispose();
}
catch { }
}
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
}
}

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