2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-04-29 02:35:47 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <algorithm> // std::min
|
2022-04-29 02:35:47 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstdint> // uint8_t, uint64_t
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/error-types.h"
|
|
|
|
#include "libtransmission/error.h"
|
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/open-files.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/tr-strbuf.h"
|
|
|
|
#include "libtransmission/utils.h" // _()
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] auto is_open(tr_sys_file_t fd) noexcept
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
return fd != TR_BAD_SYS_FILE;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
bool preallocate_file_sparse(tr_sys_file_t fd, uint64_t length, tr_error* error)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-11-12 03:09:23 +00:00
|
|
|
if (length == 0U)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto local_error = tr_error{};
|
|
|
|
|
|
|
|
if (tr_sys_file_preallocate(fd, length, TR_SYS_FILE_PREALLOC_SPARSE, &local_error))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_logAddDebug(fmt::format("Fast preallocation failed: {} ({})", local_error.message(), local_error.code()));
|
2022-04-29 02:35:47 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (!TR_ERROR_IS_ENOSPC(local_error.code()))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-11-12 03:09:23 +00:00
|
|
|
static char constexpr Zero = '\0';
|
2022-04-29 02:35:47 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
local_error = {};
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
/* fallback: the old-style seek-and-write */
|
2023-11-12 03:09:23 +00:00
|
|
|
if (tr_sys_file_write_at(fd, &Zero, 1, length - 1, nullptr, &local_error) &&
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_sys_file_truncate(fd, length, &local_error))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_logAddDebug(fmt::format("Fast prellocation fallback failed: {} ({})", local_error.message(), local_error.code()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
*error = std::move(local_error);
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
bool preallocate_file_full(tr_sys_file_t fd, uint64_t length, tr_error* error)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-11-12 03:09:23 +00:00
|
|
|
if (length == 0U)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
auto local_error = tr_error{};
|
|
|
|
|
|
|
|
if (tr_sys_file_preallocate(fd, length, 0, &local_error))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_logAddDebug(fmt::format("Full preallocation failed: {} ({})", local_error.message(), local_error.code()));
|
2022-04-29 02:35:47 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (!TR_ERROR_IS_ENOSPC(local_error.code()))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
auto buf = std::array<uint8_t, 4096>{};
|
|
|
|
bool success = true;
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
local_error = {};
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
/* fallback: the old-fashioned way */
|
|
|
|
while (success && length > 0)
|
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
uint64_t const this_pass = std::min(length, uint64_t{ std::size(buf) });
|
2022-04-29 02:35:47 +00:00
|
|
|
uint64_t bytes_written = 0;
|
2023-11-04 16:39:41 +00:00
|
|
|
success = tr_sys_file_write(fd, std::data(buf), this_pass, &bytes_written, &local_error);
|
2022-04-29 02:35:47 +00:00
|
|
|
length -= bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_logAddDebug(fmt::format("Full preallocation fallback failed: {} ({})", local_error.message(), local_error.code()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
*error = std::move(local_error);
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // unnamed namespace
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-04-29 02:35:47 +00:00
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
std::optional<tr_sys_file_t> tr_open_files::get(tr_torrent_id_t tor_id, tr_file_index_t file_num, bool writable)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-11-28 16:51:13 +00:00
|
|
|
if (auto* const found = pool_.get(make_key(tor_id, file_num)); found != nullptr)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-11-28 16:51:13 +00:00
|
|
|
if (writable && !found->writable_)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
return found->fd_;
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
std::optional<tr_sys_file_t> tr_open_files::get(
|
2022-04-29 02:35:47 +00:00
|
|
|
tr_torrent_id_t tor_id,
|
|
|
|
tr_file_index_t file_num,
|
|
|
|
bool writable,
|
|
|
|
std::string_view filename_in,
|
2023-12-02 20:16:36 +00:00
|
|
|
Preallocation allocation,
|
2022-04-29 02:35:47 +00:00
|
|
|
uint64_t file_size)
|
|
|
|
{
|
|
|
|
// is there already an entry
|
2023-05-06 04:11:05 +00:00
|
|
|
auto key = make_key(tor_id, file_num);
|
2023-11-28 16:51:13 +00:00
|
|
|
if (auto* const found = pool_.get(key); found != nullptr)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-11-28 16:51:13 +00:00
|
|
|
if (!writable || found->writable_)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-11-28 16:51:13 +00:00
|
|
|
return found->fd_;
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool_.erase(key); // close so we can re-open as writable
|
|
|
|
}
|
|
|
|
|
|
|
|
// create subfolders, if any
|
|
|
|
auto const filename = tr_pathbuf{ filename_in };
|
2023-11-04 16:39:41 +00:00
|
|
|
auto error = tr_error{};
|
2022-04-29 02:35:47 +00:00
|
|
|
if (writable)
|
|
|
|
{
|
2022-05-22 01:17:00 +00:00
|
|
|
auto dir = tr_pathbuf{ filename.sv() };
|
|
|
|
dir.popdir();
|
|
|
|
if (!tr_sys_dir_create(dir, TR_SYS_DIR_CREATE_PARENTS, 0777, &error))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't create '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", dir),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error.message()),
|
|
|
|
fmt::arg("error_code", error.code())));
|
2022-04-29 02:35:47 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
auto const info = tr_sys_path_get_info(filename);
|
|
|
|
bool const already_existed = info && info->isFile();
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
// we need write permissions to resize the file
|
2022-08-02 17:41:04 +00:00
|
|
|
bool const resize_needed = already_existed && (file_size < info->size);
|
2022-04-29 02:35:47 +00:00
|
|
|
writable |= resize_needed;
|
|
|
|
|
|
|
|
// open the file
|
|
|
|
int flags = writable ? (TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE) : 0;
|
|
|
|
flags |= TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL;
|
|
|
|
auto const fd = tr_sys_file_open(filename, flags, 0666, &error);
|
2023-05-06 04:11:05 +00:00
|
|
|
if (!is_open(fd))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't open '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", filename),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error.message()),
|
|
|
|
fmt::arg("error_code", error.code())));
|
2022-04-29 02:35:47 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-12-02 20:16:36 +00:00
|
|
|
if (writable && !already_existed && allocation != Preallocation::None)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
bool success = false;
|
2023-11-28 16:51:13 +00:00
|
|
|
char const* type = nullptr;
|
2022-04-29 02:35:47 +00:00
|
|
|
|
2023-12-02 20:16:36 +00:00
|
|
|
if (allocation == Preallocation::Full)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
success = preallocate_file_full(fd, file_size, &error);
|
2023-11-28 16:51:13 +00:00
|
|
|
type = "full";
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
2023-12-02 20:16:36 +00:00
|
|
|
else if (allocation == Preallocation::Sparse)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
success = preallocate_file_sparse(fd, file_size, &error);
|
2023-11-28 16:51:13 +00:00
|
|
|
type = "sparse";
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
TR_ASSERT(type != nullptr);
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't preallocate '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", filename),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error.message()),
|
|
|
|
fmt::arg("error_code", error.code())));
|
2022-04-29 02:35:47 +00:00
|
|
|
tr_sys_file_close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_logAddDebug(fmt::format("Preallocated file '{}' ({}, size: {})", filename, type, file_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file already exists and it's too large, truncate it.
|
|
|
|
// This is a fringe case that happens if a torrent's been updated
|
|
|
|
// and one of the updated torrent's files is smaller.
|
|
|
|
// https://trac.transmissionbt.com/ticket/2228
|
|
|
|
// https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
|
|
|
|
if (resize_needed && !tr_sys_file_truncate(fd, file_size, &error))
|
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't truncate '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", filename),
|
2023-11-04 16:39:41 +00:00
|
|
|
fmt::arg("error", error.message()),
|
|
|
|
fmt::arg("error_code", error.code())));
|
2022-04-29 02:35:47 +00:00
|
|
|
tr_sys_file_close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache it
|
2023-11-28 16:51:13 +00:00
|
|
|
auto& entry = pool_.add(std::move(key));
|
2022-04-29 02:35:47 +00:00
|
|
|
entry.fd_ = fd;
|
|
|
|
entry.writable_ = writable;
|
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
return fd;
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_open_files::close_all()
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
pool_.clear();
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_open_files::close_torrent(tr_torrent_id_t tor_id)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2024-02-13 16:42:19 +00:00
|
|
|
pool_.erase_if([&tor_id](Key const& key, Val const& /*unused*/) { return key.first == tor_id; });
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_open_files::close_file(tr_torrent_id_t tor_id, tr_file_index_t file_num)
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
pool_.erase(make_key(tor_id, file_num));
|
2022-04-29 02:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_open_files::Val::~Val()
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
if (is_open(fd_))
|
2022-04-29 02:35:47 +00:00
|
|
|
{
|
|
|
|
tr_sys_file_close(fd_);
|
|
|
|
}
|
|
|
|
}
|