diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-11-08 00:09:44 -0600 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-11-08 00:09:44 -0600 |
commit | 7e8be6bd6f6e487d3444f70f01240c542b5f8b0b (patch) | |
tree | 048b37f9fff624cf6cb374bc0f4843a21ca0992d /hscript | |
parent | 3ab436a3d897b7fc76ed9e1718198bc6f012e286 (diff) | |
download | horizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.tar.gz horizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.tar.bz2 horizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.tar.xz horizon-7e8be6bd6f6e487d3444f70f01240c542b5f8b0b.zip |
hscript: Implement UserIcon::execute, add tests
Diffstat (limited to 'hscript')
-rw-r--r-- | hscript/script_e.cc | 31 | ||||
-rw-r--r-- | hscript/user.cc | 56 |
2 files changed, 85 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 */ } |