writeToFile: centralize (#11012)

reviewable/pr11026/r1
hius07 7 months ago committed by GitHub
parent d8a48d9e1c
commit c92d94af4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -290,14 +290,6 @@ function DocSettings:open(doc_path)
return new return new
end end
local function writeToFile(data, file)
file:write("-- we can read Lua syntax here!\nreturn ")
file:write(data)
file:write("\n")
ffiutil.fsyncOpenedFile(file) -- force flush to the storage device
file:close()
end
--- Serializes settings and writes them to `metadata.lua`. --- Serializes settings and writes them to `metadata.lua`.
function DocSettings:flush(data, no_custom_metadata) function DocSettings:flush(data, no_custom_metadata)
-- Depending on the settings, doc_settings are saved to the book folder or -- Depending on the settings, doc_settings are saved to the book folder or
@ -321,29 +313,9 @@ function DocSettings:flush(data, no_custom_metadata)
for _, s in ipairs(serials) do for _, s in ipairs(serials) do
local sidecar_dir, sidecar_file = unpack(s) local sidecar_dir, sidecar_file = unpack(s)
util.makePath(sidecar_dir) util.makePath(sidecar_dir)
local directory_updated = false
if lfs.attributes(sidecar_file, "mode") == "file" then
-- As an additional safety measure (to the ffiutil.fsync* calls 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(sidecar_file, "modification")
if mtime < os.time() - 60 then
logger.dbg("DocSettings: Renamed", sidecar_file, "to", sidecar_file .. ".old")
os.rename(sidecar_file, sidecar_file .. ".old")
directory_updated = true -- fsync directory content too below
end
end
logger.dbg("DocSettings: Writing to", sidecar_file) logger.dbg("DocSettings: Writing to", sidecar_file)
local f_out = io.open(sidecar_file, "w") local directory_updated = LuaSettings:backup(sidecar_file)
if f_out ~= nil then if util.writeToFile(s_out, sidecar_file, true, true, directory_updated) then
writeToFile(s_out, f_out)
if directory_updated then
-- Ensure the file renaming is flushed to storage device
ffiutil.fsyncDirectory(sidecar_file)
end
-- move custom cover file and custom metadata file to the metadata file location -- move custom cover file and custom metadata file to the metadata file location
if not no_custom_metadata then if not no_custom_metadata then
local metadata_file, filepath, filename local metadata_file, filepath, filename
@ -614,9 +586,7 @@ function DocSettings:flushCustomMetadata(doc_path)
local s_out = dump(self.data, nil, true) local s_out = dump(self.data, nil, true)
for _, sidecar_dir in ipairs(sidecar_dirs) do for _, sidecar_dir in ipairs(sidecar_dirs) do
util.makePath(sidecar_dir) util.makePath(sidecar_dir)
local f_out = io.open(sidecar_dir .. "/" .. custom_metadata_filename, "w") if util.writeToFile(s_out, sidecar_dir .. "/" .. custom_metadata_filename, true, true) then
if f_out ~= nil then
writeToFile(s_out, f_out)
new_sidecar_dir = sidecar_dir .. "/" new_sidecar_dir = sidecar_dir .. "/"
break break
end end

@ -5,7 +5,6 @@ Subclass of LuaSettings dedicated to handling the legacy global constants.
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local LuaSettings = require("luasettings") local LuaSettings = require("luasettings")
local dump = require("dump") local dump = require("dump")
local ffiutil = require("ffi/util")
local util = require("util") local util = require("util")
local lfs = require("libs/libkoreader-lfs") local lfs = require("libs/libkoreader-lfs")
local logger = require("logger") local logger = require("logger")
@ -153,30 +152,8 @@ function LuaDefaults:reset() end
--- Writes settings to disk. --- Writes settings to disk.
function LuaDefaults:flush() function LuaDefaults:flush()
if not self.file then return end if not self.file then return end
local directory_updated = false local directory_updated = self:backup() -- LuaSettings
if lfs.attributes(self.file, "mode") == "file" then util.writeToFile(dump(self.rw, nil, true), self.file, true, true, directory_updated)
-- As an additional safety measure (to the ffiutil.fsync* calls 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(self.file, "modification")
if mtime < os.time() - 60 then
os.rename(self.file, self.file .. ".old")
directory_updated = true -- fsync directory content too below
end
end
local f_out = io.open(self.file, "w")
if f_out ~= nil then
f_out:write("-- we can read Lua syntax here!\nreturn ")
f_out:write(dump(self.rw, nil, true))
f_out:write("\n")
ffiutil.fsyncOpenedFile(f_out) -- force flush to the storage device
f_out:close()
end
if directory_updated then
-- Ensure the file renaming is flushed to storage device
ffiutil.fsyncDirectory(self.file)
end
return self return self
end end

@ -3,9 +3,9 @@ This module handles generic settings as well as KOReader's global settings syste
]] ]]
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 util = require("util")
local LuaSettings = {} local LuaSettings = {}
@ -249,33 +249,28 @@ function LuaSettings:reset(table)
return self return self
end end
--- Writes settings to disk. function LuaSettings:backup(file)
function LuaSettings:flush() file = file or self.file
if not self.file then return end local directory_updated
local directory_updated = false if lfs.attributes(file, "mode") == "file" then
if lfs.attributes(self.file, "mode") == "file" then -- As an additional safety measure (to the ffiutil.fsync* calls used in util.writeToFile),
-- As an additional safety measure (to the ffiutil.fsync* calls used below),
-- we only backup the file to .old when it has not been modified in the last 60 seconds. -- 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 -- 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. -- that the OS may have itself sync'ed that file content in the meantime.
local mtime = lfs.attributes(self.file, "modification") local mtime = lfs.attributes(file, "modification")
if mtime < os.time() - 60 then if mtime < os.time() - 60 then
os.rename(self.file, self.file .. ".old") os.rename(file, file .. ".old")
directory_updated = true -- fsync directory content too below directory_updated = true -- fsync directory content
end end
end end
local f_out = io.open(self.file, "w") return directory_updated
if f_out ~= nil then end
f_out:write("-- we can read Lua syntax here!\nreturn ")
f_out:write(dump(self.data, nil, true)) --- Writes settings to disk.
f_out:write("\n") function LuaSettings:flush()
ffiutil.fsyncOpenedFile(f_out) -- force flush to the storage device if not self.file then return end
f_out:close() local directory_updated = self:backup()
end util.writeToFile(dump(self.data, nil, true), self.file, true, true, directory_updated)
if directory_updated then
-- Ensure the file renaming is flushed to storage device
ffiutil.fsyncDirectory(self.file)
end
return self return self
end end

@ -94,12 +94,7 @@ function ReadHistory:_flush()
file = v.file file = v.file
}) })
end end
local f = io.open(history_file, "w") util.writeToFile(dump(content), history_file, true, true)
if f then
f:write("return " .. dump(content) .. "\n")
ffiutil.fsyncOpenedFile(f) -- force flush to the storage device
f:close()
end
self:ensureLastFile() self:ensureLastFile()
end end

@ -1034,6 +1034,26 @@ function util.partialMD5(filepath)
return update() return update()
end end
function util.writeToFile(data, filepath, force_flush, lua_dofile_ready, directory_updated)
if not filepath then return end
if lua_dofile_ready then
local t = { "-- ", filepath, "\nreturn ", data, "\n" }
data = table.concat(t)
end
local file, err = io.open(filepath, "wb")
if not file then
return nil, err
end
file:write(data)
if force_flush then
BaseUtil.fsyncOpenedFile(file)
end
file:close()
if directory_updated then
BaseUtil.fsyncDirectory(filepath)
end
return true
end
--[[-- --[[--
Replaces invalid UTF-8 characters with a replacement string. Replaces invalid UTF-8 characters with a replacement string.

Loading…
Cancel
Save