Improve boot script using recursion.

This commit is contained in:
Casper V. Kristensen 2019-02-08 03:49:57 +01:00
parent 6fce56aa36
commit b93d229ff5
Signed by: caspervk
GPG key ID: 289CA03790535054

View file

@ -13,43 +13,45 @@ wait until SHIP:loaded and SHIP:unpacked.
wait 0.001. wait 0.001.
if HOMECONNECTION:isconnected { if HOMECONNECTION:isconnected {
update_scripts(). copy_scripts().
} else { } else {
print "No connection to KSC: not updating scripts.". 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. // 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 for item in archive_dir:values {
local existing is LEXICON(). function iteration { // allows for quick-bailing using 'return' (kOS doesn't have the 'continue' keyword
for item in CORE:volume:files:values { local item_path is "/" + path(item):segments:join("/"). // full path of item without volume id
set existing[item:name] to item:size. if item:name[0] = "." // skip Linux hidden files and directories (e.g. .git)
} return.
for item in volume("ARCHIVE"):files:values { // Recurse on directories
copy_file_if_necessary(item). 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
}