2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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/port/FileFd.h"
|
|
|
|
|
|
|
|
#if TD_PORT_WINDOWS
|
2020-12-23 09:25:27 +01:00
|
|
|
#include "td/utils/port/FromApp.h"
|
2018-01-26 14:01:40 +01:00
|
|
|
#include "td/utils/port/Stat.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/wstring_convert.h"
|
|
|
|
#endif
|
|
|
|
|
2020-03-19 00:33:53 +01:00
|
|
|
#include "td/utils/common.h"
|
2020-10-11 00:59:27 +02:00
|
|
|
#include "td/utils/ExitGuard.h"
|
2022-03-11 19:38:48 +01:00
|
|
|
#include "td/utils/FlatHashSet.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2020-06-15 02:50:38 +02:00
|
|
|
#include "td/utils/port/detail/skip_eintr.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/sleep.h"
|
2018-10-28 18:30:47 +01:00
|
|
|
#include "td/utils/ScopeGuard.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/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
2018-10-08 20:18:06 +02:00
|
|
|
#include <mutex>
|
2020-01-03 00:41:57 +01:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
2020-06-07 17:14:52 +02:00
|
|
|
#include <cerrno>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <unistd.h>
|
2021-04-03 23:40:37 +02:00
|
|
|
#else
|
|
|
|
#include <limits>
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
|
2020-03-19 00:33:53 +01:00
|
|
|
#if TD_PORT_WINDOWS && defined(WIN32_LEAN_AND_MEAN)
|
|
|
|
#include <winioctl.h>
|
|
|
|
#endif
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct PrintFlags {
|
|
|
|
int32 flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const PrintFlags &print_flags) {
|
|
|
|
auto flags = print_flags.flags;
|
2019-07-06 13:29:15 +02:00
|
|
|
if (flags & ~(FileFd::Write | FileFd::Read | FileFd::Truncate | FileFd::Create | FileFd::Append | FileFd::CreateNew |
|
|
|
|
FileFd::Direct | FileFd::WinStat)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return sb << "opened with invalid flags " << flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FileFd::Create) {
|
|
|
|
sb << "opened/created ";
|
|
|
|
} else if (flags & FileFd::CreateNew) {
|
|
|
|
sb << "created ";
|
|
|
|
} else {
|
|
|
|
sb << "opened ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & FileFd::Write) && (flags & FileFd::Read)) {
|
|
|
|
if (flags & FileFd::Append) {
|
|
|
|
sb << "for reading and appending";
|
|
|
|
} else {
|
|
|
|
sb << "for reading and writing";
|
|
|
|
}
|
|
|
|
} else if (flags & FileFd::Write) {
|
|
|
|
if (flags & FileFd::Append) {
|
|
|
|
sb << "for appending";
|
|
|
|
} else {
|
|
|
|
sb << "for writing";
|
|
|
|
}
|
|
|
|
} else if (flags & FileFd::Read) {
|
|
|
|
sb << "for reading";
|
|
|
|
} else {
|
|
|
|
sb << "for nothing";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FileFd::Truncate) {
|
|
|
|
sb << " with truncation";
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
if (flags & FileFd::Direct) {
|
|
|
|
sb << " for direct io";
|
|
|
|
}
|
|
|
|
if (flags & FileFd::WinStat) {
|
|
|
|
sb << " for stat";
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace detail {
|
|
|
|
class FileFdImpl {
|
|
|
|
public:
|
2021-12-10 11:07:55 +01:00
|
|
|
PollableFdInfo info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
FileFd::FileFd() = default;
|
2021-10-18 13:36:15 +02:00
|
|
|
FileFd::FileFd(FileFd &&) noexcept = default;
|
|
|
|
FileFd &FileFd::operator=(FileFd &&) noexcept = default;
|
2018-08-13 19:15:09 +02:00
|
|
|
FileFd::~FileFd() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
FileFd::FileFd(unique_ptr<detail::FileFdImpl> impl) : impl_(std::move(impl)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<FileFd> FileFd::open(CSlice filepath, int32 flags, int32 mode) {
|
2019-07-06 13:29:15 +02:00
|
|
|
if (flags & ~(Write | Read | Truncate | Create | Append | CreateNew | Direct | WinStat)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(PSLICE() << "File \"" << filepath << "\" has failed to be " << PrintFlags{flags});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & (Write | Read)) == 0) {
|
|
|
|
return Status::Error(PSLICE() << "File \"" << filepath << "\" can't be " << PrintFlags{flags});
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
int native_flags = 0;
|
|
|
|
|
|
|
|
if ((flags & Write) && (flags & Read)) {
|
|
|
|
native_flags |= O_RDWR;
|
|
|
|
} else if (flags & Write) {
|
|
|
|
native_flags |= O_WRONLY;
|
|
|
|
} else {
|
|
|
|
CHECK(flags & Read);
|
|
|
|
native_flags |= O_RDONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & Truncate) {
|
|
|
|
native_flags |= O_TRUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & Create) {
|
|
|
|
native_flags |= O_CREAT;
|
|
|
|
} else if (flags & CreateNew) {
|
|
|
|
native_flags |= O_CREAT;
|
|
|
|
native_flags |= O_EXCL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & Append) {
|
|
|
|
native_flags |= O_APPEND;
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
#if TD_LINUX
|
2019-07-21 20:07:07 +02:00
|
|
|
if (flags & Direct) {
|
2019-07-06 13:29:15 +02:00
|
|
|
native_flags |= O_DIRECT;
|
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
#endif
|
|
|
|
|
2023-03-06 14:59:18 +01:00
|
|
|
while (true) {
|
2023-02-13 12:40:36 +01:00
|
|
|
int native_fd =
|
|
|
|
detail::skip_eintr([&] { return ::open(filepath.c_str(), native_flags, static_cast<mode_t>(mode)); });
|
|
|
|
if (native_fd < 0) {
|
|
|
|
return OS_ERROR(PSLICE() << "File \"" << filepath << "\" can't be " << PrintFlags{flags});
|
|
|
|
}
|
|
|
|
// Avoid the use of low-numbered file descriptors, which can be used directly by some other functions
|
|
|
|
constexpr int MINIMUM_FILE_DESCRIPTOR = 3;
|
|
|
|
if (native_fd < MINIMUM_FILE_DESCRIPTOR) {
|
|
|
|
::close(native_fd);
|
|
|
|
LOG(ERROR) << "Receive " << native_fd << " as a file descriptor";
|
|
|
|
int dummy_fd = detail::skip_eintr([&] { return ::open("/dev/null", O_RDONLY, 0); });
|
|
|
|
if (dummy_fd < 0) {
|
|
|
|
return OS_ERROR("Can't open /dev/null");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return from_native_fd(NativeFd(native_fd));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
// TODO: support modes
|
|
|
|
auto r_filepath = to_wstring(filepath);
|
|
|
|
if (r_filepath.is_error()) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to convert file path \" << filepath << \" to UTF-16");
|
|
|
|
}
|
|
|
|
auto w_filepath = r_filepath.move_as_ok();
|
|
|
|
DWORD desired_access = 0;
|
|
|
|
if ((flags & Write) && (flags & Read)) {
|
|
|
|
desired_access |= GENERIC_READ | GENERIC_WRITE;
|
|
|
|
} else if (flags & Write) {
|
|
|
|
desired_access |= GENERIC_WRITE;
|
|
|
|
} else {
|
|
|
|
CHECK(flags & Read);
|
|
|
|
desired_access |= GENERIC_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: share mode
|
|
|
|
DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE;
|
|
|
|
|
2023-01-04 14:39:33 +01:00
|
|
|
DWORD native_flags = 0;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
DWORD creation_disposition = 0;
|
|
|
|
if (flags & Create) {
|
|
|
|
if (flags & Truncate) {
|
|
|
|
creation_disposition = CREATE_ALWAYS;
|
|
|
|
} else {
|
|
|
|
creation_disposition = OPEN_ALWAYS;
|
|
|
|
}
|
|
|
|
} else if (flags & CreateNew) {
|
|
|
|
creation_disposition = CREATE_NEW;
|
|
|
|
} else {
|
|
|
|
if (flags & Truncate) {
|
|
|
|
creation_disposition = TRUNCATE_EXISTING;
|
|
|
|
} else {
|
|
|
|
creation_disposition = OPEN_EXISTING;
|
|
|
|
}
|
2023-01-10 20:56:11 +01:00
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
2023-01-04 14:39:33 +01:00
|
|
|
native_flags |= FILE_FLAG_OPEN_REPARSE_POINT;
|
2023-01-10 20:56:11 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
if (flags & Direct) {
|
|
|
|
native_flags |= FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING;
|
|
|
|
}
|
|
|
|
if (flags & WinStat) {
|
|
|
|
native_flags |= FILE_FLAG_BACKUP_SEMANTICS;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
2019-07-06 13:29:15 +02:00
|
|
|
auto handle =
|
|
|
|
CreateFile(w_filepath.c_str(), desired_access, share_mode, nullptr, creation_disposition, native_flags, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
#else
|
2019-07-06 13:29:15 +02:00
|
|
|
CREATEFILE2_EXTENDED_PARAMETERS extended_parameters;
|
2019-07-22 06:01:51 +02:00
|
|
|
std::memset(&extended_parameters, 0, sizeof(extended_parameters));
|
2019-07-24 14:30:50 +02:00
|
|
|
extended_parameters.dwSize = sizeof(extended_parameters);
|
|
|
|
extended_parameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
|
2019-07-06 13:29:15 +02:00
|
|
|
extended_parameters.dwFileFlags = native_flags;
|
2020-12-23 16:08:34 +01:00
|
|
|
auto handle = td::CreateFile2FromAppW(w_filepath.c_str(), desired_access, share_mode, creation_disposition,
|
|
|
|
&extended_parameters);
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
return OS_ERROR(PSLICE() << "File \"" << filepath << "\" can't be " << PrintFlags{flags});
|
|
|
|
}
|
2020-01-02 18:18:55 +01:00
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
|
|
|
if (flags & Write) {
|
|
|
|
DWORD bytes_returned = 0;
|
|
|
|
DeviceIoControl(handle, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, &bytes_returned, nullptr);
|
|
|
|
}
|
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
auto native_fd = NativeFd(handle);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (flags & Append) {
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
offset.QuadPart = 0;
|
|
|
|
auto set_pointer_res = SetFilePointerEx(handle, offset, nullptr, FILE_END);
|
|
|
|
if (!set_pointer_res) {
|
2018-09-07 02:41:21 +02:00
|
|
|
return OS_ERROR(PSLICE() << "Failed to seek to the end of file \"" << filepath << "\"");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
return from_native_fd(std::move(native_fd));
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
FileFd FileFd::from_native_fd(NativeFd native_fd) {
|
2018-09-27 03:19:03 +02:00
|
|
|
auto impl = make_unique<detail::FileFdImpl>();
|
2021-12-10 11:07:55 +01:00
|
|
|
impl->info_.set_native_fd(std::move(native_fd));
|
|
|
|
impl->info_.add_flags(PollFlags::Write());
|
2018-08-13 19:15:09 +02:00
|
|
|
return FileFd(std::move(impl));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> FileFd::write(Slice slice) {
|
2018-08-13 19:15:09 +02:00
|
|
|
auto native_fd = get_native_fd().fd();
|
2018-09-11 16:13:53 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
auto bytes_written = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); });
|
|
|
|
bool success = bytes_written >= 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-08-13 19:15:09 +02:00
|
|
|
DWORD bytes_written = 0;
|
2018-09-11 16:13:53 +02:00
|
|
|
BOOL success = WriteFile(native_fd, slice.data(), narrow_cast<DWORD>(slice.size()), &bytes_written, nullptr);
|
|
|
|
#endif
|
|
|
|
if (success) {
|
2021-04-03 23:47:30 +02:00
|
|
|
auto result = narrow_cast<size_t>(bytes_written);
|
|
|
|
CHECK(result <= slice.size());
|
|
|
|
return result;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-10-07 01:47:50 +02:00
|
|
|
return OS_ERROR(PSLICE() << "Write to " << get_native_fd() << " has failed");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
Result<size_t> FileFd::writev(Span<IoSlice> slices) {
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
auto native_fd = get_native_fd().fd();
|
|
|
|
TRY_RESULT(slices_size, narrow_cast_safe<int>(slices.size()));
|
|
|
|
auto bytes_written = detail::skip_eintr([&] { return ::writev(native_fd, slices.begin(), slices_size); });
|
|
|
|
bool success = bytes_written >= 0;
|
|
|
|
if (success) {
|
2021-04-03 23:47:30 +02:00
|
|
|
auto result = narrow_cast<size_t>(bytes_written);
|
|
|
|
auto left = result;
|
|
|
|
for (const auto &slice : slices) {
|
|
|
|
if (left <= slice.iov_len) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
left -= slice.iov_len;
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
|
|
|
return OS_ERROR(PSLICE() << "Writev to " << get_native_fd() << " has failed");
|
|
|
|
#else
|
|
|
|
size_t res = 0;
|
2021-04-03 23:40:37 +02:00
|
|
|
for (const auto &slice : slices) {
|
|
|
|
if (slice.size() > std::numeric_limits<size_t>::max() - res) {
|
|
|
|
break;
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
TRY_RESULT(size, write(slice));
|
|
|
|
res += size;
|
2021-04-03 23:40:37 +02:00
|
|
|
if (size != slice.size()) {
|
|
|
|
CHECK(size < slice.size());
|
|
|
|
break;
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<size_t> FileFd::read(MutableSlice slice) {
|
2018-08-13 19:15:09 +02:00
|
|
|
auto native_fd = get_native_fd().fd();
|
2018-09-11 16:13:53 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
auto bytes_read = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); });
|
|
|
|
bool success = bytes_read >= 0;
|
2020-06-24 13:47:36 +02:00
|
|
|
if (!success) {
|
|
|
|
auto read_errno = errno;
|
|
|
|
if (read_errno == EAGAIN
|
|
|
|
#if EAGAIN != EWOULDBLOCK
|
|
|
|
|| read_errno == EWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
success = true;
|
|
|
|
bytes_read = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 20:57:47 +02:00
|
|
|
bool is_eof = success && narrow_cast<size_t>(bytes_read) < slice.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-08-13 19:15:09 +02:00
|
|
|
DWORD bytes_read = 0;
|
2018-09-11 16:13:53 +02:00
|
|
|
BOOL success = ReadFile(native_fd, slice.data(), narrow_cast<DWORD>(slice.size()), &bytes_read, nullptr);
|
|
|
|
bool is_eof = bytes_read == 0;
|
|
|
|
#endif
|
|
|
|
if (success) {
|
|
|
|
if (is_eof) {
|
2018-09-10 15:52:27 +02:00
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
}
|
2021-04-03 23:47:30 +02:00
|
|
|
auto result = narrow_cast<size_t>(bytes_read);
|
|
|
|
CHECK(result <= slice.size());
|
|
|
|
return result;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-10-07 01:47:50 +02:00
|
|
|
return OS_ERROR(PSLICE() << "Read from " << get_native_fd() << " has failed");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> FileFd::pwrite(Slice slice, int64 offset) {
|
|
|
|
if (offset < 0) {
|
|
|
|
return Status::Error("Offset must be non-negative");
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
auto native_fd = get_native_fd().fd();
|
2018-09-11 16:13:53 +02:00
|
|
|
#if TD_PORT_POSIX
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(offset_off_t, narrow_cast_safe<off_t>(offset));
|
2018-09-11 16:13:53 +02:00
|
|
|
auto bytes_written =
|
|
|
|
detail::skip_eintr([&] { return ::pwrite(native_fd, slice.begin(), slice.size(), offset_off_t); });
|
|
|
|
bool success = bytes_written >= 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
DWORD bytes_written = 0;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
std::memset(&overlapped, 0, sizeof(overlapped));
|
|
|
|
overlapped.Offset = static_cast<DWORD>(offset);
|
|
|
|
overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
|
2018-09-11 16:13:53 +02:00
|
|
|
BOOL success = WriteFile(native_fd, slice.data(), narrow_cast<DWORD>(slice.size()), &bytes_written, &overlapped);
|
|
|
|
#endif
|
|
|
|
if (success) {
|
2021-04-03 23:47:30 +02:00
|
|
|
auto result = narrow_cast<size_t>(bytes_written);
|
|
|
|
CHECK(result <= slice.size());
|
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-10-07 01:47:50 +02:00
|
|
|
return OS_ERROR(PSLICE() << "Pwrite to " << get_native_fd() << " at offset " << offset << " has failed");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
Result<size_t> FileFd::pread(MutableSlice slice, int64 offset) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (offset < 0) {
|
|
|
|
return Status::Error("Offset must be non-negative");
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
auto native_fd = get_native_fd().fd();
|
2018-09-11 16:13:53 +02:00
|
|
|
#if TD_PORT_POSIX
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(offset_off_t, narrow_cast_safe<off_t>(offset));
|
2018-09-11 16:13:53 +02:00
|
|
|
auto bytes_read = detail::skip_eintr([&] { return ::pread(native_fd, slice.begin(), slice.size(), offset_off_t); });
|
|
|
|
bool success = bytes_read >= 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
DWORD bytes_read = 0;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
std::memset(&overlapped, 0, sizeof(overlapped));
|
|
|
|
overlapped.Offset = static_cast<DWORD>(offset);
|
|
|
|
overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
|
2018-09-11 16:13:53 +02:00
|
|
|
BOOL success = ReadFile(native_fd, slice.data(), narrow_cast<DWORD>(slice.size()), &bytes_read, &overlapped);
|
|
|
|
#endif
|
|
|
|
if (success) {
|
2021-04-03 23:47:30 +02:00
|
|
|
auto result = narrow_cast<size_t>(bytes_read);
|
|
|
|
CHECK(result <= slice.size());
|
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-10-07 01:47:50 +02:00
|
|
|
return OS_ERROR(PSLICE() << "Pread from " << get_native_fd() << " at offset " << offset << " has failed");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 20:18:06 +02:00
|
|
|
static std::mutex in_process_lock_mutex;
|
2022-03-11 19:38:48 +01:00
|
|
|
static FlatHashSet<string> locked_files;
|
2020-10-11 00:59:27 +02:00
|
|
|
static ExitGuard exit_guard;
|
2018-10-08 20:18:06 +02:00
|
|
|
|
2018-10-30 12:21:36 +01:00
|
|
|
static Status create_local_lock(const string &path, int32 &max_tries) {
|
2018-10-30 09:35:50 +01:00
|
|
|
while (true) {
|
|
|
|
{ // mutex lock scope
|
|
|
|
std::lock_guard<std::mutex> lock(in_process_lock_mutex);
|
2022-05-01 22:03:06 +02:00
|
|
|
if (!path.empty() && locked_files.count(path) == 0) {
|
2018-10-30 09:35:50 +01:00
|
|
|
VLOG(fd) << "Lock file \"" << path << '"';
|
|
|
|
locked_files.insert(path);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--max_tries <= 0) {
|
2018-10-30 12:21:36 +01:00
|
|
|
return Status::Error(
|
|
|
|
0, PSLICE() << "Can't lock file \"" << path << "\", because it is already in use by current program");
|
2018-10-30 09:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
usleep_for(100000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:37:21 +02:00
|
|
|
Status FileFd::lock(LockFlags flags, const string &path, int32 max_tries) {
|
2018-10-30 09:47:58 +01:00
|
|
|
if (max_tries <= 0) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return Status::Error("Can't lock file: wrong max_tries");
|
2018-10-30 09:47:58 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 20:18:06 +02:00
|
|
|
bool need_local_unlock = false;
|
|
|
|
if (!path.empty()) {
|
|
|
|
if (flags == LockFlags::Unlock) {
|
|
|
|
need_local_unlock = true;
|
|
|
|
} else if (flags == LockFlags::Read) {
|
|
|
|
LOG(FATAL) << "Local locking in Read mode is unsupported";
|
|
|
|
} else {
|
|
|
|
CHECK(flags == LockFlags::Write);
|
|
|
|
VLOG(fd) << "Trying to lock file \"" << path << '"';
|
2018-10-30 09:35:50 +01:00
|
|
|
TRY_STATUS(create_local_lock(path, max_tries));
|
|
|
|
need_local_unlock = true;
|
2018-10-08 20:18:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
|
|
|
if (need_local_unlock) {
|
|
|
|
remove_local_lock(path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
auto native_fd = get_native_fd().fd();
|
|
|
|
#elif TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
auto native_fd = get_native_fd().fd();
|
2018-08-13 19:15:09 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
struct flock lock;
|
|
|
|
std::memset(&lock, 0, sizeof(lock));
|
|
|
|
|
|
|
|
lock.l_type = static_cast<short>([&] {
|
|
|
|
switch (flags) {
|
|
|
|
case LockFlags::Read:
|
|
|
|
return F_RDLCK;
|
|
|
|
case LockFlags::Write:
|
|
|
|
return F_WRLCK;
|
|
|
|
case LockFlags::Unlock:
|
|
|
|
return F_UNLCK;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return F_UNLCK;
|
|
|
|
}
|
|
|
|
}());
|
|
|
|
|
|
|
|
lock.l_whence = SEEK_SET;
|
2018-08-13 19:15:09 +02:00
|
|
|
if (fcntl(native_fd, F_SETLK, &lock) == -1) {
|
2018-05-10 20:39:01 +02:00
|
|
|
if (errno == EAGAIN) {
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
std::memset(&overlapped, 0, sizeof(overlapped));
|
|
|
|
|
|
|
|
BOOL result;
|
|
|
|
if (flags == LockFlags::Unlock) {
|
2018-08-13 19:15:09 +02:00
|
|
|
result = UnlockFileEx(native_fd, 0, MAXDWORD, MAXDWORD, &overlapped);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
DWORD dw_flags = LOCKFILE_FAIL_IMMEDIATELY;
|
|
|
|
if (flags == LockFlags::Write) {
|
|
|
|
dw_flags |= LOCKFILE_EXCLUSIVE_LOCK;
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
result = LockFileEx(native_fd, dw_flags, 0, MAXDWORD, MAXDWORD, &overlapped);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
2018-05-10 20:39:01 +02:00
|
|
|
if (GetLastError() == ERROR_LOCK_VIOLATION) {
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
2018-05-10 20:39:01 +02:00
|
|
|
if (--max_tries > 0) {
|
|
|
|
usleep_for(100000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-08 20:18:06 +02:00
|
|
|
return OS_ERROR(PSLICE() << "Can't lock file \"" << path
|
|
|
|
<< "\", because it is already in use; check for another program instance running");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OS_ERROR("Can't lock file");
|
|
|
|
}
|
2018-10-08 20:18:06 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags == LockFlags::Write) {
|
|
|
|
need_local_unlock = false;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileFd::remove_local_lock(const string &path) {
|
2020-10-11 00:59:27 +02:00
|
|
|
if (path.empty() || ExitGuard::is_exited()) {
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-10-11 00:59:27 +02:00
|
|
|
VLOG(fd) << "Unlock file \"" << path << '"';
|
|
|
|
std::unique_lock<std::mutex> lock(in_process_lock_mutex);
|
|
|
|
auto erased_count = locked_files.erase(path);
|
2022-03-11 19:38:48 +01:00
|
|
|
CHECK(erased_count > 0 || path.empty() || ExitGuard::is_exited());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileFd::close() {
|
2018-08-13 19:15:09 +02:00
|
|
|
impl_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileFd::empty() const {
|
2018-08-13 19:15:09 +02:00
|
|
|
return !impl_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
const NativeFd &FileFd::get_native_fd() const {
|
|
|
|
return get_poll_info().native_fd();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
NativeFd FileFd::move_as_native_fd() {
|
|
|
|
auto res = get_poll_info().move_as_native_fd();
|
|
|
|
impl_.reset();
|
|
|
|
return res;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 00:41:57 +01:00
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
uint64 filetime_to_unix_time_nsec(LONGLONG filetime) {
|
|
|
|
const auto FILETIME_UNIX_TIME_DIFF = 116444736000000000ll;
|
|
|
|
return static_cast<uint64>((filetime - FILETIME_UNIX_TIME_DIFF) * 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FileSize {
|
|
|
|
int64 size_;
|
|
|
|
int64 real_size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Result<FileSize> get_file_size(const FileFd &file_fd) {
|
|
|
|
FILE_STANDARD_INFO standard_info;
|
|
|
|
if (!GetFileInformationByHandleEx(file_fd.get_native_fd().fd(), FileStandardInfo, &standard_info,
|
|
|
|
sizeof(standard_info))) {
|
|
|
|
return OS_ERROR("Get FileStandardInfo failed");
|
|
|
|
}
|
|
|
|
FileSize res;
|
|
|
|
res.size_ = standard_info.EndOfFile.QuadPart;
|
|
|
|
res.real_size_ = standard_info.AllocationSize.QuadPart;
|
|
|
|
|
2020-01-03 02:08:22 +01:00
|
|
|
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_;
|
2020-01-03 00:41:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
#endif
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
Result<int64> FileFd::get_size() const {
|
2020-01-03 00:41:57 +01:00
|
|
|
#if TD_PORT_POSIX
|
2019-06-19 16:31:25 +02:00
|
|
|
TRY_RESULT(s, stat());
|
2020-01-03 00:43:49 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
2020-01-03 00:41:57 +01:00
|
|
|
TRY_RESULT(s, get_file_size(*this));
|
|
|
|
#endif
|
2019-06-19 16:31:25 +02:00
|
|
|
return s.size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-01-02 23:01:39 +01:00
|
|
|
Result<int64> FileFd::get_real_size() const {
|
2020-01-03 00:41:57 +01:00
|
|
|
#if TD_PORT_POSIX
|
2020-01-02 23:01:39 +01:00
|
|
|
TRY_RESULT(s, stat());
|
2020-01-03 00:43:49 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
2020-01-03 00:41:57 +01:00
|
|
|
TRY_RESULT(s, get_file_size(*this));
|
|
|
|
#endif
|
2020-01-02 23:01:39 +01:00
|
|
|
return s.real_size_;
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
Result<Stat> FileFd::stat() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!empty());
|
|
|
|
#if TD_PORT_POSIX
|
2018-08-13 19:15:09 +02:00
|
|
|
return detail::fstat(get_native_fd().fd());
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
Stat res;
|
|
|
|
|
|
|
|
FILE_BASIC_INFO basic_info;
|
2018-09-11 15:27:20 +02:00
|
|
|
auto status = GetFileInformationByHandleEx(get_native_fd().fd(), FileBasicInfo, &basic_info, sizeof(basic_info));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!status) {
|
2019-06-20 04:09:03 +02:00
|
|
|
return OS_ERROR("Get FileBasicInfo failed");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-26 14:01:40 +01:00
|
|
|
res.atime_nsec_ = filetime_to_unix_time_nsec(basic_info.LastAccessTime.QuadPart);
|
|
|
|
res.mtime_nsec_ = filetime_to_unix_time_nsec(basic_info.LastWriteTime.QuadPart);
|
2018-12-31 20:04:05 +01:00
|
|
|
res.is_dir_ = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
2023-01-04 14:39:33 +01:00
|
|
|
if ((basic_info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
|
|
|
|
FILE_ATTRIBUTE_TAG_INFO tag_info;
|
|
|
|
status = GetFileInformationByHandleEx(get_native_fd().fd(), FileAttributeTagInfo, &tag_info, sizeof(tag_info));
|
|
|
|
if (!status) {
|
|
|
|
return OS_ERROR("Get FileAttributeTagInfo failed");
|
|
|
|
}
|
|
|
|
res.is_reg_ = false;
|
|
|
|
res.is_symbolic_link_ =
|
|
|
|
(tag_info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0 && tag_info.ReparseTag == IO_REPARSE_TAG_SYMLINK;
|
|
|
|
} else {
|
|
|
|
res.is_reg_ = !res.is_dir_;
|
|
|
|
res.is_symbolic_link_ = false;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-01-03 00:41:57 +01:00
|
|
|
TRY_RESULT(file_size, get_file_size(*this));
|
|
|
|
res.size_ = file_size.size_;
|
|
|
|
res.real_size_ = file_size.real_size_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileFd::sync() {
|
|
|
|
CHECK(!empty());
|
|
|
|
#if TD_PORT_POSIX
|
2019-02-22 02:20:23 +01:00
|
|
|
#if TD_DARWIN
|
2019-07-03 16:51:00 +02:00
|
|
|
if (detail::skip_eintr([&] { return fcntl(get_native_fd().fd(), F_FULLFSYNC); }) == -1) {
|
2019-02-22 02:20:23 +01:00
|
|
|
#else
|
2019-07-03 16:51:00 +02:00
|
|
|
if (detail::skip_eintr([&] { return fsync(get_native_fd().fd()); }) != 0) {
|
2019-02-22 02:20:23 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
if (FlushFileBuffers(get_native_fd().fd()) == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
return OS_ERROR("Sync failed");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-02-10 12:17:22 +01:00
|
|
|
Status FileFd::sync_barrier() {
|
|
|
|
CHECK(!empty());
|
|
|
|
#if TD_DARWIN && defined(F_BARRIERFSYNC)
|
2023-02-10 13:17:07 +01:00
|
|
|
if (detail::skip_eintr([&] { return fcntl(get_native_fd().fd(), F_BARRIERFSYNC); }) != -1) {
|
2023-02-10 12:17:22 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return sync();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status FileFd::seek(int64 position) {
|
|
|
|
CHECK(!empty());
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
TRY_RESULT(position_off_t, narrow_cast_safe<off_t>(position));
|
2018-09-10 01:16:42 +02:00
|
|
|
if (detail::skip_eintr([&] { return ::lseek(get_native_fd().fd(), position_off_t, SEEK_SET); }) < 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
offset.QuadPart = position;
|
2018-09-11 15:27:20 +02:00
|
|
|
if (SetFilePointerEx(get_native_fd().fd(), offset, nullptr, FILE_BEGIN) == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
return OS_ERROR("Seek failed");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileFd::truncate_to_current_position(int64 current_position) {
|
|
|
|
CHECK(!empty());
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
TRY_RESULT(current_position_off_t, narrow_cast_safe<off_t>(current_position));
|
2018-09-10 01:16:42 +02:00
|
|
|
if (detail::skip_eintr([&] { return ::ftruncate(get_native_fd().fd(), current_position_off_t); }) < 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
if (SetEndOfFile(get_native_fd().fd()) == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
return OS_ERROR("Truncate failed");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
PollableFdInfo &FileFd::get_poll_info() {
|
2018-09-07 02:41:21 +02:00
|
|
|
CHECK(!empty());
|
2021-12-10 11:07:55 +01:00
|
|
|
return impl_->info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
const PollableFdInfo &FileFd::get_poll_info() const {
|
2018-09-07 02:41:21 +02:00
|
|
|
CHECK(!empty());
|
2021-12-10 11:07:55 +01:00
|
|
|
return impl_->info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
} // namespace td
|