This commit is contained in:
Scott E. Graves 2024-10-25 11:24:47 -05:00
parent a77fd75687
commit f9af43309d
4 changed files with 78 additions and 42 deletions

View File

@ -36,7 +36,9 @@ auto eviction::check_minimum_requirements(const std::string &file_path)
-> bool { -> bool {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
auto opt_size = utils::file::file{file_path}.size(); auto check_file = utils::file::file{file_path};
auto opt_size = check_file.size();
if (not opt_size.has_value()) { if (not opt_size.has_value()) {
utils::error::raise_error(function_name, utils::get_last_error_code(), utils::error::raise_error(function_name, utils::get_last_error_code(),
file_path, "failed to get file size"); file_path, "failed to get file size");
@ -45,20 +47,34 @@ auto eviction::check_minimum_requirements(const std::string &file_path)
auto file_size{opt_size.value()}; auto file_size{opt_size.value()};
if (file_size == 0U) { if (file_size == 0U) {
event_system::instance().raise<debug_log>(std::string{function_name},
file_path, "no file size");
return false; return false;
} }
auto reference_time = utils::file::file{file_path}.get_time( auto reference_time =
config_.get_eviction_uses_accessed_time() check_file.get_time(config_.get_eviction_uses_accessed_time()
? utils::file::time_type::accessed ? utils::file::time_type::accessed
: utils::file::time_type::modified); : utils::file::time_type::modified);
if (not reference_time.has_value()) { if (not reference_time.has_value()) {
event_system::instance().raise<debug_log>(std::string{function_name},
file_path, "no reference_time");
return false; return false;
} }
event_system::instance().raise<debug_log>(
std::string{function_name}, file_path,
std::to_string(reference_time.value()));
auto delay = (config_.get_eviction_delay_mins() * 60UL) * auto delay = (config_.get_eviction_delay_mins() * 60UL) *
utils::time::NANOS_PER_SECOND; utils::time::NANOS_PER_SECOND;
event_system::instance().raise<debug_log>(
std::string{function_name}, file_path,
std::to_string(reference_time.value() +
static_cast<std::uint64_t>(delay)));
return ((reference_time.value() + static_cast<std::uint64_t>(delay)) <= return ((reference_time.value() + static_cast<std::uint64_t>(delay)) <=
utils::time::get_time_now()); utils::time::get_time_now());
} }
@ -92,18 +108,29 @@ void eviction::service_function() {
while (not get_stop_requested() && should_evict && while (not get_stop_requested() && should_evict &&
not cached_files_list.empty()) { not cached_files_list.empty()) {
try { try {
event_system::instance().raise<debug_log>(std::string{function_name},
cached_files_list.front(),
"analyzing0");
std::string api_path; std::string api_path;
if (provider_.get_api_path_from_source( if (provider_.get_api_path_from_source(
cached_files_list.front(), api_path) == api_error::success) { cached_files_list.front(), api_path) == api_error::success) {
event_system::instance().raise<debug_log>(std::string{function_name},
api_path, "analyzing1");
api_file file{}; api_file file{};
filesystem_item fsi{}; filesystem_item fsi{};
if (provider_.get_filesystem_item_and_file(api_path, file, fsi) == if (provider_.get_filesystem_item_and_file(api_path, file, fsi) ==
api_error::success) { api_error::success) {
event_system::instance().raise<debug_log>(
std::string{function_name}, api_path, "analyzing2");
// Only evict files that match expected size // Only evict files that match expected size
auto opt_size = utils::file::file{cached_files_list.front()}.size(); auto opt_size = utils::file::file{cached_files_list.front()}.size();
if (opt_size.has_value()) { if (opt_size.has_value()) {
event_system::instance().raise<debug_log>(
std::string{function_name}, api_path, "analyzing3");
auto file_size{opt_size.value()}; auto file_size{opt_size.value()};
if (file_size == fsi.size) { if (file_size == fsi.size) {
event_system::instance().raise<debug_log>(
std::string{function_name}, api_path, "analyzing4");
// Try to evict file // Try to evict file
if (fm_.evict_file(fsi.api_path) && if (fm_.evict_file(fsi.api_path) &&
config_.get_enable_max_cache_size()) { config_.get_enable_max_cache_size()) {

View File

@ -191,35 +191,43 @@ auto file_manager::evict_file(const std::string &api_path) -> bool {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
if (provider_.is_read_only()) { if (provider_.is_read_only()) {
event_system::instance().raise<debug_log>(std::string{function_name},
api_path, "read only");
return false; return false;
} }
recur_mutex_lock open_lock(open_file_mtx_); recur_mutex_lock open_lock(open_file_mtx_);
if (is_processing(api_path)) { if (is_processing(api_path)) {
event_system::instance().raise<debug_log>(std::string{function_name},
api_path, "processing");
return false; return false;
} }
if (get_open_file_count(api_path) != 0U) { if (get_open_file_count(api_path) != 0U) {
event_system::instance().raise<debug_log>(std::string{function_name},
api_path, "open count");
return false; return false;
} }
std::string pinned; std::string pinned;
auto res = provider_.get_item_meta(api_path, META_PINNED, pinned); auto res = provider_.get_item_meta(api_path, META_PINNED, pinned);
if (res != api_error::success && res != api_error::item_not_found) { if (res != api_error::success && res != api_error::item_not_found) {
utils::error::raise_api_path_error(function_name, api_path, res, utils::error::raise_api_path_error(std::string{function_name}, api_path,
"failed to get pinned status"); res, "failed to get pinned status");
return false; return false;
} }
if (not pinned.empty() && utils::string::to_bool(pinned)) { if (not pinned.empty() && utils::string::to_bool(pinned)) {
event_system::instance().raise<debug_log>(std::string{function_name},
api_path, "pinned");
return false; return false;
} }
std::string source_path{}; std::string source_path{};
res = provider_.get_item_meta(api_path, META_SOURCE, source_path); res = provider_.get_item_meta(api_path, META_SOURCE, source_path);
if (res != api_error::success) { if (res != api_error::success) {
utils::error::raise_api_path_error(function_name, api_path, res, utils::error::raise_api_path_error(std::string{function_name}, api_path,
"failed to get source path"); res, "failed to get source path");
return false; return false;
} }
if (source_path.empty()) { if (source_path.empty()) {

View File

@ -38,7 +38,7 @@ auto get_directory_files(std::string_view path, bool oldest_first,
#if defined(_WIN32) #if defined(_WIN32)
WIN32_FIND_DATA fd{}; WIN32_FIND_DATA fd{};
auto search = utils::path::combine(abs_path, {"*.*"}); auto search = utils::path::combine(abs_path, {"*.*"});
auto find = ::FindFirstFile(search.c_str(), &fd); auto find = ::FindFirstFileA(search.c_str(), &fd);
if (find != INVALID_HANDLE_VALUE) { if (find != INVALID_HANDLE_VALUE) {
try { try {
do { do {
@ -49,15 +49,15 @@ auto get_directory_files(std::string_view path, bool oldest_first,
auto sub_files = auto sub_files =
get_directory_files(full_path, oldest_first, recursive); get_directory_files(full_path, oldest_first, recursive);
ret.insert(ret.end(), sub_files.begin(), sub_files.end()); ret.insert(ret.end(), sub_files.begin(), sub_files.end());
} else {
ULARGE_INTEGER li{};
li.HighPart = fd.ftLastWriteTime.dwHighDateTime;
li.LowPart = fd.ftLastWriteTime.dwLowDateTime;
lookup[full_path] = li.QuadPart;
ret.emplace_back(full_path);
} }
} else {
ULARGE_INTEGER li{};
li.HighPart = fd.ftLastWriteTime.dwHighDateTime;
li.LowPart = fd.ftLastWriteTime.dwLowDateTime;
lookup[full_path] = li.QuadPart;
ret.emplace_back(full_path);
} }
} while (::FindNextFile(find, &fd) != 0); } while (::FindNextFileA(find, &fd) != 0);
} catch (const std::exception &e) { } catch (const std::exception &e) {
utils::error::raise_error(function_name, e, utils::error::raise_error(function_name, e,
"failed to get directory files"); "failed to get directory files");

View File

@ -111,8 +111,8 @@ auto get_free_drive_space(std::string_view path)
try { try {
#if defined(_WIN32) #if defined(_WIN32)
ULARGE_INTEGER li{}; ULARGE_INTEGER li{};
if (not ::GetDiskFreeSpaceEx(std::string{path}.c_str(), &li, nullptr, if (not::GetDiskFreeSpaceEx(std::string{path}.c_str(), &li, nullptr,
nullptr)) { nullptr)) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
"failed to get free disk space", "failed to get free disk space",
@ -125,7 +125,7 @@ auto get_free_drive_space(std::string_view path)
#endif // defined(_WIN32) #endif // defined(_WIN32)
#if defined(__linux__) #if defined(__linux__)
struct statfs64 st {}; struct statfs64 st{};
if (statfs64(std::string{path}.c_str(), &st) != 0) { if (statfs64(std::string{path}.c_str(), &st) != 0) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
@ -139,7 +139,7 @@ auto get_free_drive_space(std::string_view path)
#endif // defined(__linux__) #endif // defined(__linux__)
#if defined(__APPLE__) #if defined(__APPLE__)
struct statvfs st {}; struct statvfs st{};
if (statvfs(path.c_str(), &st) != 0) { if (statvfs(path.c_str(), &st) != 0) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
@ -165,8 +165,8 @@ auto get_free_drive_space(std::wstring_view path)
return get_free_drive_space(utils::string::to_utf8(path)); return get_free_drive_space(utils::string::to_utf8(path));
} }
auto get_time(std::string_view path, auto get_time(std::string_view path, time_type type)
time_type type) -> std::optional<std::uint64_t> { -> std::optional<std::uint64_t> {
auto times = get_times(path); auto times = get_times(path);
if (times.has_value()) { if (times.has_value()) {
return times->get(type); return times->get(type);
@ -175,8 +175,8 @@ auto get_time(std::string_view path,
return std::nullopt; return std::nullopt;
} }
auto get_time(std::wstring_view path, auto get_time(std::wstring_view path, time_type type)
time_type type) -> std::optional<std::uint64_t> { -> std::optional<std::uint64_t> {
return get_time(utils::string::to_utf8(path), type); return get_time(utils::string::to_utf8(path), type);
} }
@ -207,7 +207,7 @@ auto get_times(std::string_view path) -> std::optional<file_times> {
} }
} }
struct _stat64 st {}; struct _stat64 st{};
if (_stat64(std::string{path}.c_str(), &st) != 0) { if (_stat64(std::string{path}.c_str(), &st) != 0) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
@ -222,7 +222,7 @@ auto get_times(std::string_view path) -> std::optional<file_times> {
ret.modified = utils::time::windows_time_t_to_unix_time(st.st_mtime); ret.modified = utils::time::windows_time_t_to_unix_time(st.st_mtime);
ret.written = utils::time::windows_time_t_to_unix_time(st.st_mtime); ret.written = utils::time::windows_time_t_to_unix_time(st.st_mtime);
#else // !defined(_WIN32) #else // !defined(_WIN32)
struct stat64 st {}; struct stat64 st{};
if (stat64(std::string{path}.c_str(), &st) != 0) { if (stat64(std::string{path}.c_str(), &st) != 0) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
@ -268,8 +268,8 @@ auto get_total_drive_space(std::string_view path)
try { try {
#if defined(_WIN32) #if defined(_WIN32)
ULARGE_INTEGER li{}; ULARGE_INTEGER li{};
if (not ::GetDiskFreeSpaceEx(std::string{path}.c_str(), nullptr, &li, if (not::GetDiskFreeSpaceEx(std::string{path}.c_str(), nullptr, &li,
nullptr)) { nullptr)) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
"failed to get total disk space", "failed to get total disk space",
@ -282,7 +282,7 @@ auto get_total_drive_space(std::string_view path)
#endif // defined(_WIN32) #endif // defined(_WIN32)
#if defined(__linux__) #if defined(__linux__)
struct statfs64 st {}; struct statfs64 st{};
if (statfs64(std::string{path}.c_str(), &st) != 0) { if (statfs64(std::string{path}.c_str(), &st) != 0) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
@ -296,7 +296,7 @@ auto get_total_drive_space(std::string_view path)
#endif // defined(__linux__) #endif // defined(__linux__)
#if defined(__APPLE__) #if defined(__APPLE__)
struct statvfs st {}; struct statvfs st{};
if (statvfs(path.c_str(), &st) != 0) { if (statvfs(path.c_str(), &st) != 0) {
throw utils::error::create_exception( throw utils::error::create_exception(
function_name, { function_name, {
@ -368,7 +368,7 @@ auto read_json_file(std::string_view path, nlohmann::json &data) -> bool {
try { try {
auto abs_path = utils::path::absolute(path); auto abs_path = utils::path::absolute(path);
auto file = file::open_file(abs_path); auto file = file::open_file(abs_path);
if (not *file) { if (not*file) {
return false; return false;
} }
@ -416,8 +416,8 @@ auto read_json_file(std::string_view path, nlohmann::json &data) -> bool {
auto write_json_file(std::string_view path, const nlohmann::json &data, auto write_json_file(std::string_view path, const nlohmann::json &data,
std::optional<std::string_view> password) -> bool { std::optional<std::string_view> password) -> bool {
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
auto write_json_file(std::string_view path, auto write_json_file(std::string_view path, const nlohmann::json &data)
const nlohmann::json &data) -> bool { -> bool {
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
@ -482,8 +482,8 @@ auto read_json_file(std::wstring_view path, nlohmann::json &data) -> bool {
return read_json_file(utils::string::to_utf8(path), data); return read_json_file(utils::string::to_utf8(path), data);
} }
auto write_json_file(std::wstring_view path, auto write_json_file(std::wstring_view path, const nlohmann::json &data)
const nlohmann::json &data) -> bool { -> bool {
return write_json_file(utils::string::to_utf8(path), data); return write_json_file(utils::string::to_utf8(path), data);
} }
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST) #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
@ -498,8 +498,8 @@ static constexpr const auto validate_smb_path =
std::count(path.begin(), path.end(), '/') >= 3U); std::count(path.begin(), path.end(), '/') >= 3U);
}; };
auto smb_create_smb_path(std::string_view smb_path, auto smb_create_smb_path(std::string_view smb_path, std::string_view rel_path)
std::string_view rel_path) -> std::string { -> std::string {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
if (not validate_smb_path(smb_path)) { if (not validate_smb_path(smb_path)) {
@ -532,8 +532,9 @@ auto smb_create_smb_path(std::string_view smb_path,
return path; return path;
} }
auto smb_create_and_validate_relative_path( auto smb_create_and_validate_relative_path(std::string_view smb_path,
std::string_view smb_path, std::string_view path) -> std::string { std::string_view path)
-> std::string {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
if (not validate_smb_path(smb_path)) { if (not validate_smb_path(smb_path)) {
@ -689,8 +690,8 @@ auto smb_get_uri_path(std::string_view smb_path, std::string_view user,
std::string{smb_path.substr(2U)}; std::string{smb_path.substr(2U)};
} }
auto smb_parent_is_same(std::string_view smb_path1, auto smb_parent_is_same(std::string_view smb_path1, std::string_view smb_path2)
std::string_view smb_path2) -> bool { -> bool {
if (not(validate_smb_path(smb_path1) && validate_smb_path(smb_path2))) { if (not(validate_smb_path(smb_path1) && validate_smb_path(smb_path2))) {
return false; return false;
} }