Add Directory.CheckWriteAccess and File.CheckWriteAccess#25
Conversation
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/55625865-2452-46a8-89c6-b587912161da Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/55625865-2452-46a8-89c6-b587912161da Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/55625865-2452-46a8-89c6-b587912161da Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
There was a problem hiding this comment.
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.
| var existed = File.Exists(path); | ||
|
|
||
| try | ||
| { | ||
| using (new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) { } |
There was a problem hiding this comment.
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).
…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>
Done. Merged |
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>
|
@copilot getting test errors |
There was a problem hiding this comment.
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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
The failures were on intermediate commits where the probe file wasn't being cleaned up reliably on Windows. The current HEAD ( |
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/4228dde5-5923-4d9f-a787-019beceffb06 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
|
@copilot let's use SkippableFact to short-circuit tests in unsupported environments |
Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/6384344d-82f6-407d-8a05-e4a5b9a73112 Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Done in commit |
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; returnsfalseonUnauthorizedAccessException.File.CheckWriteAccess(string path)— opens the path withOpenOrCreate + Write; cleans up the created file if it didn't previously exist; returnsfalseonUnauthorizedAccessException.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, sinceFileAttributes.ReadOnlyhas no effect on directories on Windows).FileExtensionsTests: writable file →true;FileAttributes.ReadOnlyfile →false(cross-platform); non-existent file in writable directory →truewith no file left behind.