Skip to content

Commit

Permalink
add update-myself.lua
Browse files Browse the repository at this point in the history
Signed-off-by: Bao Haojun <[email protected]>
  • Loading branch information
baohaojun committed Nov 26, 2014
1 parent 76402c8 commit 4e14399
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions release/update-myself.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/lua

local my_error = error
error = function(msg, level)
print(msg)
print("Update failed! Press Enter to exit... ")
io.stdin:read("*l")
my_error(msg, level)
end

local t1wrench, system, debug
do
t1wrench = require"t1wrench"
system = t1wrench.system
debug = t1wrench.debug
end

local md5file = io.open("t1wrench.md5")
if not md5file then
error("Can't open local md5 file")
end

local md5s_here = {}
local urls_here = {}
for line in md5file:lines() do
local file, url, md5 = line:match("^(%S+)%s+(%S+)%s+(%S+)%s*$")
md5s_here[file] = md5
urls_here[file] = url
end
md5file:close()

local mv, rm = "mv", "rm"
if urls_here.myself:match("-windows/") then
mv, rm = "move", "del"
end


local _s = system{"./download", urls_here.myself, "t1wrench.md5.up"}

local md5s_remote = {}
local urls_remote = {}
local remote_files_md5 = ""

local remote_md5file = io.open("t1wrench.md5.up", "rb")
if not remote_md5file then
error("Can't open t1wrench.md5.up, download failed?")
end

local md5lib = require"md5"

for line in remote_md5file:lines() do
local file, url, md5 = line:match("^(%S+)%s+(%S+)%s+(%S+)%s*$")
if file ~= "myself" then
remote_files_md5 = remote_files_md5 .. line .. "\n";
end
md5s_remote[file] = md5
urls_remote[file] = url
end

remote_md5file:close()

if md5lib.sumhexa(remote_files_md5) ~= md5s_remote["myself"] then
error ("Invalid t1wrench.md5 md5, download invalid data? " .. md5lib.sumhexa(remote_files_md5) .. " : " .. md5s_remote["myself"])
end

for file, md5 in pairs(md5s_remote) do
if file == "myself" then
goto continue
end

if true then
local stream = io.open(file, "rb")
if stream then
local data = stream:read("*a")
stream:close()
if md5lib.sumhexa(data) == md5s_remote[file] then
debug("file %s already updated, continue", file)
goto continue
end
end

system{"./download", urls_remote[file], file .. ".up"}

local stream_up = io.open(file .. ".up", "rb")
if not stream_up then
error("can't open " .. file .. ", download failed?")
end

local data = stream_up:read("*a")
stream_up:close()

local stream, _, errno = io.open(file, "wb")
if not stream then
error("Can't open '" .. file .. "': " .. _)
end


if md5lib.sumhexa(data) ~= md5s_remote[file] and file ~= "myself" then
error("md5 mismatch for " .. file .. ", download failed?")
end

stream:write(data)
stream:close()
system{rm, file .. ".up"}
end
::continue::
end

system{rm, "t1wrench.md5"}
system{mv, "t1wrench.md5.up", "t1wrench.md5"}
print("Update OK!, press Enter to exit... ")
io.stdin:read("*l")

0 comments on commit 4e14399

Please sign in to comment.