OC-XPM/src/cache.lua

188 lines
4.8 KiB
Lua

local cache = {}
do
local function load_cache(name)
local cfile = io.open("/etc/xpm/cache/"..name.."/manifest.dat", "r")
local cachemf = blt.deserialize(cfile:read("*a"))
cfile:close()
local cached_files = {}
local mt = setmetatable({}, {__index=function(self, i)
if (cachemf[i]) then
local h = io.open(string.format("/etc/xpm/cache/%s/%x.dat", name, cachemf[i]), "r")
local t = blt.deserialize(h:read("*a"))
h:close()
for k, v in pairs(t) do
rawset(self, k, v)
end
return rawget(self, i)
end
end})
rawset(mt, -1, cachemf)
return mt
end
local function save_cache(name, packages)
fs.makeDirectory("/etc/xpm/cache/"..name)
local manifest = io.open("/etc/xpm/cache/"..name.."/manifest.dat", "w")
local mf = {}
local current_cache = {}
local count = 0
local idx = 0
for k, v in pairs(packages) do
if (k ~= -1) then
mf[k] = count
current_cache[k] = v
idx = idx+1
if (idx > config.cache.clustersize) then
idx = 0
print(string.format("/etc/xpm/cache/%s/%x.dat", name, count))
local cf = io.open(string.format("/etc/xpm/cache/%s/%x.dat", name, count), "w")
cf:write(blt.serialize(current_cache))
cf:close()
current_cache = {}
count = count + 1
for i=1, 11 do
os.sleep(0)
end
end
end
end
local cf = io.open(string.format("/etc/xpm/cache/%s/%x.dat", name, count), "w")
cf:write(blt.serialize(current_cache))
cf:close()
current_cache = {}
count = count + 1
for i=1, 10 do
os.sleep(0)
end
manifest:write(blt.serialize(mf))
manifest:close()
end
local pcache = {}
local lcache = {}
if (fs.exists("/etc/xpm/cache/local")) then
lcache = load_cache("local")
end
if (fs.exists("/etc/xpm/cache/remote")) then
pcache = load_cache("remote")
end
function cache.check_cache(pkg)
return pcache[pkg]
end
function cache.update_cache()
print("Updating cache...")
pcache = {}
fs.makeDirectory("/etc/xpm/cache/remote")
local manifest = io.open("/etc/xpm/cache/remote/manifest.dat", "w")
local mf = {}
local current_cache = {}
local count = 0
local idx = 0
for i=#config.repos, 1, -1 do
print("Hit "..(#config.repos-i)..": "..config.repos[i])
local repo = serialization.unserialize(github_download(config.repos[i], "master/programs.cfg"))
if repo then
for k, v in pairs(repo) do
local e = {repo = config.repos[i], pkginfo=v}
mf[k] = count
current_cache[k] = e
idx = idx+1
if (idx > config.cache.clustersize) then
idx = 0
print(string.format("/etc/xpm/cache/%s/%x.dat", "remote", count))
local cf = io.open(string.format("/etc/xpm/cache/%s/%x.dat", "remote", count), "w")
cf:write(blt.serialize(current_cache))
cf:close()
current_cache = {}
count = count + 1
for i=1, 11 do
os.sleep(0)
end
end
end
else
print("Error "..(#config.repos-i)..": Not found.")
end
end
local cf = io.open(string.format("/etc/xpm/cache/%s/%x.dat", "remote", count), "w")
cf:write(blt.serialize(current_cache))
cf:close()
current_cache = {}
count = count + 1
for i=1, 10 do
os.sleep(0)
end
manifest:write(blt.serialize(mf))
manifest:close()
--[[local h = io.open("/etc/xpm/cache/remote.dat", "w")
print("Serializing...")
local ser = blt.serialize(pcache)
print(#ser)
os.sleep(0)
print("Compressing...")
local comp = lzss.compress(ser)
print(#comp, "=", #ser/#comp)
h:write(comp)
h:close()]]
--save_cache("remote", pcache)
print("Saved cache.")
end
function cache.get_upgradable()
if not fs.exists("/etc/xpm/cache/local") then
--cache.update_cache()
--local h = io.open("/etc/xpm/cache/local.dat", "w")
--h:write(lzss.compress(blt.serialize({xpm = pcache.xpm})))
save_cache("local", {xpm = pcache.xpm})
lcache = {xpm = pcache.xpm}
--h:close()
end
local upgradable = {}
for k, v in pairs(lcache) do
if (k ~= -1) then
local lpkg, rpkg = v.pkginfo, pcache[k].pkginfo
if lpkg.xpm and rpkg.xpm and lpkg.xpm.version ~= rpkg.xpm.version then
upgradable[#upgradable+1] = k
end
end
end
return upgradable
end
function cache.fullload(cdat, name)
for k, v in pairs(cdat[-1]) do
if (not cdat[k]) then
local h = io.open(string.format("/etc/xpm/cache/%s/%x.dat", name, v), "r")
local t = blt.deserialize(h:read("*a"))
h:close()
for l, b in pairs(t) do
rawset(cdat, l, b)
end
end
end
end
function cache.update_local_cache(pkg, info)
lcache[pkg] = info
--local h = io.open("/etc/xpm/cache/local.dat", "w")
--h:write(lzss.compress(blt.serialize(lcache)))
--h:close()
local mf = lcache[-1]
if not mf.loaded then
cache.fullload(lcache, "local")
mf.loaded = true
end
lcache[-1] = nil
save_cache("local", lcache)
lcache[-1] = mf
end
function cache.getlocal()
return lcache
end
function cache.getremote()
return pcache
end
end