summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2019-11-08 00:09:44 -0600
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2019-11-08 00:09:44 -0600
commit7e8be6bd6f6e487d3444f70f01240c542b5f8b0b (patch)
tree048b37f9fff624cf6cb374bc0f4843a21ca0992d
parent3ab436a3d897b7fc76ed9e1718198bc6f012e286 (diff)
downloadhorizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.tar.gz
horizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.tar.bz2
horizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.tar.xz
horizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.zip
hscript: Implement UserIcon::execute, add tests
-rw-r--r--hscript/script_e.cc31
-rw-r--r--hscript/user.cc56
-rw-r--r--tests/spec/simulator_spec.rb17
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 <time.h>
#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