2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2013-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-12-16 22:58:58 +00:00
|
|
|
#include <algorithm>
|
2021-12-28 02:32:22 +00:00
|
|
|
#include <array>
|
2022-08-05 19:16:25 +00:00
|
|
|
#include <cctype> // for isalpha()
|
2022-09-21 23:34:18 +00:00
|
|
|
#include <cstring>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <iterator> // for std::back_inserter
|
2022-09-23 13:39:13 +00:00
|
|
|
#include <optional>
|
2022-08-05 19:16:25 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <shlobj.h> /* SHCreateDirectoryEx() */
|
2014-07-08 00:15:12 +00:00
|
|
|
#include <winioctl.h> /* FSCTL_SET_SPARSE */
|
|
|
|
|
2022-04-01 23:00:51 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-21 20:22:50 +00:00
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
#include "transmission.h"
|
2022-08-12 00:59:58 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_rand_int() */
|
2015-04-11 14:54:01 +00:00
|
|
|
#include "error.h"
|
2014-07-03 21:58:39 +00:00
|
|
|
#include "file.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2014-07-03 21:58:39 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2021-11-16 00:50:14 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifndef MAXSIZE_T
|
2021-08-15 09:41:48 +00:00
|
|
|
#define MAXSIZE_T ((SIZE_T) ~((SIZE_T)0))
|
2014-07-08 00:15:12 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
/* MSDN (http://msdn.microsoft.com/en-us/library/2k2xf226.aspx) only mentions
|
|
|
|
"i64" suffix for C code, but no warning is issued */
|
|
|
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
|
|
|
|
|
2014-09-21 17:52:36 +00:00
|
|
|
struct tr_sys_dir_win32
|
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
std::wstring pattern;
|
|
|
|
HANDLE find_handle = INVALID_HANDLE_VALUE;
|
|
|
|
WIN32_FIND_DATAW find_data = {};
|
|
|
|
std::string utf8_name;
|
2014-09-21 17:52:36 +00:00
|
|
|
};
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
static auto constexpr NativeLocalPathPrefix = L"\\\\?\\"sv;
|
|
|
|
static auto constexpr NativeUncPathPrefix = L"\\\\?\\UNC\\"sv;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void set_system_error(tr_error** error, DWORD code)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
if (auto const message = tr_win32_format_message(code); !std::empty(message))
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-12-28 02:32:22 +00:00
|
|
|
tr_error_set(error, code, message);
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
tr_error_set(error, code, fmt::format(FMT_STRING("Unknown error: {:#08x}"), code));
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void set_system_error_if_file_found(tr_error** error, DWORD code)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (code != ERROR_FILE_NOT_FOUND && code != ERROR_PATH_NOT_FOUND && code != ERROR_NO_MORE_FILES)
|
|
|
|
{
|
|
|
|
set_system_error(error, code);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
static constexpr time_t filetime_to_unix_time(FILETIME const& t)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
uint64_t tmp = 0;
|
2022-08-02 17:41:04 +00:00
|
|
|
tmp |= t.dwHighDateTime;
|
2017-04-19 12:04:45 +00:00
|
|
|
tmp <<= 32;
|
2022-08-02 17:41:04 +00:00
|
|
|
tmp |= t.dwLowDateTime;
|
2017-04-19 12:04:45 +00:00
|
|
|
tmp /= 10; /* to microseconds */
|
|
|
|
tmp -= DELTA_EPOCH_IN_MICROSECS;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tmp / 1000000UL;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
static constexpr auto stat_to_sys_path_info(DWORD attributes, DWORD size_low, DWORD size_high, FILETIME const& mtime)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
auto info = tr_sys_path_info{};
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2019-07-14 19:57:40 +00:00
|
|
|
if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
info.type = TR_SYS_PATH_IS_DIRECTORY;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2019-07-14 19:57:40 +00:00
|
|
|
else if ((attributes & (FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_VIRTUAL)) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
info.type = TR_SYS_PATH_IS_FILE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
info.type = TR_SYS_PATH_IS_OTHER;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
info.size = size_high;
|
|
|
|
info.size <<= 32;
|
|
|
|
info.size |= size_low;
|
|
|
|
|
|
|
|
info.last_modified_at = filetime_to_unix_time(mtime);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
return info;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:29:00 +00:00
|
|
|
static auto constexpr Slashes = "\\/"sv;
|
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
static constexpr bool is_slash(char c)
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2022-11-15 14:29:00 +00:00
|
|
|
return tr_strvContains(Slashes, c);
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:08:50 +00:00
|
|
|
static constexpr bool is_unc_path(std::string_view path)
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2021-11-13 16:08:50 +00:00
|
|
|
return std::size(path) >= 2 && is_slash(path[0]) && path[1] == path[0];
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 00:50:14 +00:00
|
|
|
static bool is_valid_path(std::string_view path)
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (is_unc_path(path))
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (path[2] != '\0' && !isalnum(path[2]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2021-11-16 00:50:14 +00:00
|
|
|
auto pos = path.find(':');
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-16 00:50:14 +00:00
|
|
|
if (pos != path.npos)
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2021-11-16 00:50:14 +00:00
|
|
|
if (pos != 1 || !isalpha(path[0]))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:50:14 +00:00
|
|
|
path.remove_prefix(2);
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 00:50:14 +00:00
|
|
|
return path.find_first_of("<>:\"|?*"sv) == path.npos;
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace path_to_native_path_helpers
|
2015-10-23 05:29:47 +00:00
|
|
|
{
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
auto path_to_fixed_native_path(std::string_view path)
|
|
|
|
{
|
|
|
|
auto wide_path = tr_win32_utf8_to_native(path);
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
// convert '/' to '\'
|
|
|
|
static auto constexpr Convert = [](wchar_t wch)
|
2015-10-23 05:29:47 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
return wch == L'/' ? L'\\' : wch;
|
|
|
|
};
|
|
|
|
std::transform(std::begin(wide_path), std::end(wide_path), std::begin(wide_path), Convert);
|
2021-12-05 13:32:39 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
// squash multiple consecutive separators into one to avoid ERROR_INVALID_NAME
|
|
|
|
static auto constexpr Equal = [](wchar_t a, wchar_t b)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
return a == b && a == L'\\';
|
|
|
|
};
|
|
|
|
auto const tmp = wide_path;
|
|
|
|
wide_path.clear();
|
|
|
|
std::unique_copy(std::begin(tmp), std::end(tmp), std::back_inserter(wide_path), Equal);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
return wide_path;
|
|
|
|
}
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
} // namespace path_to_native_path_helpers
|
|
|
|
} // namespace
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
/* Extending maximum path length limit up to ~32K. See "Naming Files, Paths, and Namespaces"
|
|
|
|
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx for more info */
|
|
|
|
static auto path_to_native_path(std::string_view path)
|
|
|
|
{
|
|
|
|
using namespace path_to_native_path_helpers;
|
2021-12-05 13:32:39 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (is_unc_path(path))
|
2021-12-05 13:32:39 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
// UNC path: "\\server\share" -> "\\?\UNC\server\share"
|
|
|
|
path.remove_prefix(2); // remove path's UNC prefix slashes
|
|
|
|
auto wide_path = path_to_fixed_native_path(path);
|
|
|
|
wide_path.insert(0, NativeUncPathPrefix);
|
|
|
|
return wide_path;
|
2021-12-05 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (!tr_sys_path_is_relative(path))
|
2022-08-02 17:41:04 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
// local path: "C:" -> "\\?\C:"
|
|
|
|
auto wide_path = path_to_fixed_native_path(path);
|
|
|
|
wide_path.insert(0, NativeLocalPathPrefix);
|
|
|
|
return wide_path;
|
2022-08-02 17:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
return path_to_fixed_native_path(path);
|
2022-08-02 17:41:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
static std::string native_path_to_path(std::wstring_view wide_path)
|
2018-01-24 20:10:21 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
if (std::empty(wide_path))
|
2018-01-24 20:10:21 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
return {};
|
2018-01-24 20:10:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
if (tr_strvStartsWith(wide_path, NativeUncPathPrefix))
|
|
|
|
{
|
|
|
|
wide_path.remove_prefix(std::size(NativeUncPathPrefix));
|
|
|
|
auto path = tr_win32_native_to_utf8(wide_path);
|
|
|
|
path.insert(0, "\\\\"sv);
|
|
|
|
return path;
|
|
|
|
}
|
2018-01-24 20:10:21 +00:00
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
if (tr_strvStartsWith(wide_path, NativeLocalPathPrefix))
|
2018-01-24 20:10:21 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
wide_path.remove_prefix(std::size(NativeLocalPathPrefix));
|
|
|
|
return tr_win32_native_to_utf8(wide_path);
|
2018-01-24 20:10:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
return tr_win32_native_to_utf8(wide_path);
|
2018-01-24 20:10:21 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
static tr_sys_file_t open_file(std::string_view path, DWORD access, DWORD disposition, DWORD flags, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_sys_file_t ret = TR_BAD_SYS_FILE;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (auto const wide_path = path_to_native_path(path); !std::empty(wide_path))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
ret = CreateFileW(
|
2022-08-03 23:07:38 +00:00
|
|
|
wide_path.c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
access,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
disposition,
|
|
|
|
flags,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
static bool create_dir(std::string_view path, int flags, int /*permissions*/, bool okay_if_exists, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret;
|
|
|
|
DWORD error_code = ERROR_SUCCESS;
|
2022-08-05 19:16:25 +00:00
|
|
|
auto const wide_path = path_to_native_path(path);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2022-09-05 00:40:34 +00:00
|
|
|
// already exists (no-op)
|
|
|
|
if (auto const info = tr_sys_path_get_info(path); info && info->isFolder())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & TR_SYS_DIR_CREATE_PARENTS) != 0)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
error_code = SHCreateDirectoryExW(nullptr, wide_path.c_str(), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = error_code == ERROR_SUCCESS;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
ret = CreateDirectoryW(wide_path.c_str(), nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
error_code = GetLastError();
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret && error_code == ERROR_ALREADY_EXISTS && okay_if_exists)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
DWORD const attributes = GetFileAttributesW(wide_path.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-07-14 19:57:40 +00:00
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, error_code);
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void create_temp_path(
|
|
|
|
char* path_template,
|
|
|
|
void (*callback)(char const* path, void* param, tr_error** error),
|
|
|
|
void* callback_param,
|
|
|
|
tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path_template != nullptr);
|
|
|
|
TR_ASSERT(callback != nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
auto path = std::string{ path_template };
|
|
|
|
auto path_size = std::size(path);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path_size > 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_error* my_error = nullptr;
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int attempt = 0; attempt < 100; ++attempt)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t i = path_size;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (i > 0 && path_template[i - 1] == 'X')
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const c = tr_rand_int(26 + 26 + 10);
|
2017-04-19 12:04:45 +00:00
|
|
|
path[i - 1] = c < 26 ? c + 'A' : (c < 26 + 26 ? (c - 26) + 'a' : (c - 26 - 26) + '0');
|
|
|
|
--i;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(path_size >= i + 6);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_clear(&my_error);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2022-08-12 00:59:58 +00:00
|
|
|
(*callback)(path.c_str(), callback_param, &my_error);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (my_error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (my_error != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
std::copy_n(std::begin(path), path_size, path_template);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_exists(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (auto const wide_path = path_to_native_path(path); !std::empty(wide_path))
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
DWORD attributes = GetFileAttributesW(wide_path.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2019-07-14 19:57:40 +00:00
|
|
|
if ((attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
handle = CreateFileW(wide_path.c_str(), 0, 0, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = handle != INVALID_HANDLE_VALUE;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = true;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error_if_file_found(error, GetLastError());
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(handle);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 00:40:34 +00:00
|
|
|
static std::optional<tr_sys_path_info> tr_sys_file_get_info_(tr_sys_file_t handle, tr_error** error)
|
|
|
|
{
|
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
|
|
|
|
|
|
|
auto attributes = BY_HANDLE_FILE_INFORMATION{};
|
|
|
|
if (GetFileInformationByHandle(handle, &attributes))
|
|
|
|
{
|
|
|
|
return stat_to_sys_path_info(
|
|
|
|
attributes.dwFileAttributes,
|
|
|
|
attributes.nFileSizeLow,
|
|
|
|
attributes.nFileSizeHigh,
|
|
|
|
attributes.ftLastWriteTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
std::optional<tr_sys_path_info> tr_sys_path_get_info(std::string_view path, int flags, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-05 19:16:25 +00:00
|
|
|
if (auto const wide_path = path_to_native_path(path); std::empty(wide_path))
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
// do nothing
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
2022-08-02 17:41:04 +00:00
|
|
|
else if ((flags & TR_SYS_PATH_NO_FOLLOW) != 0)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
auto attributes = WIN32_FILE_ATTRIBUTE_DATA{};
|
|
|
|
if (GetFileAttributesExW(wide_path.c_str(), GetFileExInfoStandard, &attributes))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 17:41:04 +00:00
|
|
|
return stat_to_sys_path_info(
|
2021-08-15 09:41:48 +00:00
|
|
|
attributes.dwFileAttributes,
|
|
|
|
attributes.nFileSizeLow,
|
|
|
|
attributes.nFileSizeHigh,
|
2022-08-02 17:41:04 +00:00
|
|
|
attributes.ftLastWriteTime);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
2022-08-02 17:41:04 +00:00
|
|
|
else if (auto const
|
|
|
|
handle = CreateFileW(wide_path.c_str(), 0, 0, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
|
|
|
|
handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2022-09-05 00:40:34 +00:00
|
|
|
auto ret = tr_sys_file_get_info_(handle, error);
|
2022-08-02 17:41:04 +00:00
|
|
|
CloseHandle(handle);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-02 17:41:04 +00:00
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
return {};
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:08:50 +00:00
|
|
|
bool tr_sys_path_is_relative(std::string_view path)
|
2015-10-20 21:22:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* UNC path: `\\...`. */
|
|
|
|
if (is_unc_path(path))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-20 21:22:19 +00:00
|
|
|
|
2021-11-13 16:08:50 +00:00
|
|
|
/* Local path: `X:` */
|
|
|
|
if (std::size(path) == 2 && isalpha(path[0]) && path[1] == ':')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Local path: `X:\...`. */
|
|
|
|
if (std::size(path) > 2 && isalpha(path[0]) && path[1] == ':' && is_slash(path[2]))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-20 21:22:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2015-10-20 21:22:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
static std::optional<BY_HANDLE_FILE_INFORMATION> get_file_info(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
auto const wpath = path_to_native_path(path);
|
|
|
|
if (std::empty(wpath))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
set_system_error_if_file_found(error, GetLastError());
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
auto const handle = CreateFileW(wpath.c_str(), 0, 0, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
set_system_error_if_file_found(error, GetLastError());
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
// TODO: Use GetFileInformationByHandleEx on >= Server 2012
|
|
|
|
auto info = BY_HANDLE_FILE_INFORMATION{};
|
|
|
|
if (!GetFileInformationByHandle(handle, &info))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
set_system_error_if_file_found(error, GetLastError());
|
|
|
|
CloseHandle(handle);
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
CloseHandle(handle);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_sys_path_is_same(char const* path1, char const* path2, tr_error** error)
|
|
|
|
{
|
|
|
|
TR_ASSERT(path1 != nullptr);
|
|
|
|
TR_ASSERT(path2 != nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
auto const fi1 = get_file_info(path1, error);
|
|
|
|
if (!fi1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 13:39:13 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
auto const fi2 = get_file_info(path2, error);
|
|
|
|
if (!fi2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-09-23 13:39:13 +00:00
|
|
|
return fi1->dwVolumeSerialNumber == fi2->dwVolumeSerialNumber && fi1->nFileIndexHigh == fi2->nFileIndexHigh &&
|
|
|
|
fi1->nFileIndexLow == fi2->nFileIndexLow;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
std::string tr_sys_path_resolve(std::string_view path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
auto ret = std::string{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (auto const wide_path = path_to_native_path(path); !std::empty(wide_path))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
if (auto const handle = CreateFileW(
|
|
|
|
wide_path.c_str(),
|
|
|
|
FILE_READ_EA,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
|
|
nullptr,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS,
|
|
|
|
nullptr);
|
|
|
|
handle != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if (auto const wide_ret_size = GetFinalPathNameByHandleW(handle, nullptr, 0, 0); wide_ret_size != 0)
|
|
|
|
{
|
|
|
|
auto wide_ret = std::wstring{};
|
|
|
|
wide_ret.resize(wide_ret_size);
|
|
|
|
if (GetFinalPathNameByHandleW(handle, std::data(wide_ret), wide_ret_size, 0) == wide_ret_size - 1)
|
|
|
|
{
|
|
|
|
// `wide_ret_size` includes the terminating '\0'; remove it from `wide_ret`
|
|
|
|
wide_ret.resize(std::size(wide_ret) - 1);
|
|
|
|
TR_ASSERT(tr_strvStartsWith(wide_ret, NativeLocalPathPrefix));
|
|
|
|
ret = native_path_to_path(wide_ret);
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
CloseHandle(handle);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 16:36:01 +00:00
|
|
|
if (!std::empty(ret))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
return ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
2022-08-05 16:36:01 +00:00
|
|
|
return {};
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:29:00 +00:00
|
|
|
std::string_view tr_sys_path_basename(std::string_view path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-11-16 00:50:14 +00:00
|
|
|
if (std::empty(path))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-15 14:29:00 +00:00
|
|
|
return "."sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!is_valid_path(path))
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, ERROR_PATH_NOT_FOUND);
|
2022-03-21 20:22:50 +00:00
|
|
|
return {};
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-11-15 14:29:00 +00:00
|
|
|
// Remove all trailing slashes.
|
|
|
|
// If nothing is left, return "/"
|
|
|
|
if (auto const pos = path.find_last_not_of(Slashes); pos != std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-15 14:29:00 +00:00
|
|
|
path = path.substr(0, pos + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-11-15 14:29:00 +00:00
|
|
|
else // all slashes
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-15 14:29:00 +00:00
|
|
|
return "/"sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-11-15 14:29:00 +00:00
|
|
|
if (auto pos = path.find_last_of("\\/:"); pos != std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-15 14:29:00 +00:00
|
|
|
path.remove_prefix(pos + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-10-23 03:42:11 +00:00
|
|
|
|
2022-11-15 14:29:00 +00:00
|
|
|
return !std::empty(path) ? path : "/"sv;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
[[nodiscard]] static bool isWindowsDeviceRoot(char ch) noexcept
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-05-21 16:10:58 +00:00
|
|
|
return isalpha(static_cast<int>(ch)) != 0;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
[[nodiscard]] static constexpr bool isPathSeparator(char ch) noexcept
|
|
|
|
{
|
|
|
|
return ch == '/' || ch == '\\';
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
// This function is adapted from Node.js's path.win32.dirname() function,
|
|
|
|
// which is copyrighted by Joyent, Inc. and other Node contributors
|
|
|
|
// and is distributed under MIT (SPDX:MIT) license.
|
|
|
|
std::string_view tr_sys_path_dirname(std::string_view path)
|
|
|
|
{
|
|
|
|
auto const len = std::size(path);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
if (len == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-21 16:10:58 +00:00
|
|
|
return "."sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-01-02 04:02:13 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
if (len == 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-21 16:10:58 +00:00
|
|
|
return isPathSeparator(path[0]) ? path : "."sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
auto root_end = std::string_view::npos;
|
|
|
|
auto offset = std::string_view::size_type{ 0 };
|
|
|
|
|
|
|
|
// Try to match a root
|
|
|
|
if (isPathSeparator(path[0]))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-21 16:10:58 +00:00
|
|
|
// Possible UNC root
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
root_end = offset = 1;
|
2015-10-23 03:42:11 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
if (isPathSeparator(path[1]))
|
|
|
|
{
|
|
|
|
// Matched double path separator at beginning
|
|
|
|
std::string_view::size_type j = 2;
|
|
|
|
std::string_view::size_type last = j;
|
|
|
|
// Match 1 or more non-path separators
|
|
|
|
while (j < len && !isPathSeparator(path[j]))
|
|
|
|
{
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (j < len && j != last)
|
|
|
|
{
|
|
|
|
// Matched!
|
|
|
|
last = j;
|
|
|
|
// Match 1 or more path separators
|
|
|
|
while (j < len && isPathSeparator(path[j]))
|
|
|
|
{
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (j < len && j != last)
|
|
|
|
{
|
|
|
|
// Matched!
|
|
|
|
last = j;
|
|
|
|
// Match 1 or more non-path separators
|
|
|
|
while (j < len && !isPathSeparator(path[j]))
|
|
|
|
{
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (j == len)
|
|
|
|
{
|
|
|
|
// We matched a UNC root only
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
if (j != last)
|
|
|
|
{
|
|
|
|
// We matched a UNC root with leftovers
|
|
|
|
|
|
|
|
// Offset by 1 to include the separator after the UNC root to
|
|
|
|
// treat it as a "normal root" on top of a (UNC) root
|
|
|
|
root_end = offset = j + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Possible device root
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-05-21 16:10:58 +00:00
|
|
|
else if (isWindowsDeviceRoot(path[0]) && path[1] == ':')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-21 16:10:58 +00:00
|
|
|
root_end = len > 2 && isPathSeparator(path[2]) ? 3 : 2;
|
|
|
|
offset = root_end;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
auto end = std::string_view::npos;
|
|
|
|
auto matched_slash = bool{ true };
|
|
|
|
for (std::string_view::size_type i = len - 1; i >= offset; --i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-21 16:10:58 +00:00
|
|
|
if (isPathSeparator(path[i]))
|
|
|
|
{
|
|
|
|
if (!matched_slash)
|
|
|
|
{
|
|
|
|
end = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We saw the first non-path separator
|
|
|
|
matched_slash = false;
|
|
|
|
}
|
|
|
|
if (i <= offset)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
if (end == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-05-21 16:10:58 +00:00
|
|
|
if (root_end == std::string_view::npos)
|
|
|
|
{
|
|
|
|
return "."sv;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-05-21 16:10:58 +00:00
|
|
|
end = root_end;
|
|
|
|
}
|
|
|
|
return path.substr(0, end);
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(src_path != nullptr);
|
|
|
|
TR_ASSERT(dst_path != nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
2022-08-05 19:16:25 +00:00
|
|
|
auto const wide_src_path = path_to_native_path(src_path);
|
|
|
|
auto const wide_dst_path = path_to_native_path(dst_path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
if (!std::empty(wide_src_path) && !std::empty(wide_dst_path))
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD flags = MOVEFILE_REPLACE_EXISTING;
|
|
|
|
DWORD attributes;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
attributes = GetFileAttributesW(wide_src_path.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
flags = 0;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
attributes = GetFileAttributesW(wide_dst_path.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
|
|
|
{
|
|
|
|
flags = 0;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
ret = MoveFileExW(wide_src_path.c_str(), wide_dst_path.c_str(), flags);
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
bool tr_sys_path_copy(char const* src_path, char const* dst_path, tr_error** error)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(src_path != nullptr);
|
|
|
|
TR_ASSERT(dst_path != nullptr);
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
auto const wide_src_path = path_to_native_path(src_path);
|
|
|
|
auto const wide_dst_path = path_to_native_path(dst_path);
|
2022-08-03 23:07:38 +00:00
|
|
|
if (std::empty(wide_src_path) || std::empty(wide_dst_path))
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, ERROR_INVALID_PARAMETER);
|
2022-08-03 23:07:38 +00:00
|
|
|
return false;
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto cancel = BOOL{ FALSE };
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
DWORD const flags = COPY_FILE_ALLOW_DECRYPTED_DESTINATION | COPY_FILE_FAIL_IF_EXISTS;
|
2022-08-03 23:07:38 +00:00
|
|
|
if (CopyFileExW(wide_src_path.c_str(), wide_dst_path.c_str(), nullptr, nullptr, &cancel, flags) == 0)
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
2022-08-03 23:07:38 +00:00
|
|
|
return false;
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
return true;
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_remove(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
if (auto const wide_path = path_to_native_path(path); !std::empty(wide_path))
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
DWORD const attributes = GetFileAttributesW(wide_path.c_str());
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
ret = RemoveDirectoryW(wide_path.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
ret = DeleteFileW(wide_path.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 22:22:44 +00:00
|
|
|
char* tr_sys_path_native_separators(char* path)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (path == nullptr)
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-11-27 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
for (char* slash = strchr(path, '/'); slash != nullptr; slash = strchr(slash, '/'))
|
2017-11-27 22:22:44 +00:00
|
|
|
{
|
|
|
|
*slash = '\\';
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t tr_sys_file_get_std(tr_std_sys_file_t std_file, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t ret = TR_BAD_SYS_FILE;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (std_file)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
|
|
|
case TR_STD_SYS_FILE_IN:
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
break;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
case TR_STD_SYS_FILE_OUT:
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
break;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
case TR_STD_SYS_FILE_ERR:
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
break;
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
default:
|
2022-04-01 23:00:51 +00:00
|
|
|
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unknown standard file {:d}"), std_file));
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, ERROR_INVALID_PARAMETER);
|
|
|
|
return TR_BAD_SYS_FILE;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2021-09-15 00:18:09 +00:00
|
|
|
else if (ret == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = TR_BAD_SYS_FILE;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_sys_file_t tr_sys_file_open(char const* path, int flags, int /*permissions*/, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) != 0);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t ret;
|
|
|
|
DWORD native_access = 0;
|
|
|
|
DWORD native_disposition = OPEN_EXISTING;
|
|
|
|
DWORD native_flags = FILE_ATTRIBUTE_NORMAL;
|
|
|
|
bool success;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-05-01 15:47:49 +00:00
|
|
|
if ((flags & TR_SYS_FILE_READ) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
native_access |= GENERIC_READ;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-05-01 16:07:35 +00:00
|
|
|
if ((flags & TR_SYS_FILE_WRITE) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
native_access |= GENERIC_WRITE;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2022-08-28 19:10:00 +00:00
|
|
|
if ((flags & TR_SYS_FILE_CREATE) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-01 15:47:49 +00:00
|
|
|
native_disposition = (flags & TR_SYS_FILE_TRUNCATE) != 0 ? CREATE_ALWAYS : OPEN_ALWAYS;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-05-01 16:07:35 +00:00
|
|
|
else if ((flags & TR_SYS_FILE_TRUNCATE) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
native_disposition = TRUNCATE_EXISTING;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-05-01 16:07:35 +00:00
|
|
|
if ((flags & TR_SYS_FILE_SEQUENTIAL) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
native_flags |= FILE_FLAG_SEQUENTIAL_SCAN;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = open_file(path, native_access, native_disposition, native_flags, error);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
success = ret != TR_BAD_SYS_FILE;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-05-01 15:47:49 +00:00
|
|
|
if (success && (flags & TR_SYS_FILE_APPEND) != 0)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
success = SetFilePointer(ret, 0, nullptr, FILE_END) != INVALID_SET_FILE_POINTER;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!success)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (error == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(ret);
|
|
|
|
ret = TR_BAD_SYS_FILE;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void file_open_temp_callback(char const* path, void* param, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t* result = (tr_sys_file_t*)param;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(result != nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*result = open_file(path, GENERIC_READ | GENERIC_WRITE, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY, error);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t tr_sys_file_open_temp(char* path_template, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path_template != nullptr);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_sys_file_t ret = TR_BAD_SYS_FILE;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
create_temp_path(path_template, file_open_temp_callback, &ret, error);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_close(tr_sys_file_t handle, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = CloseHandle(handle);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_read(tr_sys_file_t handle, void* buffer, uint64_t size, uint64_t* bytes_read, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size > MAXDWORD)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, ERROR_INVALID_PARAMETER);
|
|
|
|
return false;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
DWORD my_bytes_read;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ReadFile(handle, buffer, (DWORD)size, &my_bytes_read, nullptr))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_read != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_read = my_bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
bool tr_sys_file_read_at(
|
|
|
|
tr_sys_file_t handle,
|
|
|
|
void* buffer,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t* bytes_read,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size > MAXDWORD)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, ERROR_INVALID_PARAMETER);
|
|
|
|
return false;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
DWORD my_bytes_read;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
overlapped.Offset = (DWORD)offset;
|
|
|
|
offset >>= 32;
|
|
|
|
overlapped.OffsetHigh = (DWORD)offset;
|
2021-09-15 00:18:09 +00:00
|
|
|
overlapped.hEvent = nullptr;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ReadFile(handle, buffer, (DWORD)size, &my_bytes_read, &overlapped))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_read != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_read = my_bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_file_write(tr_sys_file_t handle, void const* buffer, uint64_t size, uint64_t* bytes_written, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size > MAXDWORD)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, ERROR_INVALID_PARAMETER);
|
|
|
|
return false;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
DWORD my_bytes_written;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (WriteFile(handle, buffer, (DWORD)size, &my_bytes_written, nullptr))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_written != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_written = my_bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
bool tr_sys_file_write_at(
|
|
|
|
tr_sys_file_t handle,
|
|
|
|
void const* buffer,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t* bytes_written,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(buffer != nullptr || size == 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size > MAXDWORD)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, ERROR_INVALID_PARAMETER);
|
|
|
|
return false;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
DWORD my_bytes_written;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
overlapped.Offset = (DWORD)offset;
|
|
|
|
offset >>= 32;
|
|
|
|
overlapped.OffsetHigh = (DWORD)offset;
|
2021-09-15 00:18:09 +00:00
|
|
|
overlapped.hEvent = nullptr;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (WriteFile(handle, buffer, (DWORD)size, &my_bytes_written, &overlapped))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (bytes_written != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*bytes_written = my_bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_flush(tr_sys_file_t handle, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = FlushFileBuffers(handle);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_truncate(tr_sys_file_t handle, uint64_t size, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
FILE_END_OF_FILE_INFO info;
|
2017-04-19 12:04:45 +00:00
|
|
|
info.EndOfFile.QuadPart = size;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = SetFileInformationByHandle(handle, FileEndOfFileInfo, &info, sizeof(info));
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
bool tr_sys_file_advise(
|
|
|
|
[[maybe_unused]] tr_sys_file_t handle,
|
2021-10-24 16:41:54 +00:00
|
|
|
uint64_t /*offset*/,
|
2021-10-10 16:52:26 +00:00
|
|
|
[[maybe_unused]] uint64_t size,
|
|
|
|
[[maybe_unused]] tr_sys_file_advice_t advice,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_error** /*error*/)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
|
|
|
TR_ASSERT(size > 0);
|
2017-07-08 13:09:37 +00:00
|
|
|
TR_ASSERT(advice == TR_SYS_FILE_ADVICE_WILL_NEED || advice == TR_SYS_FILE_ADVICE_DONT_NEED);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-07-08 09:51:36 +00:00
|
|
|
bool ret = true;
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* ??? */
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_preallocate(tr_sys_file_t handle, uint64_t size, int flags, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & TR_SYS_FILE_PREALLOC_SPARSE) != 0)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD tmp;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!DeviceIoControl(handle, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, &tmp, nullptr))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
return false;
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_sys_file_truncate(handle, size, error);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_lock(tr_sys_file_t handle, int operation, tr_error** error)
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_FILE);
|
|
|
|
TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB | TR_SYS_FILE_LOCK_UN)) == 0);
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_ASSERT(
|
|
|
|
!!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) + !!(operation & TR_SYS_FILE_LOCK_UN) == 1);
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto overlapped = OVERLAPPED{};
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((operation & TR_SYS_FILE_LOCK_UN) == 0)
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD native_flags = 0;
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((operation & TR_SYS_FILE_LOCK_EX) != 0)
|
|
|
|
{
|
|
|
|
native_flags |= LOCKFILE_EXCLUSIVE_LOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((operation & TR_SYS_FILE_LOCK_NB) != 0)
|
|
|
|
{
|
|
|
|
native_flags |= LOCKFILE_FAIL_IMMEDIATELY;
|
|
|
|
}
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = LockFileEx(handle, native_flags, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE;
|
2016-09-21 20:56:03 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2016-09-21 20:56:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = UnlockFileEx(handle, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE;
|
2016-09-21 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2016-09-21 20:56:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 04:59:41 +00:00
|
|
|
std::string tr_sys_dir_get_current(tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
if (auto const size = GetCurrentDirectoryW(0, nullptr); size != 0)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
auto wide_ret = std::wstring{};
|
|
|
|
wide_ret.resize(size);
|
|
|
|
if (GetCurrentDirectoryW(std::size(wide_ret), std::data(wide_ret)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-05 16:36:01 +00:00
|
|
|
// `size` includes the terminating '\0'; remove it from `wide_ret`
|
|
|
|
wide_ret.resize(std::size(wide_ret) - 1);
|
2022-08-04 04:59:41 +00:00
|
|
|
return tr_win32_native_to_utf8(wide_ret);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 04:59:41 +00:00
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
return {};
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return create_dir(path, flags, permissions, true, error);
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void dir_create_temp_callback(char const* path, void* param, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool* result = (bool*)param;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(result != nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*result = create_dir(path, 0, 0, false, error);
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_dir_create_temp(char* path_template, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(path_template != nullptr);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
create_temp_path(path_template, dir_create_temp_callback, &ret, error);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_sys_dir_t tr_sys_dir_open(char const* path, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
TR_ASSERT(path != nullptr);
|
|
|
|
|
2022-09-05 00:40:34 +00:00
|
|
|
if (auto const info = tr_sys_path_get_info(path, 0); !info || !info->isFolder())
|
|
|
|
{
|
|
|
|
set_system_error(error, ERROR_DIRECTORY);
|
|
|
|
return TR_BAD_SYS_DIR;
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2022-08-05 19:16:25 +00:00
|
|
|
auto pattern = path_to_native_path(path);
|
2022-08-03 23:07:38 +00:00
|
|
|
if (std::empty(pattern))
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
2022-09-05 00:40:34 +00:00
|
|
|
return TR_BAD_SYS_DIR;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
auto* const ret = new tr_sys_dir_win32{};
|
|
|
|
ret->pattern = pattern;
|
|
|
|
ret->pattern.append(L"\\*");
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_sys_dir_read_name(tr_sys_dir_t handle, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_DIR);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
DWORD error_code = ERROR_SUCCESS;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (handle->find_handle == INVALID_HANDLE_VALUE)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
handle->find_handle = FindFirstFileW(handle->pattern.c_str(), &handle->find_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (handle->find_handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
error_code = GetLastError();
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!FindNextFileW(handle->find_handle, &handle->find_data))
|
|
|
|
{
|
|
|
|
error_code = GetLastError();
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error_code != ERROR_SUCCESS)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error_if_file_found(error, error_code);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
if (auto const utf8 = tr_win32_native_to_utf8(handle->find_data.cFileName); !std::empty(utf8))
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2022-08-03 23:07:38 +00:00
|
|
|
handle->utf8_name = utf8;
|
|
|
|
return handle->utf8_name.c_str();
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
return nullptr;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_dir_close(tr_sys_dir_t handle, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(handle != TR_BAD_SYS_DIR);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool ret = FindClose(handle->find_handle);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2022-08-03 23:07:38 +00:00
|
|
|
delete handle;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|