From b93d229ff551db9f28fcce7fcd324ff7c1e32f00 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Fri, 8 Feb 2019 03:49:57 +0100 Subject: [PATCH] Improve boot script using recursion. --- boot/default.ks | 56 +++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/boot/default.ks b/boot/default.ks index 359141a..035496d 100644 --- a/boot/default.ks +++ b/boot/default.ks @@ -13,43 +13,45 @@ wait until SHIP:loaded and SHIP:unpacked. wait 0.001. if HOMECONNECTION:isconnected { - update_scripts(). + copy_scripts(). } else { print "No connection to KSC: not updating scripts.". } -print "============= BOOT SEQUENCE COMPLETE =============". +print "=========== BOOT SEQUENCE COMPLETE ============". -function update_scripts { +function copy_scripts { // // Copy all .ks-files from the archive to the CPU's internal storage. // + parameter archive_dir is ARCHIVE:files. - // Get lexicon of existing items (files/directories) and sizes on the internal storage - local existing is LEXICON(). - for item in CORE:volume:files:values { - set existing[item:name] to item:size. - } + for item in archive_dir:values { + function iteration { // allows for quick-bailing using 'return' (kOS doesn't have the 'continue' keyword + local item_path is "/" + path(item):segments:join("/"). // full path of item without volume id + if item:name[0] = "." // skip Linux hidden files and directories (e.g. .git) + return. - for item in volume("ARCHIVE"):files:values { - copy_file_if_necessary(item). + // Recurse on directories + if not item:isfile { + copy_scripts(item:list). + return. + } + + if item:extension <> "ks" // skip non-script items (e.g. README.md) + return. + if CORE:volume:exists(item_path) and item:size = CORE:volume:open(item_path):size // skip if file exists locally with the same size + return. + + print "Copying " + item_path. + copypath(item, path(CORE:volume):combine(item_path)). + if item_path = CORE:bootfilename { + print "Rebooting to apply new bootfile". + wait 2. + reboot. + } + } + iteration(). } } - -function copy_file_if_necessary { - parameter file. - - if item:name[0] = "." // skip linux hidden dirs (e.g. .git) - return. - if item:isfile and item:extension <> "ks" // skip non-script items (e.g. README.md) - return. - if existing:haskey(item:name) and item:size = existing[item:name] // skip if local item is same size - return. - - print "Copying " + item:name. - copypath(path(volume("ARCHIVE")):combine(item:name), CORE:volume). - - if item:name = "boot" - reboot. // reboot CPU if boot/ directory was updated -}