@ -6,9 +6,9 @@ in the so-called sidecar directory
local DataStorage = require ( " datastorage " )
local DataStorage = require ( " datastorage " )
local dump = require ( " dump " )
local dump = require ( " dump " )
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 purgeDir = require ( " ffi/util " ) . purgeDir
local DocSettings = { }
local DocSettings = { }
@ -187,9 +187,19 @@ function DocSettings:flush()
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 pairs ( serials ) do
local directory_updated = false
if lfs.attributes ( f , " mode " ) == " file " then
if lfs.attributes ( f , " mode " ) == " file " then
logger.dbg ( " Rename " , f , " to " , f .. " .old " )
-- As an additional safety measure (to the ffiutil.fsync* calls
os.rename ( f , f .. " .old " )
-- used below), we only backup the file to .old when it has
-- not been modified in the last 60 seconds. This should ensure
-- in the case the fsync calls are not supported that the OS
-- may have itself sync'ed that file content in the meantime.
local mtime = lfs.attributes ( f , " modification " )
if mtime < os.time ( ) - 60 then
logger.dbg ( " Rename " , f , " to " , f .. " .old " )
os.rename ( f , f .. " .old " )
directory_updated = true -- fsync directory content too below
end
end
end
logger.dbg ( " Write to " , f )
logger.dbg ( " Write to " , f )
local f_out = io.open ( f , " w " )
local f_out = io.open ( f , " w " )
@ -197,6 +207,7 @@ function DocSettings:flush()
f_out : write ( " -- we can read Lua syntax here! \n return " )
f_out : write ( " -- we can read Lua syntax here! \n return " )
f_out : write ( s_out )
f_out : write ( s_out )
f_out : write ( " \n " )
f_out : write ( " \n " )
ffiutil.fsyncOpenedFile ( f_out ) -- force flush to the storage device
f_out : close ( )
f_out : close ( )
if self.candidates ~= nil
if self.candidates ~= nil
@ -212,6 +223,10 @@ function DocSettings:flush()
end
end
end
end
if directory_updated then
-- Ensure the file renaming is flushed to storage device
ffiutil.fsyncDirectory ( f )
end
break
break
end
end
end
end
@ -227,7 +242,7 @@ function DocSettings:purge()
os.remove ( self.history_file )
os.remove ( self.history_file )
end
end
if lfs.attributes ( self.sidecar , " mode " ) == " directory " then
if lfs.attributes ( self.sidecar , " mode " ) == " directory " then
purgeDir( self.sidecar )
ffiutil. purgeDir( self.sidecar )
end
end
self.data = { }
self.data = { }
end
end