[latex3-commits] [latex3/luaotfload] database-iteration-refactor: Don't use lfs.chdir for error detection in find_files_indeed (e19db1da)

github at latex-project.org github at latex-project.org
Sat Jan 27 18:09:56 CET 2024


Repository : https://github.com/latex3/luaotfload
On branch  : database-iteration-refactor
Link       : https://github.com/latex3/luaotfload/commit/e19db1daee0c7a1194de2419b9525c1bf1ea9018

>---------------------------------------------------------------

commit e19db1daee0c7a1194de2419b9525c1bf1ea9018
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sat Jan 27 10:35:33 2024 +0100

    Don't use lfs.chdir for error detection in find_files_indeed


>---------------------------------------------------------------

e19db1daee0c7a1194de2419b9525c1bf1ea9018
 src/luaotfload-database.lua | 44 ++++++++++++++++++++++++++------------------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/src/luaotfload-database.lua b/src/luaotfload-database.lua
index b328379a..c432ca08 100644
--- a/src/luaotfload-database.lua
+++ b/src/luaotfload-database.lua
@@ -318,36 +318,44 @@ local function sanitize_fontnames (rawnames)
     return result
 end
 
+local list_dir_or_empty do
+    local function inner_list_dir_or_empty (dir, success, ...)
+        if success then
+            return ...
+        else
+            -- Skipping unreadable directory.
+            -- TODO: Print warning
+            return next, {}
+        end
+    end
+    function list_dir_or_empty (dir)
+        return inner_list_dir_or_empty(dir, pcall(lfsdir, dir))
+    end
+end
+
 local function find_files_indeed (acc, dirs, filter)
     if not next (dirs) then --- done
         return acc
     end
 
-    local pwd   = lfscurrentdir ()
     local dir   = dirs[#dirs]
     dirs[#dirs] = nil
 
-    if lfschdir (dir) then
-        lfschdir (pwd)
-
-        local newfiles = { }
-        for ent in lfsdir (dir) do
-            if ent ~= "." and ent ~= ".." then
-                local fullpath = dir .. "/" .. ent
-                if filter (fullpath) == true then
-                    if lfsisdir (fullpath) then
-                        dirs[#dirs+1] = fullpath
-                    elseif lfsisfile (fullpath) then
-                        newfiles[#newfiles+1] = fullpath
-                    end
+    local newfiles = { }
+    for ent in list_dir_or_empty (dir) do
+        if ent ~= "." and ent ~= ".." then
+            local fullpath = dir .. "/" .. ent
+            if filter (fullpath) == true then
+                if lfsisdir (fullpath) then
+                    dirs[#dirs+1] = fullpath
+                elseif lfsisfile (fullpath) then
+                    newfiles[#newfiles+1] = fullpath
                 end
             end
         end
-        return find_files_indeed (tableappend (acc, newfiles),
-                                  dirs, filter)
     end
-    --- could not cd into, so we skip it
-    return find_files_indeed (acc, dirs, filter)
+    return find_files_indeed (tableappend (acc, newfiles),
+                              dirs, filter)
 end
 
 local function dummyfilter () return true end





More information about the latex3-commits mailing list.