Skip to content

System.IO.FileSystem.AccessControl can't set ACLs on files with long paths #91980

@hach-que

Description

@hach-que

Description

When attempting to set ACLs on a file with a long path on Windows, SetAccessControl throws ArgumentException: Invalid name:

Failed to grant Everyone full control of 'C:\ProgramData\UET\SDKs\Android-android-33-33.0.1-3.10.2.4988404-25.1.8937393-jdk-11.0.19+7\Sdk\cmdline-tools\lib\external\com\google\guava\listenablefuture\9999.0-empty-to-avoid-conflict-with-guava\listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar'. Exception was: System.ArgumentException: Invalid name. (Parameter 'name')    at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext)    at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath)

Reproduction Steps

var dacl = file.GetAccessControl(AccessControlSections.Access);
dacl.AddAccessRule(new FileSystemAccessRule(_everyoneWindows.Value, FileSystemRights.FullControl, AccessControlType.Allow));
file.SetAccessControl(dacl);

where file is a FileInfo pointing to a file with a long path.

Expected behavior

SetAccessControl should work with long paths on Windows.

Actual behavior

SetAccessControl throws ArgumentException: Invalid name.

Regression?

No response

Known Workarounds

I tried to workaround this with this code:

var dacl = new FileSecurity(@"\\?\" + file.FullName, AccessControlSections.Access);
dacl.AddAccessRule(new FileSystemAccessRule(_everyoneWindows.Value, FileSystemRights.FullControl, AccessControlType.Allow));
dacl.Persist(@"\\?\" + file.FullName);

however, Persist is internal/protected and not available to user code. You also can't inherit from FileSystemSecurity to access the protected method, as all constructors on that class are internal.

Configuration

.NET 7

Other information

No response

Metadata

Metadata

Assignees

Labels

area-System.IOhelp wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions