updated build system
This commit is contained in:
308
support/src/utils/file_smb_file.cpp
Normal file
308
support/src/utils/file_smb_file.cpp
Normal file
@ -0,0 +1,308 @@
|
||||
/*
|
||||
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#include "utils/file.hpp"
|
||||
|
||||
#include "utils/error.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
#if defined(PROJECT_ENABLE_LIBDSM)
|
||||
|
||||
namespace repertory::utils::file {
|
||||
void smb_file::close() {
|
||||
if (fd_.has_value()) {
|
||||
smb_fclose(session_.get(), *fd_);
|
||||
fd_.reset();
|
||||
}
|
||||
}
|
||||
|
||||
auto smb_file::exists() const -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (not session_) {
|
||||
throw std::runtime_error("session not found|" + path_);
|
||||
}
|
||||
|
||||
smb_stat_t st{smb_fstat(session_.get(), tid_,
|
||||
smb_create_relative_path(path_).c_str())};
|
||||
if (not st) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return smb_stat_get(st.get(), SMB_STAT_ISDIR) == 0U;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void smb_file::flush() const {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
throw std::runtime_error("failed to flush file|" + path_ +
|
||||
"|not implemented");
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
}
|
||||
|
||||
auto smb_file::move_to(std::string_view new_path) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (utils::string::begins_with(new_path, "//")) {
|
||||
throw std::runtime_error("failed to move file|" + path_ + '|' +
|
||||
std::string{new_path} +
|
||||
"|new path must be in same share");
|
||||
}
|
||||
|
||||
auto from_path = smb_create_relative_path(path_);
|
||||
auto to_path = smb_create_and_validate_relative_path(
|
||||
utils::string::begins_with(new_path, "/") ? smb_get_root_path(path_)
|
||||
: smb_get_parent_path(path_),
|
||||
new_path);
|
||||
|
||||
auto was_open{false};
|
||||
if (fd_.has_value()) {
|
||||
close();
|
||||
was_open = true;
|
||||
}
|
||||
|
||||
auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_);
|
||||
if (res != DSM_SUCCESS) {
|
||||
throw std::runtime_error("failed to connect to share|" + share_name_ +
|
||||
'|' + std::to_string(res));
|
||||
}
|
||||
|
||||
res = smb_file_mv(session_.get(), tid_, from_path.c_str(), to_path.c_str());
|
||||
if (res != DSM_SUCCESS) {
|
||||
throw std::runtime_error("failed to move file|" + path_ + '|' +
|
||||
from_path + '|' + to_path + '|' +
|
||||
std::to_string(res));
|
||||
}
|
||||
|
||||
path_ = smb_create_smb_path(path_, to_path);
|
||||
if (was_open) {
|
||||
return open(read_only_);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto smb_file::open(bool read_only) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (fd_.has_value()) {
|
||||
if (read_only == read_only_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
auto rel_path = smb_create_relative_path(path_);
|
||||
|
||||
auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_);
|
||||
if (res != DSM_SUCCESS) {
|
||||
throw std::runtime_error("failed to connect to share|" + share_name_ +
|
||||
'|' + std::to_string(res));
|
||||
}
|
||||
|
||||
smb_fd fd{};
|
||||
res = smb_fopen(session_.get(), tid_, rel_path.c_str(),
|
||||
read_only ? SMB_MOD_RO : SMB_MOD_RW2, &fd);
|
||||
if (res != DSM_SUCCESS) {
|
||||
throw std::runtime_error("failed to open file|" + path_ + '|' + rel_path +
|
||||
'|' + utils::string::from_bool(read_only) + '|' +
|
||||
std::to_string(res));
|
||||
}
|
||||
|
||||
fd_ = fd;
|
||||
read_only_ = read_only;
|
||||
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto smb_file::read(unsigned char *data, std::size_t to_read,
|
||||
std::uint64_t offset, std::size_t *total_read) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (not fd_.has_value()) {
|
||||
throw std::runtime_error("failed to read file|" + path_ +
|
||||
"|file not open");
|
||||
}
|
||||
|
||||
auto res = smb_fseek(session_.get(), *fd_, static_cast<off_t>(offset),
|
||||
SMB_SEEK_SET);
|
||||
if (res == -1) {
|
||||
throw std::runtime_error("failed to seek file|" + path_ + '|' +
|
||||
std::to_string(offset) + '|' +
|
||||
std::to_string(res));
|
||||
}
|
||||
|
||||
res = smb_fread(session_.get(), *fd_, data, to_read);
|
||||
if (res == -1) {
|
||||
throw std::runtime_error("failed to read file|" + path_ + '|' +
|
||||
std::to_string(to_read) + '|' +
|
||||
std::to_string(res));
|
||||
}
|
||||
|
||||
if (total_read != nullptr) {
|
||||
(*total_read) = static_cast<std::uint64_t>(res);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto smb_file::remove() -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
close();
|
||||
|
||||
auto res = smb_tree_connect(session_.get(), share_name_.c_str(), &tid_);
|
||||
if (res != DSM_SUCCESS) {
|
||||
throw std::runtime_error("failed to connect to share|" + share_name_ +
|
||||
'|' + std::to_string(res));
|
||||
}
|
||||
|
||||
auto rel_path = smb_create_relative_path(path_);
|
||||
res = smb_file_rm(session_.get(), tid_, rel_path.c_str());
|
||||
if (res != DSM_SUCCESS) {
|
||||
throw std::runtime_error(
|
||||
"failed to remove file|" + path_ + '|' + rel_path + '|' +
|
||||
std::to_string(res) + '|' +
|
||||
std::to_string(smb_session_get_nt_status(session_.get())));
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto smb_file::truncate(std::size_t size) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
throw std::runtime_error("failed to truncate file|" + path_ + '|' +
|
||||
std::to_string(size) + "|not implemented");
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
auto smb_file::write(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset, std::size_t *total_written) -> bool {
|
||||
static constexpr const std::string_view function_name{
|
||||
static_cast<const char *>(__FUNCTION__),
|
||||
};
|
||||
|
||||
try {
|
||||
if (not fd_.has_value()) {
|
||||
throw std::runtime_error("failed to write file|" + path_ +
|
||||
"|file not open");
|
||||
}
|
||||
|
||||
auto res = smb_fseek(session_.get(), *fd_, static_cast<off_t>(offset),
|
||||
SMB_SEEK_SET);
|
||||
if (res == -1) {
|
||||
throw std::runtime_error("failed to seek file|" + path_ + '|' +
|
||||
std::to_string(offset) + '|' +
|
||||
std::to_string(res));
|
||||
}
|
||||
|
||||
res = smb_fwrite(session_.get(), *fd_, const_cast<unsigned char *>(data),
|
||||
to_write);
|
||||
if (res == -1) {
|
||||
throw std::runtime_error("failed to write file|" + path_ + '|' +
|
||||
std::to_string(to_write) + '|' +
|
||||
std::to_string(res));
|
||||
}
|
||||
|
||||
if (total_written != nullptr) {
|
||||
(*total_written) = static_cast<std::uint64_t>(res);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
utils::error::handle_exception(function_name, e);
|
||||
} catch (...) {
|
||||
utils::error::handle_exception(function_name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
} // namespace repertory::utils::file
|
||||
|
||||
#endif // defined(PROJECT_ENABLE_LIBDSM)
|
Reference in New Issue
Block a user