2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2022-2023 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-12 15:00:02 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-05-20 04:07:17 +00:00
|
|
|
#include <algorithm> // std::sort()
|
2022-04-25 01:49:52 +00:00
|
|
|
#include <cstddef>
|
2022-04-15 23:39:04 +00:00
|
|
|
#include <cstdint> // uint64_t
|
|
|
|
#include <functional>
|
2022-05-20 04:07:17 +00:00
|
|
|
#include <iterator>
|
2022-04-12 15:00:02 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <utility>
|
2022-04-12 15:00:02 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-04-12 15:00:02 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/tr-macros.h"
|
|
|
|
#include "libtransmission/tr-strbuf.h"
|
2022-04-12 15:00:02 +00:00
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
struct tr_error;
|
|
|
|
|
2022-04-12 15:00:02 +00:00
|
|
|
/**
|
2022-04-15 23:39:04 +00:00
|
|
|
* A simple collection of files & utils for finding them, moving them, etc.
|
2022-04-12 15:00:02 +00:00
|
|
|
*/
|
2022-04-15 23:39:04 +00:00
|
|
|
struct tr_torrent_files
|
2022-04-12 15:00:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-12-23 21:21:40 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool empty() const noexcept
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
|
|
|
return std::empty(files_);
|
|
|
|
}
|
|
|
|
|
2022-12-23 21:21:40 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 size_t fileCount() const noexcept
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
|
|
|
return std::size(files_);
|
|
|
|
}
|
|
|
|
|
2022-12-23 21:21:40 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 uint64_t fileSize(tr_file_index_t file_index) const
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
|
|
|
return files_.at(file_index).size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto totalSize() const noexcept
|
|
|
|
{
|
|
|
|
return total_size_;
|
|
|
|
}
|
|
|
|
|
2022-12-23 21:21:40 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 std::string const& path(tr_file_index_t file_index) const
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
|
|
|
return files_.at(file_index).path_;
|
|
|
|
}
|
2022-04-12 15:00:02 +00:00
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
void setPath(tr_file_index_t file_index, std::string_view path)
|
|
|
|
{
|
|
|
|
files_.at(file_index).setPath(path);
|
|
|
|
}
|
2022-04-12 15:00:02 +00:00
|
|
|
|
2022-05-24 04:05:16 +00:00
|
|
|
void insertSubpathPrefix(std::string_view path)
|
|
|
|
{
|
|
|
|
auto const buf = tr_pathbuf{ path, '/' };
|
|
|
|
|
|
|
|
for (auto& file : files_)
|
|
|
|
{
|
|
|
|
file.path_.insert(0, buf.sv());
|
|
|
|
file.path_.shrink_to_fit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
void reserve(size_t n_files)
|
|
|
|
{
|
|
|
|
files_.reserve(n_files);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shrinkToFit()
|
|
|
|
{
|
|
|
|
files_.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2023-01-02 17:34:36 +00:00
|
|
|
TR_CONSTEXPR20 void clear() noexcept
|
2022-04-15 23:39:04 +00:00
|
|
|
{
|
|
|
|
files_.clear();
|
|
|
|
total_size_ = uint64_t{};
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
[[nodiscard]] auto sortedByPath() const
|
2022-05-20 04:07:17 +00:00
|
|
|
{
|
|
|
|
auto ret = std::vector<std::pair<std::string /*path*/, uint64_t /*size*/>>{};
|
|
|
|
ret.reserve(std::size(files_));
|
|
|
|
std::transform(
|
|
|
|
std::begin(files_),
|
|
|
|
std::end(files_),
|
|
|
|
std::back_inserter(ret),
|
|
|
|
[](auto const& in) { return std::make_pair(in.path_, in.size_); });
|
|
|
|
|
|
|
|
std::sort(std::begin(ret), std::end(ret), [](auto const& lhs, auto const& rhs) { return lhs.first < rhs.first; });
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
tr_file_index_t add(std::string_view path, uint64_t file_size)
|
|
|
|
{
|
|
|
|
auto const ret = static_cast<tr_file_index_t>(std::size(files_));
|
|
|
|
files_.emplace_back(path, file_size);
|
|
|
|
total_size_ += file_size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool move(
|
2022-08-03 06:15:37 +00:00
|
|
|
std::string_view old_parent_in,
|
|
|
|
std::string_view parent_in,
|
2022-04-15 23:39:04 +00:00
|
|
|
double volatile* setme_progress,
|
2022-08-03 06:15:37 +00:00
|
|
|
std::string_view parent_name = "",
|
2022-04-15 23:39:04 +00:00
|
|
|
tr_error** error = nullptr) const;
|
|
|
|
|
|
|
|
using FileFunc = std::function<void(char const* filename)>;
|
2022-08-03 06:15:37 +00:00
|
|
|
void remove(std::string_view parent_in, std::string_view tmpdir_prefix, FileFunc const& func) const;
|
2022-04-12 15:00:02 +00:00
|
|
|
|
|
|
|
struct FoundFile : public tr_sys_path_info
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FoundFile(tr_sys_path_info info, tr_pathbuf&& filename_in, size_t base_len_in)
|
|
|
|
: tr_sys_path_info{ info }
|
|
|
|
, filename_{ std::move(filename_in) }
|
|
|
|
, base_len_{ base_len_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& filename() const noexcept
|
|
|
|
{
|
|
|
|
// /home/foo/Downloads/torrent/01-file-one.txt
|
|
|
|
return filename_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto base() const noexcept
|
|
|
|
{
|
|
|
|
// /home/foo/Downloads
|
|
|
|
return filename_.sv().substr(0, base_len_);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto subpath() const noexcept
|
|
|
|
{
|
|
|
|
// torrent/01-file-one.txt
|
|
|
|
return filename_.sv().substr(base_len_ + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
tr_pathbuf filename_;
|
|
|
|
size_t base_len_;
|
|
|
|
};
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
[[nodiscard]] std::optional<FoundFile> find(tr_file_index_t file, std::string_view const* paths, size_t n_paths) const;
|
|
|
|
[[nodiscard]] bool hasAnyLocalData(std::string_view const* paths, size_t n_paths) const;
|
2022-04-12 15:00:02 +00:00
|
|
|
|
2022-05-23 22:53:26 +00:00
|
|
|
static void makeSubpathPortable(std::string_view path, tr_pathbuf& append_me);
|
|
|
|
|
|
|
|
[[nodiscard]] static auto makeSubpathPortable(std::string_view path)
|
|
|
|
{
|
|
|
|
auto tmp = tr_pathbuf{};
|
|
|
|
makeSubpathPortable(path, tmp);
|
|
|
|
return std::string{ tmp.sv() };
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] static bool isSubpathPortable(std::string_view path)
|
|
|
|
{
|
|
|
|
return makeSubpathPortable(path) == path;
|
|
|
|
}
|
|
|
|
|
2022-04-12 15:00:02 +00:00
|
|
|
static constexpr std::string_view PartialFileSuffix = ".part";
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct file_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void setPath(std::string_view subpath)
|
|
|
|
{
|
2022-05-24 04:05:16 +00:00
|
|
|
if (path_ != subpath)
|
|
|
|
{
|
|
|
|
path_ = subpath;
|
|
|
|
path_.shrink_to_fit();
|
|
|
|
}
|
2022-04-12 15:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file_t(std::string_view path, uint64_t size)
|
|
|
|
: path_{ path }
|
|
|
|
, size_{ size }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path_;
|
|
|
|
uint64_t size_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<file_t> files_;
|
2022-04-18 04:21:18 +00:00
|
|
|
uint64_t total_size_ = 0;
|
2022-04-12 15:00:02 +00:00
|
|
|
};
|