Reputation: 76
From the question below, I was able to figure out how to check if it can be run on a posix compatible file system.
How to check if a file is executable in go?
However, I don't think this works for Windows. Is there a good way?
Upvotes: 4
Views: 1315
Reputation: 1
I have seen four ways of doing this:
Based on this comment:
Windows has
AccessCheck
with which you can check forFILE_GENERIC_EXECUTE
access. First you have to callOpenThreadToken
(or, if that fails,OpenProcessToken
andDuplicateToken
) to get an impersonation token andGetNamedSecurityInfoW
to get the file security (owner, label, attribute, and DACL).
I found a Go package with the function UserHasPermission. I think this is the correct method to do this.
Used by PHP and Rust. This checks if a file if of executable type, but does check executable permission.
Used by Go and Python and Nim. The logic here is all files are readable and executable, and only differentiates on if a file is writable.
Used by Ruby. The logic here is all files are readable and executable, and only differentiates on if a file is writable.
Upvotes: 4