Search code examples
lualuafilesystem

How to check if a file is read only?


Neither io nor lfs seem to have an option to check for this. I need to use this feature to simulate some code that does this check.


Solution

  • It seems there's an undocumented permissions field in the table returned by lfs.attributes(). Thanks to @siffiejoe for pointing this out, didn't see it when I read the code.

    Another approach is of course to try to read from the file, that will always work but might be slower.

    You could also try opening the file and setting a (temporary, of course) read lock on it. Not sure about the portability of file locking in Lua, though.

    Note: checking if a file is readable and then doing something can break due to race conditions; the permissions of a file can change between your check and the following action.