Skip to content

[BUG] FileInfo.Create wraps file handle with wrong FileSystemRights. #111540

@mikelatcs

Description

@mikelatcs

Description

Calling FileInfo.Create extension method with FileSystemRights.Read as access rights to create a FileStream.

Reproduction Steps

FileInfo fi = new(path);
FileStream stream = fi.Create(
    FileMode.Open,
    FileSystemRights.Read, // <---- Open with read-only access.
    FileShare.ReadWrite | FileShare.Delete,
    4 * KB,
    FileOptions.None,
    null);

// Check stream.CanWrite!

Expected behavior

  • A read-only file handle is opened and wrapped it into a FileStream.
  • The stream file access should be set to FileAccess.Read.
  • The stream CanWrite property should read to false.

Actual behavior

  • A read-only file handle is opened and wrapped it into a FileStream (so far so good).
  • The FileStream file access is wrongly set internally to FileAccess.ReadWrite.
  • As a result, CanWrite is true for the stream. The underlying handle with throw an exception if we try to write.

Regression?

No response

Known Workarounds

No response

Configuration

Windows 10, .NET SDK 7.0.203, using System.IO.FileSystem.AccessControl package.

Other information

The issue is most probably within the FileSystemAclExtensions.GetFileAccessFromRights method.

Almost any value for the FileSystemRights will match the first condition, and most of them will also match the second.

See:

// rights = FileSystemRights.Read;

private static FileAccess GetFileAccessFromRights(FileSystemRights rights)
{
	FileAccess fileAccess = (FileAccess)0;
	if ((rights & FileSystemRights.FullControl) != 0 || (rights & FileSystemRights.Modify) != 0)
	{
		return FileAccess.ReadWrite; // Returns here.
	}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions