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

github at latex-project.org github at latex-project.org
Sat Jan 27 10:35:59 CET 2024


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

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

commit 5d485091941ec50da721f1f39ae89720ceff68ed
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


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

5d485091941ec50da721f1f39ae89720ceff68ed
 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.