blob: 12495cdee1d6ec33fa81347b8e5aea970ec424ac [file] [log] [blame]
#pragma once
#include <sys/mount.h>
#include <filesystem>
#include <string>
namespace estoraged
{
/** @class FilesystemInterface
* @brief Interface to the filesystem operations that eStoraged needs.
* @details This class is used to mock out the filesystem operations.
*/
class FilesystemInterface
{
public:
virtual ~FilesystemInterface() = default;
FilesystemInterface() = default;
FilesystemInterface(const FilesystemInterface&) = delete;
FilesystemInterface& operator=(const FilesystemInterface&) = delete;
FilesystemInterface(FilesystemInterface&&) = delete;
FilesystemInterface& operator=(FilesystemInterface&&) = delete;
/** @brief Runs the mkfs command to create the filesystem.
* @details Used for mocking purposes.
*
* @param[in] logicalVolume - name of the mapped LUKS device.
*
* @returns 0 on success, nonzero on failure.
*/
virtual int runMkfs(const std::string& logicalVolume) = 0;
/** @brief Wrapper around mount().
* @details Used for mocking purposes.
*
* @param[in] source - device where the filesystem is located.
* @param[in] target - path to where the filesystem should be mounted.
* @param[in] filesystemType - (e.g. "ext4").
* @param[in] mountflags - flags bit mask (see mount() documentation).
* @param[in] data - options for specific filesystem type, can be NULL
* (see mount() documentation).
*
* @returns On success, zero is returned. On error, -1 is returned, and
* errno is set to indicate the error.
*/
virtual int doMount(const char* source, const char* target,
const char* filesystemtype, unsigned long mountflags,
const void* data) = 0;
/** @brief Wrapper around umount().
* @details Used for mocking purposes.
*
* @param[in] target - path location where the filesystem is mounted.
*
* @returns On success, zero is returned. On error, -1 is returned, and
* errno is set to indicate the error.
*/
virtual int doUnmount(const char* target) = 0;
/** @brief Wrapper around std::filesystem::create_directory.
* @details Used for mocking purposes.
*
* @param[in] p - path to directory that should be created.
*
* @returns true on success, false otherwise.
*/
virtual bool createDirectory(const std::filesystem::path& p) = 0;
/** @brief Wrapper around std::filesystem::remove.
* @details Used for mocking purposes.
*
* @param[in] p - path to directory that should be removed.
*
* @returns true on success, false otherwise.
*/
virtual bool removeDirectory(const std::filesystem::path& p) = 0;
/** @brief Wrapper around std::filesystem::is_directory
*
* @param[in] p - path to directory that we want to query.
*
* @returns true if the path exists and represents a directory.
*/
virtual bool directoryExists(const std::filesystem::path& p) = 0;
};
/** @class Filesystem
* @brief Implements FilesystemInterface
*/
class Filesystem : public FilesystemInterface
{
public:
~Filesystem() override = default;
Filesystem() = default;
Filesystem(const Filesystem&) = delete;
Filesystem& operator=(const Filesystem&) = delete;
Filesystem(Filesystem&&) = delete;
Filesystem& operator=(Filesystem&&) = delete;
int runMkfs(const std::string& logicalVolume) override
{
std::string mkfsCommand("mkfs.ext4 /dev/mapper/" + logicalVolume);
// calling 'system' uses a command processor //NOLINTNEXTLINE
return system(mkfsCommand.c_str());
}
int doMount(const char* source, const char* target,
const char* filesystemtype, unsigned long mountflags,
const void* data) override
{
return mount(source, target, filesystemtype, mountflags, data);
}
int doUnmount(const char* target) override
{
return umount(target);
}
bool createDirectory(const std::filesystem::path& p) override
{
return std::filesystem::create_directory(p);
}
bool removeDirectory(const std::filesystem::path& p) override
{
return std::filesystem::remove(p);
}
bool directoryExists(const std::filesystem::path& p) override
{
return std::filesystem::is_directory(std::filesystem::status(p));
}
};
} // namespace estoraged