File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed
src/libraries/System.IO.FileSystem.AccessControl Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -218,8 +218,8 @@ private static FileAccess GetFileAccessFromRights(FileSystemRights rights)
218218 {
219219 FileAccess access = 0 ;
220220
221- if ( ( rights & FileSystemRights . FullControl ) != 0 ||
222- ( rights & FileSystemRights . Modify ) != 0 )
221+ if ( ( rights & FileSystemRights . FullControl ) == FileSystemRights . FullControl ||
222+ ( rights & FileSystemRights . Modify ) == FileSystemRights . Modify )
223223 {
224224 return FileAccess . ReadWrite ;
225225 }
Original file line number Diff line number Diff line change @@ -507,6 +507,42 @@ public void FileInfo_Create_FileSecurity_DenyAccessRule(FileSystemRights rightsT
507507 VerifyAccessSecurity ( expectedSecurity , actualSecurity ) ;
508508 }
509509
510+ [ Fact ]
511+ public void FileInfo_Create_ReadRights_CanWriteIsFalse ( )
512+ {
513+ // Regression test: When opening a file with FileSystemRights.Read, the FileStream should have CanWrite = false
514+ using var tempRootDir = new TempAclDirectory ( ) ;
515+ string path = tempRootDir . GenerateSubItemPath ( ) ;
516+ var fileInfo = new FileInfo ( path ) ;
517+
518+ // First create the file
519+ using ( FileStream createStream = fileInfo . Create (
520+ FileMode . CreateNew ,
521+ FileSystemRights . Write ,
522+ FileShare . ReadWrite | FileShare . Delete ,
523+ DefaultBufferSize ,
524+ FileOptions . None ,
525+ null ) )
526+ {
527+ Assert . True ( fileInfo . Exists ) ;
528+ tempRootDir . CreatedSubfiles . Add ( fileInfo ) ;
529+ }
530+
531+ // Now open it with read-only rights
532+ using ( FileStream readStream = fileInfo . Create (
533+ FileMode . Open ,
534+ FileSystemRights . Read ,
535+ FileShare . ReadWrite | FileShare . Delete ,
536+ DefaultBufferSize ,
537+ FileOptions . None ,
538+ null ) )
539+ {
540+ // The stream should be read-only
541+ Assert . False ( readStream . CanWrite , "FileStream opened with FileSystemRights.Read should have CanWrite = false" ) ;
542+ Assert . True ( readStream . CanRead , "FileStream opened with FileSystemRights.Read should have CanRead = true" ) ;
543+ }
544+ }
545+
510546 #endregion
511547
512548 #region DirectorySecurity CreateDirectory
You can’t perform that action at this time.
0 commit comments