sessiond: Drop fs_helpers namespace 27/280827/1
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Mon, 5 Sep 2022 11:33:54 +0000 (13:33 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Mon, 5 Sep 2022 11:35:54 +0000 (13:35 +0200)
This is small project, probably code could be moved to sessiond namespace evetually.

Currently no other file uses sparate namespace (wait manager, hash, globals)
so dropping it from fs_helpers should make code a bit more consistent.

Change-Id: I5d5786f55e844abaf13e1d73358d0d980611f0fc

src/service/src/fs_helpers.cpp
src/service/src/fs_helpers.hpp
src/service/src/main.cpp

index 9314d7d..69475d2 100644 (file)
@@ -43,7 +43,7 @@
 namespace fs = std::filesystem;
 using namespace std::string_literals;
 
-std::string fs_helpers::get_home_dir_by_user_id(const int uid)
+std::string get_home_dir_by_user_id(const int uid)
 {
        auto max_buf_size = static_cast<ssize_t>(sysconf(_SC_GETPW_R_SIZE_MAX));
        std::runtime_error err("Couldn't get home directory for session_uid=" + std::to_string(uid));
@@ -64,7 +64,7 @@ std::string fs_helpers::get_home_dir_by_user_id(const int uid)
        return std::string(pass_ptr->pw_dir);
 }
 
-void fs_helpers::copy_ownership(std::string_view src_path, std::string_view dest_path)
+void copy_ownership(std::string_view src_path, std::string_view dest_path)
 {
        struct stat info;
        int ret = stat(src_path.data(), &info);
@@ -78,7 +78,7 @@ void fs_helpers::copy_ownership(std::string_view src_path, std::string_view dest
        change_owner_and_group(dest_path, info.st_uid, info.st_gid);
 }
 
-std::string fs_helpers::get_smack_label(std::string_view src_path, smack_label_type type)
+std::string get_smack_label(std::string_view src_path, smack_label_type type)
 {
        char *label_raw = nullptr;
        int ret = smack_lgetlabel(src_path.data(), &label_raw, type);
@@ -99,7 +99,7 @@ std::string fs_helpers::get_smack_label(std::string_view src_path, smack_label_t
        return out_str;
 }
 
-int fs_helpers::copy_label(auto label, auto dest_path, auto type)
+int copy_label(auto label, auto dest_path, auto type)
 {
        if (type != SMACK_LABEL_TRANSMUTE)
                return smack_lsetlabel(dest_path.data(), label.c_str(), type);
@@ -123,7 +123,7 @@ int fs_helpers::copy_label(auto label, auto dest_path, auto type)
        return ret;
 }
 
-void fs_helpers::copy_smack_attributes(std::string_view src_path, std::string_view dest_path)
+void copy_smack_attributes(std::string_view src_path, std::string_view dest_path)
 {
        static const enum smack_label_type label_types[] = {
                SMACK_LABEL_ACCESS,
@@ -140,7 +140,7 @@ void fs_helpers::copy_smack_attributes(std::string_view src_path, std::string_vi
        }
 }
 
-int fs_helpers::get_gid_from_name(std::string_view group_name)
+int get_gid_from_name(std::string_view group_name)
 {
        auto max_grp_buf_size = static_cast<ssize_t>(sysconf(_SC_GETGR_R_SIZE_MAX));
        if (max_grp_buf_size <= 0)
@@ -157,7 +157,7 @@ int fs_helpers::get_gid_from_name(std::string_view group_name)
        return pass_grp_ptr->gr_gid;
 }
 
-void fs_helpers::change_owner_and_group(std::string_view path, const int session_uid, const int group_id)
+void change_owner_and_group(std::string_view path, const int session_uid, const int group_id)
 {
        if (lchown(path.data(), session_uid, group_id) == -1)
                throw std::system_error(errno, std::system_category(),
@@ -167,7 +167,7 @@ void fs_helpers::change_owner_and_group(std::string_view path, const int session
 }
 
 // Create `$HOME/subsession` directory if it doesn't exist
-void fs_helpers::create_main_subdirectory(const int session_uid, std::string_view main_dir)
+void create_main_subdirectory(const int session_uid, std::string_view main_dir)
 {
        if (fs::exists(main_dir))
                return;
@@ -189,8 +189,8 @@ void fs_helpers::create_main_subdirectory(const int session_uid, std::string_vie
                        + "` subdirectory");
 }
 
-bool fs_helpers::subsession_exists(const int session_uid, const std::string_view subsession_id) try {
-       std::string home_dir = fs_helpers::get_home_dir_by_user_id(session_uid);
+bool subsession_exists(const int session_uid, const std::string_view subsession_id) try {
+       std::string home_dir = get_home_dir_by_user_id(session_uid);
        std::string main_dir = std::move(home_dir) + "/" + main_dir_name.data();
        std::string subsession_dir = std::move(main_dir) + "/" + subsession_id.data();
        fs::path subsession_path { subsession_dir };
@@ -201,7 +201,7 @@ catch (std::exception const &ex) {
        return false;
 }
 
-void fs_helpers::add_user_subsession(const int session_uid, const std::string_view subsession_id)
+void add_user_subsession(const int session_uid, const std::string_view subsession_id)
 {
        try {
                std::string home_dir = get_home_dir_by_user_id(session_uid);
@@ -286,7 +286,7 @@ void fs_helpers::add_user_subsession(const int session_uid, const std::string_vi
        }
 }
 
-void fs_helpers::remove_user_subsession(const int session_uid, const std::string_view subsession_id)
+void remove_user_subsession(const int session_uid, const std::string_view subsession_id)
 {
        try {
                std::string home_dir = get_home_dir_by_user_id(session_uid);
@@ -310,7 +310,7 @@ void fs_helpers::remove_user_subsession(const int session_uid, const std::string
        }
 }
 
-fs::path fs_helpers::get_subsession_dir_by_uid(const int session_uid)
+fs::path get_subsession_dir_by_uid(const int session_uid)
 {
        std::string home_dir = get_home_dir_by_user_id(session_uid);
 
@@ -319,7 +319,7 @@ fs::path fs_helpers::get_subsession_dir_by_uid(const int session_uid)
        };
 }
 
-std::vector<std::string> fs_helpers::get_user_list(const int session_uid) try
+std::vector<std::string> get_user_list(const int session_uid) try
 {
        auto const subsession_path = get_subsession_dir_by_uid(session_uid);
        std::vector<std::string> subsessions = { SUBSESSION_INITIAL_SID };
index 4d0afe2..b2a133f 100644 (file)
@@ -8,25 +8,21 @@
 
 namespace fs = std::filesystem;
 
-namespace fs_helpers
-{
-       std::string get_home_dir_by_user_id(const int uid);
-       int get_gid_from_name(std::string_view group_name);
-       fs::path get_subsession_dir_by_uid(const int session_uid);
+std::string get_home_dir_by_user_id(const int uid);
+int get_gid_from_name(std::string_view group_name);
+fs::path get_subsession_dir_by_uid(const int session_uid);
 
-       void create_main_subdirectory(const int session_uid, std::string_view main_dir);
-       void change_owner_and_group(std::string_view path, const int session_uid, const int group_id);
-       void copy_ownership(std::string_view src_path, std::string_view dest_path);
-       std::string get_smack_label(std::string_view src_path, smack_label_type type);
-       int copy_label(auto label, auto dest_path, auto type);
-       void copy_smack_attributes(std::string_view src_path, std::string_view dest_path);
-       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);
-       void remove_user_subsession(const int session_uid, const std::string_view subsession_id);
-       std::vector<std::string> get_user_list(const int session_uid);
-
-       constexpr static std::string_view main_dir_name  = "subsession";
-       constexpr static std::string_view main_dir_group = "system_share";
-       constexpr static std::string_view main_dir_smack = "User::Home";
-}
+void create_main_subdirectory(const int session_uid, std::string_view main_dir);
+void change_owner_and_group(std::string_view path, const int session_uid, const int group_id);
+void copy_ownership(std::string_view src_path, std::string_view dest_path);
+std::string get_smack_label(std::string_view src_path, smack_label_type type);
+int copy_label(auto label, auto dest_path, auto type);
+void copy_smack_attributes(std::string_view src_path, std::string_view dest_path);
+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);
+void remove_user_subsession(const int session_uid, const std::string_view subsession_id);
+std::vector<std::string> get_user_list(const int session_uid);
 
+constexpr static std::string_view main_dir_name  = "subsession";
+constexpr static std::string_view main_dir_group = "system_share";
+constexpr static std::string_view main_dir_smack = "User::Home";
index f6a1915..abafc87 100644 (file)
@@ -175,7 +175,7 @@ struct sessiond_context {
                                vals_to_g_variant(session_uid, subsession_id), &err))
                        g_error_throw(err, "Failed to emit a signal: ");
 
-               fs_helpers::add_user_subsession(session_uid, subsession_id);
+               add_user_subsession(session_uid, subsession_id);
 
                wait_add.try_emplace(session_uid, session_uid, connection, "AddUserCompleted");
                wait_add.at(session_uid).on_start(subsession_id, { });
@@ -204,7 +204,7 @@ struct sessiond_context {
                                vals_to_g_variant(session_uid, subsession_id), &err))
                        g_error_throw(err, "Failed to emit a signal: ");
 
-               fs_helpers::remove_user_subsession(session_uid, subsession_id);
+               remove_user_subsession(session_uid, subsession_id);
 
                wait_remove.try_emplace(session_uid, session_uid, connection, "RemoveUserCompleted");
                wait_remove.at(session_uid).on_start(subsession_id, { });
@@ -228,7 +228,7 @@ struct sessiond_context {
                                get_dbus_error_mapping(SUBSESSION_ERROR_INVALID_PARAMETER), "Incorrect subsession_id passed");
                        return;
                }
-               if (next_subsession_id != SUBSESSION_INITIAL_SID && !fs_helpers::subsession_exists(session_uid, next_subsession_id)) {
+               if (next_subsession_id != SUBSESSION_INITIAL_SID && !subsession_exists(session_uid, next_subsession_id)) {
                        g_dbus_method_invocation_return_dbus_error(invocation,
                                get_dbus_error_mapping(SUBSESSION_ERROR_NOT_AVAILABLE), "Subsession does not exist");
                        return;
@@ -353,7 +353,7 @@ struct sessiond_context {
                        return;
                }
 
-               auto users = fs_helpers::get_user_list(session_uid);
+               auto users = get_user_list(session_uid);
 
                // TODO: It would be cool to be able to use vals_to_g_variant here.
                g_autoptr(GVariantBuilder) builder = g_variant_builder_new(G_VARIANT_TYPE("as"));