From 7e8be6bd6f6e487d3444f70f01240c542b5f8b0b Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Fri, 8 Nov 2019 00:09:44 -0600 Subject: hscript: Implement UserIcon::execute, add tests --- hscript/script_e.cc | 31 ++++++++++++++++++++++++ hscript/user.cc | 56 ++++++++++++++++++++++++++++++++++++++++++-- tests/spec/simulator_spec.rb | 17 ++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/hscript/script_e.cc b/hscript/script_e.cc index 85c43d3..f8c4ff6 100644 --- a/hscript/script_e.cc +++ b/hscript/script_e.cc @@ -25,6 +25,36 @@ namespace Horizon { +static bool icon_dir_created = false; + +void maybe_create_icon_dir(ScriptOptions opts) { + if(icon_dir_created) return; + icon_dir_created = true; + + if(opts.test(Simulate)) { + std::cout << "mkdir -p /target/var/lib/AccountsService/icons" + << std::endl + << "chown root:root /target/var/lib/AccountsService/icons" + << std::endl + << "chmod 775 /target/var/lib/AccountsService/icons" + << std::endl; + return; + } +#ifdef HAS_INSTALL_ENV + else { + error_code ec; + if(!fs::exists("/target/var/lib/AccountsService/icons", ec)) { + fs::create_directories("/target/var/lib/AccountsService/icons", + ec); + if(ec) { + output_error("internal", "couldn't create icon dir", + ec.message()); + } + } + } +#endif /* HAS_INSTALL_ENV */ +} + bool Script::execute() const { bool success; error_code ec; @@ -497,6 +527,7 @@ bool Script::execute() const { } } if(acct.second->icon) { + maybe_create_icon_dir(opts); EXECUTE_OR_FAIL("usericon", acct.second->icon) } } diff --git a/hscript/user.cc b/hscript/user.cc index 4930677..47f7b89 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -18,6 +18,7 @@ #include #include "user.hh" #include "util.hh" +#include "util/filesystem.hh" #include "util/net.hh" #include "util/output.hh" @@ -345,8 +346,59 @@ bool UserIcon::validate(ScriptOptions) const { return true; } -bool UserIcon::execute(ScriptOptions) const { - return false; +bool UserIcon::execute(ScriptOptions opts) const { + const std::string as_path("/target/var/lib/AccountsService/icons/" + + _username); + const std::string face_path("/target/home/" + _username + "/.face"); + + output_info("installfile:" + std::to_string(line), + "usericon: setting avatar for " + _username); + + if(opts.test(Simulate)) { + if(_icon_path[0] == '/') { + std::cout << "cp " << _icon_path << " " << as_path << std::endl; + } else { + std::cout << "curl -LO " << as_path << " " << _icon_path + << std::endl; + } + std::cout << "cp " << as_path << " " << face_path << ".icon" << std::endl; + std::cout << "chown $(hscript-printowner /target/home/" << _username + << ") " << face_path << ".icon" << std::endl; + std::cout << "ln -s .face.icon " << face_path << std::endl; + return true; + } + +#ifdef HAS_INSTALL_ENV + error_code ec; + if(_icon_path[0] == '/') { + fs::copy_file(_icon_path, as_path, ec); + if(ec) { + output_error("installfile:" + std::to_string(line), + "usericon: failed to copy icon", ec.message()); + return false; + } + } else { + if(!download_file(_icon_path, as_path)) { + output_error("installfile:" + std::to_string(line), + "usericon: failed to download icon"); + return false; + } + } + + fs::copy_file(as_path, face_path + ".icon", ec); + if(ec) { + output_error("installfile:" + std::to_string(line), + "usericon: failed to copy icon to home", ec.message()); + return false; + } + + fs::create_symlink(".face.icon", face_path, ec); + if(ec) { + output_warning("installfile:" + std::to_string(line), + "usericon: failed to create legacy symlink"); + } +#endif /* HAS_INSTALL_ENV */ + return true; /* LCOV_EXCL_LINE */ } diff --git a/tests/spec/simulator_spec.rb b/tests/spec/simulator_spec.rb index a208be7..df2b2ae 100644 --- a/tests/spec/simulator_spec.rb +++ b/tests/spec/simulator_spec.rb @@ -344,4 +344,21 @@ printf '%s\\t%s\\t%s\\t%s\\t0\\t0\\n' /dev/gwyn/source /usr/src auto noatime >> expect(last_command_started.stdout).to include('usermod -aG ') end end + context "simulating 'usericon' execution" do + it "creates the dir if needed" do + use_fixture '0098-usericon-basic.installfile' + run_simulate + expect(last_command_started.stdout).to include("mkdir -p /target/var/lib/AccountsService/icons") + end + it "downloads remote icons" do + use_fixture '0102-usericon-protocols.installfile' + run_simulate + expect(last_command_started.stdout).to include("curl -LO /target/var/lib/AccountsService/icons/chris http://www.adelielinux.org/") + end + it "copies the correct icon" do + use_fixture '0098-usericon-basic.installfile' + run_simulate + expect(last_command_started.stdout).to include("cp /usr/share/user-manager/avatars/circles/Cat.png /target/var/lib/AccountsService/icons/awilfox") + end + end end -- cgit v1.2.3-60-g2f50