2015-04-24 21:38:11 +00:00
|
|
|
#include "File.h"
|
2016-02-01 21:55:43 +00:00
|
|
|
#include "StrFmt.h"
|
|
|
|
#include "Macro.h"
|
|
|
|
#include "SharedMutex.h"
|
2016-04-25 10:49:12 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
#include <unordered_map>
|
2016-04-25 10:49:12 +00:00
|
|
|
#include <algorithm>
|
2016-04-26 22:27:24 +00:00
|
|
|
#include <cerrno>
|
2014-07-11 17:06:59 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
|
|
|
|
#include <cwchar>
|
2014-08-28 22:49:26 +00:00
|
|
|
#include <Windows.h>
|
2015-04-15 14:27:37 +00:00
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
static std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
|
2015-04-13 18:10:31 +00:00
|
|
|
{
|
2016-01-05 23:52:48 +00:00
|
|
|
const auto buf_size = source.size() + 1; // size + null terminator
|
2015-04-15 14:27:37 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
const int size = source.size() < INT_MAX ? static_cast<int>(buf_size) : throw fmt::exception("to_wchar(): invalid source length (0x%llx)", source.size());
|
2015-04-15 14:27:37 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
std::unique_ptr<wchar_t[]> buffer(new wchar_t[buf_size]); // allocate buffer assuming that length is the max possible size
|
2015-04-15 14:27:37 +00:00
|
|
|
|
|
|
|
if (!MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.get(), size))
|
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
throw fmt::exception("to_wchar(): MultiByteToWideChar() failed: error %u.", GetLastError());
|
2014-07-11 17:06:59 +00:00
|
|
|
}
|
2015-04-15 14:27:37 +00:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
static void to_utf8(std::string& result, const wchar_t* source)
|
2015-04-25 19:15:53 +00:00
|
|
|
{
|
2016-01-05 23:52:48 +00:00
|
|
|
const auto length = std::wcslen(source);
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
const int buf_size = length <= INT_MAX / 3 ? static_cast<int>(length) * 3 + 1 : throw fmt::exception("to_utf8(): invalid source length (0x%llx)", length);
|
2015-05-08 09:45:21 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
result.resize(buf_size); // set max possible length for utf-8 + null terminator
|
2015-05-08 09:45:21 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
if (const int nwritten = WideCharToMultiByte(CP_UTF8, 0, source, static_cast<int>(length) + 1, &result.front(), buf_size, NULL, NULL))
|
2015-05-08 09:45:21 +00:00
|
|
|
{
|
2016-01-05 23:52:48 +00:00
|
|
|
result.resize(nwritten - 1); // fix the size, remove null terminator
|
2015-05-08 09:45:21 +00:00
|
|
|
}
|
2016-01-05 23:52:48 +00:00
|
|
|
else
|
2015-04-25 19:15:53 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
throw fmt::exception("to_utf8(): WideCharToMultiByte() failed: error %u.", GetLastError());
|
2015-04-25 19:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
static time_t to_time(const ULARGE_INTEGER& ft)
|
2015-04-19 17:57:04 +00:00
|
|
|
{
|
|
|
|
return ft.QuadPart / 10000000ULL - 11644473600ULL;
|
|
|
|
}
|
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
static time_t to_time(const LARGE_INTEGER& ft)
|
2015-04-19 19:25:04 +00:00
|
|
|
{
|
|
|
|
ULARGE_INTEGER v;
|
|
|
|
v.LowPart = ft.LowPart;
|
|
|
|
v.HighPart = ft.HighPart;
|
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
return to_time(v);
|
2015-04-19 19:25:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
static time_t to_time(const FILETIME& ft)
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
2015-04-19 19:25:04 +00:00
|
|
|
ULARGE_INTEGER v;
|
2015-04-15 14:27:37 +00:00
|
|
|
v.LowPart = ft.dwLowDateTime;
|
|
|
|
v.HighPart = ft.dwHighDateTime;
|
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
return to_time(v);
|
2014-07-11 17:06:59 +00:00
|
|
|
}
|
2015-04-15 14:27:37 +00:00
|
|
|
|
2016-05-13 14:01:48 +00:00
|
|
|
static fs::error to_error(DWORD e)
|
|
|
|
{
|
|
|
|
switch (e)
|
|
|
|
{
|
|
|
|
case ERROR_FILE_NOT_FOUND: return fs::error::noent;
|
|
|
|
case ERROR_PATH_NOT_FOUND: return fs::error::noent;
|
|
|
|
case ERROR_ALREADY_EXISTS: return fs::error::exist;
|
|
|
|
case ERROR_FILE_EXISTS: return fs::error::exist;
|
|
|
|
case ERROR_NEGATIVE_SEEK: return fs::error::inval;
|
|
|
|
default: throw fmt::exception("Unknown Win32 error: %u.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:46:10 +00:00
|
|
|
#else
|
2016-01-05 23:52:48 +00:00
|
|
|
|
2015-11-01 10:33:28 +00:00
|
|
|
#include <sys/mman.h>
|
2015-04-19 16:02:35 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2015-04-25 20:21:06 +00:00
|
|
|
#include <dirent.h>
|
2015-04-13 14:46:10 +00:00
|
|
|
#include <fcntl.h>
|
2015-12-24 07:22:09 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <string.h>
|
2015-04-13 14:46:10 +00:00
|
|
|
#include <unistd.h>
|
2016-01-05 23:52:48 +00:00
|
|
|
|
2015-04-13 14:46:10 +00:00
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__)
|
|
|
|
#include <copyfile.h>
|
2015-12-16 19:50:45 +00:00
|
|
|
#include <mach-o/dyld.h>
|
2015-04-13 14:46:10 +00:00
|
|
|
#else
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2016-05-13 14:01:48 +00:00
|
|
|
static fs::error to_error(int e)
|
|
|
|
{
|
|
|
|
switch (e)
|
|
|
|
{
|
|
|
|
case ENOENT: return fs::error::noent;
|
|
|
|
case EEXIST: return fs::error::exist;
|
|
|
|
case EINVAL: return fs::error::inval;
|
|
|
|
default: throw fmt::exception("Unknown system error: %d.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
namespace fs
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
thread_local error g_tls_error = error::ok;
|
2016-04-26 22:27:24 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
class device_manager final
|
|
|
|
{
|
|
|
|
mutable shared_mutex m_mutex;
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::shared_ptr<device_base>> m_map;
|
|
|
|
|
|
|
|
public:
|
2016-04-25 10:49:12 +00:00
|
|
|
std::shared_ptr<device_base> get_device(const std::string& path);
|
2016-02-01 21:55:43 +00:00
|
|
|
std::shared_ptr<device_base> set_device(const std::string& name, const std::shared_ptr<device_base>&);
|
|
|
|
};
|
|
|
|
|
|
|
|
static device_manager& get_device_manager()
|
|
|
|
{
|
|
|
|
// Use magic static
|
|
|
|
static device_manager instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 10:49:12 +00:00
|
|
|
std::shared_ptr<fs::device_base> fs::device_manager::get_device(const std::string& path)
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
|
|
|
reader_lock lock(m_mutex);
|
|
|
|
|
2016-04-25 10:49:12 +00:00
|
|
|
const auto found = m_map.find(path.substr(0, path.find_first_of('/', 2)));
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
if (found == m_map.end())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found->second;
|
|
|
|
}
|
|
|
|
|
2016-04-25 10:49:12 +00:00
|
|
|
std::shared_ptr<fs::device_base> fs::device_manager::set_device(const std::string& name, const std::shared_ptr<device_base>& device)
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
2016-04-25 10:49:12 +00:00
|
|
|
writer_lock lock(m_mutex);
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
return m_map[name] = device;
|
|
|
|
}
|
|
|
|
|
2016-04-25 10:49:12 +00:00
|
|
|
std::shared_ptr<fs::device_base> fs::get_virtual_device(const std::string& path)
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
|
|
|
// Every virtual device path must have "//" at the beginning
|
2016-04-25 10:49:12 +00:00
|
|
|
if (path.size() > 2 && reinterpret_cast<const u16&>(path.front()) == "//"_u16)
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
2016-04-25 10:49:12 +00:00
|
|
|
return get_device_manager().get_device(path);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-25 10:49:12 +00:00
|
|
|
std::shared_ptr<fs::device_base> fs::set_virtual_device(const std::string& name, const std::shared_ptr<device_base>& device)
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
EXPECTS(name.size() > 2 && name[0] == '/' && name[1] == '/' && name.find('/', 2) == -1);
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
return get_device_manager().set_device(name, device);
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
std::string fs::get_parent_dir(const std::string& path)
|
|
|
|
{
|
|
|
|
// Search upper bound (set to the last character, npos for empty string)
|
|
|
|
auto last = path.size() - 1;
|
2015-04-15 14:27:37 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
const auto& delim = "/\\";
|
|
|
|
#else
|
|
|
|
const auto& delim = "/";
|
2014-10-24 13:24:09 +00:00
|
|
|
#endif
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const auto pos = path.find_last_of(delim, last, sizeof(delim) - 1);
|
2015-08-12 01:52:26 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
// Contiguous slashes are ignored at the end
|
|
|
|
if (std::exchange(last, pos - 1) != pos)
|
|
|
|
{
|
|
|
|
// Return empty string if the path doesn't contain at least 2 elements
|
|
|
|
return path.substr(0, pos != -1 && path.find_last_not_of(delim, pos, sizeof(delim) - 1) != -1 ? pos : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const auto test_get_parent_dir = []() -> bool
|
2015-04-13 18:10:31 +00:00
|
|
|
{
|
2016-01-05 23:52:48 +00:00
|
|
|
// Success:
|
2016-05-13 14:01:48 +00:00
|
|
|
VERIFY(fs::get_parent_dir("/x/y///") == "/x");
|
|
|
|
VERIFY(fs::get_parent_dir("/x/y/") == "/x");
|
|
|
|
VERIFY(fs::get_parent_dir("/x/y") == "/x");
|
|
|
|
VERIFY(fs::get_parent_dir("x:/y") == "x:");
|
|
|
|
VERIFY(fs::get_parent_dir("//x/y") == "//x");
|
2016-01-05 23:52:48 +00:00
|
|
|
|
|
|
|
// Failure:
|
2016-05-13 14:01:48 +00:00
|
|
|
VERIFY(fs::get_parent_dir("").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("x/").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("x").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("x///").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("/x/").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("/x").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("/").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("//").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("//x").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("//x/").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("///").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("///x").empty());
|
|
|
|
VERIFY(fs::get_parent_dir("///x/").empty());
|
2016-01-05 23:52:48 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}();
|
2015-08-12 01:52:26 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::stat(const std::string& path, stat_t& info)
|
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
return device->stat(path, info);
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:20:00 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA attrs;
|
2015-06-18 11:55:11 +00:00
|
|
|
if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs))
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2014-07-31 18:20:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-15 14:27:37 +00:00
|
|
|
|
2015-04-24 21:38:11 +00:00
|
|
|
info.is_directory = (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
info.is_writable = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
|
2015-04-25 19:15:53 +00:00
|
|
|
info.size = (u64)attrs.nFileSizeLow | ((u64)attrs.nFileSizeHigh << 32);
|
2015-12-16 19:50:45 +00:00
|
|
|
info.atime = to_time(attrs.ftLastAccessTime);
|
|
|
|
info.mtime = to_time(attrs.ftLastWriteTime);
|
|
|
|
info.ctime = to_time(attrs.ftCreationTime);
|
2014-07-31 18:20:00 +00:00
|
|
|
#else
|
2016-01-05 23:52:48 +00:00
|
|
|
struct ::stat file_info;
|
|
|
|
if (::stat(path.c_str(), &file_info) != 0)
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2014-07-31 18:20:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-24 21:38:11 +00:00
|
|
|
info.is_directory = S_ISDIR(file_info.st_mode);
|
|
|
|
info.is_writable = file_info.st_mode & 0200; // HACK: approximation
|
2015-04-15 14:27:37 +00:00
|
|
|
info.size = file_info.st_size;
|
|
|
|
info.atime = file_info.st_atime;
|
|
|
|
info.mtime = file_info.st_mtime;
|
|
|
|
info.ctime = file_info.st_ctime;
|
2014-07-31 18:20:00 +00:00
|
|
|
#endif
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2014-07-31 18:20:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-24 21:38:11 +00:00
|
|
|
bool fs::exists(const std::string& path)
|
2015-04-20 15:53:31 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
stat_t info;
|
|
|
|
return device->stat(path, info);
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:53:31 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
if (GetFileAttributesW(to_wchar(path).get()) == INVALID_FILE_ATTRIBUTES)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-01-05 23:52:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-04-20 15:53:31 +00:00
|
|
|
#else
|
2016-01-05 23:52:48 +00:00
|
|
|
struct ::stat file_info;
|
2016-04-26 22:27:24 +00:00
|
|
|
if (::stat(path.c_str(), &file_info) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-04-26 22:27:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-04-20 15:53:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::is_file(const std::string& path)
|
2015-04-20 15:53:31 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
stat_t info;
|
|
|
|
if (!device->stat(path, info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.is_directory)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = error::exist;
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:53:31 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
const DWORD attrs = GetFileAttributesW(to_wchar(path).get());
|
|
|
|
if (attrs == INVALID_FILE_ATTRIBUTES)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-01-05 23:52:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct ::stat file_info;
|
|
|
|
if (::stat(path.c_str(), &file_info) != 0)
|
2015-04-20 15:53:31 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2015-04-20 15:53:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-05 23:52:48 +00:00
|
|
|
#endif
|
2015-04-20 15:53:31 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
// TODO: correct file type check
|
|
|
|
#ifdef _WIN32
|
|
|
|
if ((attrs & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
2015-04-20 15:53:31 +00:00
|
|
|
#else
|
2016-01-05 23:52:48 +00:00
|
|
|
if (S_ISDIR(file_info.st_mode))
|
|
|
|
#endif
|
2015-04-20 15:53:31 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = error::exist;
|
2015-04-20 15:53:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
return true;
|
2015-04-20 15:53:31 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::is_dir(const std::string& path)
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
stat_t info;
|
|
|
|
if (!device->stat(path, info))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.is_directory == false)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = error::exist;
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
const DWORD attrs = GetFileAttributesW(to_wchar(path).get());
|
|
|
|
if (attrs == INVALID_FILE_ATTRIBUTES)
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2015-04-15 14:27:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2016-01-05 23:52:48 +00:00
|
|
|
struct ::stat file_info;
|
|
|
|
if (::stat(path.c_str(), &file_info) != 0)
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2015-04-15 14:27:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2015-08-12 01:52:26 +00:00
|
|
|
|
2014-08-29 13:06:58 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
if ((attrs & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
2014-08-29 13:06:58 +00:00
|
|
|
#else
|
2016-01-05 23:52:48 +00:00
|
|
|
if (!S_ISDIR(file_info.st_mode))
|
2014-08-29 13:06:58 +00:00
|
|
|
#endif
|
2015-04-18 13:38:42 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = error::exist;
|
2015-04-18 13:38:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::create_dir(const std::string& path)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
return device->create_dir(path);
|
|
|
|
}
|
|
|
|
|
2015-12-24 07:22:09 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
if (!CreateDirectoryW(to_wchar(path).get(), NULL))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-01-05 23:52:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-12-24 07:22:09 +00:00
|
|
|
#else
|
2016-04-26 22:27:24 +00:00
|
|
|
if (::mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-04-26 22:27:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-12-24 07:22:09 +00:00
|
|
|
#endif
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
2015-04-20 15:53:31 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::create_path(const std::string& path)
|
|
|
|
{
|
|
|
|
const auto& parent = get_parent_dir(path);
|
|
|
|
|
|
|
|
if (!parent.empty() && !is_dir(parent) && !create_path(parent))
|
|
|
|
{
|
2015-12-24 07:22:09 +00:00
|
|
|
return false;
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
2015-12-24 07:22:09 +00:00
|
|
|
|
|
|
|
return create_dir(path);
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::remove_dir(const std::string& path)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
return device->remove_dir(path);
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:20:00 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
if (!RemoveDirectoryW(to_wchar(path).get()))
|
2014-10-24 13:24:09 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2014-07-31 18:20:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-13 14:46:10 +00:00
|
|
|
|
2014-07-31 18:20:00 +00:00
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#else
|
2016-04-26 22:27:24 +00:00
|
|
|
if (::rmdir(path.c_str()) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-04-26 22:27:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#endif
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 21:38:11 +00:00
|
|
|
bool fs::rename(const std::string& from, const std::string& to)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
const auto device = get_virtual_device(from);
|
|
|
|
|
|
|
|
if (device != get_virtual_device(to))
|
|
|
|
{
|
|
|
|
throw fmt::exception("fs::rename() between different devices not implemented.\nFrom: %s\nTo: %s", from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device)
|
|
|
|
{
|
|
|
|
return device->rename(from, to);
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:20:00 +00:00
|
|
|
#ifdef _WIN32
|
2015-06-18 11:55:11 +00:00
|
|
|
if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get()))
|
2015-04-13 14:46:10 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2015-04-13 14:46:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#else
|
2016-04-26 22:27:24 +00:00
|
|
|
if (::rename(from.c_str(), to.c_str()) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-04-26 22:27:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#endif
|
2015-04-13 14:46:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite)
|
2015-04-13 14:46:10 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
const auto device = get_virtual_device(from);
|
|
|
|
|
|
|
|
if (device != get_virtual_device(to) || device) // TODO
|
|
|
|
{
|
|
|
|
throw fmt::exception("fs::copy_file() for virtual devices not implemented.\nFrom: %s\nTo: %s", from, to);
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-01-05 23:52:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#else
|
2015-11-01 10:33:28 +00:00
|
|
|
/* Source: http://stackoverflow.com/questions/2180079/how-can-i-copy-a-file-on-unix-using-c */
|
2015-04-13 14:46:10 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
const int input = ::open(from.c_str(), O_RDONLY);
|
2015-11-01 10:33:28 +00:00
|
|
|
if (input == -1)
|
2015-04-13 14:46:10 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-01-05 23:52:48 +00:00
|
|
|
return false;
|
2015-04-13 14:46:10 +00:00
|
|
|
}
|
2015-11-01 10:33:28 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
const int output = ::open(to.c_str(), O_WRONLY | O_CREAT | (overwrite ? O_TRUNC : O_EXCL), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2015-11-01 10:33:28 +00:00
|
|
|
if (output == -1)
|
2015-04-13 14:46:10 +00:00
|
|
|
{
|
2016-01-05 23:52:48 +00:00
|
|
|
const int err = errno;
|
|
|
|
|
|
|
|
::close(input);
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(err);
|
2016-01-05 23:52:48 +00:00
|
|
|
return false;
|
2015-04-13 14:46:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
// Here we use kernel-space copying for performance reasons
|
2015-04-13 14:46:10 +00:00
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__)
|
2016-01-05 23:52:48 +00:00
|
|
|
// fcopyfile works on FreeBSD and OS X 10.5+
|
|
|
|
if (::fcopyfile(input, output, 0, COPYFILE_ALL))
|
2015-04-13 14:46:10 +00:00
|
|
|
#else
|
2016-01-05 23:52:48 +00:00
|
|
|
// sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+
|
|
|
|
off_t bytes_copied = 0;
|
|
|
|
struct ::stat fileinfo = { 0 };
|
|
|
|
if (::fstat(input, &fileinfo) || ::sendfile(output, input, &bytes_copied, fileinfo.st_size))
|
2015-04-13 14:46:10 +00:00
|
|
|
#endif
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
|
|
|
const int err = errno;
|
2015-04-13 14:46:10 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
::close(input);
|
|
|
|
::close(output);
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(err);
|
2016-01-05 23:52:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-13 14:46:10 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
::close(input);
|
|
|
|
::close(output);
|
|
|
|
return true;
|
2015-04-13 14:46:10 +00:00
|
|
|
#endif
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
2015-04-13 14:46:10 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::remove_file(const std::string& path)
|
2015-04-13 14:46:10 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
return device->remove(path);
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:46:10 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
if (!DeleteFileW(to_wchar(path).get()))
|
2015-03-16 16:20:02 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2015-03-16 16:20:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-13 14:46:10 +00:00
|
|
|
|
2015-03-16 16:20:02 +00:00
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#else
|
2016-04-26 22:27:24 +00:00
|
|
|
if (::unlink(path.c_str()) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-04-26 22:27:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#endif
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
bool fs::truncate_file(const std::string& path, u64 length)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
|
|
|
{
|
|
|
|
return device->trunc(path, length);
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:20:00 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-05 23:52:48 +00:00
|
|
|
// Open the file
|
|
|
|
const auto handle = CreateFileW(to_wchar(path).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2014-10-24 13:24:09 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2014-08-03 23:52:36 +00:00
|
|
|
return false;
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
2015-04-18 13:38:42 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
LARGE_INTEGER distance;
|
|
|
|
distance.QuadPart = length;
|
2015-08-12 01:52:26 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
// Seek and truncate
|
|
|
|
if (!SetFilePointerEx(handle, distance, NULL, FILE_BEGIN) || !SetEndOfFile(handle))
|
2015-04-18 13:38:42 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-01-05 23:52:48 +00:00
|
|
|
CloseHandle(handle);
|
2015-04-18 13:38:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
CloseHandle(handle);
|
2014-08-03 23:52:36 +00:00
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#else
|
2016-04-26 22:27:24 +00:00
|
|
|
if (::truncate(path.c_str(), length) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-04-26 22:27:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-01-05 23:52:48 +00:00
|
|
|
#endif
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
void fs::file::xnull() const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
throw std::logic_error("fs::file is null");
|
2015-04-19 16:02:35 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
void fs::file::xfail() const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Unexpected fs::error %u", g_tls_error);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2016-04-26 22:27:24 +00:00
|
|
|
bool fs::file::open(const std::string& path, bitset_t<open_mode> mode)
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
|
|
|
if (auto device = get_virtual_device(path))
|
2015-08-12 01:52:26 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto&& _file = device->open(path, mode))
|
|
|
|
{
|
|
|
|
m_file = std::move(_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-04-19 16:02:35 +00:00
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD access = 0;
|
|
|
|
if (mode & fs::read) access |= GENERIC_READ;
|
|
|
|
if (mode & fs::write) access |= mode & fs::append ? FILE_APPEND_DATA : GENERIC_WRITE;
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
DWORD disp = 0;
|
2016-02-01 21:55:43 +00:00
|
|
|
if (mode & fs::create)
|
2015-04-19 13:19:24 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
disp =
|
|
|
|
mode & fs::excl ? CREATE_NEW :
|
|
|
|
mode & fs::trunc ? CREATE_ALWAYS : OPEN_ALWAYS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mode & fs::excl)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = error::inval;
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
disp = mode & fs::trunc ? TRUNCATE_EXISTING : OPEN_EXISTING;
|
2015-04-19 16:02:35 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
const HANDLE handle = CreateFileW(to_wchar(path).get(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
|
2016-01-05 23:52:48 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2015-04-19 13:19:24 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2015-04-19 13:19:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
class windows_file final : public file_base
|
2015-04-19 16:02:35 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
const HANDLE m_handle;
|
2015-04-19 16:02:35 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
public:
|
|
|
|
windows_file(HANDLE handle)
|
|
|
|
: m_handle(handle)
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
~windows_file() override
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
CloseHandle(m_handle);
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
stat_t stat() override
|
|
|
|
{
|
|
|
|
FILE_BASIC_INFO basic_info;
|
|
|
|
if (!GetFileInformationByHandleEx(m_handle, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO)))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Win32 error: %u." HERE, GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stat_t info;
|
|
|
|
info.is_directory = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
info.is_writable = (basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
|
|
|
|
info.size = this->size();
|
|
|
|
info.atime = to_time(basic_info.LastAccessTime);
|
|
|
|
info.mtime = to_time(basic_info.ChangeTime);
|
|
|
|
info.ctime = to_time(basic_info.CreationTime);
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
2016-01-05 23:52:48 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
bool trunc(u64 length) override
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
LARGE_INTEGER old, pos;
|
|
|
|
|
|
|
|
pos.QuadPart = 0;
|
|
|
|
if (!SetFilePointerEx(m_handle, pos, &old, FILE_CURRENT)) // get old position
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos.QuadPart = length;
|
|
|
|
if (!SetFilePointerEx(m_handle, pos, NULL, FILE_BEGIN)) // set new position
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BOOL result = SetEndOfFile(m_handle); // change file size
|
|
|
|
|
2016-05-13 14:01:48 +00:00
|
|
|
if (!result || !SetFilePointerEx(m_handle, old, NULL, FILE_BEGIN)) // restore position
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Win32 error: %u." HERE, GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result != FALSE;
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
2015-04-19 17:57:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 read(void* buffer, u64 count) override
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
// TODO (call ReadFile multiple times if count is too big)
|
|
|
|
const int size = ::narrow<int>(count, "Too big count" HERE);
|
2016-05-13 14:01:48 +00:00
|
|
|
EXPECTS(size >= 0);
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
DWORD nread;
|
|
|
|
if (!ReadFile(m_handle, buffer, size, &nread, NULL))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Win32 error: %u." HERE, GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 write(const void* buffer, u64 count) override
|
|
|
|
{
|
|
|
|
// TODO (call WriteFile multiple times if count is too big)
|
|
|
|
const int size = ::narrow<int>(count, "Too big count" HERE);
|
2016-05-13 14:01:48 +00:00
|
|
|
EXPECTS(size >= 0);
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
DWORD nwritten;
|
|
|
|
if (!WriteFile(m_handle, buffer, size, &nwritten, NULL))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Win32 error: %u." HERE, GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nwritten;
|
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 seek(s64 offset, seek_mode whence) override
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
LARGE_INTEGER pos;
|
|
|
|
pos.QuadPart = offset;
|
|
|
|
|
|
|
|
const DWORD mode =
|
|
|
|
whence == seek_set ? FILE_BEGIN :
|
|
|
|
whence == seek_cur ? FILE_CURRENT :
|
|
|
|
whence == seek_end ? FILE_END :
|
|
|
|
throw fmt::exception("Invalid whence (0x%x)" HERE, whence);
|
|
|
|
|
|
|
|
if (!SetFilePointerEx(m_handle, pos, &pos, mode))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Win32 error: %u." HERE, GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos.QuadPart;
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 size() override
|
|
|
|
{
|
|
|
|
LARGE_INTEGER size;
|
|
|
|
if (!GetFileSizeEx(m_handle, &size))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Win32 error: %u." HERE, GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size.QuadPart;
|
|
|
|
}
|
|
|
|
};
|
2015-04-19 17:57:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
m_file = std::make_unique<windows_file>(handle);
|
2015-04-19 17:57:04 +00:00
|
|
|
#else
|
2016-02-01 21:55:43 +00:00
|
|
|
int flags = 0;
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
if (mode & fs::read && mode & fs::write) flags |= O_RDWR;
|
|
|
|
else if (mode & fs::read) flags |= O_RDONLY;
|
|
|
|
else if (mode & fs::write) flags |= O_WRONLY;
|
2015-04-19 17:57:04 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
if (mode & fs::append) flags |= O_APPEND;
|
|
|
|
if (mode & fs::create) flags |= O_CREAT;
|
|
|
|
if (mode & fs::trunc) flags |= O_TRUNC;
|
|
|
|
if (mode & fs::excl) flags |= O_EXCL;
|
2015-04-24 14:06:30 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
const int fd = ::open(path.c_str(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
2015-04-24 14:06:30 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
if (fd == -1)
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
class unix_file final : public file_base
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
const int m_fd;
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
public:
|
|
|
|
unix_file(int fd)
|
|
|
|
: m_fd(fd)
|
|
|
|
{
|
|
|
|
}
|
2015-06-18 11:55:11 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
~unix_file() override
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
::close(m_fd);
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
stat_t stat() override
|
|
|
|
{
|
|
|
|
struct ::stat file_info;
|
|
|
|
if (::fstat(m_fd, &file_info) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("System error: %d." HERE, errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stat_t info;
|
|
|
|
info.is_directory = S_ISDIR(file_info.st_mode);
|
|
|
|
info.is_writable = file_info.st_mode & 0200; // HACK: approximation
|
|
|
|
info.size = file_info.st_size;
|
|
|
|
info.atime = file_info.st_atime;
|
|
|
|
info.mtime = file_info.st_mtime;
|
|
|
|
info.ctime = file_info.st_ctime;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
bool trunc(u64 length) override
|
|
|
|
{
|
|
|
|
if (::ftruncate(m_fd, length) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-18 11:55:11 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 read(void* buffer, u64 count) override
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
const auto result = ::read(m_fd, buffer, count);
|
|
|
|
if (result == -1)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("System error: %d." HERE, errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 write(const void* buffer, u64 count) override
|
|
|
|
{
|
|
|
|
const auto result = ::write(m_fd, buffer, count);
|
|
|
|
if (result == -1)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("System error: %d." HERE, errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 seek(s64 offset, seek_mode whence) override
|
|
|
|
{
|
|
|
|
const int mode =
|
|
|
|
whence == seek_set ? SEEK_SET :
|
|
|
|
whence == seek_cur ? SEEK_CUR :
|
|
|
|
whence == seek_end ? SEEK_END :
|
|
|
|
throw fmt::exception("Invalid whence (0x%x)" HERE, whence);
|
|
|
|
|
|
|
|
const auto result = ::lseek(m_fd, offset, mode);
|
|
|
|
if (result == -1)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("System error: %d." HERE, errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 size() override
|
|
|
|
{
|
|
|
|
struct ::stat file_info;
|
|
|
|
if (::fstat(m_fd, &file_info) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("System error: %d." HERE, errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return file_info.st_size;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
m_file = std::make_unique<unix_file>(fd);
|
2015-04-19 13:19:24 +00:00
|
|
|
#endif
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
return true;
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
fs::file::file(const void* ptr, std::size_t size)
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2016-04-25 10:49:12 +00:00
|
|
|
class memory_stream : public file_base
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
u64 m_pos{};
|
2015-08-12 01:52:26 +00:00
|
|
|
|
2016-04-25 10:49:12 +00:00
|
|
|
const char* const m_ptr;
|
|
|
|
const u64 m_size;
|
2016-02-01 21:55:43 +00:00
|
|
|
|
2016-04-25 10:49:12 +00:00
|
|
|
public:
|
|
|
|
memory_stream(const void* ptr, u64 size)
|
|
|
|
: m_ptr(static_cast<const char*>(ptr))
|
|
|
|
, m_size(size)
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
fs::stat_t stat() override
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw std::logic_error("Not supported" HERE);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
bool trunc(u64 length) override
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw std::logic_error("Not allowed" HERE);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
2015-08-12 01:52:26 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 read(void* buffer, u64 count) override
|
|
|
|
{
|
|
|
|
const u64 start = m_pos;
|
|
|
|
const u64 end = seek(count, fs::seek_cur);
|
2016-05-13 14:01:48 +00:00
|
|
|
const u64 read_size = end >= start ? end - start : throw std::logic_error("Stream overflow" HERE);
|
2016-04-25 10:49:12 +00:00
|
|
|
std::memcpy(buffer, m_ptr + start, read_size);
|
2016-02-01 21:55:43 +00:00
|
|
|
return read_size;
|
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 write(const void* buffer, u64 count) override
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw std::logic_error("Not allowed" HERE);
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 seek(s64 offset, fs::seek_mode whence) override
|
|
|
|
{
|
2016-04-25 10:49:12 +00:00
|
|
|
return
|
|
|
|
whence == fs::seek_set ? m_pos = std::min<u64>(offset, m_size) :
|
|
|
|
whence == fs::seek_cur ? m_pos = std::min<u64>(offset + m_pos, m_size) :
|
|
|
|
whence == fs::seek_end ? m_pos = std::min<u64>(offset + m_size, m_size) :
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("Invalid whence (0x%x)" HERE, whence);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
u64 size() override
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
};
|
2015-04-19 13:19:24 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
m_file = std::make_unique<memory_stream>(ptr, size);
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
void fs::dir::xnull() const
|
2014-08-05 22:34:26 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
throw std::logic_error("fs::dir is null");
|
2014-08-05 22:34:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
bool fs::dir::open(const std::string& path)
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto device = get_virtual_device(path))
|
2015-04-25 19:15:53 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
if (auto&& _dir = device->open_dir(path))
|
|
|
|
{
|
|
|
|
m_dir = std::move(_dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-25 19:15:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FIND_DATAW found;
|
2016-02-01 21:55:43 +00:00
|
|
|
const auto handle = FindFirstFileW(to_wchar(path + "/*").get(), &found);
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2015-04-25 20:55:15 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
return false;
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
2015-08-12 01:52:26 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
class windows_dir final : public dir_base
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
const HANDLE m_handle;
|
|
|
|
|
|
|
|
std::vector<dir_entry> m_entries;
|
|
|
|
std::size_t m_pos = 0;
|
|
|
|
|
|
|
|
void add_entry(const WIN32_FIND_DATAW& found)
|
2016-01-05 23:52:48 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
dir_entry info;
|
|
|
|
|
|
|
|
to_utf8(info.name, found.cFileName);
|
|
|
|
info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
|
|
|
|
info.size = ((u64)found.nFileSizeHigh << 32) | (u64)found.nFileSizeLow;
|
|
|
|
info.atime = to_time(found.ftLastAccessTime);
|
|
|
|
info.mtime = to_time(found.ftLastWriteTime);
|
|
|
|
info.ctime = to_time(found.ftCreationTime);
|
|
|
|
|
|
|
|
m_entries.emplace_back(std::move(info));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
windows_dir(HANDLE handle, const WIN32_FIND_DATAW& found)
|
|
|
|
: m_handle(handle)
|
2015-09-26 20:46:04 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
add_entry(found);
|
2015-09-26 20:46:04 +00:00
|
|
|
}
|
2016-01-05 23:52:48 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
~windows_dir()
|
|
|
|
{
|
|
|
|
FindClose(m_handle);
|
2016-01-05 23:52:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
bool read(dir_entry& out) override
|
|
|
|
{
|
|
|
|
if (m_pos == m_entries.size())
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATAW found;
|
|
|
|
if (!FindNextFileW(m_handle, &found))
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
if (ERROR_NO_MORE_FILES == GetLastError())
|
2016-02-01 21:55:43 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
return false;
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
2016-05-13 14:01:48 +00:00
|
|
|
|
|
|
|
throw fmt::exception("Win32 error: %u." HERE, GetLastError());
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add_entry(found);
|
|
|
|
}
|
|
|
|
|
|
|
|
out = m_entries[m_pos++];
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
void rewind() override
|
|
|
|
{
|
|
|
|
m_pos = 0;
|
|
|
|
}
|
|
|
|
};
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
m_dir = std::make_unique<windows_dir>(handle, found);
|
2015-04-25 19:15:53 +00:00
|
|
|
#else
|
2016-02-01 21:55:43 +00:00
|
|
|
::DIR* const ptr = ::opendir(path.c_str());
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
if (!ptr)
|
2015-04-25 19:15:53 +00:00
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
g_tls_error = to_error(errno);
|
2015-04-25 19:15:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
class unix_dir final : public dir_base
|
|
|
|
{
|
|
|
|
::DIR* m_dd;
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
public:
|
|
|
|
unix_dir(::DIR* dd)
|
|
|
|
: m_dd(dd)
|
|
|
|
{
|
|
|
|
}
|
2015-04-25 19:15:53 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
~unix_dir() override
|
|
|
|
{
|
|
|
|
::closedir(m_dd);
|
|
|
|
}
|
2015-12-16 19:50:45 +00:00
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
bool read(dir_entry& info) override
|
|
|
|
{
|
|
|
|
const auto found = ::readdir(m_dd);
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ::stat file_info;
|
|
|
|
if (::fstatat(::dirfd(m_dd), found->d_name, &file_info, 0) != 0)
|
|
|
|
{
|
2016-05-13 14:01:48 +00:00
|
|
|
throw fmt::exception("System error: %d." HERE, errno);
|
2016-02-01 21:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info.name = found->d_name;
|
|
|
|
info.is_directory = S_ISDIR(file_info.st_mode);
|
|
|
|
info.is_writable = file_info.st_mode & 0200; // HACK: approximation
|
|
|
|
info.size = file_info.st_size;
|
|
|
|
info.atime = file_info.st_atime;
|
|
|
|
info.mtime = file_info.st_mtime;
|
|
|
|
info.ctime = file_info.st_ctime;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewind() override
|
|
|
|
{
|
|
|
|
::rewinddir(m_dd);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
m_dir = std::make_unique<unix_dir>(ptr);
|
2015-12-22 19:24:35 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-01 21:55:43 +00:00
|
|
|
return true;
|
2015-12-22 19:24:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
const std::string& fs::get_config_dir()
|
2015-12-16 19:50:45 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
// Use magic static
|
2015-12-16 19:50:45 +00:00
|
|
|
static const std::string s_dir = []
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return get_executable_dir(); // ?
|
|
|
|
#else
|
|
|
|
std::string dir;
|
|
|
|
|
|
|
|
if (const char* home = ::getenv("XDG_CONFIG_HOME"))
|
|
|
|
dir = home;
|
|
|
|
else if (const char* home = ::getenv("HOME"))
|
|
|
|
dir = home + "/.config"s;
|
|
|
|
else // Just in case
|
|
|
|
dir = "./config";
|
|
|
|
|
|
|
|
dir += "/rpcs3/";
|
|
|
|
|
2015-12-24 07:22:09 +00:00
|
|
|
if (!is_dir(dir) && !create_path(dir))
|
2015-12-16 19:50:45 +00:00
|
|
|
{
|
|
|
|
std::printf("Failed to create configuration directory '%s' (%d).\n", dir.c_str(), errno);
|
2016-01-05 23:52:48 +00:00
|
|
|
return get_executable_dir();
|
2015-12-16 19:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
#endif
|
|
|
|
}();
|
|
|
|
|
|
|
|
return s_dir;
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
const std::string& fs::get_executable_dir()
|
2015-12-16 19:50:45 +00:00
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
// Use magic static
|
2015-12-16 19:50:45 +00:00
|
|
|
static const std::string s_dir = []
|
|
|
|
{
|
|
|
|
std::string dir;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
wchar_t buf[2048];
|
|
|
|
if (GetModuleFileName(NULL, buf, ::size32(buf)) - 1 >= ::size32(buf) - 1)
|
|
|
|
{
|
2016-02-01 21:55:43 +00:00
|
|
|
MessageBoxA(0, fmt::format("GetModuleFileName() failed: error %u.", GetLastError()).c_str(), "fs::get_config_dir()", MB_ICONERROR);
|
2015-12-16 19:50:45 +00:00
|
|
|
return dir; // empty
|
|
|
|
}
|
|
|
|
|
|
|
|
to_utf8(dir, buf); // Convert to UTF-8
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
std::replace(dir.begin(), dir.end(), '\\', '/');
|
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
#elif __APPLE__
|
|
|
|
char buf[4096];
|
|
|
|
u32 size = sizeof(buf);
|
|
|
|
if (_NSGetExecutablePath(buf, &size))
|
|
|
|
{
|
|
|
|
std::printf("_NSGetExecutablePath() failed (size=0x%x).\n", size);
|
|
|
|
return dir; // empty
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = buf;
|
|
|
|
#else
|
|
|
|
char buf[4096];
|
|
|
|
const auto size = ::readlink("/proc/self/exe", buf, sizeof(buf));
|
|
|
|
if (size <= 0 || size >= sizeof(buf))
|
|
|
|
{
|
|
|
|
std::printf("readlink(/proc/self/exe) failed (%d).\n", errno);
|
|
|
|
return dir; // empty
|
|
|
|
}
|
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
dir.assign(buf, size);
|
2015-12-16 19:50:45 +00:00
|
|
|
#endif
|
2016-01-05 23:52:48 +00:00
|
|
|
|
2015-12-16 19:50:45 +00:00
|
|
|
// Leave only path
|
|
|
|
dir.resize(dir.rfind('/') + 1);
|
|
|
|
return dir;
|
|
|
|
}();
|
|
|
|
|
|
|
|
return s_dir;
|
|
|
|
}
|
2016-02-01 21:55:43 +00:00
|
|
|
|
|
|
|
void fs::remove_all(const std::string& path)
|
|
|
|
{
|
|
|
|
for (const auto& entry : dir(path))
|
|
|
|
{
|
|
|
|
if (entry.name == "." || entry.name == "..")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.is_directory == false)
|
|
|
|
{
|
|
|
|
remove_file(path + '/' + entry.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.is_directory == true)
|
|
|
|
{
|
|
|
|
remove_all(path + '/' + entry.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_dir(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 fs::get_dir_size(const std::string& path)
|
|
|
|
{
|
|
|
|
u64 result = 0;
|
|
|
|
|
|
|
|
for (const auto entry : dir(path))
|
|
|
|
{
|
|
|
|
if (entry.name == "." || entry.name == "..")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.is_directory == false)
|
|
|
|
{
|
|
|
|
result += entry.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry.is_directory == true)
|
|
|
|
{
|
|
|
|
result += get_dir_size(path + '/' + entry.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|