Make OS helpers use fs::path instead of strings 78/318778/5
authorMichal Bloch <m.bloch@samsung.com>
Wed, 22 Jan 2025 12:16:49 +0000 (13:16 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 4 Feb 2025 13:30:03 +0000 (14:30 +0100)
Change-Id: I06a5912a98033fbaeaaa01acc3ebf66ed3bffb5f
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/service/src/fs_helpers.cpp
src/service/src/os_ops.cpp
src/service/src/os_ops.hpp

index fbd72622408a2bef0ce50dc07d505295524f63ef..a70d8b4f03d40fbe4e9e2b06875a8ff2ce298d90 100644 (file)
@@ -59,7 +59,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, std::string_view main_dir)
+static void create_main_subdirectory(const int session_uid, fs::path main_dir)
 {
        if (fs::exists(main_dir))
                return;
@@ -68,16 +68,16 @@ static void create_main_subdirectory(const int session_uid, std::string_view mai
 
        int gid = OS::get_gid_from_name(system_share_group);
 
-       if (chown(main_dir.data(), session_uid, gid) == -1)
+       if (chown(main_dir.c_str(), session_uid, gid) == -1)
                throw std::system_error(errno, std::system_category(),
                        "Couldn't set owner of the `"s
-                       + main_dir_name.data()
+                       + main_dir.native()
                        + "` subdirectory");
 
-       int ret = smack_setlabel(main_dir.data(), main_dir_smack.data(), SMACK_LABEL_ACCESS);
+       int ret = smack_setlabel(main_dir.c_str(), main_dir_smack.data(), SMACK_LABEL_ACCESS);
        if (ret)
                throw std::runtime_error("Couldn't set SMACK attributes for `"s
-                       + main_dir_name.data()
+                       + main_dir.native()
                        + "` subdirectory");
 }
 
@@ -148,7 +148,7 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
                fs::create_directory(tmp_subsession_dir);
 
                int system_share_gid = OS::get_gid_from_name(system_share_group);
-               OS::change_owner_and_group(tmp_subsession_dir, session_uid, system_share_gid);
+               OS::change_owner_and_group(tmp_subsession_path, session_uid, system_share_gid);
 
                std::string apps_rw_dir = tmp_subsession_dir + "/apps_rw";
                fs::path apps_rw_path { apps_rw_dir };
@@ -203,12 +203,12 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
                                                              | group_read | group_exec;
 
                for (auto const& entry : fs::recursive_directory_iterator(source_dir)) {
-                       std::string s_path = entry.path();
+                       const auto s_path = entry.path();
 
-                       std::string_view tmp_path = s_path;
+                       std::string_view tmp_path = s_path.native();
                        tmp_path.remove_prefix(source_dir_len + 1);
 
-                       std::string d_path = apps_rw_dir + "/" + tmp_path.data();
+                       const auto d_path = apps_rw_path / tmp_path;
 
                        int gid;
                        fs::perms permissions;
@@ -230,9 +230,9 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
                }
 
                // Last but not least - the `apps_rw` directory itself
-               OS::change_owner_and_group(apps_rw_dir, session_uid, system_share_gid);
-               fs::permissions(apps_rw_dir, apps_rw_dir_perms);
-               OS::copy_smack_attributes(source_dir, apps_rw_dir);
+               OS::change_owner_and_group(apps_rw_path, session_uid, system_share_gid);
+               fs::permissions(apps_rw_path, apps_rw_dir_perms);
+               OS::copy_smack_attributes(source_dir, apps_rw_path);
 
                // Copy + rename so that the replacement is atomic
                fs::rename(tmp_subsession_path, subsession_path);
index fa38161e5c4f047b8ac1fac32dcf43af69695895..3b20f0921470ded8c2a719844f7acfc18aa5efab 100644 (file)
@@ -69,19 +69,19 @@ int OS::get_gid_from_name(std::string_view group_name)
        return pass_grp_ptr->gr_gid;
 }
 
-void OS::change_owner_and_group(std::string_view path, const int session_uid, const int group_id)
+void OS::change_owner_and_group(fs::path path, const int session_uid, const int group_id)
 {
-       if (lchown(path.data(), session_uid, group_id) == -1)
+       if (lchown(path.c_str(), session_uid, group_id) == -1)
                throw std::system_error(errno, std::system_category(),
                        "Couldn't set owner/group of the `"s
-                       + path.data()
+                       + path.native()
                        + "` file/directory");
 }
 
