I am making a software launcher for Windows and Linux, and for some checks I want to see if the files alleged location is pointed to an executable.
Luckily for windows this is as easy as location.get_extension() == "exe", but for Linux as I have found out they use file metadata to determine if a file is executable or not.
Is there a way to easily check this in Godot? Originally I was just going to load the file and check the respective bytes but seemingly its more complex then that; so does Godot have a method for doing this / is there an easy way to check this?
Even on windows that check isn’t guaranteed, as anyone can change the extension of a file to be anything else they wish.
What you really need to do is check for “magic numbers” that are present in the file.
For windows:
0x4d, 0x5a. This is the "magic number" of an EXE file. The first byte of the file is 0x4d and the second is 0x5a.
Source: https://www.delorie.com/djgpp/doc/exe/
For linux, the easiest way to do this is to use the build-in command called file
I believe you can open a file to read it with GDScript easily, so you can check the first and second byte on windows, and on linux, I believe the OS.execute can be used on linux to execute a linux terminal command, see:
I wasn’t entirely sure if the OP also wanted to include bash scripts, as they’re technically executables too, so using the file command, they can check themselves and determine what files they want to mark as executable and which ones they don’t.
From what I have heard when asking / searching about this Linux does not require these header bits to be set like Windows does for a file to be executable, unless I am mistaken?
I suppose there are clever ways to fool every foolproof system, but you can generally rely on an ELF file to be executable. As others have pointed out, the file command can be used for this purpose. It does a lot of gruntwork so you don’t have to.