fs-helpers: Refactor some code 07/274707/3
authorAdam Michalski <a.michalski2@partner.samsung.com>
Fri, 29 Apr 2022 17:13:16 +0000 (19:13 +0200)
committerAdam Michalski <a.michalski2@partner.samsung.com>
Fri, 6 May 2022 15:45:54 +0000 (17:45 +0200)
Change-Id: Idbfdcc8fd014075781f7cd34da187d8754763532

sessiond/src/fs_helpers.cpp
sessiond/src/fs_helpers.h

index 41b5530..0fcf0f3 100644 (file)
@@ -35,6 +35,7 @@
 #include "fs_helpers.h"
 
 namespace fs = std::filesystem;
+using namespace std::string_literals;
 
 std::string fs_helpers::get_home_dir_by_user_id(const int uid)
 {
@@ -64,18 +65,39 @@ void fs_helpers::copy_ownership(std::string_view src_path, std::string_view dest
 
        if (ret)
                throw std::system_error(errno, std::system_category(),
-                       std::string("Couldn't stat `")
+                       "Couldn't stat `"s
                        + src_path.data()
                        + "` file/directory");
 
        if (chown(dest_path.data(), info.st_uid, info.st_gid) == -1)
                throw std::system_error(errno, std::system_category(),
-                       std::string("Couldn't set owner/group of the `")
+                       "Couldn't set owner/group of the `"s
                        + dest_path.data()
                        + "` file/directory");
 }
 
-void fs_helpers::copy_smack_attributes(const std::string &src_path, const std::string &dest_path)
+std::string fs_helpers::get_smack_label(std::string_view src_path, smack_label_type type)
+{
+       char *label_raw = nullptr;
+       int ret = smack_getlabel(src_path.data(), &label_raw, type);
+
+       std::unique_ptr<char, decltype([] (char *p) {
+               free(p);
+       })> label { label_raw };
+
+       if (ret)
+               throw std::runtime_error(
+                       "Couldn't get SMACK attributes from source directory: "s +
+                       src_path.data());
+
+       std::string out_str = "";
+       if (label.get())
+               out_str = label.get();
+
+       return out_str;
+}
+
+void fs_helpers::copy_smack_attributes(std::string_view src_path, std::string_view dest_path)
 {
        static const enum smack_label_type label_types[] = {
                SMACK_LABEL_ACCESS,
@@ -83,32 +105,22 @@ void fs_helpers::copy_smack_attributes(const std::string &src_path, const std::s
        };
 
        for (const auto type : label_types) {
-               char *label = nullptr;
-               int ret = smack_getlabel(src_path.c_str(), &label, type);
-               if (ret) {
-                       if (label)
-                               free(label);
-                       throw std::runtime_error("Couldn't get SMACK attributes from source directory: " +
-                               src_path);
-               }
+               auto label = get_smack_label(src_path, type);
+               int ret = 0;
 
                if (type == SMACK_LABEL_TRANSMUTE) {
                        // N.B. Setting TRANSMUTE attribute needs special attention:
                        // the only correct values are: NULL, "", "0" or "1".
-                       if (label != nullptr && strcmp(label, "TRUE") == 0)
-                               ret = smack_setlabel(dest_path.c_str(), "1", type);
+                       if (label == "TRUE")
+                               ret = smack_setlabel(dest_path.data(), "1", type);
                }
                else
-                       ret = smack_setlabel(dest_path.c_str(), label, type);
-
-               if (ret) {
-                       if (label)
-                               free(label);
-                       throw std::runtime_error("Couldn't set SMACK attributes of destination directory: " +
-                               dest_path);
-               }
+                       ret = smack_setlabel(dest_path.data(), label.c_str(), type);
 
-               free(label);
+               if (ret)
+                       throw std::runtime_error(
+                               "Couldn't set SMACK attributes of destination directory: "s +
+                               dest_path.data());
        }
 }
 
@@ -123,9 +135,9 @@ int fs_helpers::get_gid_from_name(std::string_view group_name)
        group pass_grp_buf, *pass_grp_ptr;
        getgrnam_r(main_dir_group.data(), &pass_grp_buf, str_grp_buf.get(), max_grp_buf_size, &pass_grp_ptr);
        if (!pass_grp_ptr)
-               throw std::runtime_error(std::string("Couldn't get Unix gid for `")
+               throw std::runtime_error("Couldn't get Unix gid for `"s
                        + main_dir_group.data()
-                       + std::string("` group"));
+                       + "` group");
        return pass_grp_ptr->gr_gid;
 }
 
@@ -141,13 +153,13 @@ void fs_helpers::create_main_subdirectory(const int session_uid, std::string_vie
 
        if (chown(main_dir.data(), session_uid, gid) == -1)
                throw std::system_error(errno, std::system_category(),
-                       std::string("Couldn't set owner of the `")
+                       "Couldn't set owner of the `"s
                        + main_dir_name.data()
                        + "` subdirectory");
 
        int ret = smack_setlabel(main_dir.data(), main_dir_smack.data(), SMACK_LABEL_ACCESS);
        if (ret)
-               throw std::runtime_error(std::string("Couldn't set SMACK attributes for `")
+               throw std::runtime_error("Couldn't set SMACK attributes for `"s
                        + main_dir_name.data()
                        + "` subdirectory");
 }
@@ -188,7 +200,7 @@ void fs_helpers::add_user_subsession(const int session_uid, const int subsession
 
                        std::string tmp_path = s_path;
                        tmp_path.erase(0, source_dir_len);
-                       std::string d_path = apps_rw_tmp_dir + tmp_path;
+                       std::string d_path = apps_rw_tmp_dir + std::move(tmp_path);
 
                        copy_ownership(s_path, d_path);
                        copy_smack_attributes(s_path, d_path);
index ca2d605..8b61be5 100644 (file)
@@ -1,4 +1,6 @@
 #pragma once
+
+#include <sys/smack.h>
 #include <filesystem>
 #include <string>
 #include <string_view>
@@ -14,7 +16,8 @@ namespace fs_helpers
 
        void create_main_subdirectory(const int session_uid, std::string_view main_dir);
        void copy_ownership(std::string_view src_path, std::string_view dest_path);
-       void copy_smack_attributes(const std::string &src_path, const std::string &dest_path);
+       std::string get_smack_label(std::string_view src_path, smack_label_type type);
+       void copy_smack_attributes(std::string_view src_path, std::string_view dest_path);
        void add_user_subsession(const int session_uid, const int subsession_id);
        void remove_user_subsession(const int session_uid, const int subsession_id);
        std::vector<int> get_user_list(const int session_uid);