Skip to content

Polyfill File.GetUnixFileMode, File.SetUnixFileMode, and Directory.CreateDirectory(path, UnixFileMode)#111

Merged
Tyrrrz merged 26 commits intoprimefrom
copilot/add-polyfill-file-open-methods
Apr 16, 2026
Merged

Polyfill File.GetUnixFileMode, File.SetUnixFileMode, and Directory.CreateDirectory(path, UnixFileMode)#111
Tyrrrz merged 26 commits intoprimefrom
copilot/add-polyfill-file-open-methods

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

Extends the Unix file permission polyfills beyond the no-op FileStreamOptions.UnixCreateMode property to include functional implementations of the related .NET 7 APIs.

New polyfills

  • File.GetUnixFileMode(string path) — P/Invoke to stat (Linux) / stat$INODE64 (macOS); extracts st_mode from a raw 256-byte buffer at the platform-specific offset (macOS: offset 4 / uint16; Linux arm64: offset 16 / uint32; Linux x86_64: offset 24 / uint32). Throws PlatformNotSupportedException on Windows.
  • File.SetUnixFileMode(string path, UnixFileMode mode) — P/Invoke to chmod. Throws PlatformNotSupportedException on Windows.
  • Directory.CreateDirectory(string path, UnixFileMode unixCreateMode) (Net70/Directory.cs, new file) — creates the directory then calls chmod. Throws PlatformNotSupportedException on Windows.

Updated polyfill

File.Open(string, FileStreamOptions) now calls File.SetUnixFileMode when UnixCreateMode is set, for modes that can create files (CreateNew, Create, OpenOrCreate, Append). Previously the property was accepted but silently ignored.

var options = new FileStreamOptions
{
    Mode = FileMode.Create,
    Access = FileAccess.Write,
    UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite,
};
using var stream = File.Open(path, options); // now actually sets 0600 on the created file

Not polyfilled

  • FileStream(FileStreamOptions) constructor — extension constructors aren't supported by the C# extension member syntax.
  • File.Get/SetUnixFileMode(SafeFileHandle, ...) — requires extracting the native fd from the handle across framework generations; out of scope.

Copilot AI and others added 2 commits April 16, 2026 14:00
… polyfills

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/1b411be0-eb5c-43a7-9fd6-93384627f60a

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
…tory(path, UnixFileMode) polyfills and update File.Open to honor UnixCreateMode

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/e7853e0c-e624-4a1d-8215-ca7fe6187d7f

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Comment thread PolyShim.Tests/Net60/FileTests.cs Outdated
Comment thread PolyShim/Net60/File.cs Outdated
Comment thread PolyShim/Net60/FileStreamOptions.cs Outdated
Comment thread PolyShim/Net70/Directory.cs Outdated
Copilot AI requested a review from Tyrrrz April 16, 2026 15:45
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 16, 2026

@Tyrrrz Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub.

You can ask me to try again later by mentioning me in a new comment.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 8f47c64e-a015-4d5f-b883-1b91c3dcfb4e

Sorry for the inconvenience!

Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net70/UnixFileMode.cs Outdated
Comment thread PolyShim/Net60/FileStreamOptions.cs Outdated
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 functional Unix file-permission polyfills (beyond the existing UnixCreateMode surface area) so older TFMs can use the .NET 7-style APIs for getting/setting modes and creating directories with an initial mode.

Changes:

  • Add UnixFileMode enum polyfill and implement File.GetUnixFileMode(string) / File.SetUnixFileMode(string, UnixFileMode) via stat/chmod P/Invokes.
  • Add Directory.CreateDirectory(string, UnixFileMode) polyfill that creates the directory then applies chmod.
  • Add/update tests for Unix permission APIs and update Signatures.md.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
