wip-add-resize
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 23 Apr 2025 23:43:10 +0000 (01:43 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 23 Apr 2025 23:43:10 +0000 (01:43 +0200)
Change-Id: I2c647a5bf8f61eaa3c9c74a2ddb1a37d0149364e

src/service/src/dir_backend_fixed_size.cpp
src/service/src/fs_helpers.cpp
src/service/src/fs_helpers.hpp

index 77354350be2beeaa0b6ebb6801abc4f1b78bcd93..81674705ac2aaa00da6dbd617fb9f795ae25e517 100644 (file)
@@ -44,6 +44,13 @@ fs::path DirBackendFixedSize::GetImagePathFromSubsessionPath(fs::path subsession
        return subsession_path;
 }
 
+fs::path DirBackendAddFixedSize::AddSubsessionPrepareFromTemplate (const fs::path& subsession_path, const fs::path& main_path, int uid, int gid) const
+{
+       fs::path template_path = main_path / ".template";
+       fs::copy(GetImagePathFromSubsessionPath(subsession_path), GetImagePathFromSubsessionPath(template_path));
+       do_resizefs(GetImagePathFromSubsessionPath(subsession_path);
+}
+
 fs::path DirBackendAddFixedSize::AddSubsessionPrepare (const fs::path& subsession_path, int uid, int gid) const
 {
        /* Work off a temp image first so that the "real" image
index f5167383f5efdb1af8d6d50492977121b28ac15a..1c8503d6f5041363afbd76b2022f0e807db9c2cf 100644 (file)
@@ -146,6 +146,33 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
 
                const int system_share_gid = OS::get_gid_from_name(system_share_group);
 
+               // Work off a temporary dir and then switch it at once so that it appears atomically
+               const auto tmp_subsession_path = backend.AddSubsessionPrepareFromTemplate
+                       ( subsession_path, main_path,
+                       , session_uid, system_share_gid
+               );
+       }
+       catch (std::exception const &ex) {
+               LOGE("Exception while creating user subsession data [session_uid=%d subsession_id=%s]: %s", session_uid, subsession_id.data(), ex.what());
+               throw std::runtime_error("Couldn't add user subsession data");
+       }
+}
+
+void add_user_subsession_internal(const int session_uid, const std::string_view subsession_id, const DirBackendAdd &backend)
+{
+       try {
+               fs::path main_path = get_main_dir_by_user_id(session_uid);
+
+               create_main_subdirectory(session_uid, main_path);
+
+               fs::path subsession_path = main_path / subsession_id;
+
+               if (fs::exists(subsession_path))
+                       throw std::system_error(EEXIST, std::generic_category(),
+                               "Subsession directory already exists");
+
+               const int system_share_gid = OS::get_gid_from_name(system_share_group);
+
                // Work off a temporary dir and then switch it at once so that it appears atomically
                const auto tmp_subsession_path = backend.AddSubsessionPrepare
                        ( subsession_path
@@ -344,6 +371,28 @@ std::vector<std::string> get_user_list(const int session_uid) try
        throw std::runtime_error("Couldn't enumerate user subsessions");
 }
 
+void do_resizefs(const fs::path& image_path, uint64_t size_kB)
+{
+       const auto child_pid = OS::throwing_fork();
+       if (child_pid == 0) {
+               const auto cmd = "/usr/sbin/resize2fs"sv;
+
+               std::string size_arg = std::to_string(size_kB);
+
+               execl
+                       ( cmd.data(), cmd.data() /* argv[0] convention */
+                       , image_path.c_str()
+                       , size_arg.c_str()
+                       , (char *) NULL
+               );
+
+               _exit(1);
+       } else {
+               OS::throw_if_child_failed(child_pid, "resize2fs failed!");
+       }
+}
+
+
 void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB)
 {
        const auto child_pid = OS::throwing_fork();
index ecaaf2335b2474440bb43d59cc4ab4222679dd85..4a50dee8405717109dbbd31cb52cf8d226cc389b 100644 (file)
@@ -15,6 +15,7 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
 void remove_user_subsession(const int session_uid, const std::string_view subsession_id);
 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_resize2fs(const fs::path& image_path, uint64_t size_kB);
 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);