Make sessiond more fail-resistant 76/280476/1
authorAdam Michalski <a.michalski2@partner.samsung.com>
Tue, 30 Aug 2022 14:46:47 +0000 (16:46 +0200)
committerAdam Michalski <a.michalski2@partner.samsung.com>
Tue, 30 Aug 2022 14:46:47 +0000 (16:46 +0200)
Change-Id: I5e7c95564960523d5d6ffea4d2d24ed94a82d0dd

sessiond/src/fs_helpers.cpp

index 35946c4..f295550 100644 (file)
@@ -215,6 +215,23 @@ void fs_helpers::add_user_subsession(const int session_uid, const std::string_vi
                std::string source_dir = "/etc/skel/apps_rw";
                auto const source_dir_len = source_dir.length();
 
+               /* N.B. Removing temporary destination directory needs some explanation.
+                * We need to consider the case of partial copying of subsession data
+                * due to failure. In such a case at least some files will remain in the
+                * filesystem in the temporary directory (e.g., `.tmpnewsubsession1`).
+                * The default behaviour of `fs::copy` is to report an error when it
+                * encounters existing files/directories in the destination directory.
+                * This in turn would result in the inability to create a subsession
+                * with a previously chosen name. One might think the simplest remedy
+                * would be to add the `copy_options::overwrite_existing` flag to the
+                * `fs::copy` call below. Unfortunately, it acts weird when there are
+                * symlinks in the destination directory: instead of just replacing them,
+                * it fails with an `Invalid argument` exception. Refer to the
+                * `std::filesystem` documentation for details.
+                */
+               if (fs::exists(apps_rw_path))
+                       fs::remove_all(apps_rw_path);
+
                fs::copy
                        ( fs::path{ source_dir }
                        , apps_rw_path