Add missing Invalidate() calls to FileInfo state-changing methods#124429
Open
lufen wants to merge 4 commits intodotnet:mainfrom
Open
Add missing Invalidate() calls to FileInfo state-changing methods#124429lufen wants to merge 4 commits intodotnet:mainfrom
lufen wants to merge 4 commits intodotnet:mainfrom
Conversation
Fixes dotnet#117709 — FileInfo methods that alter file system state now call Invalidate() so cached properties (Exists, Length, timestamps) are lazily refreshed on next access, matching the pattern already used by Create(), Delete(), and MoveTo(). Methods fixed (FileInfo.cs): - Open(FileStreamOptions) - Open(FileMode, FileAccess, FileShare) - OpenRead() - OpenWrite() - OpenText() - Replace(string, string?) - Encrypt() - Decrypt() - CreateAsHardLink(string) Comprehensive method audit across FileInfo, DirectoryInfo, FileSystemInfo: FileInfo — already correct (no change needed): Create(), Delete(), MoveTo(), CreateText(), AppendText(), CopyTo() (returns new obj), IsReadOnly setter (via Attributes) DirectoryInfo — already correct (no change needed): Create(), Delete(), Delete(bool), MoveTo() CreateSubdirectory() returns new obj, parent unchanged FileSystemInfo — already correct (no change needed): CreateAsSymbolicLink(), Attributes setter, CreationTime(Utc) setter, LastAccessTime(Utc) setter, LastWriteTime(Utc) setter, UnixFileMode setter, ResolveLinkTarget() (read-only) Read-only methods (no invalidation needed): GetFiles, GetDirectories, GetFileSystemInfos, Enumerate* Includes 12 regression tests in InvalidateOnStateChange.cs covering all fixed methods plus CreateText/AppendText (already worked). Co-authored-by: Copilot <[email protected]>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes issue #117709 by ensuring FileInfo methods that alter or access file system state consistently call Invalidate() to refresh cached metadata. The fix addresses an inconsistency where some state-changing methods (like Create() and Delete()) called Invalidate() while others (like Open(FileMode), OpenRead(), etc.) did not, leading to stale cached properties.
Changes:
- Added
Invalidate()calls to 9 FileInfo methods that were missing them (Open variants, Replace, Encrypt/Decrypt, CreateAsHardLink) - Added comprehensive regression tests (12 test methods) covering all fixed methods plus verification of existing correct behavior
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/IO/FileInfo.cs | Added Invalidate() calls to Open(FileStreamOptions), Open(FileMode, FileAccess, FileShare), OpenRead(), OpenWrite(), OpenText(), Replace(), Encrypt(), Decrypt(), and CreateAsHardLink() methods |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileInfo/InvalidateOnStateChange.cs | New comprehensive test file with 12 tests verifying Invalidate() behavior for all file-opening and state-changing methods |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj | Added reference to new test file |
…filesystems The CreateAsHardLink_InvalidatesExists test fails with 'Too many links' (EMLINK) on filesystems that do not support hard links. Guard the test with MountHelper.CanCreateHardLinks, consistent with BaseHardLinks_FileSystem. Co-authored-by: Copilot <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #117709 — FileInfo methods that alter file system state now call Invalidate() so cached properties (Exists, Length, timestamps) are lazily refreshed on next access, matching the pattern already used by Create(), Delete(), and MoveTo().
Methods fixed (FileInfo.cs):
Comprehensive method audit across FileInfo, DirectoryInfo, FileSystemInfo:
FileInfo — already correct (no change needed):
Create(), Delete(), MoveTo(), CreateText(), AppendText(),
CopyTo() (returns new obj), IsReadOnly setter (via Attributes)
DirectoryInfo — already correct (no change needed):
Create(), Delete(), Delete(bool), MoveTo()
CreateSubdirectory() returns new obj, parent unchanged
FileSystemInfo — already correct (no change needed):
CreateAsSymbolicLink(), Attributes setter, CreationTime(Utc) setter,
LastAccessTime(Utc) setter, LastWriteTime(Utc) setter,
UnixFileMode setter, ResolveLinkTarget() (read-only)
Read-only methods (no invalidation needed):
GetFiles, GetDirectories, GetFileSystemInfos, Enumerate*
Includes 12 regression tests in InvalidateOnStateChange.cs covering all fixed methods plus CreateText/AppendText (already worked).