diff --git a/include/appFs.hpp b/include/appFs.hpp index 5b53dce..c9178f8 100644 --- a/include/appFs.hpp +++ b/include/appFs.hpp @@ -29,6 +29,8 @@ class AppFsFuse { * @param rootDirectory A shared pointer to the telemetry root directory. * @param mountPoint The mount point directory path. * @param tryToUnmountOnStart Whether to attempt unmounting the mount point if it's already + * mounted. + * @param createMountPoint Whether to create the mount point directory if it doesn't exist. * * @throws std::runtime_error if rootDirectory is nullptr. * @throws std::runtime_error if setup and mount process fails. @@ -36,7 +38,8 @@ class AppFsFuse { AppFsFuse( std::shared_ptr rootDirectory, const std::string& mountPoint, - bool tryToUnmountOnStart = true); + bool tryToUnmountOnStart = true, + bool createMountPoint = false); /** * @brief Creates a new thread to run the FUSE event loop. diff --git a/src/appFs/CMakeLists.txt b/src/appFs/CMakeLists.txt index cc73b06..cda73c6 100644 --- a/src/appFs/CMakeLists.txt +++ b/src/appFs/CMakeLists.txt @@ -11,7 +11,7 @@ endif() add_library(telemetry::appFs ALIAS appFs) target_compile_definitions(appFs PUBLIC FUSE_USE_VERSION=30 _FILE_OFFSET_BITS=64) -target_link_libraries(appFs PUBLIC telemetry PkgConfig::fuse) +target_link_libraries(appFs PUBLIC telemetry PkgConfig::fuse stdc++fs) target_include_directories(appFs PUBLIC ${CMAKE_SOURCE_DIR}/include) if (TELEMETRY_INSTALL_TARGETS) diff --git a/src/appFs/appFs.cpp b/src/appFs/appFs.cpp index c683f60..858c016 100644 --- a/src/appFs/appFs.cpp +++ b/src/appFs/appFs.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -342,6 +343,19 @@ static void fillFuseArgs(struct fuse_args* fuseArgs) fuse_opt_add_arg(fuseArgs, "attr_timeout=0"); } +static void createDirectories(const std::string& path) +{ + if (std::filesystem::exists(path)) { + return; + } + + std::error_code errorCode; + if (!std::filesystem::create_directories(path, errorCode)) { + throw std::runtime_error( + "Failed to create directory (" + path + "). Error: " + errorCode.message()); + } +} + class FuseArgs { public: FuseArgs() @@ -360,7 +374,8 @@ class FuseArgs { AppFsFuse::AppFsFuse( std::shared_ptr rootDirectory, const std::string& mountPoint, - bool tryToUnmountOnStart) + bool tryToUnmountOnStart, + bool createMountPoint) { m_rootDirectory = std::move(rootDirectory); if (m_rootDirectory == nullptr) { @@ -373,6 +388,10 @@ AppFsFuse::AppFsFuse( struct fuse_operations fuseOps = {}; setFuseOperations(&fuseOps); + if (createMountPoint) { + createDirectories(mountPoint); + } + m_fuse.reset(fuse_new(fuseArgs.get(), &fuseOps, sizeof(fuseOps), (void*) &m_rootDirectory)); if (m_fuse == nullptr) { throw std::runtime_error("fuse_new() has failed.");