Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
42 changes: 42 additions & 0 deletions PowerKit.Tests/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using FluentAssertions;
using PowerKit;
Expand All @@ -8,6 +9,47 @@ namespace PowerKit.Tests;

public class DirectoryExtensionsTests
{
[Fact]
public void CheckWriteAccess_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();

// Act
var result = Directory.CheckWriteAccess(tempDir.Path);

// Assert
result.Should().BeTrue();
}

[Fact]
public void CheckWriteAccess_ReadOnly_Test()
{
// FileAttributes.ReadOnly removes write bits on Unix but has no effect on directories on Windows
if (OperatingSystem.IsWindows() || Environment.IsPrivilegedProcess)
{
return;
}

// Arrange
using var tempDir = TempDirectory.Create();
var dirInfo = new DirectoryInfo(tempDir.Path);
dirInfo.Attributes |= FileAttributes.ReadOnly;

try
{
// Act
var result = Directory.CheckWriteAccess(tempDir.Path);

// Assert
result.Should().BeFalse();
}
finally
{
dirInfo.Attributes &= ~FileAttributes.ReadOnly;
}
}

[Fact]
public void Reset_Test()
{
Expand Down
49 changes: 49 additions & 0 deletions PowerKit.Tests/FileExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,55 @@ namespace PowerKit.Tests;

public class FileExtensionsTests
{
[Fact]
public void CheckWriteAccess_Test()
{
// Arrange
using var tempFile = TempFile.Create();

// Act
var result = File.CheckWriteAccess(tempFile.Path);

// Assert
result.Should().BeTrue();
}

[Fact]
public void CheckWriteAccess_ReadOnly_Test()
{
// Arrange
using var tempFile = TempFile.Create();
File.SetAttributes(tempFile.Path, File.GetAttributes(tempFile.Path) | FileAttributes.ReadOnly);

try
{
// Act
var result = File.CheckWriteAccess(tempFile.Path);

// Assert
result.Should().BeFalse();
Comment thread
Tyrrrz marked this conversation as resolved.
}
finally
{
File.SetAttributes(tempFile.Path, File.GetAttributes(tempFile.Path) & ~FileAttributes.ReadOnly);
}
}

[Fact]
public void CheckWriteAccess_NonExistent_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();
var path = Path.Combine(tempDir.Path, "new-file.txt");

// Act
var result = File.CheckWriteAccess(path);

// Assert
result.Should().BeTrue();
File.Exists(path).Should().BeFalse();
}

[Fact]
public void TryDelete_Test()
{
Expand Down
23 changes: 23 additions & 0 deletions PowerKit/Extensions/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;

namespace PowerKit.Extensions;
Expand Down Expand Up @@ -36,5 +37,27 @@ public static bool TryDelete(string path, bool recursive = false)
return false;
}
}

/// <summary>
/// Checks if it's possible to write to the specified directory.
/// </summary>
public static bool CheckWriteAccess(string path)
{
var tempFilePath = Path.Combine(path, Guid.NewGuid().ToString());

try
{
using var tempFile = File.Create(tempFilePath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
finally
{
File.TryDelete(tempFilePath);
}
}
}
}
25 changes: 25 additions & 0 deletions PowerKit/Extensions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ public static bool TryDelete(string path)
}
}

/// <summary>
/// Checks if it's possible to write to the specified file.
/// </summary>
public static bool CheckWriteAccess(string path)
{
var wasExisting = File.Exists(path);

try
{
using var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
Comment thread
Tyrrrz marked this conversation as resolved.
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
finally
{
if (!wasExisting)
{
File.TryDelete(path);
Comment thread
Tyrrrz marked this conversation as resolved.
}
}
}

/// <summary>
/// Creates a file at the specified path and fills it with zeroes.
/// </summary>
Expand Down