Initial subsession dir backend abstraction 77/318777/4
authorMichal Bloch <m.bloch@samsung.com>
Wed, 22 Jan 2025 12:11:19 +0000 (13:11 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 4 Feb 2025 11:53:02 +0000 (12:53 +0100)
Just subsession removal for now.

Change-Id: Id811f5811fb842f581952f149e58de9918209196

src/service/CMakeLists.txt
src/service/src/dir_backend_regular_dir.cpp [new file with mode: 0644]
src/service/src/dir_backend_regular_dir.hpp [new file with mode: 0644]
src/service/src/fs_helpers.cpp

index 3d81ff82736c2c59e24d598e52df65a12c6f7535..3214eb97638a609252af8207e6278dbfa5928220 100644 (file)
@@ -10,6 +10,7 @@ set(
        src/main.cpp
        src/fs_helpers.cpp
        src/os_ops.cpp
+       src/dir_backend_regular_dir.cpp
        src/tuple_g_variant_helpers.hpp
 )
 add_executable(sessiond ${sessiond_SRCS})
diff --git a/src/service/src/dir_backend_regular_dir.cpp b/src/service/src/dir_backend_regular_dir.cpp
new file mode 100644 (file)
index 0000000..4342203
--- /dev/null
@@ -0,0 +1,47 @@
+/* MIT License
+ *
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#include "dir_backend_regular_dir.hpp"
+
+namespace fs = std::filesystem;
+using namespace std::string_literals;
+
+void DirBackendRegularDir::RemoveSubsession (const fs::path& subsession_path)
+{
+       /* We are going to rename the subsession dir before deleting it,
+        * because that makes the removal appear atomic. The temporary
+        * name is different than the one for adding a subsession to avoid
+        * a possible collision with `add_user_subsession` which also uses
+        * a temporary dir. */
+       auto tmp_subsession_path = subsession_path;
+       tmp_subsession_path.replace_filename(".tmpremove"s + subsession_path.filename().native());
+
+       /* Ensure that any possible residue from previously
+        * failed subsession deletion is cleaned up. */
+       if (fs::exists(tmp_subsession_path))
+               fs::remove_all(tmp_subsession_path);
+
+       /* Renaming also removes whatever was under the tmp path, which
+        * could be residue left over from an earlier failed removal attempt. */
+       fs::rename(subsession_path, tmp_subsession_path);
+       fs::remove_all(tmp_subsession_path);
+}
diff --git a/src/service/src/dir_backend_regular_dir.hpp b/src/service/src/dir_backend_regular_dir.hpp
new file mode 100644 (file)
index 0000000..e7a0ad4
--- /dev/null
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <filesystem>
+
+namespace DirBackendRegularDir {
+       void RemoveSubsession (const std::filesystem::path& subsession_path);
+};
index 8a579d11d76ccda8dcf7cc6e33448d027a35d362..fbd72622408a2bef0ce50dc07d505295524f63ef 100644 (file)
@@ -33,6 +33,9 @@
 #include "os_ops.hpp"
 #include "fs_helpers.hpp"
 
+#include "dir_backend_regular_dir.hpp"
+namespace SubsessionDirBackend = DirBackendRegularDir;
+
 namespace fs = std::filesystem;
 using namespace std::string_literals;
 
@@ -254,22 +257,7 @@ void remove_user_subsession(const int session_uid, const std::string_view subses
                        throw std::system_error(ENOENT, std::generic_category(),
                                "Subsession directory does not exist");
 
-               /* We are going to rename the subsession dir before deleting it,
-                * because that makes the removal appear atomic. The temporary
-                * name is different than the one for adding a subsession to avoid
-                * a possible collision with `add_user_subsession` which also uses
-                * a temporary dir. */
-               const auto tmp_subsession_path = fs::path(subsession_path)
-                       .replace_filename(".tmpremove"s + subsession_path.filename().native());
-
-               /* Ensure that any possible residue from previously
-                * failed subsession deletion is cleaned up. */
-               if (fs::exists(tmp_subsession_path))
-                       fs::remove_all(tmp_subsession_path);
-
-               /* Perform the rename-and-delete. */
-               fs::rename(subsession_path, tmp_subsession_path);
-               fs::remove_all(tmp_subsession_path);
+               SubsessionDirBackend::RemoveSubsession(subsession_path);
        }
        catch (std::system_error const &ex) {
                LOGE("Logic exception %s\nwhile removing user subsession data [session_uid=%d subsession_id=%s]", ex.what(), session_uid, subsession_id.data());