diff --git a/td/telegram/StorageManager.cpp b/td/telegram/StorageManager.cpp index 1cfe9d1b..373332cf 100644 --- a/td/telegram/StorageManager.cpp +++ b/td/telegram/StorageManager.cpp @@ -170,7 +170,7 @@ int64 StorageManager::get_file_size(CSlice path) { return 0; } - auto size = r_info.ok().size_; + auto size = r_info.ok().real_size_; LOG(DEBUG) << "Add file \"" << path << "\" of size " << size << " to fast storage statistics"; return size; } diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index c306a0ad..ab97f7d7 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -544,6 +544,18 @@ int64 FileView::size() const { return node_->size_; } +int64 FileView::get_allocated_local_size() const { + auto file_path = path(); + if (file_path.empty()) { + return 0; + } + auto r_stat = stat(file_path); + if (r_stat.is_error()) { + return 0; + } + return r_stat.ok().real_size_; +} + int64 FileView::expected_size(bool may_guess) const { if (node_->size_ != 0) { return node_->size_; @@ -2008,8 +2020,8 @@ void FileManager::delete_file(FileId file_id, Promise promise, const char LOG(INFO) << "Unlink file " << file_id << " at " << file_view.local_location().path_; clear_from_pmc(node); + context_->on_new_file(-file_view.get_allocated_local_size(), -1); unlink(file_view.local_location().path_).ignore(); - context_->on_new_file(-file_view.size(), -1); node->drop_local_location(); try_flush_node(node, "delete_file 1"); } @@ -3264,7 +3276,7 @@ void FileManager::on_download_ok(QueryId query_id, const FullLocalFileLocation & LOG(ERROR) << "Can't register local file after download: " << r_new_file_id.error(); } else { if (is_new) { - context_->on_new_file(size, 1); + context_->on_new_file(get_file_view(r_new_file_id.ok()).get_allocated_local_size(), 1); } LOG_STATUS(merge(r_new_file_id.ok(), file_id)); } @@ -3431,7 +3443,7 @@ void FileManager::on_generate_ok(QueryId query_id, const FullLocalFileLocation & FileView file_view(file_node); if (!file_view.has_generate_location() || !begins_with(file_view.generate_location().conversion_, "#file_id#")) { - context_->on_new_file(file_view.size(), 1); + context_->on_new_file(file_view.get_allocated_local_size(), 1); } run_upload(file_node, {}); diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index 3c46945f..cfbda8fa 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -271,6 +271,8 @@ class FileView { int64 remote_size() const; string path() const; + int64 get_allocated_local_size() const; + bool can_download_from_server() const; bool can_generate() const; bool can_delete() const; diff --git a/td/telegram/files/FileStatsWorker.cpp b/td/telegram/files/FileStatsWorker.cpp index ede2f025..34d9edb8 100644 --- a/td/telegram/files/FileStatsWorker.cpp +++ b/td/telegram/files/FileStatsWorker.cpp @@ -126,7 +126,7 @@ void scan_fs(CancellationToken &token, CallbackT &&callback) { FsFileInfo info; info.path = path.str(); - info.size = stat.size_; + info.size = stat.real_size_; info.file_type = file_type; info.atime_nsec = stat.atime_nsec_; info.mtime_nsec = stat.mtime_nsec_; diff --git a/tdutils/td/utils/port/FileFd.cpp b/tdutils/td/utils/port/FileFd.cpp index ad8bc6fb..f49184fd 100644 --- a/tdutils/td/utils/port/FileFd.cpp +++ b/tdutils/td/utils/port/FileFd.cpp @@ -499,8 +499,9 @@ Result get_file_size(const FileFd &file_fd) { res.size_ = standard_info.EndOfFile.QuadPart; res.real_size_ = standard_info.AllocationSize.QuadPart; - if (res.size_ > 0 && res.real_size_ <= 0) { - res.real_size_ = res.size_; // just in case + if (res.size_ > 0 && res.real_size_ <= 0) { // just in case + LOG(ERROR) << "Fix real file size from " << res.real_size_ << " to " << res.size_; + res.real_size_ = res.size_; } return res;