This commit is contained in:
2024-08-29 14:39:18 -05:00
parent 92c0e4dabd
commit 272d9a1502
4 changed files with 146 additions and 119 deletions

View File

@@ -28,6 +28,84 @@
#include "utils/time.hpp"
namespace repertory::utils::file {
auto get_time(std::string_view path,
time_type type) -> std::optional<std::uint64_t> {
auto times = get_times(path);
if (times.has_value()) {
return times->get(type);
}
return std::nullopt;
}
auto get_time(std::wstring_view path,
time_type type) -> std::optional<std::uint64_t> {
return get_time(utils::string::to_utf8(path), type);
}
auto get_times(std::string_view path) -> std::optional<file_times> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
#if defined(_WIN32)
struct _stat64 st {};
auto res = _stat64(std::string{path}.c_str(), &st);
#else // !defined(_WIN32)
struct stat st {};
auto res = stat(std::string{path}.c_str(), &st);
#endif // defined(_WIN32)
if (res != 0) {
throw std::runtime_error("failed to get file times|" + std::string{path} +
'|' + std::to_string(errno));
}
file_times ret{};
#if defined(_WIN32)
ret.accessed = utils::time::windows_time_t_to_unix_time(st.st_atime);
#else // !defined(_WIN32)
ret.accessed = static_cast<std::uint64_t>(
st.st_atim.tv_nsec + st.st_atim.tv_sec * utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
#if defined(_WIN32)
ret.created = utils::time::windows_time_t_to_unix_time(st.st_ctime);
#else // !defined(_WIN32)
ret.created = static_cast<std::uint64_t>(
st.st_ctim.tv_nsec + st.st_ctim.tv_sec * utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
#if defined(_WIN32)
ret.modified = utils::time::windows_time_t_to_unix_time(st.st_mtime);
#else // !defined(_WIN32)
ret.modified = static_cast<std::uint64_t>(
st.st_mtim.tv_nsec + st.st_mtim.tv_sec * utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
#if defined(_WIN32)
ret.written = utils::time::windows_time_t_to_unix_time(st.st_mtime);
#else // !defined(_WIN32)
ret.written = static_cast<std::uint64_t>(
st.st_mtim.tv_nsec + st.st_mtim.tv_sec * utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return std::nullopt;
}
auto get_times(std::wstring_view path) -> std::optional<file_times> {
return get_times(utils::string::to_utf8(path));
}
auto i_fs_item::get_time(time_type type) const -> std::optional<std::uint64_t> {
return utils::file::get_time(get_path(), type);
}
auto i_file::read_all(data_buffer &data, std::uint64_t offset,
std::size_t *total_read) -> bool {
data_buffer buffer;
@@ -58,66 +136,6 @@ auto i_file::read_all(data_buffer &data, std::uint64_t offset,
return false;
}
auto i_fs_item::get_time(time_types type) const -> std::uint64_t {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
try {
#if defined(_WIN32)
struct _stat64 st {};
_stat64(get_path().c_str(), &st);
#else // !defined(_WIN32)
struct stat st {};
stat(get_path().c_str(), &st);
#endif // defined(_WIN32)
switch (type) {
case time_types::access:
#if defined(_WIN32)
return utils::time::windows_time_t_to_unix_time(st.st_atime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_atim.tv_nsec +
st.st_atim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::creation:
#if defined(_WIN32)
return utils::time::windows_time_t_to_unix_time(st.st_ctime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_ctim.tv_nsec +
st.st_ctim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::modified:
#if defined(_WIN32)
return utils::time::windows_time_t_to_unix_time(st.st_mtime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
st.st_mtim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
case time_types::write:
#if defined(_WIN32)
return utils::time::windows_time_t_to_unix_time(st.st_mtime);
#else // !defined(_WIN32)
return static_cast<std::uint64_t>(st.st_mtim.tv_nsec +
st.st_mtim.tv_sec *
utils::time::NANOS_PER_SECOND);
#endif // defined(_WIN32)
}
} catch (const std::exception &e) {
utils::error::handle_exception(function_name, e);
} catch (...) {
utils::error::handle_exception(function_name);
}
return false;
}
#if defined(PROJECT_ENABLE_JSON)
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
auto read_json_file(std::string_view path, nlohmann::json &data,

View File

@@ -101,7 +101,7 @@ void smb_file::flush() const {
}
auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path,
time_types type) -> std::uint64_t {
time_type type) -> std::optional<std::uint64_t> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
@@ -118,16 +118,16 @@ auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path,
}
switch (type) {
case time_types::access:
case time_type::access:
return smb_stat_get(st.get(), SMB_STAT_ATIME);
case time_types::creation:
case time_type::creation:
return smb_stat_get(st.get(), SMB_STAT_CTIME);
case time_types::modified:
case time_type::modified:
return smb_stat_get(st.get(), SMB_STAT_MTIME);
case time_types::write:
case time_type::write:
return smb_stat_get(st.get(), SMB_STAT_WTIME);
}
} catch (const std::exception &e) {
@@ -136,7 +136,7 @@ auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path,
utils::error::handle_exception(function_name);
}
return 0U;
return std::nullopt;
}
auto smb_file::is_symlink() const -> bool {