Signatures.md Updates generated signature inventory for the new APIs/types.
PolyShim/Net70/UnixFileMode.cs Adds UnixFileMode enum type polyfill.
PolyShim/Net70/File.cs Implements GetUnixFileMode/SetUnixFileMode using native stat/chmod.
PolyShim/Net70/Directory.cs Adds Directory.CreateDirectory(path, UnixFileMode) polyfill using chmod.
PolyShim/Net60/FileStreamOptions.cs Adds FileStreamOptions type polyfill including UnixCreateMode.
PolyShim/Net60/File.cs Adds File.Open(path, FileStreamOptions) and applies UnixCreateMode via SetUnixFileMode.
PolyShim.Tests/Net70/FileTests.cs Adds a Unix-only test for GetUnixFileMode/SetUnixFileMode.
PolyShim.Tests/Net70/DirectoryTests.cs Adds a Unix-only test for Directory.CreateDirectory(path, UnixFileMode).
PolyShim.Tests/Net60/FileTests.cs Adds tests for File.Open(path, FileStreamOptions) including a UnixCreateMode scenario.

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

Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net60/File.cs Outdated
Comment thread PolyShim/Net70/File.cs Outdated
Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net60/File.cs Outdated
Comment thread PolyShim/Net70/Directory.cs Outdated
Tyrrrz and others added 3 commits April 16, 2026 19:38
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 16, 2026

@copilot please fix formatting for me

Done in 8a0bf70. Moved the #if !NETSTANDARD || NETSTANDARD1_3_OR_GREATER guard outside the class in Net60/File.cs, so the entire class is inside the guard — consistent with the pattern used in Path.cs and Directory.cs.

Comment thread PolyShim/Net60/FileStreamOptions.cs Outdated
…fills .NET 7 API via ConditionalWeakTable)

Agent-Logs-Url: https://github.com/Tyrrrz/PolyShim/sessions/0d511e8d-7d5e-4bc1-ae77-083e9eeb10b2

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
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

This PR extends PolyShim’s Unix permission support by adding .NET 7-style UnixFileMode APIs and wiring them into existing file/directory creation polyfills so callers can set/read permissions on Unix platforms.

Changes:

  • Added UnixFileMode type polyfill plus File.GetUnixFileMode(string) / File.SetUnixFileMode(string, UnixFileMode) implementations via stat/chmod P/Invokes.
  • Added Directory.CreateDirectory(string, UnixFileMode) polyfill (create + chmod) and updated signatures accordingly.
  • Updated/added tests for the new Unix permission APIs and File.Open(string, FileStreamOptions) UnixCreateMode behavior.

Reviewed changes

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

Show a summary per file
File Description
Signatures.md Updates generated signature list and totals to include new Unix permission APIs.
PolyShim/Net70/UnixFileMode.cs Adds the UnixFileMode enum polyfill.
PolyShim/Net70/FileStreamOptions.cs Adds FileStreamOptions.UnixCreateMode via extension member storage.
PolyShim/Net70/File.cs Implements File.GetUnixFileMode/SetUnixFileMode using stat/chmod.
PolyShim/Net70/Directory.cs Adds Directory.CreateDirectory(path, UnixFileMode) and applies mode via chmod.
PolyShim/Net60/FileStreamOptions.cs Adds the FileStreamOptions type polyfill for pre-.NET 6 targets.
PolyShim/Net60/File.cs Implements File.Open(path, FileStreamOptions) and applies UnixCreateMode on create.
PolyShim.Tests/Net70/FileTests.cs Adds Unix-only tests for File.Get/SetUnixFileMode.
PolyShim.Tests/Net70/DirectoryTests.cs Adds Unix-only tests for Directory.CreateDirectory(path, UnixFileMode).
PolyShim.Tests/Net60/FileTests.cs Adds tests for the File.Open(path, FileStreamOptions) polyfill including UnixCreateMode.

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

Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net70/File.cs
Comment thread PolyShim/Net70/Directory.cs Outdated
Comment thread PolyShim/Net70/Directory.cs Outdated
Tyrrrz and others added 3 commits April 16, 2026 21:16
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Tyrrrz Tyrrrz merged commit 8437037 into prime Apr 16, 2026
5 checks passed
@Tyrrrz Tyrrrz deleted the copilot/add-polyfill-file-open-methods branch April 16, 2026 18:26
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