2022-04-29 02:35:47 +00:00
|
|
|
// This file Copyright © 2005-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-04-29 02:35:47 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef __TRANSMISSION__
|
|
|
|
#error only libtransmission should #include this header.
|
|
|
|
#endif
|
|
|
|
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstddef> // for size_t
|
|
|
|
#include <cstdint> // for uintX_t
|
2022-04-29 02:35:47 +00:00
|
|
|
#include <optional>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <string_view>
|
2022-05-01 04:30:49 +00:00
|
|
|
#include <utility>
|
2022-04-29 02:35:47 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "file.h" // tr_sys_file_t
|
|
|
|
#include "lru-cache.h"
|
|
|
|
|
|
|
|
struct tr_session;
|
|
|
|
|
|
|
|
// A pool of open files that are cached while reading / writing torrents' data
|
|
|
|
class tr_open_files
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
[[nodiscard]] std::optional<tr_sys_file_t> get(tr_torrent_id_t tor_id, tr_file_index_t file_num, bool writable);
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<tr_sys_file_t> get(
|
|
|
|
tr_torrent_id_t tor_id,
|
|
|
|
tr_file_index_t file_num,
|
|
|
|
bool writable,
|
|
|
|
std::string_view filename,
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_preallocation_mode allocation,
|
2022-04-29 02:35:47 +00:00
|
|
|
uint64_t file_size);
|
|
|
|
|
|
|
|
void closeAll();
|
|
|
|
void closeTorrent(tr_torrent_id_t tor_id);
|
|
|
|
void closeFile(tr_torrent_id_t tor_id, tr_file_index_t file_num);
|
|
|
|
|
|
|
|
private:
|
|
|
|
using Key = std::pair<tr_torrent_id_t, tr_file_index_t>;
|
|
|
|
|
|
|
|
[[nodiscard]] static Key makeKey(tr_torrent_id_t tor_id, tr_file_index_t file_num) noexcept
|
|
|
|
{
|
|
|
|
return std::make_pair(tor_id, file_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Val
|
|
|
|
{
|
2022-05-01 04:30:49 +00:00
|
|
|
Val() noexcept = default;
|
2022-04-29 02:35:47 +00:00
|
|
|
Val(Val const&) = delete;
|
2022-05-01 04:30:49 +00:00
|
|
|
Val& operator=(Val const&) = delete;
|
2022-05-01 18:06:32 +00:00
|
|
|
Val(Val&& that) noexcept
|
|
|
|
{
|
2022-05-01 04:30:49 +00:00
|
|
|
*this = std::move(that);
|
|
|
|
}
|
2022-05-01 18:06:32 +00:00
|
|
|
Val& operator=(Val&& that) noexcept
|
|
|
|
{
|
2022-05-01 04:30:49 +00:00
|
|
|
std::swap(this->fd_, that.fd_);
|
|
|
|
std::swap(this->writable_, that.writable_);
|
|
|
|
return *this;
|
|
|
|
}
|
2022-04-29 02:35:47 +00:00
|
|
|
~Val();
|
|
|
|
|
|
|
|
tr_sys_file_t fd_ = TR_BAD_SYS_FILE;
|
|
|
|
bool writable_ = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr size_t MaxOpenFiles = 32;
|
|
|
|
tr_lru_cache<Key, Val, MaxOpenFiles> pool_;
|
|
|
|
};
|