lunarmodules / luafilesystem

LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution.
https://lunarmodules.github.io/luafilesystem/
MIT License
900 stars 291 forks source link

Function lfs.mkdir() is not idempotent and hence not thread safe #156

Open alerque opened 2 years ago

alerque commented 2 years ago

Related to #55, but not quite the same ... using lfs.mkdir() is not thread safe. Because it is not idempotent it is quite common to wrap it in a check such as:

if not lfs.attributes(path, "mode") == "directory" then
   lfs.mkdir(path)
end

Unfortunately in a multi-threaded environment is is possible for the directory check to return false but another thread to create the directory in the intervening milliseconds.

A new flag needs to be added so that ERROR_FILE_EXISTS is considered a success because no operation was needed. It's likely the existing syscalls can be leveraged by passing the right flags so the success is properly reported.