gochist
gochist

Reputation: 76

How to check if a file is executable on Windows with golang?

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

Answers (1)

Zombo
Zombo

Reputation: 1

I have seen four ways of doing this:

AccessCheck

Based on this comment:

Windows has AccessCheck with which you can check for FILE_GENERIC_EXECUTE access. First you have to call OpenThreadToken (or, if that fails, OpenProcessToken and DuplicateToken) to get an impersonation token and GetNamedSecurityInfoW 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.

GetBinaryTypeW

Used by PHP and Rust. This checks if a file if of executable type, but does check executable permission.

GetFileAttributesW

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.

GetFileInformationByHandle

Used by Ruby. The logic here is all files are readable and executable, and only differentiates on if a file is writable.

Upvotes: 4

Related Questions