diff --git a/install.lua b/install.lua index b735206..268fa3c 100644 --- a/install.lua +++ b/install.lua @@ -64,34 +64,39 @@ local function main() end end - log.info(#toDownload .. " files to download") - for k, v in ipairs(toDownload) do - if not fs.exists("/tmp") then - fs.makeDir("/tmp") + if #toDownload > 0 then + log.info(#toDownload .. " files to download") + for k, v in ipairs(toDownload) do + if not fs.exists("/tmp") then + fs.makeDir("/tmp") + end + log.info("Downloading '" .. v.dest .. "'") + local filerequest = http.get(repoRoot .. v.src) + local tempfile = fs.open("/tmp/" .. v.hash .. ".lua", "w") + tempfile.write(filerequest.readAll()) + filerequest.close() + tempfile.close() + -- Check the validity of the hash, this acts both as a checksum + -- and a way to raise an error if the index is incorrect + tempfile = fs.open("/tmp/" .. v.hash .. ".lua", "r") + local filehash = hash.sha256(tempfile.readAll() .. "\n") -- newline is weird hack because of awkward hashes + tempfile.close() + if filehash ~= v.hash then + log.error("File " .. v.dest .. " (from " .. v.src .. ") has a mismatched hash.") + log.error("Installation aborted. Remember to clean /tmp manually.") + return + end end - log.info("Downloading '" .. v.dest .. "'") - local filerequest = http.get(repoRoot .. v.src) - local tempfile = fs.open("/tmp/" .. v.hash .. ".lua", "w") - tempfile.write(filerequest.readAll()) - filerequest.close() - tempfile.close() - -- Check the validity of the hash, this acts both as a checksum - -- and a way to raise an error if the index is incorrect - tempfile = fs.open("/tmp/" .. v.hash .. ".lua", "r") - local filehash = hash.sha256(tempfile.readAll() .. "\n") -- newline is weird hack because of awkward hashes - tempfile.close() - if filehash ~= v.hash then - log.error("File " .. v.dest .. " (from " .. v.src .. ") has a mismatched hash.") - log.error("Installation aborted.") - return + + log.info("Installing") + for k, v in ipairs(toDownload) do + fs.delete(v.dest) + fs.move("/tmp/" .. v.hash .. ".lua", v.dest) end + else + log.info("Nothing to do") end - - log.info("Installing") - for k, v in ipairs(toDownload) do - fs.move("/tmp/" .. v.hash .. ".lua", v.dest) - end - + log.info("Installation complete!") end