From b1fe5973b7f47a0c5ff64e4900d65417903b2cd0 Mon Sep 17 00:00:00 2001 From: Mateusz Piesta Date: Fri, 9 Dec 2022 10:41:02 +0100 Subject: [PATCH] [MOS-807] Update scripts Added minor tweaks and fixed bugs. --- scripts/lua/entry.lua | 3 -- .../products/BellHybrid/update_product.lua | 2 +- .../lua/products/PurePhone/update_product.lua | 6 ++-- scripts/lua/share/helpers.lua | 16 +++++++++ scripts/lua/share/ltar.lua | 2 ++ scripts/lua/test/test.lua | 14 ++++++++ scripts/lua/update.lua | 16 ++++++++- scripts/lua/update_udm.lua | 33 +++++++++++++++---- 8 files changed, 78 insertions(+), 14 deletions(-) diff --git a/scripts/lua/entry.lua b/scripts/lua/entry.lua index 5c2d824f57c21257f6629891798f8f41eb72b995..5f6b5a01579ed68b5c98b220830b2f3e3b059bf8 100644 --- a/scripts/lua/entry.lua +++ b/scripts/lua/entry.lua @@ -1,7 +1,4 @@ -package.path = package.path .. ";share/?.lua" - local helpers = require('helpers') - local paths = require('paths') local rec = require('recovery') local backup = require('backup') diff --git a/scripts/lua/products/BellHybrid/update_product.lua b/scripts/lua/products/BellHybrid/update_product.lua index 7b9ef2bb8192c4d214fd7fa33b4c9d96ddd5439b..f3ad6b493c06482bc868bf5fcf148db4210e1c38 100644 --- a/scripts/lua/products/BellHybrid/update_product.lua +++ b/scripts/lua/products/BellHybrid/update_product.lua @@ -10,7 +10,7 @@ function update_product.execute() local new_music_dir = recovery.sys.user() .. "/media/app/relaxation" assert(helpers.mkdirp(new_music_dir)) - helpers.copy_dir(old_music_dir, new_music_dir) + helpers.move_dir(old_music_dir, new_music_dir) helpers.rmdir(old_music_dir) end diff --git a/scripts/lua/products/PurePhone/update_product.lua b/scripts/lua/products/PurePhone/update_product.lua index 5d10a7fe369753a20f1e81e2cce4f41f77836536..18cb18df1e399a78dc84a100de716b8e7cf9cb4d 100644 --- a/scripts/lua/products/PurePhone/update_product.lua +++ b/scripts/lua/products/PurePhone/update_product.lua @@ -12,9 +12,11 @@ function update_product.execute() local new_battery_config = paths.target.var_dir .. "/batteryFuelGaugeConfig.cfg" assert(helpers.mkdirp(new_music_dir)) - helpers.copy_dir(old_music_dir, new_music_dir) + helpers.move_dir(old_music_dir, new_music_dir) helpers.rmdir(old_music_dir) - helpers.copy_file(old_battery_config, new_battery_config) + if helpers.exists(old_battery_config) then + helpers.copy_file(old_battery_config, new_battery_config) + end end return update_product diff --git a/scripts/lua/share/helpers.lua b/scripts/lua/share/helpers.lua index 352e5e05d2b0a4870bb539df4ff75fbde26881f1..9725898696d409193eedc6d77eef211fbab7cf5f 100644 --- a/scripts/lua/share/helpers.lua +++ b/scripts/lua/share/helpers.lua @@ -68,6 +68,7 @@ function helpers.copy_file(filename_in, filename_out) local size_in = lfs.attributes(filename_in, "size") local fd_in = assert(io.open(filename_in, "r")) local fd_out = assert(io.open(filename_out, "w")) + collectgarbage() while true do local block = fd_in:read(size) if not block then @@ -141,6 +142,21 @@ function helpers.copy_dir(from, where) end end +--- Move directory recursively +-- @function move_dir +-- @param from source directory +-- @param where target directory +function helpers.move_dir(from, where) + for filename, attr in dirtree(from) do + local name = filename:gsub(from, "") + if attr.mode == "directory" then + assert(lfs.mkdir(build_path(where, name))) + else + assert(os.rename(build_path(from, name),build_path(where, name))) + end + end +end + --- Copy directory recursively using regex filter -- @function copy_dir_filtered -- @param from source directory diff --git a/scripts/lua/share/ltar.lua b/scripts/lua/share/ltar.lua index d9c55e4d869564b9f4dd4835719de18e32bdb3c8..41a5482968a6d30d1334951616e18af1e4857bcb 100644 --- a/scripts/lua/share/ltar.lua +++ b/scripts/lua/share/ltar.lua @@ -60,6 +60,7 @@ end local function write_tarfile_chunks(handle, fd) local size = 1024 * 512 + collectgarbage() while true do local block = fd:read(size) if not block then @@ -73,6 +74,7 @@ local function read_tarfile_chunks(handle, fd, total_size) local block_size = 1024 * 512 local to_read = {}; + collectgarbage() while total_size > 0 do if total_size > block_size then to_read = block_size diff --git a/scripts/lua/test/test.lua b/scripts/lua/test/test.lua index d33ca9f9465513c4244fe3b9672ed17de709cc6a..809b90b5b72f13fbe900d2990839c19165b8b4f5 100644 --- a/scripts/lua/test/test.lua +++ b/scripts/lua/test/test.lua @@ -44,6 +44,16 @@ end function bootctrl.mark_as_bootable(slot) end +function bootctrl.mark_as_unbootable(slot) +end + +function bootctrl.mark_as_successful() +end + +function bootctrl.get_current_slot() + return bootctrl.slot.b +end + function bootctrl.get_next_active() end @@ -59,6 +69,10 @@ end function gui.display_raw_img(width, height, data) end +-- Overwrite lfs.chdir method as we do not want to change working directory during running tests on the host +lfs.chdir = function(dir) +end + sys.user = stub() sys.source_slot = stub() sys.target_slot = stub() diff --git a/scripts/lua/update.lua b/scripts/lua/update.lua index e2d77fea643ebe45e3b4390e43dbe4b0d1532789..904e7e8248fbb50e44f849fb70d377e004911f1e 100644 --- a/scripts/lua/update.lua +++ b/scripts/lua/update.lua @@ -1,4 +1,5 @@ local update = {} +local lfs = require('lfs') local json = require('lunajson') local paths = require('paths') local consts = require('consts') @@ -76,16 +77,29 @@ local function copy_var_directory() end +local function enter() + -- Mark the current slot as successful + recovery.bootctrl.mark_as_successful() + -- Mark the target slot as unbootable + recovery.bootctrl.mark_as_unbootable(recovery.bootctrl.get_next_active()) +end + local function exit() - print("Finishing update process") + print("Finishing update process...") + helpers.rmdir(paths.update_dir) os.remove(paths.update_file) + + -- Update the working directory to the newly updated scripts directory + lfs.chdir(recovery.sys.target_slot() .. "/scripts") + recovery.bootctrl.mark_as_bootable(recovery.bootctrl.get_next_active()) recovery.bootctrl.mark_as_active(recovery.bootctrl.get_next_active()) recovery.sys.set_os_boot_status(false) end function update.execute() + enter() purge_target_slot() copy_update_package() copy_databases() diff --git a/scripts/lua/update_udm.lua b/scripts/lua/update_udm.lua index fac5488850c1b4c16528a319a686adb9d3667f51..1636be3fd9c6a8863f9e5b806576e9bc469a2ca3 100644 --- a/scripts/lua/update_udm.lua +++ b/scripts/lua/update_udm.lua @@ -34,19 +34,19 @@ end local function copy_update_package() local target_dir = recovery.sys.target_slot() - print(string.format("Copying content of the update package '%s to '%s'", paths.update_dir, target_dir)) + print(string.format("Copying content of the update package '%s' to '%s'", paths.update_dir, target_dir)) helpers.copy_dir(paths.update_dir, target_dir) end local function copy_databases() - local from = paths.db_dir + local from = recovery.sys.user() local to = paths.target.db_dir print(string.format("Copying databases from '%s' to '%s'", from, to)) helpers.copy_dir_filtered(from, to, match_db_files) end local function create_directories() - print("Creating 'log', 'crash_dumps' and 'var' directories") + print("Creating 'log', 'crash_dumps' and 'sys' directories") local target_dir = recovery.sys.target_slot() lfs.mkdir(target_dir .. "/log") @@ -66,22 +66,41 @@ local function migrate_db() assert(result == migration.retcode.OK, string.format("Database migration process failed with %d", result)) end +local function enter() + -- Mark the current slot as successful + recovery.bootctrl.mark_as_successful() + -- Mark the target slot as unbootable + recovery.bootctrl.mark_as_unbootable(recovery.bootctrl.get_next_active()) +end + local function exit() print("Finishing update process") helpers.rmdir(paths.update_dir) os.remove(paths.update_file) + + -- Update the working directory to the newly updated scripts directory + lfs.chdir(recovery.sys.target_slot() .. "/scripts") + + -- Mark the old 'MUDITAOS' partition as unbootable&unsuccessful + recovery.bootctrl.mark_as_unbootable(recovery.bootctrl.get_current_slot()) + -- Mark the 'system_b' as bootable and active recovery.bootctrl.mark_as_bootable(recovery.bootctrl.get_next_active()) recovery.bootctrl.mark_as_active(recovery.bootctrl.get_next_active()) recovery.sys.set_os_boot_status(false) end local function repartition_filesystem() - print("Repartinioning 'MUDITAOS' and 'BACKUP' partitions and changing theirs labels") + print("Repartitioning 'MUDITAOS' and 'BACKUP' partitions and changing theirs labels") + + -- Repartitioning filesystem remounts all available disks. During this process, working directory + -- will be set to the default one hence we need to recreate it. + local wdir = lfs.currentdir() recovery.sys.repartition_fs() + lfs.chdir(wdir) end local function user_remove_directories() - print("->Removing backup, crash_dums, data, db, logs, tmp directories") + print("->Removing backup, crash_dumps, data, db, logs, tmp directories") if helpers.exists(recovery.sys.user() .. "/backup") then helpers.rmdir(recovery.sys.user() .. "/backup") end @@ -108,9 +127,8 @@ local function user_remove_files() end local function user_create_directories() - print("->Creating media, temp directories") + print("->Creating media directory") lfs.mkdir(recovery.sys.user() .. "/media") - lfs.mkdir(recovery.sys.user() .. "/temp") end local function user_product_specific() @@ -127,6 +145,7 @@ local function adjust_user_partition_layout() end function update.execute() + enter() repartition_filesystem() purge_target_slot() copy_update_package()