简体   繁体   中英

Getting intermittent "Access is Denied" exception when accessing a file after changing the permissions on windows

Getting intermittent "Access is Denied" while accessing a file after changing the permission using icacls on windows.

Details: Please refer to the script which has been run as administrator.

import os


for x in range(50):

    #this exe will check the existence of a file , is not present then copy the file and change the permission to ADMINISTRATOR/SYSTEM:F and BUILTIN/Users:(RX)

    file = 'C:\\Program Files\\***\\test.exe'
    retval = os.system('"' +  file +  '" -i')
    print("retVal : %d" %retval)

    #reset the permission of the same file and the parent folder

    os.system("icacls C:\\ProgramData\\***\\ParentFolder /reset /T /Q /C")
    os.system("icacls C:\\ProgramData\\***\\file.xml /grant *S-1-5-32-545:(W) /T /Q /C")

We got the answer – icacls was running asynchronously hence while the permissions were getting changed, and at the same time if we will try to access the file then windows was giving "Access denied". Therefore, instead of using icacls we starting using windows os APIs like https://learn.microsoft.com/en-us/windows/win32/api/aclapi/nf-aclapi-setnamedsecurityinfoa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM