2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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/utils/filesystem.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2018-12-19 21:35:13 +01:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/PathView.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
2020-06-24 13:47:36 +02:00
|
|
|
#include "td/utils/port/path.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#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/Status.h"
|
|
|
|
#include "td/utils/unicode.h"
|
|
|
|
#include "td/utils/utf8.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2019-12-31 00:08:57 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T create_empty(size_t size);
|
|
|
|
|
|
|
|
template <>
|
|
|
|
string create_empty<string>(size_t size) {
|
|
|
|
return string(size, '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
BufferSlice create_empty<BufferSlice>(size_t size) {
|
|
|
|
return BufferSlice{size};
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
template <>
|
|
|
|
SecureString create_empty<SecureString>(size_t size) {
|
|
|
|
return SecureString{size};
|
|
|
|
}
|
2019-12-31 00:08:57 +01:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Result<T> read_file_impl(CSlice path, int64 size, int64 offset) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(from_file, FileFd::open(path, FileFd::Read));
|
2020-06-24 13:47:36 +02:00
|
|
|
TRY_RESULT(file_size, from_file.get_size());
|
|
|
|
if (offset < 0 || offset > file_size) {
|
|
|
|
return Status::Error("Failed to read file: invalid offset");
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
if (size < 0 || size > file_size - offset) {
|
2020-06-24 13:47:36 +02:00
|
|
|
size = file_size - offset;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-12-31 00:08:57 +01:00
|
|
|
auto content = create_empty<T>(narrow_cast<size_t>(size));
|
2019-08-12 13:45:57 +02:00
|
|
|
TRY_RESULT(got_size, from_file.pread(as_mutable_slice(content), offset));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (got_size != static_cast<size_t>(size)) {
|
|
|
|
return Status::Error("Failed to read file");
|
|
|
|
}
|
|
|
|
from_file.close();
|
2018-12-19 21:35:13 +01:00
|
|
|
return std::move(content);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-19 21:35:13 +01:00
|
|
|
|
2019-12-31 00:08:57 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Result<BufferSlice> read_file(CSlice path, int64 size, int64 offset) {
|
|
|
|
return read_file_impl<BufferSlice>(path, size, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<string> read_file_str(CSlice path, int64 size, int64 offset) {
|
|
|
|
return read_file_impl<string>(path, size, offset);
|
2018-12-19 15:48:39 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
Result<SecureString> read_file_secure(CSlice path, int64 size, int64 offset) {
|
|
|
|
return read_file_impl<SecureString>(path, size, offset);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// Very straightforward function. Don't expect much of it.
|
|
|
|
Status copy_file(CSlice from, CSlice to, int64 size) {
|
|
|
|
TRY_RESULT(content, read_file(from, size));
|
|
|
|
return write_file(to, content.as_slice());
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
Status write_file(CSlice to, Slice data, WriteFileOptions options) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto size = data.size();
|
|
|
|
TRY_RESULT(to_file, FileFd::open(to, FileFd::Truncate | FileFd::Create | FileFd::Write));
|
2020-06-24 13:47:36 +02:00
|
|
|
if (options.need_lock) {
|
|
|
|
TRY_STATUS(to_file.lock(FileFd::LockFlags::Write, to.str(), 10));
|
|
|
|
TRY_STATUS(to_file.truncate_to_current_position(0));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(written, to_file.write(data));
|
2019-04-26 02:18:00 +02:00
|
|
|
if (written != size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(PSLICE() << "Failed to write file: written " << written << " bytes instead of " << size);
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
if (options.need_sync) {
|
|
|
|
TRY_STATUS(to_file.sync());
|
|
|
|
}
|
|
|
|
if (options.need_lock) {
|
|
|
|
to_file.lock(FileFd::LockFlags::Unlock, to.str(), 10).ignore();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
to_file.close();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
static string clean_filename_part(Slice name, int max_length) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto is_ok = [](uint32 code) {
|
|
|
|
if (code < 32) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (code < 127) {
|
|
|
|
switch (code) {
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case ':':
|
|
|
|
case '"':
|
|
|
|
case '/':
|
|
|
|
case '\\':
|
|
|
|
case '|':
|
|
|
|
case '?':
|
|
|
|
case '*':
|
|
|
|
case '&':
|
|
|
|
case '`':
|
|
|
|
case '\'':
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto category = get_unicode_simple_category(code);
|
|
|
|
|
|
|
|
return category == UnicodeSimpleCategory::Letter || category == UnicodeSimpleCategory::DecimalNumber ||
|
|
|
|
category == UnicodeSimpleCategory::Number;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string new_name;
|
|
|
|
int size = 0;
|
|
|
|
for (auto *it = name.ubegin(); it != name.uend() && size < max_length;) {
|
|
|
|
uint32 code;
|
2022-08-19 15:37:44 +02:00
|
|
|
it = next_utf8_unsafe(it, &code);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_ok(code)) {
|
2018-11-27 02:24:43 +01:00
|
|
|
if (prepare_search_character(code) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
code = ' ';
|
|
|
|
}
|
|
|
|
if (new_name.empty() && (code == ' ' || code == '.')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
append_utf8_character(new_name, code);
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!new_name.empty() && (new_name.back() == ' ' || new_name.back() == '.')) {
|
|
|
|
new_name.pop_back();
|
|
|
|
}
|
|
|
|
return new_name;
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
string clean_filename(CSlice name) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!check_utf8(name)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
PathView path_view(name);
|
2022-04-20 18:31:30 +02:00
|
|
|
auto filename = clean_filename_part(path_view.file_stem(), 64);
|
|
|
|
auto extension = clean_filename_part(path_view.extension(), 16);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!extension.empty()) {
|
|
|
|
if (filename.empty()) {
|
|
|
|
filename = std::move(extension);
|
|
|
|
} else {
|
|
|
|
filename.reserve(filename.size() + 1 + extension.size());
|
|
|
|
filename += '.';
|
|
|
|
filename += extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
Status atomic_write_file(CSlice path, Slice data, CSlice path_tmp) {
|
|
|
|
string path_tmp_buf;
|
|
|
|
if (path_tmp.empty()) {
|
|
|
|
path_tmp_buf = path.str() + ".tmp";
|
|
|
|
path_tmp = path_tmp_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteFileOptions options;
|
|
|
|
options.need_sync = true;
|
|
|
|
options.need_lock = true;
|
|
|
|
TRY_STATUS(write_file(path_tmp, data, options));
|
|
|
|
return rename(path_tmp, path);
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|