From e41fbb67cd830841ec9e8eb6337a5715e8caf861 Mon Sep 17 00:00:00 2001 From: Pavel Siska Date: Mon, 15 Jul 2024 10:51:12 +0200 Subject: [PATCH] appFs - umount mountPoint before directory status checking to prevent access failure If tryToUnmountOnStart is true, code now attempts to unmount the specified mount point. This is necessary because if the application terminates unexpectedly, the filesystem might remain mounted, which can prevent proper status checking and cause subsequent attempts to mount it to fail. By forcing an unmount here, we ensure the mount point is in a clean state before proceeding. If the unmount fails, an exception is thrown to indicate that the cleanup process was unsuccessful. --- src/appFs/appFs.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/appFs/appFs.cpp b/src/appFs/appFs.cpp index 858c016..f12c316 100644 --- a/src/appFs/appFs.cpp +++ b/src/appFs/appFs.cpp @@ -388,6 +388,22 @@ AppFsFuse::AppFsFuse( struct fuse_operations fuseOps = {}; setFuseOperations(&fuseOps); + /** + * If tryToUnmountOnStart is true, this code attempts to unmount the specified mount point. + * This is necessary because if the application terminates unexpectedly, the filesystem + * might remain mounted, which can prevent proper status checking and cause subsequent + * attempts to mount it to fail. By forcing an unmount here, we ensure the mount point is + * in a clean state before proceeding. If the unmount fails, an exception is thrown to + * indicate that the cleanup process was unsuccessful. + */ + if (tryToUnmountOnStart) { + int ret = umount2(mountPoint.c_str(), MNT_FORCE | UMOUNT_NOFOLLOW); + if (ret < 0 && errno != ENOENT) { // ENOENT means that No such directory exists + throw std::runtime_error( + "umount of " + mountPoint + " has failed. Error: " + std::to_string(errno)); + } + } + if (createMountPoint) { createDirectories(mountPoint); } @@ -398,18 +414,6 @@ AppFsFuse::AppFsFuse( } int ret = fuse_mount(m_fuse.get(), mountPoint.c_str()); - if (ret < 0 && tryToUnmountOnStart) { - ret = umount2(mountPoint.c_str(), MNT_FORCE | UMOUNT_NOFOLLOW); - if (ret < 0) { - throw std::runtime_error("umount of " + mountPoint + " has failed."); - } - - ret = fuse_mount(m_fuse.get(), mountPoint.c_str()); - if (ret < 0) { - throw std::runtime_error("fuse_mount() has failed again."); - } - } - if (ret < 0) { throw std::runtime_error("fuse_mount() has failed."); }