diff --git a/src/libraries/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/FileSystemSecurity.cs b/src/libraries/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/FileSystemSecurity.cs index c438a4e9eb36f..cece82a21343a 100644 --- a/src/libraries/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/FileSystemSecurity.cs +++ b/src/libraries/System.IO.FileSystem.AccessControl/src/System/Security/AccessControl/FileSystemSecurity.cs @@ -121,7 +121,7 @@ internal void Persist(string fullPath) try { AccessControlSections persistRules = GetAccessControlSectionsFromChanges(); - base.Persist(fullPath, persistRules); + base.Persist(PathInternal.EnsureExtendedPrefixIfNeeded(fullPath), persistRules); OwnerModified = GroupModified = AuditRulesModified = AccessRulesModified = false; } finally diff --git a/src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs b/src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs index 271f28fb2bd0a..cc119fc6ad8d8 100644 --- a/src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs +++ b/src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs @@ -143,6 +143,25 @@ public void SetAccessControl_FileInfo_FileSecurity_Success() fileInfo.SetAccessControl(fileSecurity); } + [Fact] + public void SetAccessControl_FileInfo_FileSecurity_Success_NameLongerThanMaxShortPath() + { + using var directory = new TempAclDirectory(); + + const int MaxShortPath = 260; + int fileNameLength = Math.Max(MaxShortPath - directory.Path.Length, 1); + + string path = Path.Combine(directory.Path, new string('1', fileNameLength) + ".txt"); + using var file = new TempFile(path, 1); + var fileInfo = new FileInfo(file.Path); + FileSecurity fileSecurity = fileInfo.GetAccessControl(AccessControlSections.Access); + + var newAccessRule = new FileSystemAccessRule(Helpers.s_NetworkServiceNTAccount, FileSystemRights.Write, AccessControlType.Allow); + fileSecurity.SetAccessRule(newAccessRule); + + fileInfo.SetAccessControl(fileSecurity); + } + [Fact] public void SetAccessControl_FileStream_FileSecurity_InvalidArguments() {