Fix file manager

This commit is contained in:
Andrea Cavalli 2021-03-12 21:12:01 +01:00
parent 5303da675f
commit 9115565603
2 changed files with 39 additions and 10 deletions

View File

@ -800,9 +800,17 @@ void prepare_path_for_pmc(FileType file_type, string &path) {
FileManager::FileManager(unique_ptr<Context> context) : context_(std::move(context)) {
if (G()->parameters().use_file_db) {
file_db_ = G()->td_db()->get_file_db_shared();
use_standard_algorithm_ = true;
} else {
use_standard_algorithm_ = false;
}
parent_ = context_->create_reference();
// only the original tdlib algorithm requires a next_file_id() call at startup
if (use_standard_algorithm_) {
next_file_id();
next_file_node_id();
}
std::unordered_set<string> dir_paths;
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
@ -1031,9 +1039,15 @@ bool FileManager::try_fix_partial_local_location(FileNodePtr node) {
}
FileManager::FileIdInfo *FileManager::get_file_id_info(FileId file_id) {
if (use_standard_algorithm_) {
LOG_CHECK(0 <= file_id.get() && file_id.get() < static_cast<int32>(file_id_info_.size()))
<< file_id << " " << file_id_info_.size();
return &file_id_info_[file_id.get()];
} else {
file_id.set_time(); // update last access time of FileId
return &file_id_info_.at(file_id.get());
}
}
FileId FileManager::dup_file_id(FileId file_id) {
int32 file_node_id;
@ -1067,6 +1081,9 @@ void FileManager::try_forget_file_id(FileId file_id) {
bool is_removed = td::remove(file_node->file_ids_, file_id);
CHECK(is_removed);
*info = FileIdInfo();
if (use_standard_algorithm_) {
file_id_info_.erase(file_id.get());
} else {
file_id_info_.erase(file_id.get());
// Start custom-patches
file_id.reset_time(); // delete last access time of FileId
@ -1074,6 +1091,7 @@ void FileManager::try_forget_file_id(FileId file_id) {
context_->destroy_file_source(file_id);
// End custom-patches
}
}
FileId FileManager::register_empty(FileType type) {
return register_local(FullLocalFileLocation(type, "", 0), DialogId(), 0, false, true).ok();
@ -1087,9 +1105,13 @@ void FileManager::on_file_unlink(const FullLocalFileLocation &location) {
}
auto file_id = it->second;
auto file_node = get_sync_file_node(file_id);
if (use_standard_algorithm_) {
CHECK(file_node);
} else {
if (!file_node) {
return;
}
}
file_node->drop_local_location();
try_flush_node_info(file_node, "on_file_unlink");
}
@ -3862,6 +3884,9 @@ void FileManager::hangup() {
}
void FileManager::destroy_query(int32 file_id) {
if (use_standard_algorithm_) {
return;
}
for (auto &query_id : queries_container_.ids()) {
auto query = queries_container_.get(query_id);
if (query != nullptr && file_id == query->file_id_.get()) {
@ -3876,6 +3901,9 @@ void FileManager::memory_cleanup() {
}
void FileManager::memory_cleanup(bool full) {
if (use_standard_algorithm_) {
return;
}
if (!full) {
LOG(INFO) << "Initial registered ids: " << file_id_info_.size() << " registered nodes: " << file_nodes_.size();
}

View File

@ -564,6 +564,7 @@ class FileManager : public FileLoadManager::Callback {
ActorShared<> parent_;
unique_ptr<Context> context_;
std::shared_ptr<FileDbInterface> file_db_;
bool use_standard_algorithm_;
FileIdInfo *get_file_id_info(FileId file_id);