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

Use Lua 5.4 __close value for lfs.dir() iterator #137

Closed hishamhm closed 4 years ago

hishamhm commented 4 years ago

Adds support for the __close value in the lfs.dir() iterator when using Lua 5.4. This requires no changes from the user, and ensures that the directory will be closed correctly even if you break from the loop.

For example, with uname -n set below 10000, this script crashes on Lua < 5.4, and works in Lua 5.4:

local lfs = require("lfs")
for t = 1, 10000 do
   local i = 0
   for e in lfs.dir("/usr/lib") do
      i = i + 1
      if i == 100 then
         break
      end
   end
end

(This is not a forced example: I have hit this problem before when recursing over large directory trees and collecting files.)

coveralls commented 4 years ago

Coverage Status

Coverage decreased (-0.3%) to 81.922% when pulling 88044c606f12c47891c5cb63c2a350b470e88c08 on 5.4-iterator into 4f7e1b5a49575c77657b77bea383f79cddd768e3 on master.