diff options
Diffstat (limited to 'virtual-media/src/smb.hpp')
-rw-r--r-- | virtual-media/src/smb.hpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/virtual-media/src/smb.hpp b/virtual-media/src/smb.hpp new file mode 100644 index 0000000..3189770 --- /dev/null +++ b/virtual-media/src/smb.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include "logger.hpp" + +#include <sys/mount.h> + +#include <filesystem> +#include <optional> + +namespace fs = std::filesystem; + +class SmbShare +{ + public: + SmbShare(const fs::path& mountDir) : mountDir(mountDir) + { + } + + bool mount(const fs::path& remote, bool rw) + { + LogMsg(Logger::Debug, "Trying to mount remote : ", remote); + + const std::string params = "nolock,sec=ntlmsspi,seal,vers=3.0"; + const std::string perm = rw ? "rw" : "ro"; + auto options = params + "," + perm; + LogMsg(Logger::Debug, "Mounting with options: ", options); + + auto ec = ::mount(remote.c_str(), mountDir.c_str(), "cifs", 0, + options.c_str()); + if (ec) + { + LogMsg(Logger::Error, "Mount failed with ec = ", ec, + " errno = ", errno); + return false; + } + + return true; + } + + static std::optional<fs::path> createMountDir(const fs::path& name) + { + auto destPath = fs::temp_directory_path() / name; + std::error_code ec; + + if (fs::create_directory(destPath, ec)) + { + return destPath; + } + + LogMsg(Logger::Error, ec, + " : Unable to create mount directory: ", destPath); + return {}; + } + + static void unmount(const fs::path& mountDir) + { + int result; + std::error_code ec; + + result = ::umount(mountDir.string().c_str()); + if (result) + { + LogMsg(Logger::Error, result, " : Unable to unmout directory ", + mountDir); + } + + if (!fs::remove_all(mountDir, ec)) + { + LogMsg(Logger::Error, ec, " : Unable to remove mount directory ", + mountDir); + } + } + + private: + std::string mountDir; +};
\ No newline at end of file |