More fs::path instead of string parsing 71/318571/7
authorMichal Bloch <m.bloch@samsung.com>
Mon, 3 Feb 2025 18:14:00 +0000 (19:14 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 4 Feb 2025 13:30:04 +0000 (14:30 +0100)
Change-Id: I6d5365f58f5bf588ca209e65644118586c9e37de
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/service/src/fs_helpers.cpp

index a70d8b4f03d40fbe4e9e2b06875a8ff2ce298d90..e51fa4eec533251cbf4434e6999099db363b90ba 100644 (file)
@@ -131,29 +131,27 @@ static Directory_Class get_directory_type(std::string_view path)
 void add_user_subsession(const int session_uid, const std::string_view subsession_id)
 {
        try {
-               std::string main_dir = get_main_dir_by_user_id(session_uid);
+               fs::path main_path = get_main_dir_by_user_id(session_uid);
 
-               create_main_subdirectory(session_uid, main_dir);
+               create_main_subdirectory(session_uid, main_path);
 
-               std::string subsession_dir = main_dir + "/" + subsession_id.data();
-               fs::path subsession_path { subsession_dir };
+               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");
 
-               std::string tmp_subsession_dir = std::move(main_dir) + "/" + ".tmpnew" + subsession_id.data();
-               fs::path tmp_subsession_path { tmp_subsession_dir };
+               const auto tmp_subsession_path = fs::path(subsession_path)
+                       .replace_filename(".tmpnew"s + subsession_path.filename().native());
 
-               fs::create_directory(tmp_subsession_dir);
+               fs::create_directory(tmp_subsession_path);
 
                int system_share_gid = OS::get_gid_from_name(system_share_group);
                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 };
-               std::string source_dir = "/etc/skel/apps_rw";
-               auto const source_dir_len = source_dir.length();
+               const auto apps_rw_path = tmp_subsession_path / "apps_rw";
+               const fs::path source_path = "/etc/skel/apps_rw";
+               auto const source_dir_len = source_path.native().length();
 
                /* N.B. Removing temporary destination directory needs some explanation.
                 * We need to consider the case of partial copying of subsession data
@@ -181,7 +179,7 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
                 * don't need to follow their parent dir's inheritance. Our method
                 * aims to replicate that possibly inconsistent state as-is. */
                fs::copy
-                       ( fs::path{ source_dir }
+                       ( source_path
                        , apps_rw_path
                        , fs::copy_options::recursive
                        | fs::copy_options::copy_symlinks
@@ -202,9 +200,11 @@ void add_user_subsession(const int session_uid, const std::string_view subsessio
                static constexpr fs::perms apps_rw_dir_perms  = owner_all
                                                              | group_read | group_exec;
 
-               for (auto const& entry : fs::recursive_directory_iterator(source_dir)) {
+               for (auto const& entry : fs::recursive_directory_iterator(source_path)) {
                        const auto s_path = entry.path();
 
+                       /* Do string manipulation instead of `fs::relative(source_path, entry)`,
+                        * because relative() resolves symlinks which gives different behaviour. */
                        std::string_view tmp_path = s_path.native();
                        tmp_path.remove_prefix(source_dir_len + 1);
 
@@ -232,7 +232,7 @@ 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_path, session_uid, system_share_gid);
                fs::permissions(apps_rw_path, apps_rw_dir_perms);
-               OS::copy_smack_attributes(source_dir, apps_rw_path);
+               OS::copy_smack_attributes(source_path, apps_rw_path);
 
                // Copy + rename so that the replacement is atomic
                fs::rename(tmp_subsession_path, subsession_path);