Skip to content

Commit

Permalink
Merge pull request #8 from CESNET/directory-creation
Browse files Browse the repository at this point in the history
appFs - add the option to create mountPoint directories
  • Loading branch information
SiskaPavel authored Jun 13, 2024
2 parents 2fff5ad + f94a3bf commit cb7ab15
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion include/appFs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ 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.
*/
AppFsFuse(
std::shared_ptr<Directory> 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.
Expand Down
2 changes: 1 addition & 1 deletion src/appFs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion src/appFs/appFs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <appFs.hpp>

#include <cstring>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <sys/mount.h>
Expand Down Expand Up @@ -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()
Expand All @@ -360,7 +374,8 @@ class FuseArgs {
AppFsFuse::AppFsFuse(
std::shared_ptr<Directory> rootDirectory,
const std::string& mountPoint,
bool tryToUnmountOnStart)
bool tryToUnmountOnStart,
bool createMountPoint)
{
m_rootDirectory = std::move(rootDirectory);
if (m_rootDirectory == nullptr) {
Expand All @@ -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.");
Expand Down

0 comments on commit cb7ab15

Please sign in to comment.