From e87d00fce2a63adda421f9092cfae362be6d21ed Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Tue, 25 Mar 2025 15:18:32 +0100 Subject: [PATCH] First attempt at writing the installer --- index.json | 7 ++++ install.lua | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ src/startup.lua | 1 + updateindex.nu | 10 ++++++ 4 files changed, 111 insertions(+) create mode 100644 index.json create mode 100644 install.lua create mode 100644 src/startup.lua create mode 100755 updateindex.nu diff --git a/index.json b/index.json new file mode 100644 index 0000000..758572c --- /dev/null +++ b/index.json @@ -0,0 +1,7 @@ +[ + { + "src": "src/startup.lua", + "dest": "/startup.lua", + "hash": "07219cd9561b41ce1f39209958076c471b17855679c968b42767b0122423c782" + } +] \ No newline at end of file diff --git a/install.lua b/install.lua new file mode 100644 index 0000000..34252ad --- /dev/null +++ b/install.lua @@ -0,0 +1,93 @@ +local repoRoot = "https://git.colon-three.com/kodi/snowier/raw/branch/main/" + +local URLs = { + fileIndex = repoRoot .. "index.json", + -- Implementation of sha256 pulled from here + sha2 = "https://raw.githubusercontent.com/Egor-Skriptunoff/pure_lua_SHA/refs/heads/master/sha2.lua" +} + +local log = {} + +--- @param value string +function log.info(value) + print("[I] " .. value) +end + +--- @param value string +function log.error(value) + printError("[E] " .. value) +end + +local function main() + log.info("Starting installer") + + if not fs.exists("sha2.lua") then + log.info("Downloading sha2.lua") + local sharequest = http.get(URLs.sha2) + local shafile = fs.open("sha2.lua", "w") + shafile.write(sharequest.readAll()) + shafile.close() + sharequest.close() + end + local hash = require("sha2") + + log.info("Downloading index file") + local indexrequest = http.get(URLs.fileIndex) + local index = textutils.unserializeJson(request.readAll()) + indexrequest.close() + + if (index == nil) then + log.error("Failed to deserialize index file.") + return + end + + log.info("Checking for updates") + local toDownload = {} + for k, v in ipairs(index) do + -- A file should be redownloaded if it either: + -- - Doesn't exist locally + -- - Exists locally but has a different hash + if not fs.exists(v.dest) then + toDownload[#toDownload+1] = v + else + local destfile = fs.open(v.dest, "r") + local filehash = hash.sha256(destfile.readAll()) + destfile.close() + if filehash ~= v.sha256 then + toDownload[#toDownload+1] = v + end + end + end + + 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()) + tempfile.close() + if filehash ~= v.hash then + log.error("File " .. v.dest .. " (from " .. v.src .. ") has a mismatched hash.") + log.error("Installation aborted.") + return + end + 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 + +main() diff --git a/src/startup.lua b/src/startup.lua new file mode 100644 index 0000000..7df869a --- /dev/null +++ b/src/startup.lua @@ -0,0 +1 @@ +print("Hello, World!") diff --git a/updateindex.nu b/updateindex.nu new file mode 100755 index 0000000..2b643c6 --- /dev/null +++ b/updateindex.nu @@ -0,0 +1,10 @@ +#!/usr/bin/env nix-shell +#!nix-shell -p jq nushell -i nu + +open index.json +| each {|entry| + {src: $entry.src, dest: $entry.dest, + hash: (cat $entry.src | hash sha256)} +} +| collect +| save -f index.json