Add helper
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 17 Apr 2025 21:04:11 +0000 (23:04 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 17 Apr 2025 21:04:11 +0000 (23:04 +0200)
Change-Id: I76199e60be36bff7c6ddda596914a7ae99f29e5a

src/service/src/fs_helpers.cpp
src/service/src/fs_helpers.hpp
src/tpkplugin/CMakeLists.txt
src/tpkplugin/helper.cpp

index 3bb5dde52d8c02cf9df5e0bbc6e1e03e017e5ae7..f5167383f5efdb1af8d6d50492977121b28ac15a 100644 (file)
@@ -374,6 +374,38 @@ void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB)
        }
 }
 
+void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB, const fs::path& contents_path)
+{
+       const auto child_pid = OS::throwing_fork();
+       if (child_pid == 0) {
+               const auto mkfs = "/usr/sbin/mkfs.ext2"sv;
+
+               /* Would ideally be std::format instead of this ugly stack,
+                * but some important downstream forks use obsolete gcc. */
+               std::string owner_arg = "root_owner=";
+               owner_arg += std::to_string(uid);
+               owner_arg += ":";
+               owner_arg += std::to_string(gid);
+
+               std::string size_arg = std::to_string(size_kB);
+
+               execl
+                       ( mkfs.data(), mkfs.data() /* argv[0] convention */
+                       , "-E", owner_arg.c_str()
+                       , "-m", "0"
+                       , "-d", contents_path.c_str()
+                       , image_path.c_str()
+                       , size_arg.c_str()
+                       , (char *) NULL
+               );
+
+               _exit(1);
+       } else {
+               OS::throw_if_child_failed(child_pid, "mkfs.ext2 failed!");
+       }
+}
+
+
 void do_mount(const fs::path& image_path, const fs::path& mount_path)
 {
        /* Don't just call mount() since there's some extra steps involved
index 7d2a5d3d8193ee8e3b416c1fc10e6d92ace142f9..ecaaf2335b2474440bb43d59cc4ab4222679dd85 100644 (file)
@@ -16,6 +16,7 @@ void remove_user_subsession(const int session_uid, const std::string_view subses
 bool switch_user_subsession(const int session_uid, const std::string_view prev_subsession, const std::string_view next_subsession);
 std::vector<std::string> get_user_list(const int session_uid);
 void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB);
+void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB, const fs::path& contents_path);
 void do_mount(const fs::path& image_path, const fs::path& mount_path);
 void do_umount(const fs::path& path);
 void umount_and_remove (const fs::path& path);
index 6f61505605d74b96cc599062f77f0c8258311a9f..3348a21de50147ed68d598d3a22a65b69a85b36e 100644 (file)
@@ -5,6 +5,7 @@ pkg_check_modules(deps REQUIRED
                   dlog
                   capi-system-info
                   libxml-2.0
+                 libsmack
                   pkgmgr-info
                   )
 
@@ -12,7 +13,9 @@ ADD_LIBRARY(libsessiond-update-skelimg SHARED plugin.c)
 TARGET_COMPILE_OPTIONS(libsessiond-update-skelimg PUBLIC -fPIC ${deps_CFLAGS})
 TARGET_LINK_LIBRARIES(libsessiond-update-skelimg PRIVATE ${deps_LDFLAGS})
 
-ADD_EXECUTABLE(sessiond-update-skelimg helper.cpp)
+ADD_EXECUTABLE(sessiond-update-skelimg helper.cpp ../service/src/os_ops.cpp ../service/src/fs_helpers.cpp ../service/src/dir_backend_fixed_size.cpp ../service/src/dir_backend_regular_dir.cpp)
+INCLUDE_DIRECTORIES(../library/include ../common/include)
+TARGET_COMPILE_FEATURES(sessiond-update-skelimg PUBLIC cxx_std_20)
 TARGET_COMPILE_OPTIONS(sessiond-update-skelimg PUBLIC -fPIC -fPIE ${deps_CFLAGS})
 TARGET_INCLUDE_DIRECTORIES(sessiond-update-skelimg PRIVATE ../service/src)
 TARGET_LINK_LIBRARIES(sessiond-update-skelimg PRIVATE ${deps_LDFLAGS})
index 795c3ce9fcf331c0071ca9fe440a3c265c3b38c1..cfa2dd6e2dc288a8f3a02e8059edbf68adb1f9d6 100644 (file)
@@ -1,6 +1,46 @@
+#include <filesystem>
+#include <iostream>
+
 #include "fs_helpers.hpp"
+#include "os_ops.hpp"
+
+namespace fs = std::filesystem;
+
+using namespace std;
+
+int main(int argc, char **argv) {
+
+       int r = 0;
+
+       try {
+               fs::current_path("/opt/usr/home");
+       } catch (std::exception& ex) {
+               std::cerr << "Unable to chdir to HOME. Can not continue" << std::endl;
+       }
+
+       for (auto const& entry : fs::directory_iterator(".")) {
+               if (!fs::is_directory(entry.status()))
+                       continue;
+
+               const auto &username = entry.path().filename();
+
+               fs::path skel_path = ".etc-skel-";
+               skel_path += username;
+               skel_path += ".img";
+               fs::path skel_tmp_path = skel_path;
+               skel_tmp_path += ".tmp";
+
+               const int username_uid = OS::get_uid_from_name(username.string());
+               const int system_share_gid = OS::get_gid_from_name(system_share_group);
+
+               try {
+                       do_mkfs(skel_tmp_path, username_uid, system_share_gid, 10000, "/etc/skel");
+                       fs::rename(skel_tmp_path, skel_path);
+               } catch (const std::exception& ex) {
+                       std::cerr << "Caught exception " << ex.what() << std::endl;
+                       r = 1;
+               }
+       }
 
-int main(int argc, char **argv)
-{
-       return 0;
+       return r;
 }