Fixed-size dir: reuse dirs via rename where possible 59/324259/2
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 14 May 2025 14:06:58 +0000 (16:06 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 20 May 2025 17:33:17 +0000 (19:33 +0200)
Change-Id: I181244f0cc16efcd1a22a1b35b8c0b13e2f6a609

src/service/src/dir_backend_fixed_size.cpp

index dca72bcfdc47cc1df63b6f33ebbb587c807bd856..9bf05a14eebb9f55a81d35215082c95956b14dfc 100644 (file)
@@ -59,13 +59,21 @@ fs::path DirBackendAddFixedSize::AddSubsessionPrepare (const fs::path& subsessio
        try {
                fs::create_directory(tmp_subsession_path);
        } catch (const std::exception& ex) {
+               LOGE("Failed to create temp subsession dir '%s', uid=%d, gid=%d: %s", tmp_subsession_path.c_str(), uid, gid, ex.what());
                fs::remove(image_path);
                throw;
        }
 
        try {
+               /* The mountpoint's properties don't actually matter, since
+                * they are completely replaced with the image filesystem's
+                * root dir's, but this way they are consistent regardless of
+                * whether the image is mounted or not. */
+               OS::change_owner_and_group(tmp_subsession_path, uid, gid);
+
                OS::do_mount(image_path, tmp_subsession_path);
        } catch (const std::exception& ex) {
+               LOGE("Failed to change owner/group, path='%s', uid=%d, gid=%d: %s", tmp_subsession_path.c_str(), uid, gid, ex.what());
                fs::remove(tmp_subsession_path);
                fs::remove(image_path);
                throw;
@@ -93,7 +101,10 @@ void DirBackendAddFixedSize::AddSubsessionCleanupFailure (const fs::path& tmpdir
 
 void DirBackendAddFixedSize::AddSubsessionFinalize (const fs::path& tmpdir_path, const fs::path& subsession_path, int uid, int gid) const
 {
-       umount_and_remove(tmpdir_path);
+       /* TODO: keep mounts! It should be possible to reuse the mount too,
+        * would need to introduce some sort of `mount --move` here */
+       if (OS::is_mountpoint(tmpdir_path))
+               OS::do_umount(tmpdir_path);
 
        const auto image_path = DirBackendFixedSize::GetImagePathFromSubsessionPath(subsession_path);
        auto temp_image_path = image_path;
@@ -110,13 +121,7 @@ void DirBackendAddFixedSize::AddSubsessionFinalize (const fs::path& tmpdir_path,
        /* Order matters - handle .img first and the directory last,
         * since all other logic assumes that the existence of the dir
         * signifies that a valid subsession exists. */
-       fs::create_directory(subsession_path);
-
-       /* The mountpoint's properties don't actually matter, since
-        * they are completely replaced with the image filesystem's
-        * root dir's, but this way they are consistent regardless of
-        * whether the image is mounted or not. */
-       OS::change_owner_and_group(subsession_path, uid, gid);
+       fs::rename(tmpdir_path, subsession_path);
 }
 
 void DirBackendFixedSize::RemoveSubsession (const fs::path& subsession_path) const