mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2024-12-21 03:16:54 +01:00
426 lines
12 KiB
C++
426 lines
12 KiB
C++
/*************************************************************************/
|
|
/* file_cache.cpp */
|
|
/*************************************************************************/
|
|
/* This file is part of: */
|
|
/* PANDEMONIUM ENGINE */
|
|
/* https://github.com/Relintai/pandemonium_engine */
|
|
/*************************************************************************/
|
|
/* Copyright (c) 2022-present Péter Magyar. */
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
/* */
|
|
/* 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 "file_cache.h"
|
|
|
|
#include "core/config/project_settings.h"
|
|
#include "core/os/dir_access.h"
|
|
#include "core/os/file_access.h"
|
|
#include "core/os/os.h"
|
|
#include "core/string/print_string.h"
|
|
|
|
FileCache::PathCacheMode FileCache::get_path_cache_mode() const {
|
|
return _path_cache_mode;
|
|
}
|
|
void FileCache::set_path_cache_mode(const PathCacheMode p_mode) {
|
|
_path_cache_mode = p_mode;
|
|
}
|
|
|
|
String FileCache::get_wwwroot() {
|
|
return _wwwroot_orig;
|
|
}
|
|
void FileCache::set_wwwroot(const String &val) {
|
|
_wwwroot_orig = val;
|
|
_wwwroot = _wwwroot_orig.path_clean_end_slash();
|
|
|
|
if (!_wwwroot.empty()) {
|
|
_wwwroot_abs = DirAccess::get_filesystem_abspath_for(_wwwroot_orig).path_clean_end_slash();
|
|
} else {
|
|
_wwwroot_abs = "";
|
|
}
|
|
}
|
|
|
|
String FileCache::get_wwwroot_abs() {
|
|
return _wwwroot_abs;
|
|
}
|
|
|
|
int FileCache::get_cache_invalidation_time() {
|
|
return static_cast<int>(cache_invalidation_time);
|
|
}
|
|
void FileCache::set_cache_invalidation_time(const int &val) {
|
|
cache_invalidation_time = static_cast<uint64_t>(val);
|
|
}
|
|
|
|
String FileCache::wwwroot_get_file_abspath(const String &file_path) {
|
|
if (file_path.empty() || file_path == "/") {
|
|
return String();
|
|
}
|
|
|
|
String fp = _wwwroot_abs + file_path;
|
|
|
|
if (!FileAccess::exists(fp)) {
|
|
return String();
|
|
}
|
|
|
|
Error err;
|
|
FileAccess *f = FileAccess::open(fp, FileAccess::READ, &err);
|
|
|
|
if (!f) {
|
|
return String();
|
|
}
|
|
|
|
if (err != OK) {
|
|
memdelete(f);
|
|
return String();
|
|
}
|
|
|
|
String absp = f->get_path_absolute();
|
|
memdelete(f);
|
|
|
|
//likely a directory walking attempt. e.g. ../../../../../etc/passwd
|
|
if (!absp.begins_with(_wwwroot_abs)) {
|
|
return String();
|
|
}
|
|
|
|
return absp;
|
|
}
|
|
|
|
void FileCache::wwwroot_register_file(const String &file_path) {
|
|
_cache_lock.write_lock();
|
|
|
|
RegisteredFileEntry e;
|
|
e.orig_path = file_path;
|
|
e.lowercase_path = file_path.to_lower();
|
|
|
|
_registered_files.push_back(e);
|
|
|
|
_cache_lock.write_unlock();
|
|
}
|
|
|
|
void FileCache::wwwroot_deregister_file(const String &file_path) {
|
|
_cache_lock.write_lock();
|
|
|
|
for (int i = 0; i < _registered_files.size(); ++i) {
|
|
const RegisteredFileEntry &e = _registered_files[i];
|
|
|
|
if (file_path == e.orig_path) {
|
|
_registered_files.remove(i);
|
|
_cache_lock.write_unlock();
|
|
return;
|
|
}
|
|
}
|
|
|
|
_cache_lock.write_unlock();
|
|
}
|
|
|
|
bool FileCache::wwwroot_has_file(const String &file_path) {
|
|
//return registered_files.has(file_path);
|
|
|
|
if (_path_cache_mode == PATH_CACHE_MODE_OFF) {
|
|
String absp = DirAccess::get_filesystem_abspath_for(_wwwroot + file_path);
|
|
|
|
//likely a directory walking attempt. e.g. ../../../../../etc/passwd
|
|
if (!absp.begins_with(_wwwroot_abs)) {
|
|
return false;
|
|
}
|
|
|
|
return FileAccess::exists(absp);
|
|
|
|
} else if (_path_cache_mode == PATH_CACHE_MODE_STATIC) {
|
|
_cache_lock.read_lock();
|
|
|
|
for (int i = 0; i < _registered_files.size(); ++i) {
|
|
const RegisteredFileEntry &e = _registered_files[i];
|
|
|
|
if (file_path == e.lowercase_path) {
|
|
_cache_lock.read_unlock();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
_cache_lock.read_unlock();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
int FileCache::wwwroot_get_file_index(const String &file_path) {
|
|
_cache_lock.read_lock();
|
|
|
|
for (int i = 0; i < _registered_files.size(); ++i) {
|
|
const RegisteredFileEntry &e = _registered_files[i];
|
|
|
|
if (file_path == e.lowercase_path) {
|
|
_cache_lock.read_unlock();
|
|
return i;
|
|
}
|
|
}
|
|
|
|
_cache_lock.read_unlock();
|
|
|
|
if (_path_cache_mode == PATH_CACHE_MODE_OFF) {
|
|
String np = _wwwroot;
|
|
np.append_path(file_path);
|
|
|
|
String absp = DirAccess::get_filesystem_abspath_for(np);
|
|
|
|
//likely a directory walking attempt. e.g. ../../../../../etc/passwd
|
|
if (!absp.begins_with(_wwwroot_abs)) {
|
|
return -1;
|
|
}
|
|
|
|
if (!FileAccess::exists(absp)) {
|
|
return -1;
|
|
}
|
|
|
|
RegisteredFileEntry e;
|
|
e.orig_path = file_path;
|
|
e.lowercase_path = file_path.to_lower();
|
|
|
|
_cache_lock.write_lock();
|
|
|
|
_registered_files.push_back(e);
|
|
|
|
int s = _registered_files.size() - 1;
|
|
|
|
_cache_lock.write_unlock();
|
|
|
|
return s;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
String FileCache::wwwroot_get_file_orig_path(const int index) {
|
|
_cache_lock.read_lock();
|
|
|
|
if (index < 0 || index >= _registered_files.size()) {
|
|
_cache_lock.read_unlock();
|
|
ERR_FAIL_V("");
|
|
}
|
|
|
|
String s = _registered_files[index].orig_path;
|
|
|
|
_cache_lock.read_unlock();
|
|
|
|
return s;
|
|
}
|
|
|
|
String FileCache::wwwroot_get_file_orig_path_abs(const int index) {
|
|
return get_wwwroot_abs() + wwwroot_get_file_orig_path(index);
|
|
}
|
|
|
|
void FileCache::wwwroot_refresh_cache() {
|
|
_cache_lock.write_lock();
|
|
_registered_files.clear();
|
|
_cache_lock.write_unlock();
|
|
|
|
if (_path_cache_mode == PATH_CACHE_MODE_STATIC) {
|
|
if (_wwwroot != "") {
|
|
wwwroot_evaluate_dir(_wwwroot);
|
|
}
|
|
}
|
|
}
|
|
|
|
void FileCache::wwwroot_evaluate_dir(const String &path, const bool should_exist) {
|
|
DirAccess *da = DirAccess::open(path);
|
|
|
|
ERR_FAIL_COND_MSG(!da, "Error opening wwwroot! folder: " + path);
|
|
|
|
da->list_dir_begin();
|
|
String f = da->get_next();
|
|
|
|
while (f != String()) {
|
|
if (f == "." || f == "..") {
|
|
f = da->get_next();
|
|
continue;
|
|
}
|
|
|
|
if (!da->current_is_dir()) {
|
|
String np = path + "/" + f;
|
|
np = np.substr(_wwwroot.size() - 1, np.size() - _wwwroot.size());
|
|
wwwroot_register_file(np);
|
|
|
|
} else {
|
|
wwwroot_evaluate_dir(path + "/" + f);
|
|
}
|
|
|
|
f = da->get_next();
|
|
}
|
|
da->list_dir_end();
|
|
|
|
memdelete(da);
|
|
}
|
|
|
|
bool FileCache::get_cached_body(const String &path, String *body) {
|
|
//TODO ERROR MACRO body == null
|
|
|
|
_body_lock.read_lock();
|
|
CacheEntry *e = cache_map[path];
|
|
_body_lock.read_unlock();
|
|
|
|
if (!e) {
|
|
return false;
|
|
}
|
|
|
|
if (cache_invalidation_time > 0) {
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
|
uint64_t diff = current_timestamp - e->timestamp;
|
|
|
|
if (diff > cache_invalidation_time) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
body->operator+=(e->body);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool FileCache::has_cached_body(const String &path) {
|
|
//TODO ERROR MACRO body == null
|
|
|
|
_body_lock.read_lock();
|
|
CacheEntry *e = cache_map[path];
|
|
_body_lock.read_unlock();
|
|
|
|
if (!e) {
|
|
return false;
|
|
}
|
|
|
|
if (cache_invalidation_time > 0) {
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
|
uint64_t diff = current_timestamp - e->timestamp;
|
|
|
|
if (diff > cache_invalidation_time) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
String FileCache::get_cached_body_bind(const String &path) {
|
|
//TODO ERROR MACRO body == null
|
|
|
|
_body_lock.read_lock();
|
|
CacheEntry *e = cache_map[path];
|
|
_body_lock.read_unlock();
|
|
|
|
if (!e) {
|
|
return "";
|
|
}
|
|
|
|
if (cache_invalidation_time > 0) {
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
|
uint64_t diff = current_timestamp - e->timestamp;
|
|
|
|
if (diff > cache_invalidation_time) {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
return e->body;
|
|
}
|
|
|
|
void FileCache::set_cached_body(const String &path, const String &body) {
|
|
_body_lock.write_lock();
|
|
|
|
CacheEntry *e = cache_map[path];
|
|
|
|
if (!e) {
|
|
e = memnew(CacheEntry());
|
|
cache_map[path] = e;
|
|
}
|
|
|
|
uint64_t current_timestamp = OS::get_singleton()->get_unix_time();
|
|
|
|
e->timestamp = current_timestamp;
|
|
e->body = body;
|
|
|
|
_body_lock.write_unlock();
|
|
}
|
|
|
|
void FileCache::clear() {
|
|
_cache_lock.write_lock();
|
|
_registered_files.clear();
|
|
_cache_lock.write_unlock();
|
|
|
|
_body_lock.write_lock();
|
|
|
|
for (RBMap<String, CacheEntry *>::Element *E = cache_map.front(); E; E++) {
|
|
CacheEntry *ce = E->get();
|
|
|
|
if (ce) {
|
|
memdelete(ce);
|
|
}
|
|
}
|
|
|
|
cache_map.clear();
|
|
|
|
_body_lock.write_unlock();
|
|
}
|
|
|
|
FileCache::FileCache() {
|
|
cache_invalidation_time = 0;
|
|
_path_cache_mode = PATH_CACHE_MODE_OFF;
|
|
}
|
|
|
|
FileCache::~FileCache() {
|
|
_registered_files.clear();
|
|
}
|
|
|
|
void FileCache::_bind_methods() {
|
|
ClassDB::bind_method(D_METHOD("get_path_cache_mode"), &FileCache::get_path_cache_mode);
|
|
ClassDB::bind_method(D_METHOD("set_path_cache_mode", "mode"), &FileCache::set_path_cache_mode);
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "path_cache_mode", PROPERTY_HINT_ENUM, "Off,Static"), "set_path_cache_mode", "get_path_cache_mode");
|
|
|
|
ClassDB::bind_method(D_METHOD("get_wwwroot"), &FileCache::get_wwwroot);
|
|
ClassDB::bind_method(D_METHOD("set_wwwroot", "val"), &FileCache::set_wwwroot);
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "wwwroot"), "set_wwwroot", "get_wwwroot");
|
|
|
|
ClassDB::bind_method(D_METHOD("get_wwwroot_abs"), &FileCache::get_wwwroot_abs);
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_get_file_abspath", "file_path"), &FileCache::wwwroot_get_file_abspath);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cache_invalidation_time"), &FileCache::get_cache_invalidation_time);
|
|
ClassDB::bind_method(D_METHOD("set_cache_invalidation_time", "val"), &FileCache::set_cache_invalidation_time);
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "cache_invalidation_time"), "set_cache_invalidation_time", "get_cache_invalidation_time");
|
|
|
|
ClassDB::bind_method(D_METHOD("wwwroot_register_file", "file_path"), &FileCache::wwwroot_register_file);
|
|
ClassDB::bind_method(D_METHOD("wwwroot_deregister_file", "file_path"), &FileCache::wwwroot_deregister_file);
|
|
ClassDB::bind_method(D_METHOD("wwwroot_has_file", "file_path"), &FileCache::wwwroot_has_file);
|
|
ClassDB::bind_method(D_METHOD("wwwroot_get_file_index", "file_path"), &FileCache::wwwroot_get_file_index);
|
|
ClassDB::bind_method(D_METHOD("wwwroot_get_file_orig_path", "index"), &FileCache::wwwroot_get_file_orig_path);
|
|
ClassDB::bind_method(D_METHOD("wwwroot_get_file_orig_path_abs", "index"), &FileCache::wwwroot_get_file_orig_path_abs);
|
|
ClassDB::bind_method(D_METHOD("wwwroot_refresh_cache"), &FileCache::wwwroot_refresh_cache);
|
|
ClassDB::bind_method(D_METHOD("wwwroot_evaluate_dir", "file_path", "should_exist "), &FileCache::wwwroot_evaluate_dir, true);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cached_body", "path"), &FileCache::get_cached_body_bind);
|
|
ClassDB::bind_method(D_METHOD("has_cached_body", "path"), &FileCache::has_cached_body);
|
|
ClassDB::bind_method(D_METHOD("set_cached_body", "path", "body"), &FileCache::set_cached_body);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &FileCache::clear);
|
|
|
|
BIND_ENUM_CONSTANT(PATH_CACHE_MODE_OFF);
|
|
BIND_ENUM_CONSTANT(PATH_CACHE_MODE_STATIC);
|
|
}
|