Get ACL - write access for directory

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Taurus
Posts: 94
Joined: 20 Jan 2015, 10:31

Get ACL - write access for directory

20 Sep 2017, 07:26

Hi,

I try to get the ACL access list for a specified directory. It has to work on Windows drives as well as on shared drives (Win Server, NAS and Linux-Server).

At the moment, this is my code:

Code: Select all

Check_Rights(ByRef Dir)
{
	File_Write_Test := Dir "\" A_UserName A_TickCount ".ini"
	
	FileAppend, 0, % File_Write_Test		
	if ErrorLevel
	{
		Sleep, 1000
		FileAppend, 0, % File_Write_Test
		if ErrorLevel
			return "no write"
	}
	
	FileAppend, 0, % File_Write_Test		
	if ErrorLevel
	{
		Sleep, 1000
		FileAppend, 0, % File_Write_Test
		if ErrorLevel
			return "no change"
	}
		
	
	FileDelete, % File_Write_Test
	if ErrorLevel
	{
		Sleep, 1000
		FileDelete, % File_Write_Test
		if ErrorLevel
			return "no delete"
	}		
	
	return "ok"
}
It's working, but it's a little bit slow, because i scan for it a lot of times. Is there any faster/better way?

Thanks! ;)
:beard: Full Stack Developer > Dev for a better world | PHP for Web | AHK H for Local | with KISS (Keep IT Short and Simple) on Win 10 Pro (Version 2004) x64
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get ACL - write access for directory

20 Sep 2017, 14:19

Hi, Taurus,

The easiest way to get an access list is using WMI like this:

Code: Select all

; only for folders, for files use CIM_DataFile instead of CIM_Directory

filePath := A_ProgramFiles   ; specify a folder path
AccessMask := ComObjGet("winmgmts:").Get("CIM_Directory.name='" . filePath . "'").AccessMask
if (AccessMask = "")  {
   MsgBox, Failed to get AccessMask
   Return
}

FILE_ADD_FILE := 2, FILE_DELETE_CHILD := 64

MsgBox, % "ADD_FILE = "     . !!(AccessMask & FILE_ADD_FILE) . "`n"
        . "DELETE_CHILD = " . !!(AccessMask & FILE_DELETE_CHILD)
Results may vary, if you run the script as admin.
The code to get the full access list for files or folders:

Code: Select all

if !AccessMask := GetAccessMask(A_ProgramFiles)
   Return

MsgBox, % AccessMask.FILE_ADD_FILE

for k, v in AccessMask
   MsgBox, % k . " = " . v

GetAccessMask(filePath)  {
   IsDirectory := InStr( FileExist(filePath), "D" )
   AccessMask := { 1:       IsDirectory ? "FILE_LIST_DIRECTORY"   : "FILE_READ_DATA"
                 , 2:       IsDirectory ? "FILE_ADD_FILE"         : "FILE_WRITE_DATA"
                 , 4:       IsDirectory ? "FILE_ADD_SUBDIRECTORY" : "FILE_APPEND_DATA"
                 , 8:       "FILE_READ_EA"
                 , 16:      "FILE_WRITE_EA"
                 , 32:      IsDirectory ? "FILE_TRAVERSE"         : "FILE_EXECUTE"
                 , 64:      IsDirectory ? "FILE_DELETE_CHILD"     : ""
                 , 128:     "FILE_READ_ATTRIBUTES"
                 , 256:     "FILE_WRITE_ATTRIBUTES"
                 , 65536:   "DELETE"
                 , 131072:  "READ_CONTROL"
                 , 262144:  "WRITE_DAC"
                 , 524288:  "WRITE_OWNER"
                 , 1048576: "SYNCHRONIZE" }
                 
   fileAccessMask := ComObjGet("winmgmts:").Get( (IsDirectory ? "CIM_Directory" : "CIM_DataFile") . ".name='" . filePath . "'" ).AccessMask
   if (fileAccessMask = "")  {
      MsgBox, Failed to get AccessMask
      Return
   }
   accsessList := {}
   for k, v in AccessMask
      ( v && accsessList[v] := !!(fileAccessMask & k) )
   Return accsessList
}
CIM_DataFile
CIM_Directory
File and Directory Access Rights Constants
User avatar
Taurus
Posts: 94
Joined: 20 Jan 2015, 10:31

Re: Get ACL - write access for directory

22 Sep 2017, 04:24

Thanks. I will try that out. Is there any ahk native way? I had some systems where winmgmts was broken.
:beard: Full Stack Developer > Dev for a better world | PHP for Web | AHK H for Local | with KISS (Keep IT Short and Simple) on Win 10 Pro (Version 2004) x64
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get ACL - write access for directory

22 Sep 2017, 06:03

Yes with DllCall's...
See 2nd CodeBox https://autohotkey.com/boards/viewtopic.php?t=27709 by qwerty12
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Taurus
Posts: 94
Joined: 20 Jan 2015, 10:31

Re: Get ACL - write access for directory

25 Sep 2017, 05:05

DllCalls... maybe i should dig into it.

I found a full list of it here: https://hotkeyit.github.io/v2/docs/commands/WinApi.htm

It should be something like this: https://msdn.microsoft.com/en-us/librar ... s.85).aspx

One question: Would you rely on WinAPI or should i better stay with ahk functions?
:beard: Full Stack Developer > Dev for a better world | PHP for Web | AHK H for Local | with KISS (Keep IT Short and Simple) on Win 10 Pro (Version 2004) x64
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get ACL - write access for directory

25 Sep 2017, 13:05

Learning winapi extends your abilities. There are many things you can't achieve without using DllCall().

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 207 guests