-std::string OS::get_smack_label(std::string_view src_path, smack_label_type type)
+std::string OS::get_smack_label(fs::path src_path, smack_label_type type)
 {
        char *label_raw = nullptr;
-       int ret = smack_lgetlabel(src_path.data(), &label_raw, type);
+       int ret = smack_lgetlabel(src_path.c_str(), &label_raw, type);
 
        std::unique_ptr<char, decltype([] (char *p) {
                free(p);
@@ -90,7 +90,7 @@ std::string OS::get_smack_label(std::string_view src_path, smack_label_type type
        if (ret)
                throw std::runtime_error(
                        "Couldn't get SMACK attributes from source directory: "s +
-                       src_path.data());
+                       src_path.native());
 
        std::string out_str = "";
        if (label.get())
@@ -99,21 +99,21 @@ std::string OS::get_smack_label(std::string_view src_path, smack_label_type type
        return out_str;
 }
 
-static int copy_label(std::string label, std::string_view dest_path, smack_label_type type)
+static int copy_label(std::string label, fs::path dest_path, smack_label_type type)
 {
        if (type != SMACK_LABEL_TRANSMUTE)
-               return smack_lsetlabel(dest_path.data(), label.c_str(), type);
+               return smack_lsetlabel(dest_path.c_str(), label.c_str(), type);
 
        /* Setting TRANSMUTE attribute needs special attention:
         * the only correct values are: NULL, "", "0" or "1" */
        if (label == "TRUE")
-               return smack_lsetlabel(dest_path.data(), "1", type);
+               return smack_lsetlabel(dest_path.c_str(), "1", type);
 
        /* N.B. This is a bit tricky. Since TRANSMUTE attribute is inheritable,
         * it is possible that it was set to TRUE while copying the files from
         * /etc/skel, but originally it wasn't there in the source directory for
         * some subdirectories/files. Therefore it must be removed explicitly. */
-       int ret = smack_lsetlabel(dest_path.data(), "0", type);
+       int ret = smack_lsetlabel(dest_path.c_str(), "0", type);
        if (ret == -1 && errno == ENODATA) {
                /* We tried to drop the label, but it already didn't exist,
                 * so the "error" is expected and not a problem. */
@@ -132,7 +132,7 @@ static int copy_label(std::string label, std::string_view dest_path, smack_label
        return ret;
 }
 
-void OS::copy_smack_attributes(std::string_view src_path, std::string_view dest_path)
+void OS::copy_smack_attributes(fs::path src_path, fs::path dest_path)
 {
        static const enum smack_label_type label_types[] = {
                SMACK_LABEL_ACCESS,
@@ -145,11 +145,11 @@ void OS::copy_smack_attributes(std::string_view src_path, std::string_view dest_
                if (copy_label(label, dest_path, type))
                        throw std::runtime_error(
                                "Couldn't set SMACK attributes of destination directory: "s +
-                               dest_path.data());
+                               dest_path.native());
        }
 }
 
-void OS::set_ownership_and_perms(std::string_view src_path, std::string_view dest_path, fs::perms permissions,
+void OS::set_ownership_and_perms(fs::path src_path, fs::path dest_path, fs::perms permissions,
        const int session_uid, const int gid, bool copy_perms_from_skel)
 {
        const auto info = fs::symlink_status (src_path);
index 5358bf7e4df8e910bba280a3273096d99a459e8c..76ca4db483081281837d492b1c9c37d708a16c80 100644 (file)
@@ -6,17 +6,19 @@
 #include <string>
 #include <string_view>
 
+namespace fs = std::filesystem;
+
 namespace OS {
        std::string get_home_dir_by_user_id(const int uid);
 
        int get_gid_from_name(std::string_view group_name);
 
-       void change_owner_and_group(std::string_view path, const int session_uid, const int group_id);
+       void change_owner_and_group(fs::path path, const int session_uid, const int group_id);
 
-       std::string get_smack_label(std::string_view src_path, smack_label_type type);
+       std::string get_smack_label(fs::path src_path, smack_label_type type);
 
-       void copy_smack_attributes(std::string_view src_path, std::string_view dest_path);
+       void copy_smack_attributes(fs::path src_path, fs::path dest_path);
 
-       void set_ownership_and_perms(std::string_view src_path, std::string_view dest_path, std::filesystem::perms permissions,
+       void set_ownership_and_perms(fs::path src_path, fs::path dest_path, fs::perms permissions,
                const int session_uid, const int gid, bool copy_perms_from_skel);
 }