@ -9,6 +9,7 @@ local dump = require("dump")
local ffiutil = require ( " ffi/util " )
local ffiutil = require ( " ffi/util " )
local lfs = require ( " libs/libkoreader-lfs " )
local lfs = require ( " libs/libkoreader-lfs " )
local logger = require ( " logger " )
local logger = require ( " logger " )
local util = require ( " util " )
local DocSettings = { }
local DocSettings = { }
@ -140,7 +141,7 @@ function DocSettings:open(docfile)
end
end
end )
end )
local ok , stored , filepath
local ok , stored , filepath
for _ , k in pairs( candidates ) do
for _ , k in i pairs( candidates ) do
-- Ignore empty files
-- Ignore empty files
if lfs.attributes ( k [ 1 ] , " size " ) > 0 then
if lfs.attributes ( k [ 1 ] , " size " ) > 0 then
ok , stored = pcall ( dofile , k [ 1 ] )
ok , stored = pcall ( dofile , k [ 1 ] )
@ -312,7 +313,7 @@ function DocSettings:flush()
self : ensureSidecar ( self.sidecar )
self : ensureSidecar ( self.sidecar )
local s_out = dump ( self.data )
local s_out = dump ( self.data )
os.setlocale ( ' C ' , ' numeric ' )
os.setlocale ( ' C ' , ' numeric ' )
for _ , f in pairs( serials ) do
for _ , f in i pairs( serials ) do
local directory_updated = false
local directory_updated = false
if lfs.attributes ( f , " mode " ) == " file " then
if lfs.attributes ( f , " mode " ) == " file " then
-- As an additional safety measure (to the ffiutil.fsync* calls
-- As an additional safety measure (to the ffiutil.fsync* calls
@ -338,7 +339,7 @@ function DocSettings:flush()
if self.candidates ~= nil
if self.candidates ~= nil
and G_reader_settings : nilOrFalse ( " preserve_legacy_docsetting " ) then
and G_reader_settings : nilOrFalse ( " preserve_legacy_docsetting " ) then
for _ , k in pairs( self.candidates ) do
for _ , k in i pairs( self.candidates ) do
if k [ 1 ] ~= f and k [ 1 ] ~= f .. " .old " then
if k [ 1 ] ~= f and k [ 1 ] ~= f .. " .old " then
logger.dbg ( " Remove legacy file " , k [ 1 ] )
logger.dbg ( " Remove legacy file " , k [ 1 ] )
os.remove ( k [ 1 ] )
os.remove ( k [ 1 ] )
@ -366,12 +367,51 @@ function DocSettings:getFilePath()
end
end
--- Purges (removes) sidecar directory.
--- Purges (removes) sidecar directory.
function DocSettings : purge ( )
function DocSettings : purge ( full )
-- Remove any of the old ones we may consider as candidates
-- in DocSettings:open()
if self.history_file then
if self.history_file then
os.remove ( self.history_file )
os.remove ( self.history_file )
os.remove ( self.history_file .. " .old " )
end
if self.legacy_sidecar_file then
os.remove ( self.legacy_sidecar_file )
end
end
if lfs.attributes ( self.sidecar , " mode " ) == " directory " then
if lfs.attributes ( self.sidecar , " mode " ) == " directory " then
if full then
-- Asked to remove all the content of this .sdr directory,
-- whether it's ours or not
ffiutil.purgeDir ( self.sidecar )
ffiutil.purgeDir ( self.sidecar )
else
-- Only remove the files we know we may have created
-- with our usual names.
for f in lfs.dir ( self.sidecar ) do
local fullpath = self.sidecar .. " / " .. f
local to_remove = false
if lfs.attributes ( fullpath , " mode " ) == " file " then
-- Currently, we only create a single file in there,
-- named metadata.suffix.lua (ie. metadata.epub.lua),
-- with possibly backups named metadata.epub.lua.old and
-- metadata.epub.lua.old_dom20180528, so all sharing the
-- same base: self.sidecar_file
if util.stringStartsWith ( fullpath , self.sidecar_file ) then
to_remove = true
end
end
if to_remove then
os.remove ( fullpath )
logger.dbg ( " purge: removed " , fullpath )
end
end
-- If the sidecar folder ends up empty, os.remove() can delete it.
-- Otherwise, the following statement has no effect.
os.remove ( self.sidecar )
end
end
-- We should have meet the candidate we used and remove it above. But in
-- case we didn't, remove it
if self.filepath and lfs.attributes ( self.filepath , " mode " ) == " file " then
os.remove ( self.filepath )
end
end
self.data = { }
self.data = { }
end
end