Question

How can I check in Lua if a string is the path to an executable file? It seems that neither the standard library nor, surprisingly, LuaFileSystem provides a way to do this.

Was it helpful?

Solution

LuaFileSystem has the lfs.attributes() function which returns a table. That, rather perversely, has a key named "mode" which contains a string describing the "type" of node (file, directory, socket, etc).

Although it's not listed in the manual at: http://keplerproject.github.io/luafilesystem/manual.html ... which seems to be the canonical reference for that module ... there is also a 'permissions' key in that table. I think you could parse it for any "x" characters.

I discovered this with:

#!lua 
local lfs = require 'lfs'
attr = lfs.attributes('./some_file')
for name, value in pairs(attr) do
    print (name,value)
    end

OTHER TIPS

luaposix has the stat function. It returns a number for the mode field which includes execute permission bits (unlike LuaFileSystem which only gives you the file type.)

There is also access which will check permissions for only the current user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top