From 5fa488d55de0b89708e6028491fffa5f43ca279d Mon Sep 17 00:00:00 2001
From: Philipp Gesang <phg42.2a@gmail.com>
Date: Sat, 22 Jun 2013 13:11:31 +0200
Subject: add extra safeguard against moved files to cached resolver

---
 luaotfload-database.lua | 78 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 56 insertions(+), 22 deletions(-)

diff --git a/luaotfload-database.lua b/luaotfload-database.lua
index 8106898..41adf88 100644
--- a/luaotfload-database.lua
+++ b/luaotfload-database.lua
@@ -34,6 +34,7 @@ local ioopen                  = io.open
 local kpseexpand_path         = kpse.expand_path
 local kpseexpand_var          = kpse.expand_var
 local kpselookup              = kpse.lookup
+local kpsefind_file           = kpse.find_file
 local kpsereadable_file       = kpse.readable_file
 local lfsisdir                = lfs.isdir
 local lfsisfile               = lfs.isfile
@@ -424,6 +425,52 @@ crude_file_lookup = function (filename)
     return filename, nil
 end
 
+--[[doc--
+Existence of the resolved file name is verified differently depending
+on whether the index entry has a texmf flag set.
+--doc]]--
+
+local get_font_file = function (fullnames, entry)
+    local basename = entry.basename
+    if entry.texmf == true then
+        if kpselookup(basename) then
+            return true, basename, entry.subfont
+        end
+    else
+        local fullname = fullnames[entry.index]
+        if lfsisfile(fullname) then
+            return true, basename, entry.subfont
+        end
+    end
+    return false
+end
+
+--[[doc--
+We need to verify if the result of a cached lookup actually exists in
+the texmf or filesystem.
+--doc]]--
+
+local verify_font_file = function (basename)
+    local filenames = names.data.filenames
+    local idx = filenames.base[basename]
+    if not idx then
+        return false
+    end
+
+    --- firstly, check filesystem
+    local fullname = filenames.full[idx]
+    if fullname and lfsisfile(fullname) then
+        return true
+    end
+
+    --- secondly, locate via kpathsea
+    if kpsefind_file(basename) then
+        return true
+    end
+
+    return false
+end
+
 --[[doc--
 Lookups can be quite costly, more so the less specific they are.
 Even if we find a matching font eventually, the next time the
@@ -496,9 +543,16 @@ resolve_cached = function (_, _, specification)
     --- case 1) cache positive ----------------------------------------
     if found then --- replay fields from cache hit
         report("info", 4, "cache", "found!")
-        return found[1], found[2], true
+        local basename = found[1]
+        --- check the presence of the file in case it’s been removed
+        local success = verify_font_file(basename)
+        if success == true then
+            return basename, found[2], true
+        end
+        report("both", 4, "cache", "cached file not found; resolving again")
+    else
+        report("both", 4, "cache", "not cached; resolving")
     end
-    report("both", 4, "cache", "not cached; resolving")
 
     --- case 2) cache negative ----------------------------------------
     --- first we resolve normally ...
@@ -541,26 +595,6 @@ local add_to_match = function (
     return found, continue
 end
 
---[[doc--
-Existence of the resolved file name is verified differently depending
-on whether the index entry has a texmf flag set.
---doc]]--
-
-local get_font_file = function (fullnames, entry)
-    local basename = entry.basename
-    if entry.texmf == true then
-        if kpselookup(basename) then
-            return true, basename, entry.subfont
-        end
-    else
-        local fullname = fullnames[entry.index]
-        if lfsisfile(fullname) then
-            return true, basename, entry.subfont
-        end
-    end
-    return false
-end
-
 --[[doc--
 
 Luatex-fonts, the font-loader package luaotfload imports, comes with
-- 
cgit v1.2.3