From 3a8e9f0333dd26f6bef5735f8fc21be84c926eac Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Mon, 12 Dec 2022 02:07:01 +0100 Subject: [PATCH] DocSettings: Refine the primary/backup mtime shenanigans Use the same mtime (the newest) for both entries, so that priority order wins the tie, giving priority to the primary. Avoids preferring an older legacy sidecar in case of issues with the new files. --- frontend/docsettings.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/frontend/docsettings.lua b/frontend/docsettings.lua index 0b151ec64..ca49711a5 100644 --- a/frontend/docsettings.lua +++ b/frontend/docsettings.lua @@ -24,12 +24,18 @@ local function buildCandidates(list) -- Ignore missing files. if file_path ~= "" and lfs.attributes(file_path, "mode") == "file" then local mtime = lfs.attributes(file_path, "modification") - -- NOTE: Extra trickery: if we're inserting a "backup" file, and its main buddy exists, - -- make sure it will *never* sort ahead of it by negating its mtime. + -- NOTE: Extra trickery: if we're inserting a "backup" file, and its primary buddy exists, + -- make sure it will *never* sort ahead of it by using the same mtime. -- This aims to avoid weird UTC/localtime issues when USBMS is involved, -- c.f., https://github.com/koreader/koreader/issues/9227#issuecomment-1345263324 if file_path:sub(-4) == ".old" and previous_entry_exists then - mtime = -mtime + local primary_mtime = candidates[#candidates].mtime + -- Only proceed with the switcheroo when necessary, and warn about it. + if primary_mtime < mtime then + logger.warn("DocSettings: Backup", file_path, "is newer (", mtime, ") than its primary (", primary_mtime, "), fudging timestamps!") + -- Use the most recent timestamp for both (i.e., the backup's). + candidates[#candidates].mtime = mtime + end end table.insert(candidates, { path = file_path, @@ -44,7 +50,7 @@ local function buildCandidates(list) end -- MRU sort, tie breaker is insertion order (higher priority locations were inserted first). - -- Iff a main/backup pair of file both exist, of the two of them, the main one *always* has priority, + -- Iff a primary/backup pair of file both exist, of the two of them, the primary one *always* has priority, -- regardless of mtime (c.f., NOTE above). table.sort(candidates, function(l, r) if l.mtime == r.mtime then