2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/files/FileStatsWorker.h"
|
|
|
|
|
|
|
|
#include "td/telegram/DialogId.h"
|
2019-01-19 22:26:23 +01:00
|
|
|
#include "td/telegram/files/FileData.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileDb.h"
|
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileLocation.h"
|
|
|
|
#include "td/telegram/files/FileType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2019-06-12 13:42:06 +02:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/TdParameters.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-07-19 16:45:30 +02:00
|
|
|
#include "td/db/SqliteKeyValue.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
2022-11-23 17:37:32 +01:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2019-03-24 19:36:21 +01:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/PathView.h"
|
|
|
|
#include "td/utils/port/path.h"
|
|
|
|
#include "td/utils/port/Stat.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
2019-03-29 12:32:50 +01:00
|
|
|
#include "td/utils/tl_parsers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-02-09 22:59:52 +01:00
|
|
|
#include <unordered_map>
|
2020-07-04 11:00:14 +02:00
|
|
|
#include <unordered_set>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace {
|
|
|
|
// Performance ideas:
|
|
|
|
// - use slice instead of string
|
|
|
|
// - use arena memory allocator
|
|
|
|
// - store FileType or dir, no both
|
|
|
|
// - store dir relative to G()->files_dir()
|
|
|
|
|
|
|
|
struct DbFileInfo {
|
|
|
|
FileType file_type;
|
|
|
|
string path;
|
|
|
|
DialogId owner_dialog_id;
|
|
|
|
int64 size;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class CallbackT>
|
2019-05-01 16:15:54 +02:00
|
|
|
void scan_db(CancellationToken &token, CallbackT &&callback) {
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->td_db()->get_file_db_shared()->pmc().get_by_range("file0", "file:", [&](Slice key, Slice value) {
|
2019-05-01 16:15:54 +02:00
|
|
|
if (token) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
// skip reference to other data
|
|
|
|
if (value.substr(0, 2) == "@@") {
|
2019-05-01 16:15:54 +02:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-09-22 01:15:09 +02:00
|
|
|
log_event::WithVersion<TlParser> parser(value);
|
2018-12-31 20:04:05 +01:00
|
|
|
FileData data;
|
2019-03-17 22:40:10 +01:00
|
|
|
data.parse(parser, false);
|
|
|
|
if (parser.get_status().is_error()) {
|
2018-01-26 02:06:53 +01:00
|
|
|
LOG(ERROR) << "Invalid FileData in the database " << tag("value", format::escaped(value));
|
2019-05-01 16:15:54 +02:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
DbFileInfo info;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (data.local_.type() == LocalFileLocation::Type::Full) {
|
|
|
|
info.file_type = data.local_.full().file_type_;
|
2018-12-31 20:04:05 +01:00
|
|
|
info.path = data.local_.full().path_;
|
2018-01-20 15:57:52 +01:00
|
|
|
} else if (data.local_.type() == LocalFileLocation::Type::Partial) {
|
|
|
|
info.file_type = data.local_.partial().file_type_;
|
2018-12-31 20:04:05 +01:00
|
|
|
info.path = data.local_.partial().path_;
|
|
|
|
} else {
|
2019-05-01 16:15:54 +02:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
PathView path_view(info.path);
|
|
|
|
if (path_view.is_relative()) {
|
2019-01-19 23:59:37 +01:00
|
|
|
info.path = PSTRING() << get_files_base_dir(info.file_type) << info.path;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-26 02:06:53 +01:00
|
|
|
// LOG(INFO) << "Found file in the database: " << data << " " << info.path;
|
2018-12-31 20:04:05 +01:00
|
|
|
info.owner_dialog_id = data.owner_dialog_id_;
|
|
|
|
info.size = data.size_;
|
2018-01-20 15:57:52 +01:00
|
|
|
if (info.size == 0 && data.local_.type() == LocalFileLocation::Type::Full) {
|
2018-01-26 02:06:53 +01:00
|
|
|
LOG(ERROR) << "Unknown size in the database";
|
2019-05-01 16:15:54 +02:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
callback(info);
|
2019-05-01 16:15:54 +02:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FsFileInfo {
|
|
|
|
FileType file_type;
|
|
|
|
string path;
|
|
|
|
int64 size;
|
|
|
|
uint64 atime_nsec;
|
|
|
|
uint64 mtime_nsec;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class CallbackT>
|
2019-05-01 16:15:54 +02:00
|
|
|
void scan_fs(CancellationToken &token, CallbackT &&callback) {
|
2022-11-23 17:37:32 +01:00
|
|
|
std::unordered_set<string, Hash<string>> scanned_file_dirs;
|
2022-10-02 21:26:03 +02:00
|
|
|
auto scan_dir = [&](FileType file_type, const string &file_dir) {
|
|
|
|
LOG(INFO) << "Trying to scan directory " << file_dir;
|
2020-06-28 21:04:21 +02:00
|
|
|
if (!scanned_file_dirs.insert(file_dir).second) {
|
2022-10-02 21:26:03 +02:00
|
|
|
return;
|
2020-06-28 21:04:21 +02:00
|
|
|
}
|
2020-06-22 07:43:00 +02:00
|
|
|
walk_path(file_dir, [&](CSlice path, WalkPath::Type type) {
|
2019-09-28 04:14:21 +02:00
|
|
|
if (token) {
|
|
|
|
return WalkPath::Action::Abort;
|
|
|
|
}
|
|
|
|
if (type != WalkPath::Type::NotDir) {
|
|
|
|
return WalkPath::Action::Continue;
|
|
|
|
}
|
|
|
|
auto r_stat = stat(path);
|
|
|
|
if (r_stat.is_error()) {
|
|
|
|
LOG(WARNING) << "Stat in files gc failed: " << r_stat.error();
|
|
|
|
return WalkPath::Action::Continue;
|
|
|
|
}
|
|
|
|
auto stat = r_stat.move_as_ok();
|
2020-06-28 21:04:21 +02:00
|
|
|
if (stat.size_ == 0 && ends_with(path, "/.nomedia")) {
|
2019-09-28 04:14:21 +02:00
|
|
|
// skip .nomedia file
|
|
|
|
return WalkPath::Action::Continue;
|
|
|
|
}
|
2019-05-01 16:15:54 +02:00
|
|
|
|
2019-09-28 04:14:21 +02:00
|
|
|
FsFileInfo info;
|
|
|
|
info.path = path.str();
|
2020-01-03 02:08:22 +01:00
|
|
|
info.size = stat.real_size_;
|
2022-10-02 21:26:03 +02:00
|
|
|
info.file_type = file_type;
|
2019-09-28 04:14:21 +02:00
|
|
|
info.atime_nsec = stat.atime_nsec_;
|
|
|
|
info.mtime_nsec = stat.mtime_nsec_;
|
|
|
|
callback(info);
|
|
|
|
return WalkPath::Action::Continue;
|
|
|
|
}).ignore();
|
2022-10-02 21:26:03 +02:00
|
|
|
};
|
|
|
|
for (int32 i = 0; i < MAX_FILE_TYPE; i++) {
|
|
|
|
auto file_type = static_cast<FileType>(i);
|
|
|
|
scan_dir(get_main_file_type(file_type), get_files_dir(file_type));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-02 21:26:03 +02:00
|
|
|
scan_dir(get_main_file_type(FileType::Temp), get_files_temp_dir(FileType::SecureDecrypted));
|
|
|
|
scan_dir(get_main_file_type(FileType::Temp), get_files_temp_dir(FileType::Video));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_id, Promise<FileStats> promise) {
|
|
|
|
if (!G()->parameters().use_chat_info_db) {
|
|
|
|
split_by_owner_dialog_id = false;
|
|
|
|
}
|
|
|
|
if (!split_by_owner_dialog_id) {
|
2021-09-26 11:44:18 +02:00
|
|
|
FileStats file_stats(need_all_files, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto start = Time::now();
|
2019-05-01 16:15:54 +02:00
|
|
|
scan_fs(token_, [&](FsFileInfo &fs_info) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FullFileInfo info;
|
|
|
|
info.file_type = fs_info.file_type;
|
|
|
|
info.path = std::move(fs_info.path);
|
|
|
|
info.size = fs_info.size;
|
|
|
|
info.atime_nsec = fs_info.atime_nsec;
|
|
|
|
info.mtime_nsec = fs_info.mtime_nsec;
|
|
|
|
file_stats.add(std::move(info));
|
|
|
|
});
|
|
|
|
auto passed = Time::now() - start;
|
|
|
|
LOG_IF(INFO, passed > 0.5) << "Get file stats took: " << format::as_time(passed);
|
2019-05-16 14:05:22 +02:00
|
|
|
if (token_) {
|
2021-10-07 15:36:21 +02:00
|
|
|
return promise.set_error(Global::request_aborted_error());
|
2019-05-16 14:05:22 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(std::move(file_stats));
|
|
|
|
} else {
|
|
|
|
auto start = Time::now();
|
|
|
|
|
|
|
|
std::vector<FullFileInfo> full_infos;
|
2019-05-01 16:15:54 +02:00
|
|
|
scan_fs(token_, [&](FsFileInfo &fs_info) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FullFileInfo info;
|
|
|
|
info.file_type = fs_info.file_type;
|
|
|
|
info.path = std::move(fs_info.path);
|
|
|
|
info.size = fs_info.size;
|
|
|
|
info.atime_nsec = fs_info.atime_nsec;
|
|
|
|
info.mtime_nsec = fs_info.mtime_nsec;
|
2018-01-26 02:06:53 +01:00
|
|
|
|
|
|
|
// LOG(INFO) << "Found file of size " << info.size << " at " << info.path;
|
|
|
|
|
|
|
|
full_infos.push_back(std::move(info));
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
|
2019-05-16 14:05:22 +02:00
|
|
|
if (token_) {
|
2021-10-07 15:36:21 +02:00
|
|
|
return promise.set_error(Global::request_aborted_error());
|
2019-05-16 14:05:22 +02:00
|
|
|
}
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
std::unordered_map<int64, size_t, Hash<int64>> hash_to_pos;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t pos = 0;
|
|
|
|
for (auto &full_info : full_infos) {
|
2022-11-23 17:37:32 +01:00
|
|
|
hash_to_pos[Hash<string>()(full_info.path)] = pos;
|
2018-12-31 20:04:05 +01:00
|
|
|
pos++;
|
2019-05-16 14:05:22 +02:00
|
|
|
if (token_) {
|
2021-10-07 15:36:21 +02:00
|
|
|
return promise.set_error(Global::request_aborted_error());
|
2019-05-16 14:05:22 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-05-01 16:15:54 +02:00
|
|
|
scan_db(token_, [&](DbFileInfo &db_info) {
|
2022-11-23 17:37:32 +01:00
|
|
|
auto it = hash_to_pos.find(Hash<string>()(db_info.path));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == hash_to_pos.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-26 02:06:53 +01:00
|
|
|
// LOG(INFO) << "Match! " << db_info.path << " from " << db_info.owner_dialog_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
full_infos[it->second].owner_dialog_id = db_info.owner_dialog_id;
|
|
|
|
});
|
2019-05-16 14:05:22 +02:00
|
|
|
if (token_) {
|
2021-10-07 15:36:21 +02:00
|
|
|
return promise.set_error(Global::request_aborted_error());
|
2019-05-16 14:05:22 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-26 11:44:18 +02:00
|
|
|
FileStats file_stats(need_all_files, split_by_owner_dialog_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &full_info : full_infos) {
|
|
|
|
file_stats.add(std::move(full_info));
|
2019-05-16 14:05:22 +02:00
|
|
|
if (token_) {
|
2021-10-07 15:36:21 +02:00
|
|
|
return promise.set_error(Global::request_aborted_error());
|
2019-05-16 14:05:22 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto passed = Time::now() - start;
|
|
|
|
LOG_IF(INFO, passed > 0.5) << "Get file stats took: " << format::as_time(passed);
|
|
|
|
promise.set_value(std::move(file_stats));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|