Skip to content

Add Directory.CheckWriteAccess and File.CheckWriteAccess#25

Merged
Tyrrrz merged 15 commits intoprimefrom
copilot/add-directory-check-write-access
Apr 14, 2026
Merged

Add Directory.CheckWriteAccess and File.CheckWriteAccess#25
Tyrrrz merged 15 commits intoprimefrom
copilot/add-directory-check-write-access

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

Adds two new extension methods for probing write access before attempting I/O operations.

New methods

  • Directory.CheckWriteAccess(string path) — creates a GUID-named temp file inside the directory and deletes it; returns false on UnauthorizedAccessException.
  • File.CheckWriteAccess(string path) — opens the path with OpenOrCreate + Write; cleans up the created file if it didn't previously exist; returns false on UnauthorizedAccessException.
if (Directory.CheckWriteAccess(outputDir))
    // proceed with writing

if (File.CheckWriteAccess(configPath))
    // safe to overwrite

Cleanup (temp file deletion) is wrapped in its own try-catch so a deletion failure doesn't affect the boolean result. Other exceptions (IOException, DirectoryNotFoundException, etc.) propagate normally — only permission denials are swallowed.

Tests

  • DirectoryExtensionsTests: writable directory → true; read-only directory → false (Unix non-root only, since FileAttributes.ReadOnly has no effect on directories on Windows).
  • FileExtensionsTests: writable file → true; FileAttributes.ReadOnly file → false (cross-platform); non-existent file in writable directory → true with no file left behind.

Copilot AI and others added 3 commits April 12, 2026 19:21
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/55625865-2452-46a8-89c6-b587912161da

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
@Tyrrrz Tyrrrz added the enhancement New feature or request label Apr 14, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review April 14, 2026 16:41
Copilot AI review requested due to automatic review settings April 14, 2026 16:41
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 14, 2026

@copilot resolve the merge conflicts in this pull request

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds write-access probing helpers to PowerKit’s IO extensions so callers can check whether a directory/file is writable (without swallowing non-permission errors), along with tests covering expected behavior.

Changes:

  • Add File.CheckWriteAccess(string path) to probe write access to a specific file path (with cleanup for newly-created files).
  • Add Directory.CheckWriteAccess(string path) to probe write access to a directory by creating and deleting a temporary file.
  • Add unit tests for both helpers, including read-only scenarios and non-existent file cleanup.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
PowerKit/Extensions/FileExtensions.cs Adds File.CheckWriteAccess write-access probe for file paths.
PowerKit/Extensions/DirectoryExtensions.cs Introduces Directory.CheckWriteAccess write-access probe for directories.
PowerKit.Tests/FileExtensionsTests.cs Adds tests for writable/read-only/non-existent file scenarios.
PowerKit.Tests/DirectoryExtensionsTests.cs Adds tests for writable/read-only directory scenarios (with platform/privilege gating).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/FileExtensions.cs Outdated
Comment on lines +17 to +21
var existed = File.Exists(path);

try
{
using (new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) { }
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File.Exists() is used to decide whether to delete the path later, but this introduces a race: if the file is created by another process after the existence check (or replaced between the check and deletion), this method can end up deleting a file it didn't create. Consider avoiding the TOCTOU by (a) opening with FileMode.Open when the file exists, otherwise creating with FileMode.CreateNew and ensuring only that newly-created file is cleaned up (e.g., FileOptions.DeleteOnClose or tracking the create operation outcome).

Copilot uses AI. Check for mistakes.
Comment thread PowerKit/Extensions/FileExtensions.cs Outdated
Comment thread PowerKit/Extensions/FileExtensions.cs Outdated
Comment thread PowerKit/Extensions/DirectoryExtensions.cs Outdated
Comment thread PowerKit/Extensions/DirectoryExtensions.cs Outdated
…y-check-write-access

# Conflicts:
#	PowerKit.Tests/DirectoryExtensionsTests.cs
#	PowerKit.Tests/FileExtensionsTests.cs
#	PowerKit/Extensions/DirectoryExtensions.cs
#	PowerKit/Extensions/FileExtensions.cs

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 14, 2026

@copilot resolve the merge conflicts in this pull request

Done. Merged prime into the branch in commit bd4f2ea, combining the new CheckWriteAccess methods with all the additions from prime (Directory.Reset, Directory.TryDelete, File.TryDelete, and the other new extensions). All 119 tests pass.

Copilot AI requested a review from Tyrrrz April 14, 2026 16:47
Tyrrrz and others added 8 commits April 14, 2026 19:47
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Tyrrrz Tyrrrz requested a review from Copilot April 14, 2026 16:57
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 14, 2026

@copilot getting test errors

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit.Tests/FileExtensionsTests.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 14, 2026

@copilot getting test errors

The failures were on intermediate commits where the probe file wasn't being cleaned up reliably on Windows. The current HEAD (2281bb8) passes all 119 tests — CI is green and confirmed locally in Release configuration too.

Copilot AI requested a review from Tyrrrz April 14, 2026 17:06
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 14, 2026

@copilot let's use SkippableFact to short-circuit tests in unsupported environments

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 14, 2026

@copilot let's use SkippableFact to short-circuit tests in unsupported environments

Done in commit e0a17a9. Added Xunit.SkippableFact 1.5.61 to the project and replaced the if (...) { return; } early-exits in both CheckWriteAccess_ReadOnly_Test methods with [SkippableFact] + Skip.If(...). The condition logic is unchanged — the tests now properly report as skipped rather than silently passing.

Copilot AI requested a review from Tyrrrz April 14, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants