2

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.

4
  • 1
    Use loadfile() and check if it returns an error?
    – hjpotter92
    May 9, 2013 at 16:43
  • 2
    loadfile is for loading Lua code, not for checking if a file is binary executable, @hjpotter92. May 9, 2013 at 17:10
  • @Omar, the way you phrased your question it seemed that you wanted to know how to check, if a file can be run within Lua (that is, if it is a Lua script). I got the same impression and that's why loadfile came up, I suppose...
    – W.B.
    May 9, 2013 at 20:53
  • Oh, OK, @W.B., I'll rephrase. May 9, 2013 at 21:36

2 Answers 2

6

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
1
  • 2
    Oh, that's what I get for reading documentation instead of experimenting in the REPL. Thanks! May 9, 2013 at 17:12
4

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.