move mount & fs management to fs_helpers for helper to use
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 17 Apr 2025 09:15:30 +0000 (11:15 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 17 Apr 2025 10:00:44 +0000 (12:00 +0200)
Change-Id: Ida6c17d9591809fc89e1972ad11bd87fdd26228e

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

index c79c7b70c89ef21f43d0e59c7e10a290e69eb135..77354350be2beeaa0b6ebb6801abc4f1b78bcd93 100644 (file)
@@ -25,6 +25,7 @@
 #include <dlog.h>
 
 #include "dir_backend_fixed_size.hpp"
+#include "fs_helpers.hpp"
 #include "os_ops.hpp"
 
 #include <sys/mount.h>
@@ -43,78 +44,6 @@ fs::path DirBackendFixedSize::GetImagePathFromSubsessionPath(fs::path subsession
        return subsession_path;
 }
 
-static void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB)
-{
-       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"
-                       , image_path.c_str()
-                       , size_arg.c_str()
-                       , (char *) NULL
-               );
-
-               _exit(1);
-       } else {
-               OS::throw_if_child_failed(child_pid, "mkfs.ext2 failed!");
-       }
-}
-
-static 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
-        * in making it work properly, such as setting up a loop device,
-        * that the mount binary does for us. */
-
-       const auto child_pid = OS::throwing_fork();
-       if (child_pid == 0) {
-               const auto mount_exe = "/usr/bin/mount"sv;
-
-               execl
-                       ( mount_exe.data(), mount_exe.data() /* argv[0] convention */
-                       , "-o", "loop"
-                       , image_path.c_str()
-                       , mount_path.c_str()
-                       , (char *) NULL
-               );
-
-               _exit(1);
-       } else {
-               OS::throw_if_child_failed(child_pid, "mount failed!");
-       }
-}
-
-static void do_umount(const fs::path& path)
-{
-       const auto child_pid = OS::throwing_fork();
-       if (child_pid == 0) {
-               const auto umount_exe = "/usr/bin/umount"sv;
-
-               execl
-                       ( umount_exe.data(), umount_exe.data() /* argv[0] convention */
-                       , path.c_str()
-                       , (char *) NULL
-               );
-
-               _exit(1);
-       } else {
-               OS::throw_if_child_failed(child_pid, "umount failed!");
-       }
-}
-
 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
@@ -145,14 +74,6 @@ fs::path DirBackendAddFixedSize::AddSubsessionPrepare (const fs::path& subsessio
        return tmp_subsession_path;
 }
 
-static void umount_and_remove (const fs::path& path) try {
-       do_umount(path);
-       fs::remove(path);
-} catch (const std::exception& ex) {
-       LOGE("umount & rmdir (%s) failed!", path.c_str());
-       throw;
-}
-
 void DirBackendAddFixedSize::AddSubsessionCleanupFailure (const fs::path& tmpdir_path, const fs::path& subsession_path) const
 {
        auto tmp_image_path = DirBackendFixedSize::GetImagePathFromSubsessionPath(subsession_path);
index 18a83bfca8366a3bdaf2839647fb5fcf81895344..3bb5dde52d8c02cf9df5e0bbc6e1e03e017e5ae7 100644 (file)
@@ -38,6 +38,7 @@
 
 namespace fs = std::filesystem;
 using namespace std::string_literals;
+using namespace std::string_view_literals;
 
 fs::path get_main_dir_by_user_id(const int uid)
 {
@@ -343,3 +344,82 @@ std::vector<std::string> get_user_list(const int session_uid) try
        throw std::runtime_error("Couldn't enumerate user subsessions");
 }
 
+void do_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB)
+{
+       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"
+                       , 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
+        * in making it work properly, such as setting up a loop device,
+        * that the mount binary does for us. */
+
+       const auto child_pid = OS::throwing_fork();
+       if (child_pid == 0) {
+               const auto mount_exe = "/usr/bin/mount"sv;
+
+               execl
+                       ( mount_exe.data(), mount_exe.data() /* argv[0] convention */
+                       , "-o", "loop"
+                       , image_path.c_str()
+                       , mount_path.c_str()
+                       , (char *) NULL
+               );
+
+               _exit(1);
+       } else {
+               OS::throw_if_child_failed(child_pid, "mount failed!");
+       }
+}
+
+void do_umount(const fs::path& path)
+{
+       const auto child_pid = OS::throwing_fork();
+       if (child_pid == 0) {
+               const auto umount_exe = "/usr/bin/umount"sv;
+
+               execl
+                       ( umount_exe.data(), umount_exe.data() /* argv[0] convention */
+                       , path.c_str()
+                       , (char *) NULL
+               );
+
+               _exit(1);
+       } else {
+               OS::throw_if_child_failed(child_pid, "umount failed!");
+       }
+}
+
+void umount_and_remove (const fs::path& path) try {
+       do_umount(path);
+       fs::remove(path);
+} catch (const std::exception& ex) {
+       LOGE("umount & rmdir (%s) failed!", path.c_str());
+       throw;
+}
index a237379d0fc6571e0ccfee563184d49f964e879a..7d2a5d3d8193ee8e3b416c1fc10e6d92ace142f9 100644 (file)
@@ -15,7 +15,12 @@ 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_mkfs(const fs::path& image_path, int uid, int gid, uint64_t size_kB);
+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);
 
 constexpr static std::string_view main_dir_name  = "subsession";
 constexpr static std::string_view system_share_group = "system_share";
 constexpr static std::string_view main_dir_smack = "User::Home";
+