Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

windows

go

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?

like image 340
gochist Avatar asked Oct 24 '25 18:10

gochist


1 Answers

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.

like image 76
Zombo Avatar answered Oct 26 '25 07:10

Zombo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!