From: Tobias Ulmer Date: Mon, 18 Feb 2019 18:26:03 +0000 (+0100) Subject: e2lib: e2_su_2_2 factor out tool command construction X-Git-Tag: e2factory-2.3.18rc1~35 X-Git-Url: https://git.e2factory.org/?a=commitdiff_plain;h=8070e315662901fc468af4a86badc6fb418103a1;p=e2factory.git e2lib: e2_su_2_2 factor out tool command construction e2lib.get_tool_flags_argv_e2_su_2_2 is now used to return the various possible combinations of e2-su-2.2 calls, ready for arguments to be inserted. Signed-off-by: Tobias Ulmer --- diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 2f8ca18..76cd590 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -2704,37 +2704,66 @@ function e2lib.isfile(path) return false end ---- call the e2-su-2.2 command --- @param argv table: argument vector --- @return bool -function e2lib.e2_su_2_2(argv) - assert(type(argv) == "table") +--- Prepare e2-su-2.2 command with optional sudo. +-- @return Vector containing path to tool binary and its flags if any. +-- False on error. +-- @return Error object on failure. +function e2lib.get_tool_flags_argv_e2_su_2_2() local rc, re + local cmd - rc, re = e2lib.get_global_config() - if not rc then + cmd, re = tools.get_tool_flags_argv("e2-su-2.2") + if not cmd then return false, re end rc, re = tools.enabled("sudo") - if rc then - local cmd + if re then + return false, re + end - cmd, re = tools.get_tool_flags_argv("e2-su-2.2") - if not cmd then + if rc then + local s + s, re = tools.get_tool_flags_argv("sudo") + if not s then return false, re end - for _,arg in ipairs(argv) do - table.insert(cmd, arg) + table.insert(s, "--") + + for _,arg in ipairs(cmd) do + table.insert(s, arg) end + cmd = s + end + return cmd +end + +--- call the e2-su-2.2 command +-- @param argv table: argument vector +-- @return bool True if command returned with exit status 0, false on failure. +-- @return Error object on failure +-- @return Exit status of command (if any) +function e2lib.e2_su_2_2(argv) + assert(type(argv) == "table") + local rc, re, cmd - table.insert(cmd, 1, "--") + cmd, re = e2lib.get_tool_flags_argv_e2_su_2_2() + if not cmd then + return false, re + end - return e2lib.call_tool_argv("sudo", cmd, nil, nil, true) - else - return e2lib.call_tool_argv("e2-su-2.2", argv, nil, nil, true) + for _, arg in ipairs(argv) do + table.insert(cmd, arg) + end + + rc, re = e2lib.callcmd_log(cmd, nil, nil, true) + if not rc then + return false, re + elseif rc ~= 0 then + return false, re, rc end + return true, nil, rc end --- call the tar command