Verified Commit dd283bf0 authored by Karel Koci's avatar Karel Koci 🤘

syscnf: fix root_dir path to be absolute

We do change directory and it makes sense to use absolute path either
way so let's go for it.
parent 9ff5d9fa
......@@ -17,7 +17,9 @@ You should have received a copy of the GNU General Public License
along with Updater. If not, see <http://www.gnu.org/licenses/>.
]]--
local os = os
local utils = require "utils"
local getcwd = getcwd
local DIE = DIE
module "syscnf"
......@@ -27,10 +29,26 @@ module "syscnf"
-- Functions that we want to access from outside of this module
-- luacheck: globals set_root_dir set_target
local status_file_suffix = "/usr/lib/opkg/status"
local info_dir_suffix = "/usr/lib/opkg/info/"
local pkg_temp_dir_suffix = "/usr/share/updater/unpacked"
local dir_opkg_collided_suffix = "/usr/share/updater/collided"
local status_file_suffix = "usr/lib/opkg/status"
local info_dir_suffix = "usr/lib/opkg/info/"
local pkg_temp_dir_suffix = "usr/share/updater/unpacked/"
local dir_opkg_collided_suffix = "usr/share/updater/collided/"
--[[
Canonizes path to absolute path. It does no change in case path is already an
absolute but it if not then it prepends current working directory. There is also
special handling in case path starts with tilde (~) in that case that character is
replaced with content of HOME environment variable.
]]
local function path2abspath(path)
if path:match("^/") then
return path
elseif path:match("^~/") then
return os.getenv('HOME') .. "/" .. path
else
return getcwd() .. "/" .. path
end
end
--[[
Set all the configurable directories to be inside the provided dir
......@@ -38,11 +56,14 @@ Effectively sets that the whole system is mounted under some
prefix.
]]
function set_root_dir(dir)
-- A root directory
root_dir = dir or "/"
dir = dir or ""
if dir then
dir = (path2abspath(dir) .. "/"):gsub("/+", "/")
else
dir = "/"
end
-- A root directory
root_dir = dir
-- The file with status of installed packages
status_file = dir .. status_file_suffix
-- The directory where unpacked control files of the packages live
......
......@@ -936,7 +936,7 @@ end
function pkg_config_info(f, configs)
-- Make sure there are no // in there, which would confuse the directory cleaning code
f = f:gsub("/+", "/")
local path = syscnf.root_dir .. f
local path = syscnf.root_dir .. f:gsub("^/+", "")
local hash = configs[f]
return path, hash and config_modified(path, hash)
end
......@@ -1013,6 +1013,7 @@ function script_run(pkg_name, script_name, ...)
s_ecode = ecode
s_stderr = stderr
end, function ()
-- If root is / then variable is empty otherwise absolute path is used
local dir = syscnf.root_dir:gsub('^/+$', '')
setenv("PKG_ROOT", dir)
setenv("IPKG_INSTROOT", dir)
......@@ -1182,7 +1183,7 @@ function run_state_cache:init()
assert(not self.status)
-- TODO: Make it configurable? OpenWRT hardcodes this into the binary, but we may want to be usable on non-OpenWRT systems as well.
local ok, err = pcall(function()
self.lfile = locks.acquire(syscnf.root_dir .. "/var/lock/opkg.lock")
self.lfile = locks.acquire(syscnf.root_dir .. "var/lock/opkg.lock")
self.status = status_parse()
self.initialized = true
end)
......
......@@ -287,7 +287,7 @@ local function perform_internal(operations, journal_status, run_state)
local ok, err = pcall(function ()
-- Make sure the temporary directory for unpacked packages exist
local created = ""
for segment in (syscnf.pkg_temp_dir .. "/"):gmatch("([^/]*)/") do
for segment in syscnf.pkg_temp_dir:gmatch("([^/]*)/") do
created = created .. segment .. "/"
backend.dir_ensure(created)
end
......
......@@ -23,10 +23,11 @@ local SC = require "syscnf"
module("syscnf-tests", package.seeall, lunit.testcase)
function test_set_root_dir()
SC.set_root_dir("/dir")
SC.set_root_dir("/dir/")
assert_equal("/dir/usr/lib/opkg/status", SC.status_file)
assert_equal("/dir/usr/lib/opkg/info/", SC.info_dir)
assert_equal("/dir/usr/share/updater/unpacked", SC.pkg_temp_dir)
assert_equal("/dir/usr/share/updater/unpacked/", SC.pkg_temp_dir)
assert_equal("/dir/usr/share/updater/collided/", SC.dir_opkg_collided)
end
function test_set_target()
......
......@@ -23,6 +23,7 @@ UPD_SYS_TESTS := \
list-dir \
force-downgrade \
conflicts \
postinst \
reboot-recover-update
.PHONY: test-sys valgrind-sys check
......
sed -i -e 's/^Installed-Time: .*/Installed-Time: 0/' "$ROOT_DIR"/usr/lib/opkg/status
rm "$ROOT_DIR"/config
rm -r "$ROOT_DIR"/repo
for file in "$ROOT_DIR"/usr/lib/opkg/info/*.list; do
LC_ALL=C sort -d -o "$file".out "$file"
mv -f "$file".out "$file"
done
Repository("test", "file://%ROOT_DIR%/repo")
Install("test-package")
Package: test-package
Version: 1
License: GPL-2.0
Section: none
Architecture: all
Installed-Size: 0
Description: Simple test package with postinst script
#!/bin/sh
# We should have have root in our root and IPKG_INSTROOT should contain path to it
[ -f "${IPKG_INSTROOT}usr/lib/opkg/status" ] || exit 1
echo "Postinst file" > "${IPKG_INSTROOT}/postinst.txt"
Package: test-package
Version: 1
Status: install user installed
Architecture: all
Installed-Time: 0
file://$ROOT_DIR/config --batch -R "$ROOT_DIR"
sed -i -e "s#%ROOT_DIR%#$ROOT_DIR#" $ROOT_DIR/config
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment