Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appFs - add the option to create mountPoint directories #8

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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