2014-07-03 21:58:39 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <ctype.h> /* isalpha() */
|
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 */
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
#include "transmission.h"
|
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"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2014-07-08 00:15:12 +00:00
|
|
|
#ifndef MAXSIZE_T
|
2017-04-19 12:04:45 +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
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
wchar_t* pattern;
|
|
|
|
HANDLE find_handle;
|
|
|
|
WIN32_FIND_DATAW find_data;
|
|
|
|
char* utf8_name;
|
2014-09-21 17:52:36 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* message;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (error == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
message = tr_win32_format_message(code);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (message != NULL)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_set_literal(error, code, message);
|
|
|
|
tr_free(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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_set(error, code, "Unknown error: 0x%08lx", 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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static time_t filetime_to_unix_time(FILETIME const* t)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t tmp = 0;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(t != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tmp |= t->dwHighDateTime;
|
|
|
|
tmp <<= 32;
|
|
|
|
tmp |= t->dwLowDateTime;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void stat_to_sys_path_info(DWORD attributes, DWORD size_low, DWORD size_high, FILETIME const* mtime,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_path_info* info)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(mtime != NULL);
|
|
|
|
assert(info != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (attributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
{
|
|
|
|
info->type = TR_SYS_PATH_IS_DIRECTORY;
|
|
|
|
}
|
|
|
|
else if (!(attributes & (FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_VIRTUAL)))
|
|
|
|
{
|
|
|
|
info->type = TR_SYS_PATH_IS_FILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->type = TR_SYS_PATH_IS_OTHER;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
info->size = size_high;
|
|
|
|
info->size <<= 32;
|
|
|
|
info->size |= size_low;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
info->last_modified_at = filetime_to_unix_time(mtime);
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline bool is_slash(char c)
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return c == '\\' || c == '/';
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool is_unc_path(char const* path)
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return is_slash(path[0]) && path[1] == path[0];
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool is_valid_path(char const* 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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* colon_pos = strchr(path, ':');
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (colon_pos != NULL)
|
2015-10-23 03:42:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (colon_pos != path + 1 || !isalpha(path[0]))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
path += 2;
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return strpbrk(path, "<>:\"|?*") == NULL;
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static wchar_t* path_to_native_path_ex(char const* path, int extra_chars_after, int* real_result_size)
|
2015-10-23 05:29:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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 */
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
wchar_t const local_prefix[] = { '\\', '\\', '?', '\\' };
|
|
|
|
wchar_t const unc_prefix[] = { '\\', '\\', '?', '\\', 'U', 'N', 'C', '\\' };
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const is_relative = tr_sys_path_is_relative(path);
|
|
|
|
bool const is_unc = is_unc_path(path);
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* `-2` for UNC since we overwrite existing prefix slashes */
|
2017-05-13 22:38:31 +00:00
|
|
|
int const extra_chars_before = is_relative ? 0 : (is_unc ? TR_N_ELEMENTS(unc_prefix) - 2 : TR_N_ELEMENTS(local_prefix));
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* TODO (?): assert(!is_relative); */
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wchar_t* const wide_path = tr_win32_utf8_to_native_ex(path, -1, extra_chars_before, extra_chars_after, real_result_size);
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path == NULL)
|
2015-10-23 05:29:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
2015-10-23 05:29:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Relative paths cannot be used with "\\?\" prefixes. This also means that relative paths are
|
|
|
|
limited to ~260 chars... but we should rarely work with relative paths in the first place */
|
|
|
|
if (!is_relative)
|
|
|
|
{
|
|
|
|
if (is_unc)
|
|
|
|
{
|
|
|
|
/* UNC path: "\\server\share" -> "\\?\UNC\server\share" */
|
|
|
|
memcpy(wide_path, unc_prefix, sizeof(unc_prefix));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Local path: "C:" -> "\\?\C:" */
|
|
|
|
memcpy(wide_path, local_prefix, sizeof(local_prefix));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Automatic '/' to '\' conversion is disabled for "\\?\"-prefixed paths */
|
|
|
|
wchar_t* p = wide_path + extra_chars_before;
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while ((p = wcschr(p, L'/')) != NULL)
|
|
|
|
{
|
|
|
|
*p++ = L'\\';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (real_result_size != NULL)
|
|
|
|
{
|
|
|
|
*real_result_size += extra_chars_before;
|
|
|
|
}
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return wide_path;
|
2015-10-23 05:29:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static wchar_t* path_to_native_path(char const* path)
|
2015-10-23 05:29:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return path_to_native_path_ex(path, 0, NULL);
|
2015-10-23 05:29:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static tr_sys_file_t open_file(char const* path, DWORD access, DWORD disposition, DWORD flags, 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;
|
|
|
|
wchar_t* wide_path;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_path = path_to_native_path(path);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path != NULL)
|
|
|
|
{
|
|
|
|
ret = CreateFileW(wide_path, access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disposition, flags,
|
|
|
|
NULL);
|
|
|
|
}
|
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
|
|
|
tr_free(wide_path);
|
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 bool create_dir(char const* path, int flags, int permissions, bool okay_if_exists, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
|
|
|
wchar_t* wide_path;
|
|
|
|
DWORD error_code = ERROR_SUCCESS;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)permissions;
|
2015-01-02 04:02:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_path = path_to_native_path(path);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & TR_SYS_DIR_CREATE_PARENTS) != 0)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
error_code = SHCreateDirectoryExW(NULL, wide_path, NULL);
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = CreateDirectoryW(wide_path, NULL);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
DWORD const attributes = GetFileAttributesW(wide_path);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
tr_free(wide_path);
|
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
|
|
|
static void create_temp_path(char* path_template, void (* callback)(char const* path, void* param,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error** error), void* callback_param, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* path;
|
|
|
|
size_t path_size;
|
|
|
|
tr_error* my_error = NULL;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path_template != NULL);
|
|
|
|
assert(callback != NULL);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
path = tr_strdup(path_template);
|
|
|
|
path_size = strlen(path);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path_size > 0);
|
2014-07-08 00:15:12 +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-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(*callback)(path, callback_param, &my_error);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_error == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (my_error != NULL)
|
|
|
|
{
|
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(path_template, path, path_size);
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(path);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
wchar_t* wide_path;
|
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_path = path_to_native_path(path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path != NULL)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD attributes = GetFileAttributesW(wide_path);
|
|
|
|
|
|
|
|
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_REPARSE_POINT)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
handle = CreateFileW(wide_path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
|
|
|
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
|
|
|
tr_free(wide_path);
|
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-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_get_info(char const* path, int flags, tr_sys_path_info* info, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
wchar_t* wide_path;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
|
|
|
assert(info != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_path = path_to_native_path(path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((flags & TR_SYS_PATH_NO_FOLLOW) == 0)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path != NULL)
|
|
|
|
{
|
|
|
|
handle = CreateFileW(wide_path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (handle != INVALID_HANDLE_VALUE)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* my_error = NULL;
|
|
|
|
ret = tr_sys_file_get_info(handle, info, &my_error);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(handle);
|
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
|
|
|
set_system_error(error, GetLastError());
|
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
|
|
|
WIN32_FILE_ATTRIBUTE_DATA attributes;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path != NULL)
|
|
|
|
{
|
|
|
|
ret = GetFileAttributesExW(wide_path, GetFileExInfoStandard, &attributes);
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
stat_to_sys_path_info(attributes.dwFileAttributes, attributes.nFileSizeLow, attributes.nFileSizeHigh,
|
|
|
|
&attributes.ftLastWriteTime, info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(wide_path);
|
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-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_is_relative(char const* path)
|
2015-10-20 21:22:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Local path: `X:` or `X:\...`. */
|
|
|
|
if (isalpha(path[0]) && path[1] == ':' && (path[2] == '\0' || is_slash(path[2])))
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_sys_path_is_same(char const* path1, char const* path2, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
wchar_t* wide_path1 = NULL;
|
|
|
|
wchar_t* wide_path2 = NULL;
|
|
|
|
HANDLE handle1 = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE handle2 = INVALID_HANDLE_VALUE;
|
|
|
|
BY_HANDLE_FILE_INFORMATION fi1, fi2;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path1 != NULL);
|
|
|
|
assert(path2 != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_path1 = path_to_native_path(path1);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path1 == NULL)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_path2 = path_to_native_path(path2);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path2 == NULL)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle1 = CreateFileW(wide_path1, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (handle1 == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
handle2 = CreateFileW(wide_path2, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (handle2 == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* TODO: Use GetFileInformationByHandleEx on >= Server 2012 */
|
|
|
|
|
|
|
|
if (!GetFileInformationByHandle(handle1, &fi1) || !GetFileInformationByHandle(handle2, &fi2))
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber && fi1.nFileIndexHigh == fi2.nFileIndexHigh &&
|
|
|
|
fi1.nFileIndexLow == fi2.nFileIndexLow;
|
|
|
|
|
|
|
|
goto cleanup;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
fail:
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error_if_file_found(error, GetLastError());
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
cleanup:
|
2017-04-19 12:04:45 +00:00
|
|
|
CloseHandle(handle2);
|
|
|
|
CloseHandle(handle1);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(wide_path2);
|
|
|
|
tr_free(wide_path1);
|
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-04-20 16:02:19 +00:00
|
|
|
char* tr_sys_path_resolve(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret = NULL;
|
|
|
|
wchar_t* wide_path;
|
|
|
|
wchar_t* wide_ret = NULL;
|
|
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
|
|
DWORD wide_ret_size;
|
|
|
|
|
|
|
|
assert(path != NULL);
|
|
|
|
|
|
|
|
wide_path = path_to_native_path(path);
|
|
|
|
|
|
|
|
if (wide_path == NULL)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = CreateFileW(wide_path, FILE_READ_EA, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
|
|
|
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
wide_ret_size = GetFinalPathNameByHandleW(handle, NULL, 0, 0);
|
|
|
|
|
|
|
|
if (wide_ret_size == 0)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
wide_ret = tr_new(wchar_t, wide_ret_size);
|
|
|
|
|
|
|
|
if (GetFinalPathNameByHandleW(handle, wide_ret, wide_ret_size, 0) != wide_ret_size - 1)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resolved path always begins with "\\?\", so skip those first four chars. */
|
|
|
|
ret = tr_win32_native_to_utf8(wide_ret + 4, -1);
|
|
|
|
|
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
fail:
|
2017-04-19 12:04:45 +00:00
|
|
|
set_system_error(error, GetLastError());
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(ret);
|
|
|
|
ret = NULL;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
cleanup:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(wide_ret);
|
|
|
|
tr_free(wide_path);
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_sys_path_basename(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (path == NULL || path[0] == '\0')
|
|
|
|
{
|
|
|
|
return tr_strdup(".");
|
|
|
|
}
|
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);
|
|
|
|
return NULL;
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* end = path + strlen(path);
|
2015-01-02 04:02:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (end > path && is_slash(*(end - 1)))
|
|
|
|
{
|
|
|
|
--end;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (end == path)
|
|
|
|
{
|
|
|
|
return tr_strdup("/");
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* name = end;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
while (name > path && *(name - 1) != ':' && !is_slash(*(name - 1)))
|
|
|
|
{
|
|
|
|
--name;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (name == end)
|
|
|
|
{
|
|
|
|
return tr_strdup("/");
|
|
|
|
}
|
2015-10-23 03:42:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_strndup(name, end - name);
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_sys_path_dirname(char const* path, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (path == NULL || path[0] == '\0')
|
|
|
|
{
|
|
|
|
return tr_strdup(".");
|
|
|
|
}
|
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);
|
|
|
|
return NULL;
|
2015-10-23 03:42:11 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const is_unc = is_unc_path(path);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (is_unc && path[2] == '\0')
|
|
|
|
{
|
|
|
|
return tr_strdup(path);
|
|
|
|
}
|
2015-01-02 04:02:13 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* end = path + strlen(path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (end > path && is_slash(*(end - 1)))
|
|
|
|
{
|
|
|
|
--end;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (end == path)
|
|
|
|
{
|
|
|
|
return tr_strdup("/");
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* name = end;
|
2015-10-23 03:42:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (name > path && *(name - 1) != ':' && !is_slash(*(name - 1)))
|
|
|
|
{
|
|
|
|
--name;
|
|
|
|
}
|
2015-10-23 03:42:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (name > path && is_slash(*(name - 1)))
|
|
|
|
{
|
|
|
|
--name;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (name == path)
|
|
|
|
{
|
|
|
|
return tr_strdup(is_unc ? "\\\\" : ".");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name > path && *(name - 1) == ':' && *name != '\0' && !is_slash(*name))
|
|
|
|
{
|
|
|
|
return tr_strdup_printf("%c:.", path[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tr_strndup(path, name - path);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
wchar_t* wide_src_path;
|
|
|
|
wchar_t* wide_dst_path;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(src_path != NULL);
|
|
|
|
assert(dst_path != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_src_path = path_to_native_path(src_path);
|
|
|
|
wide_dst_path = path_to_native_path(dst_path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_src_path != NULL && wide_dst_path != NULL)
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
attributes = GetFileAttributesW(wide_src_path);
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
attributes = GetFileAttributesW(wide_dst_path);
|
|
|
|
|
|
|
|
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
|
|
|
{
|
|
|
|
flags = 0;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = MoveFileExW(wide_src_path, wide_dst_path, 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
|
|
|
tr_free(wide_dst_path);
|
|
|
|
tr_free(wide_src_path);
|
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-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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
wchar_t* wide_path;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_path = path_to_native_path(path);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_path != NULL)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
DWORD const attributes = GetFileAttributesW(wide_path);
|
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)
|
|
|
|
{
|
|
|
|
ret = RemoveDirectoryW(wide_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = DeleteFileW(wide_path);
|
|
|
|
}
|
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
|
|
|
tr_free(wide_path);
|
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-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:
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(0 && "Unknown standard file");
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
else if (ret == NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
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
|
|
|
{
|
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-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
|
|
|
assert((flags & (TR_SYS_FILE_READ | TR_SYS_FILE_WRITE)) != 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)permissions;
|
2015-01-02 04:02:13 +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
|
|
|
|
2017-05-01 16:07:35 +00:00
|
|
|
if ((flags & TR_SYS_FILE_CREATE_NEW) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
native_disposition = CREATE_NEW;
|
|
|
|
}
|
2017-05-01 16:07:35 +00:00
|
|
|
else 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
success = SetFilePointer(ret, 0, NULL, FILE_END) != INVALID_SET_FILE_POINTER;
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
if (error == NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(result != NULL);
|
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
|
|
|
{
|
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
|
|
|
assert(path_template != NULL);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
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-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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_get_info(tr_sys_file_t handle, tr_sys_path_info* info, tr_error** error)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
|
|
|
BY_HANDLE_FILE_INFORMATION attributes;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(info != NULL);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = GetFileInformationByHandle(handle, &attributes);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
stat_to_sys_path_info(attributes.dwFileAttributes, attributes.nFileSizeLow, attributes.nFileSizeHigh,
|
|
|
|
&attributes.ftLastWriteTime, info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_seek(tr_sys_file_t handle, int64_t offset, tr_seek_origin_t origin, uint64_t* new_offset, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
LARGE_INTEGER native_offset, new_native_pointer;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_STATIC_ASSERT(TR_SEEK_SET == FILE_BEGIN, "values should match");
|
|
|
|
TR_STATIC_ASSERT(TR_SEEK_CUR == FILE_CURRENT, "values should match");
|
|
|
|
TR_STATIC_ASSERT(TR_SEEK_END == FILE_END, "values should match");
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(origin == TR_SEEK_SET || origin == TR_SEEK_CUR || origin == TR_SEEK_END);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
native_offset.QuadPart = offset;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (SetFilePointerEx(handle, native_offset, &new_native_pointer, origin))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (new_offset != NULL)
|
|
|
|
{
|
|
|
|
*new_offset = new_native_pointer.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_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-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
DWORD my_bytes_read;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(buffer != NULL || 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-04-19 12:04:45 +00:00
|
|
|
if (ReadFile(handle, buffer, (DWORD)size, &my_bytes_read, NULL))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_read != NULL)
|
|
|
|
{
|
|
|
|
*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-19 12:04:45 +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,
|
|
|
|
tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
DWORD my_bytes_read;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(buffer != NULL || 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-04-19 12:04:45 +00:00
|
|
|
overlapped.Offset = (DWORD)offset;
|
|
|
|
offset >>= 32;
|
|
|
|
overlapped.OffsetHigh = (DWORD)offset;
|
|
|
|
overlapped.hEvent = NULL;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_read != NULL)
|
|
|
|
{
|
|
|
|
*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-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
DWORD my_bytes_written;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(buffer != NULL || 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-04-19 12:04:45 +00:00
|
|
|
if (WriteFile(handle, buffer, (DWORD)size, &my_bytes_written, NULL))
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_written != NULL)
|
|
|
|
{
|
|
|
|
*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-20 16:02:19 +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-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
OVERLAPPED overlapped;
|
|
|
|
DWORD my_bytes_written;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(buffer != NULL || 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-04-19 12:04:45 +00:00
|
|
|
overlapped.Offset = (DWORD)offset;
|
|
|
|
offset >>= 32;
|
|
|
|
overlapped.OffsetHigh = (DWORD)offset;
|
|
|
|
overlapped.hEvent = NULL;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (bytes_written != NULL)
|
|
|
|
{
|
|
|
|
*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-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
|
|
|
FILE_END_OF_FILE_INFO info;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
info.EndOfFile.QuadPart = size;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_sys_file_prefetch(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(size > 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)handle;
|
|
|
|
(void)offset;
|
|
|
|
(void)size;
|
|
|
|
(void)error;
|
2015-01-02 04:02:13 +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-04-19 12:04:45 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (!DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &tmp, NULL))
|
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
|
|
|
void* tr_sys_file_map_for_reading(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void* ret = NULL;
|
|
|
|
HANDLE mappingHandle;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert(size > 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size > MAXSIZE_T)
|
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-04-19 12:04:45 +00:00
|
|
|
mappingHandle = CreateFileMappingW(handle, NULL, PAGE_READONLY, 0, 0, NULL);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (mappingHandle != NULL)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ULARGE_INTEGER native_offset;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
native_offset.QuadPart = offset;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = MapViewOfFile(mappingHandle, FILE_MAP_READ, native_offset.u.HighPart, native_offset.u.LowPart, (SIZE_T)size);
|
2014-07-08 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
CloseHandle(mappingHandle);
|
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_unmap(void const* address, uint64_t size, tr_error** error)
|
2014-07-08 00:15:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(address != NULL);
|
|
|
|
assert(size > 0);
|
2014-07-08 00:15:12 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)size;
|
2015-01-02 04:02:13 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = UnmapViewOfFile(address);
|
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
|
|
|
}
|
2014-09-21 17:52:36 +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-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
|
|
|
OVERLAPPED overlapped = { .Pointer = 0, .hEvent = NULL };
|
2016-09-21 20:56:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_FILE);
|
|
|
|
assert((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB | TR_SYS_FILE_LOCK_UN)) == 0);
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tr_sys_dir_get_current(tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret = NULL;
|
|
|
|
wchar_t* wide_ret = NULL;
|
|
|
|
DWORD size;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
size = GetCurrentDirectoryW(0, NULL);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size != 0)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
wide_ret = tr_new(wchar_t, size);
|
|
|
|
|
|
|
|
if (GetCurrentDirectoryW(size, wide_ret) != 0)
|
|
|
|
{
|
|
|
|
ret = tr_win32_native_to_utf8(wide_ret, size);
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
{
|
|
|
|
set_system_error(error, GetLastError());
|
|
|
|
}
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(wide_ret);
|
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
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(result != NULL);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool ret = false;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path_template != NULL);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_dir_t ret;
|
|
|
|
int pattern_size;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
|
|
|
#ifndef __clang__
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Clang gives "static_assert expression is not an integral constant expression" error */
|
|
|
|
TR_STATIC_ASSERT(TR_BAD_SYS_DIR == NULL, "values should match");
|
2014-09-21 17:52:36 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(path != NULL);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = tr_new(struct tr_sys_dir_win32, 1);
|
|
|
|
ret->pattern = path_to_native_path_ex(path, 2, &pattern_size);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret->pattern != NULL)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret->pattern[pattern_size + 0] = L'\\';
|
|
|
|
ret->pattern[pattern_size + 1] = L'*';
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret->find_handle = INVALID_HANDLE_VALUE;
|
|
|
|
ret->utf8_name = NULL;
|
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
|
|
|
set_system_error(error, GetLastError());
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(ret->pattern);
|
|
|
|
tr_free(ret);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = NULL;
|
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
|
|
|
char const* tr_sys_dir_read_name(tr_sys_dir_t handle, tr_error** error)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret;
|
|
|
|
DWORD error_code = ERROR_SUCCESS;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_DIR);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (handle->find_handle == INVALID_HANDLE_VALUE)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
handle->find_handle = FindFirstFileW(handle->pattern, &handle->find_data);
|
|
|
|
|
|
|
|
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);
|
|
|
|
return NULL;
|
2014-09-21 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = tr_win32_native_to_utf8(handle->find_data.cFileName, -1);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret != NULL)
|
2014-09-21 17:52:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(handle->utf8_name);
|
|
|
|
handle->utf8_name = ret;
|
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
|
|
|
set_system_error(error, GetLastError());
|
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-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-04-19 12:04:45 +00:00
|
|
|
bool ret;
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(handle != TR_BAD_SYS_DIR);
|
2014-09-21 17:52:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(handle->utf8_name);
|
|
|
|
tr_free(handle->pattern);
|
|
|
|
tr_free(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
|
|
|
}
|