Add sessiond --regenerate-skel 57/324257/2
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 14 May 2025 11:10:23 +0000 (13:10 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 20 May 2025 17:33:17 +0000 (19:33 +0200)
(Re)generates a template copy of skel. This copy can be
quickly turned into a subsession via rename, which will
be added in a later patch.

Co-authored-by: Karol Lewandowski <k.lewandowsk@samsung.com>
Change-Id: I76fa6d4f4c82ab2994fac55c67bf54529eefe447

packaging/sessiond.spec
src/service/CMakeLists.txt
src/service/src/fs_helpers.cpp
src/service/src/fs_helpers.hpp
src/service/src/main.cpp
src/service/src/main_skel.cpp [new file with mode: 0644]
src/service/src/main_skel.hpp [new file with mode: 0644]
tests/integration/CMakeLists.txt
tests/integration/sessiond-integration-test-regenerate-skel.sh [new file with mode: 0755]
tests/integration/sessiond-integration-tests.sh

index 32fe4d0c80f5a5f2e9719389435586fb23e46f2b..c5d90e366d65ae880daf4e561b3771026831cafc 100644 (file)
@@ -107,6 +107,7 @@ popd
 %license LICENSE.MIT
 %{_bindir}/test_*
 %{_bindir}/sessiond-integration-tests.sh
+%{_bindir}/sessiond-integration-test-regenerate-skel.sh
 
 %files -n subsession-client-example
 %manifest sessiond.manifest
index 13ee438037c0c9fb914f61fd4c7a08e025879e08..2575197cdc6593b8e4d0f415b4a94c60cb0f77b4 100644 (file)
@@ -10,6 +10,7 @@ set(
        sessiond_SRCS
        src/main.cpp
        src/main_restore.cpp
+       src/main_skel.cpp
        src/fs_helpers.cpp
        src/os_ops.cpp
        src/plugin.cpp
index c0d04d8737bb1e22a02b7ac464ca410a0c192a7b..13f2aa8424437271e197a527c829cbe1fcf0995b 100644 (file)
@@ -63,7 +63,7 @@ static inline bool should_copy_perms_from_skel(Directory_Class type) {
 }
 
 // Create `$HOME/subsession` directory if it doesn't exist
-static void create_main_subdirectory(const int session_uid, const fs::path& main_dir)
+void create_main_subdirectory(const int session_uid, const fs::path& main_dir)
 {
        if (fs::exists(main_dir))
                return;
index 65a51693f59a7ac8f4266f235b44d58437591849..366d97ac9e2440800ae06a32387e6626f3de2a9e 100644 (file)
@@ -10,6 +10,7 @@
 
 std::filesystem::path get_main_dir_by_user_id(const int uid);
 std::filesystem::path get_last_subsession_path_by_user_id(const int uid);
+void create_main_subdirectory(const int session_uid, const fs::path& main_dir);
 bool subsession_exists(const int session_uid, const std::string_view subsession_id);
 void add_user_subsession(const int session_uid, const std::string_view subsession_id, const DirBackendAdd& backend);
 void add_user_subsession_inner(const std::filesystem::path &main_path, const int session_uid, const std::string_view subsession_id, const DirBackendAdd& backend, bool force);
index 07a3ff92c571780a8f634511bdb05ca444f1154e..4d3c84ed66d9721515c143b9ce0f6918043316be 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "main_context.hpp"
 #include "main_restore.hpp"
+#include "main_skel.hpp"
 #include <stdexcept>
 
 std::unique_ptr <sessiond_context> g_sessiond_context;
@@ -34,9 +35,21 @@ bool isRestoreOnly(int argc, char **argv)
        return std::string_view(argv[1]) == "--restore-only";
 }
 
+
+bool isGenSkel(int argc, char **argv)
+{
+       if (argc < 2)
+               return false;
+
+       return std::string_view(argv[1]) == "--regenerate-skel";
+}
+
+
 int main(int argc, char **argv) try {
        if (isRestoreOnly(argc, argv)) {
                restore_all_user_sessions();
+       } else if (isGenSkel(argc, argv)) {
+               regenerate_skel();
        } else {
                g_sessiond_context = std::make_unique <sessiond_context> ();
                restore_all_user_sessions();
diff --git a/src/service/src/main_skel.cpp b/src/service/src/main_skel.cpp
new file mode 100644 (file)
index 0000000..ecaab84
--- /dev/null
@@ -0,0 +1,65 @@
+/* MIT License
+ *
+ * Copyright (c) 2025 Samsung Electronics Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#include <filesystem>
+#include <iostream>
+
+#undef LOG_TAG
+#define LOG_TAG "SESSIOND_REGENERATE_SKEL"
+#include <dlog.h>
+
+#include "fs_helpers.hpp"
+#include "os_ops.hpp"
+#include "dir_backend_fixed_size.hpp"
+
+namespace fs = std::filesystem;
+
+constexpr auto TEMPLATE_IMAGE_SIZE_KB = 10240;
+
+int regenerate_skel()
+{
+       try {
+               fs::current_path("/opt/usr/home");
+       } catch (const std::exception& ex) {
+               std::cerr << "Can not regenerate skel template, unable to chdir to HOME:" << ex.what() << std::endl;
+               throw;
+       }
+
+       for (auto const& entry : fs::directory_iterator(".")) {
+               if (!fs::is_directory(entry.status()))
+                       continue;
+
+               const auto &username = entry.path().filename();
+               int username_uid = -1;
+               try {
+                       username_uid = OS::get_uid_from_name(username.string());
+                       const auto main_path = get_main_dir_by_user_id(username_uid);
+                       create_main_subdirectory(username_uid, main_path);
+                       add_user_subsession_inner(main_path, username_uid, ".template", DirBackendAddFixedSize {TEMPLATE_IMAGE_SIZE_KB}, true);
+               } catch (const std::exception& ex) {
+                       LOGW("Unable to generate skel template for username %s (uid %d): %s", username.c_str(), username_uid, ex.what());
+                       continue;
+               }
+       }
+
+       return 0;
+}
diff --git a/src/service/src/main_skel.hpp b/src/service/src/main_skel.hpp
new file mode 100644 (file)
index 0000000..c387279
--- /dev/null
@@ -0,0 +1,3 @@
+#pragma once
+
+void regenerate_skel();
index 2f02827433cea6c3156cea4b453a8c45d845baee..d6ef991e2ddcb6df17ebf396c2c498d6f7df78ef 100644 (file)
@@ -1,3 +1,3 @@
-install(FILES sessiond-integration-tests.sh
+install(FILES sessiond-integration-tests.sh sessiond-integration-test-regenerate-skel.sh
        DESTINATION bin
        PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
diff --git a/tests/integration/sessiond-integration-test-regenerate-skel.sh b/tests/integration/sessiond-integration-test-regenerate-skel.sh
new file mode 100755 (executable)
index 0000000..60a8f0f
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/bash
+
+
+echo "Testing \`sessiond --regenerate-skel\` for non-existent user..."
+rm -r /opt/usr/home/emmanuel_goldstein
+mkdir /opt/usr/home/emmanuel_goldstein
+sessiond --regenerate-skel &> /dev/null
+if [ "$?" -ne 0 ]; then
+       echo "Execution failed, bailing out"
+       exit 1
+fi
+# Won't actually create anything inside since it's not a real user, so nothing more to check
+rm -r /opt/usr/home/emmanuel_goldstein
+
+
+echo "Testing \`sessiond --regenerate-skel\` for missing subsessions dir..."
+rm -r /opt/usr/home/owner/subsession
+sessiond --regenerate-skel &> /dev/null
+if [ "$?" -ne 0 ]; then
+       echo "Execution failed, bailing out"
+       exit 1
+fi
+if [ ! -d "/opt/usr/home/owner/subsession" ]; then
+       echo "Failed to create subsession dir (/opt/usr/home/owner/subsession), bailing out"
+       exit 1
+fi
+
+
+exit 0
index 74d4bc6869708b58cc30eea778ecf09ac4f6baa9..6c3797b5638ec4aced1b98b568b190c8c4ee5aec 100755 (executable)
@@ -1,5 +1,9 @@
 #!/bin/bash
 
+sessiond-integration-test-regenerate-skel.sh \
+       && echo "[OK] Skel template regeneration tests passed" \
+       || echo "[FAIL] Skel template regeneration tests failed"
+
 readonly TESTUSER=owner
 readonly TEST_SUBSESSIONS=("TestSuite_sub1" "TestSuite_sub2" "TestSuite_sub3")
 readonly TEST_SUBSESSIONS_FIXED=("TestSuite_sub4" "TestSuite_sub5" "TestSuite_sub6")