2023-11-01 21:11:11 +00:00
|
|
|
|
// This file Copyright © 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-15 23:39:04 +00:00
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
2022-07-26 02:45:54 +00:00
|
|
|
|
#include <algorithm> // std::find()
|
2023-07-08 15:24:03 +00:00
|
|
|
|
#include <array>
|
2023-11-21 15:02:03 +00:00
|
|
|
|
#include <cstddef>
|
2022-09-21 23:34:18 +00:00
|
|
|
|
#include <cctype>
|
2022-07-26 02:45:54 +00:00
|
|
|
|
#include <functional>
|
2022-05-27 01:29:10 +00:00
|
|
|
|
#include <iterator>
|
2022-04-15 23:39:04 +00:00
|
|
|
|
#include <optional>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
|
#include <utility>
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
|
#include <fmt/core.h>
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
|
#include "libtransmission/transmission.h"
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
|
#include "libtransmission/file.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
|
#include "libtransmission/log.h"
|
|
|
|
|
#include "libtransmission/torrent-files.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
|
#include "libtransmission/tr-strbuf.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
|
#include "libtransmission/utils.h"
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using file_func_t = std::function<void(char const* filename)>;
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
bool is_folder(std::string_view path)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
|
auto const info = tr_sys_path_get_info(path);
|
|
|
|
|
return info && info->isFolder();
|
2022-04-15 23:39:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
bool is_empty_folder(char const* path)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
if (!is_folder(path))
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto const odir = tr_sys_dir_open(path); odir != TR_BAD_SYS_DIR)
|
|
|
|
|
{
|
|
|
|
|
char const* name_cstr = nullptr;
|
|
|
|
|
while ((name_cstr = tr_sys_dir_read_name(odir)) != nullptr)
|
|
|
|
|
{
|
|
|
|
|
auto const name = std::string_view{ name_cstr };
|
|
|
|
|
if (name != "." && name != "..")
|
|
|
|
|
{
|
|
|
|
|
tr_sys_dir_close(odir);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tr_sys_dir_close(odir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
void depth_first_walk(char const* path, file_func_t const& func, std::optional<int> max_depth = {})
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
if (is_folder(path) && (!max_depth || *max_depth > 0))
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
if (auto const odir = tr_sys_dir_open(path); odir != TR_BAD_SYS_DIR)
|
|
|
|
|
{
|
|
|
|
|
char const* name_cstr = nullptr;
|
|
|
|
|
while ((name_cstr = tr_sys_dir_read_name(odir)) != nullptr)
|
|
|
|
|
{
|
|
|
|
|
auto const name = std::string_view{ name_cstr };
|
|
|
|
|
if (name == "." || name == "..")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
depth_first_walk(tr_pathbuf{ path, '/', name }.c_str(), func, max_depth ? *max_depth - 1 : max_depth);
|
2022-04-15 23:39:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tr_sys_dir_close(odir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func(path);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
bool is_junk_file(std::string_view filename)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
auto const base = tr_sys_path_basename(filename);
|
|
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2022-04-21 14:28:38 +00:00
|
|
|
|
// check for resource forks. <http://web.archive.org/web/20101010051608/http://support.apple.com/kb/TA20578>
|
2023-06-30 14:49:58 +00:00
|
|
|
|
if (tr_strv_starts_with(base, "._"sv))
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
auto constexpr Files = std::array<std::string_view, 3>{
|
|
|
|
|
".DS_Store"sv,
|
|
|
|
|
"Thumbs.db"sv,
|
|
|
|
|
"desktop.ini"sv,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return std::find(std::begin(Files), std::end(Files), base) != std::end(Files);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // unnamed namespace
|
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
|
// ---
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
std::optional<tr_torrent_files::FoundFile> tr_torrent_files::find(
|
|
|
|
|
tr_file_index_t file_index,
|
2023-01-28 23:58:20 +00:00
|
|
|
|
std::string_view const* paths,
|
2022-04-15 23:39:04 +00:00
|
|
|
|
size_t n_paths) const
|
|
|
|
|
{
|
|
|
|
|
auto filename = tr_pathbuf{};
|
|
|
|
|
auto const& subpath = path(file_index);
|
|
|
|
|
|
|
|
|
|
for (size_t path_idx = 0; path_idx < n_paths; ++path_idx)
|
|
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
|
auto const base = paths[path_idx];
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
filename.assign(base, '/', subpath);
|
2022-08-02 17:41:04 +00:00
|
|
|
|
if (auto const info = tr_sys_path_get_info(filename); info)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
|
return FoundFile{ *info, std::move(filename), std::size(base) };
|
2022-04-15 23:39:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
|
filename.assign(base, '/', subpath, PartialFileSuffix);
|
|
|
|
|
if (auto const info = tr_sys_path_get_info(filename); info)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
|
return FoundFile{ *info, std::move(filename), std::size(base) };
|
2022-04-15 23:39:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
bool tr_torrent_files::has_any_local_data(std::string_view const* paths, size_t n_paths) const
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
for (tr_file_index_t i = 0, n = file_count(); i < n; ++i)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
|
if (find(i, paths, n_paths))
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
|
// ---
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
bool tr_torrent_files::move(
|
|
|
|
|
std::string_view old_parent_in,
|
|
|
|
|
std::string_view parent_in,
|
2022-06-01 15:25:06 +00:00
|
|
|
|
std::string_view parent_name,
|
2023-11-04 16:39:41 +00:00
|
|
|
|
tr_error* error) const
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
auto const old_parent = tr_pathbuf{ old_parent_in };
|
|
|
|
|
auto const parent = tr_pathbuf{ parent_in };
|
2023-11-21 15:02:03 +00:00
|
|
|
|
tr_logAddTrace(fmt::format("Moving files from '{:s}' to '{:s}'", old_parent, parent), parent_name);
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
if (tr_sys_path_is_same(old_parent, parent))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!tr_sys_dir_create(parent, TR_SYS_DIR_CREATE_PARENTS, 0777, error))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
|
auto const paths = std::array<std::string_view, 1>{ old_parent.sv() };
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
auto err = bool{};
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
for (tr_file_index_t i = 0, n = file_count(); i < n; ++i)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
|
auto const found = find(i, std::data(paths), std::size(paths));
|
2022-04-15 23:39:04 +00:00
|
|
|
|
if (!found)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto const& old_path = found->filename();
|
|
|
|
|
auto const path = tr_pathbuf{ parent, '/', found->subpath() };
|
2024-01-06 20:05:18 +00:00
|
|
|
|
tr_logAddTrace(fmt::format("Found file #{:d} '{:s}'", i, old_path), parent_name);
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
if (tr_sys_path_is_same(old_path, path))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-06 20:05:18 +00:00
|
|
|
|
tr_logAddTrace(fmt::format("Moving file #{:d} to '{:s}'", i, old_path, path), parent_name);
|
2023-06-30 14:49:58 +00:00
|
|
|
|
if (!tr_file_move(old_path, path, error))
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
err = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// after moving the files, remove any leftover empty directories
|
|
|
|
|
if (!err)
|
|
|
|
|
{
|
|
|
|
|
auto const remove_empty_directories = [](char const* filename)
|
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
if (is_empty_folder(filename))
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
tr_sys_path_remove(filename, nullptr);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-01 15:25:06 +00:00
|
|
|
|
remove(old_parent, parent_name, remove_empty_directories);
|
2022-04-15 23:39:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !err;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
|
// ---
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This convoluted code does something (seemingly) simple:
|
|
|
|
|
* remove the torrent's local files.
|
|
|
|
|
*
|
|
|
|
|
* Fun complications:
|
|
|
|
|
* 1. Try to preserve the directory hierarchy in the recycle bin.
|
|
|
|
|
* 2. If there are nontorrent files, don't delete them...
|
|
|
|
|
* 3. ...unless the other files are "junk", such as .DS_Store
|
|
|
|
|
*/
|
2024-08-13 04:26:09 +00:00
|
|
|
|
void tr_torrent_files::remove(std::string_view parent_in, std::string_view tmpdir_prefix, FileFunc const& func, tr_error* error)
|
|
|
|
|
const
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
auto const parent = tr_pathbuf{ parent_in };
|
|
|
|
|
|
|
|
|
|
// don't try to delete local data if the directory's gone missing
|
|
|
|
|
if (!tr_sys_path_exists(parent))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 04:26:09 +00:00
|
|
|
|
// try to make a tmpdir
|
2022-04-15 23:39:04 +00:00
|
|
|
|
auto tmpdir = tr_pathbuf{ parent, '/', tmpdir_prefix, "__XXXXXX"sv };
|
2024-08-13 04:26:09 +00:00
|
|
|
|
if (!tr_sys_dir_create_temp(std::data(tmpdir), error))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-15 23:39:04 +00:00
|
|
|
|
|
|
|
|
|
// move the local data to the tmpdir
|
|
|
|
|
{
|
2024-08-13 04:26:09 +00:00
|
|
|
|
struct moved_file
|
|
|
|
|
{
|
|
|
|
|
tr_pathbuf from;
|
|
|
|
|
tr_pathbuf to;
|
|
|
|
|
};
|
|
|
|
|
std::vector<moved_file> moved_files;
|
|
|
|
|
|
|
|
|
|
auto const paths = std::array<std::string_view, 1>{ parent.sv() };
|
|
|
|
|
for (tr_file_index_t idx = 0, n_files = file_count(); idx < n_files; ++idx)
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
2024-08-13 04:26:09 +00:00
|
|
|
|
if (auto const found = find(idx, std::data(paths), std::size(paths)); found)
|
|
|
|
|
{
|
|
|
|
|
auto const f = moved_file{ found->filename(), tr_pathbuf{ tmpdir, '/', found->subpath() } };
|
|
|
|
|
|
|
|
|
|
// if moving a file fails, give up and let the error propagate
|
|
|
|
|
if (!tr_file_move_strict(f.from, f.to, error))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
moved_files.push_back(f);
|
|
|
|
|
}
|
2022-04-15 23:39:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make a list of the top-level torrent files & folders
|
|
|
|
|
// because we'll need it below in the 'remove junk' phase
|
2022-06-01 15:25:06 +00:00
|
|
|
|
auto const path = tr_pathbuf{ parent, '/', tmpdir_prefix };
|
|
|
|
|
auto top_files = std::set<std::string>{ std::string{ path } };
|
2024-05-25 15:08:53 +00:00
|
|
|
|
depth_first_walk(
|
2022-04-15 23:39:04 +00:00
|
|
|
|
tmpdir,
|
|
|
|
|
[&parent, &tmpdir, &top_files](char const* filename)
|
|
|
|
|
{
|
|
|
|
|
if (tmpdir != filename)
|
|
|
|
|
{
|
|
|
|
|
top_files.emplace(tr_pathbuf{ parent, '/', tr_sys_path_basename(filename) });
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
1);
|
|
|
|
|
|
|
|
|
|
auto const func_wrapper = [&tmpdir, &func](char const* filename)
|
|
|
|
|
{
|
|
|
|
|
if (tmpdir != filename)
|
|
|
|
|
{
|
|
|
|
|
func(filename);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Remove the tmpdir.
|
|
|
|
|
// Since `func` might send files to a recycle bin, try to preserve
|
|
|
|
|
// the folder hierarchy by removing top-level files & folders first.
|
|
|
|
|
// But that can fail -- e.g. `func` might refuse to remove nonempty
|
|
|
|
|
// directories -- so plan B is to remove everything bottom-up.
|
2024-05-25 15:08:53 +00:00
|
|
|
|
depth_first_walk(tmpdir, func_wrapper, 1);
|
|
|
|
|
depth_first_walk(tmpdir, func_wrapper);
|
2022-04-15 23:39:04 +00:00
|
|
|
|
tr_sys_path_remove(tmpdir);
|
|
|
|
|
|
|
|
|
|
// OK we've removed the local data.
|
|
|
|
|
// What's left are empty folders, junk, and user-generated files.
|
|
|
|
|
// Remove the first two categories and leave the third alone.
|
|
|
|
|
auto const remove_junk = [](char const* filename)
|
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
if (is_empty_folder(filename) || is_junk_file(filename))
|
2022-04-15 23:39:04 +00:00
|
|
|
|
{
|
|
|
|
|
tr_sys_path_remove(filename);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
for (auto const& filename : top_files)
|
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
depth_first_walk(filename.c_str(), remove_junk);
|
2022-04-15 23:39:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-23 22:53:26 +00:00
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
// `is_unix_reserved_file` and `is_win32_reserved_file` kept as `maybe_unused`
|
2023-11-03 05:25:42 +00:00
|
|
|
|
// for potential support of different filesystems on the same OS
|
2024-05-25 15:08:53 +00:00
|
|
|
|
[[nodiscard, maybe_unused]] bool is_unix_reserved_file(std::string_view in) noexcept
|
2023-11-03 05:25:42 +00:00
|
|
|
|
{
|
|
|
|
|
static auto constexpr ReservedNames = std::array<std::string_view, 2>{
|
|
|
|
|
"."sv,
|
|
|
|
|
".."sv,
|
|
|
|
|
};
|
|
|
|
|
return (std::find(std::begin(ReservedNames), std::end(ReservedNames), in) != std::end(ReservedNames));
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 22:53:26 +00:00
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
|
|
|
|
|
// Do not use the following reserved names for the name of a file:
|
|
|
|
|
// CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8,
|
|
|
|
|
// COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
|
|
|
|
|
// Also avoid these names followed immediately by an extension;
|
|
|
|
|
// for example, NUL.txt is not recommended.
|
2024-05-25 15:08:53 +00:00
|
|
|
|
[[nodiscard, maybe_unused]] bool is_win32_reserved_file(std::string_view in) noexcept
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
|
|
|
|
if (std::empty(in))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shortcut to avoid extra work below.
|
|
|
|
|
// All the paths below involve filenames that begin with one of these chars
|
|
|
|
|
static auto constexpr ReservedFilesBeginWithOneOf = "ACLNP"sv;
|
|
|
|
|
if (ReservedFilesBeginWithOneOf.find(toupper(in.front())) == std::string_view::npos)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto in_upper = tr_pathbuf{ in };
|
|
|
|
|
std::transform(std::begin(in_upper), std::end(in_upper), std::begin(in_upper), [](auto ch) { return toupper(ch); });
|
|
|
|
|
auto const in_upper_sv = in_upper.sv();
|
|
|
|
|
|
|
|
|
|
static auto constexpr ReservedNames = std::array<std::string_view, 22>{
|
|
|
|
|
"AUX"sv, "CON"sv, "NUL"sv, "PRN"sv, //
|
|
|
|
|
"COM1"sv, "COM2"sv, "COM3"sv, "COM4"sv, "COM5"sv, "COM6"sv, "COM7"sv, "COM8"sv, "COM9"sv, //
|
|
|
|
|
"LPT1"sv, "LPT2"sv, "LPT3"sv, "LPT4"sv, "LPT5"sv, "LPT6"sv, "LPT7"sv, "LPT8"sv, "LPT9"sv, //
|
|
|
|
|
};
|
|
|
|
|
if (std::find(std::begin(ReservedNames), std::end(ReservedNames), in_upper_sv) != std::end(ReservedNames))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static auto constexpr ReservedPrefixes = std::array<std::string_view, 22>{
|
|
|
|
|
"AUX."sv, "CON."sv, "NUL."sv, "PRN."sv, //
|
2022-07-29 01:02:33 +00:00
|
|
|
|
"COM1."sv, "COM2."sv, "COM3."sv, "COM4."sv, "COM5."sv, "COM6."sv, "COM7."sv, "COM8."sv, "COM9."sv, //
|
2022-05-23 22:53:26 +00:00
|
|
|
|
"LPT1."sv, "LPT2."sv, "LPT3."sv, "LPT4."sv, "LPT5."sv, "LPT6."sv, "LPT7."sv, "LPT8."sv, "LPT9."sv, //
|
|
|
|
|
};
|
|
|
|
|
return std::any_of(
|
|
|
|
|
std::begin(ReservedPrefixes),
|
|
|
|
|
std::end(ReservedPrefixes),
|
2023-06-30 14:49:58 +00:00
|
|
|
|
[in_upper_sv](auto const& prefix) { return tr_strv_starts_with(in_upper_sv, prefix); });
|
2023-11-03 05:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
[[nodiscard]] bool is_reserved_file(std::string_view in, bool os_specific) noexcept
|
2023-11-03 05:25:42 +00:00
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
if (!os_specific)
|
|
|
|
|
{
|
|
|
|
|
return is_unix_reserved_file(in) || is_win32_reserved_file(in);
|
|
|
|
|
}
|
2023-11-03 05:25:42 +00:00
|
|
|
|
#ifdef _WIN32
|
2024-05-25 15:08:53 +00:00
|
|
|
|
return is_win32_reserved_file(in);
|
2023-11-03 05:25:42 +00:00
|
|
|
|
#else
|
2024-05-25 15:08:53 +00:00
|
|
|
|
return is_unix_reserved_file(in);
|
2023-11-01 20:27:04 +00:00
|
|
|
|
#endif
|
2022-05-23 22:53:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
// `is_unix_reserved_char` and `is_win32_reserved_char` kept as `maybe_unused`
|
2023-11-03 05:25:42 +00:00
|
|
|
|
// for potential support of different filesystems on the same OS
|
2024-05-25 15:08:53 +00:00
|
|
|
|
[[nodiscard, maybe_unused]] auto constexpr is_unix_reserved_char(unsigned char ch) noexcept
|
2023-11-03 05:25:42 +00:00
|
|
|
|
{
|
|
|
|
|
return ch == '/';
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 22:53:26 +00:00
|
|
|
|
// https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file
|
|
|
|
|
// Use any character in the current code page for a name, including Unicode
|
|
|
|
|
// characters and characters in the extended character set (128–255),
|
|
|
|
|
// except for the following:
|
2024-05-25 15:08:53 +00:00
|
|
|
|
[[nodiscard, maybe_unused]] auto constexpr is_win32_reserved_char(unsigned char ch) noexcept
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
|
|
|
|
switch (ch)
|
|
|
|
|
{
|
|
|
|
|
case '"':
|
|
|
|
|
case '*':
|
|
|
|
|
case '/':
|
|
|
|
|
case ':':
|
|
|
|
|
case '<':
|
|
|
|
|
case '>':
|
|
|
|
|
case '?':
|
|
|
|
|
case '\\':
|
|
|
|
|
case '|':
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
2023-11-03 05:25:42 +00:00
|
|
|
|
return ch <= 31;
|
2022-05-23 22:53:26 +00:00
|
|
|
|
}
|
2023-11-03 05:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
[[nodiscard]] auto constexpr is_reserved_char(unsigned char ch, bool os_specific) noexcept
|
2023-11-03 05:25:42 +00:00
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
if (!os_specific)
|
|
|
|
|
{
|
|
|
|
|
return is_unix_reserved_char(ch) || is_win32_reserved_char(ch);
|
|
|
|
|
}
|
2023-11-03 05:25:42 +00:00
|
|
|
|
#ifdef _WIN32
|
2024-05-25 15:08:53 +00:00
|
|
|
|
return is_win32_reserved_char(ch);
|
2023-11-03 05:25:42 +00:00
|
|
|
|
#else
|
2024-05-25 15:08:53 +00:00
|
|
|
|
return is_unix_reserved_char(ch);
|
2023-11-01 20:27:04 +00:00
|
|
|
|
#endif
|
2022-05-23 22:53:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-03 05:25:42 +00:00
|
|
|
|
// https://en.wikipedia.org/wiki/Filename#Comparison_of_filename_limitations
|
2024-05-25 15:08:53 +00:00
|
|
|
|
void append_sanitized_component(std::string_view in, tr_pathbuf& out, bool os_specific)
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
2023-11-03 05:25:42 +00:00
|
|
|
|
#ifdef _WIN32
|
2022-05-23 22:53:26 +00:00
|
|
|
|
// remove leading and trailing spaces
|
2023-06-30 14:49:58 +00:00
|
|
|
|
in = tr_strv_strip(in);
|
2022-05-23 22:53:26 +00:00
|
|
|
|
|
|
|
|
|
// remove trailing periods
|
2023-06-30 14:49:58 +00:00
|
|
|
|
while (tr_strv_ends_with(in, '.'))
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
|
|
|
|
in.remove_suffix(1);
|
|
|
|
|
}
|
2023-11-03 05:25:42 +00:00
|
|
|
|
#endif
|
2022-05-23 22:53:26 +00:00
|
|
|
|
|
2023-11-03 05:25:42 +00:00
|
|
|
|
// replace reserved filenames with an underscore
|
2024-05-25 15:08:53 +00:00
|
|
|
|
if (is_reserved_file(in, os_specific))
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
|
|
|
|
out.append('_');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// replace reserved characters with an underscore
|
2024-05-25 15:08:53 +00:00
|
|
|
|
auto const add_char = [os_specific](auto ch)
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
return is_reserved_char(ch, os_specific) ? '_' : ch;
|
2022-05-23 22:53:26 +00:00
|
|
|
|
};
|
2024-05-25 15:08:53 +00:00
|
|
|
|
std::transform(std::begin(in), std::end(in), std::back_inserter(out), add_char);
|
2022-05-23 22:53:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2024-05-25 15:08:53 +00:00
|
|
|
|
void tr_torrent_files::sanitize_subpath(std::string_view path, tr_pathbuf& append_me, bool os_specific)
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
|
|
|
|
auto segment = std::string_view{};
|
2023-06-30 14:49:58 +00:00
|
|
|
|
while (tr_strv_sep(&path, &segment, '/'))
|
2022-05-23 22:53:26 +00:00
|
|
|
|
{
|
2024-05-25 15:08:53 +00:00
|
|
|
|
append_sanitized_component(segment, append_me, os_specific);
|
2022-05-23 22:53:26 +00:00
|
|
|
|
append_me.append('/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto const n = std::size(append_me); n > 0)
|
|
|
|
|
{
|
|
|
|
|
append_me.resize(n - 1); // remove trailing slash
|
|
|
|
|
}
|
|
|
|
|
}
|