rpcs3/Utilities/File.cpp

1058 lines
24 KiB
C++
Raw Normal View History

#include "stdafx.h"
2015-04-24 21:38:11 +00:00
#include "File.h"
2014-07-11 17:06:59 +00:00
#ifdef _WIN32
2016-01-05 23:52:48 +00:00
#include <cwchar>
#undef _WIN32_WINNT
2015-11-16 15:04:49 +00:00
#define _WIN32_WINNT 0x0601
#include <Windows.h>
2015-04-15 14:27:37 +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-01-05 23:52:48 +00:00
const int size = source.size() < INT_MAX ? static_cast<int>(buf_size) : throw EXCEPTION("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-01-05 23:52:48 +00:00
throw EXCEPTION("MultiByteToWideChar() failed (0x%x).", GetLastError());
2014-07-11 17:06:59 +00:00
}
2015-04-15 14:27:37 +00:00
return buffer;
}
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-01-05 23:52:48 +00:00
const int buf_size = length <= INT_MAX / 3 ? static_cast<int>(length) * 3 + 1 : throw EXCEPTION("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-01-05 23:52:48 +00:00
throw EXCEPTION("WideCharToMultiByte() failed (0x%x).", GetLastError());
2015-04-25 19:15:53 +00:00
}
}
static time_t to_time(const ULARGE_INTEGER& ft)
2015-04-19 17:57:04 +00:00
{
return ft.QuadPart / 10000000ULL - 11644473600ULL;
}
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;
return to_time(v);
2015-04-19 19:25:04 +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;
return to_time(v);
2014-07-11 17:06:59 +00:00
}
2015-04-15 14:27:37 +00:00
2015-04-13 14:46:10 +00:00
#else
2016-01-05 23:52:48 +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>
#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>
#include <mach-o/dyld.h>
2015-04-13 14:46:10 +00:00
#else
#include <sys/sendfile.h>
#endif
2016-01-05 23:52:48 +00:00
#endif
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);
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:
CHECK_ASSERTION(fs::get_parent_dir("/x/y///") == "/x");
CHECK_ASSERTION(fs::get_parent_dir("/x/y/") == "/x");
CHECK_ASSERTION(fs::get_parent_dir("/x/y") == "/x");
CHECK_ASSERTION(fs::get_parent_dir("x:/y") == "x:");
CHECK_ASSERTION(fs::get_parent_dir("//x/y") == "//x");
// Failure:
CHECK_ASSERTION(fs::get_parent_dir("").empty());
CHECK_ASSERTION(fs::get_parent_dir("x/").empty());
CHECK_ASSERTION(fs::get_parent_dir("x").empty());
CHECK_ASSERTION(fs::get_parent_dir("x///").empty());
CHECK_ASSERTION(fs::get_parent_dir("/x/").empty());
CHECK_ASSERTION(fs::get_parent_dir("/x").empty());
CHECK_ASSERTION(fs::get_parent_dir("/").empty());
CHECK_ASSERTION(fs::get_parent_dir("//").empty());
CHECK_ASSERTION(fs::get_parent_dir("//x").empty());
CHECK_ASSERTION(fs::get_parent_dir("//x/").empty());
CHECK_ASSERTION(fs::get_parent_dir("///").empty());
CHECK_ASSERTION(fs::get_parent_dir("///x").empty());
CHECK_ASSERTION(fs::get_parent_dir("///x/").empty());
return false;
}();
2016-01-05 23:52:48 +00:00
bool fs::stat(const std::string& path, stat_t& info)
{
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs))
2015-04-15 14:27:37 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
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);
info.atime = to_time(attrs.ftLastAccessTime);
info.mtime = to_time(attrs.ftLastWriteTime);
info.ctime = to_time(attrs.ftCreationTime);
#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
{
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;
#endif
2015-04-25 19:15:53 +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
{
#ifdef _WIN32
2016-01-05 23:52:48 +00:00
if (GetFileAttributesW(to_wchar(path).get()) == INVALID_FILE_ATTRIBUTES)
{
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
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;
return !::stat(path.c_str(), &file_info);
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
{
#ifdef _WIN32
2016-01-05 23:52:48 +00:00
const DWORD attrs = GetFileAttributesW(to_wchar(path).get());
if (attrs == INVALID_FILE_ATTRIBUTES)
{
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
return false;
}
#else
struct ::stat file_info;
if (::stat(path.c_str(), &file_info) != 0)
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-01-05 23:52:48 +00:00
errno = EEXIST;
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
{
#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-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
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
{
return false;
}
#endif
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-01-05 23:52:48 +00:00
errno = EEXIST;
2015-04-18 13:38:42 +00:00
return false;
}
return true;
}
2016-01-05 23:52:48 +00:00
bool fs::create_dir(const std::string& path)
{
#ifdef _WIN32
2016-01-05 23:52:48 +00:00
if (!CreateDirectoryW(to_wchar(path).get(), NULL))
{
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
return false;
}
return true;
#else
2016-01-05 23:52:48 +00:00
return !::mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
#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))
{
return false;
2016-01-05 23:52:48 +00:00
}
return create_dir(path);
}
2016-01-05 23:52:48 +00:00
bool fs::remove_dir(const std::string& path)
{
#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-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
return false;
}
2015-04-13 14:46:10 +00:00
return true;
2016-01-05 23:52:48 +00:00
#else
return !::rmdir(path.c_str());
#endif
}
2015-04-24 21:38:11 +00:00
bool fs::rename(const std::string& from, const std::string& to)
{
#ifdef _WIN32
if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get()))
2015-04-13 14:46:10 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.\nFrom: %s\nTo: %s", error, from, to);
}
2015-04-13 14:46:10 +00:00
return false;
}
return true;
2016-01-05 23:52:48 +00:00
#else
return !::rename(from.c_str(), to.c_str());
#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-01-05 23:52:48 +00:00
#ifdef _WIN32
if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite))
{
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.\nFrom: %s\nTo: %s", error, from, to);
}
return false;
}
return true;
#else
/* 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);
if (input == -1)
2015-04-13 14:46:10 +00:00
{
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
const int output = ::open(to.c_str(), O_WRONLY | O_CREAT | (overwrite ? O_TRUNC : O_EXCL), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
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);
errno = err;
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);
errno = err;
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
{
#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-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
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
return !::unlink(path.c_str());
#endif
}
2016-01-05 23:52:48 +00:00
bool fs::truncate_file(const std::string& path, u64 length)
{
#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-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
2014-08-03 23:52:36 +00:00
return false;
}
2015-04-18 13:38:42 +00:00
2016-01-05 23:52:48 +00:00
LARGE_INTEGER distance;
distance.QuadPart = length;
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-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (length=0x%llx).", error, length);
}
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
return !::truncate(path.c_str(), length);
#endif
}
2015-04-24 21:38:11 +00:00
fs::file::~file()
{
2015-04-25 19:15:53 +00:00
if (m_fd != null)
{
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
2015-04-25 19:15:53 +00:00
CloseHandle((HANDLE)m_fd);
2015-04-19 13:19:24 +00:00
#else
2015-04-25 20:24:45 +00:00
::close(m_fd);
2015-04-19 13:19:24 +00:00
#endif
2015-04-25 19:15:53 +00:00
}
2015-04-19 16:02:35 +00:00
}
2016-01-05 23:52:48 +00:00
bool fs::file::open(const std::string& path, u32 mode)
{
this->close();
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
DWORD access = 0;
switch (mode & (fom::read | fom::write | fom::append))
{
case fom::read: access |= GENERIC_READ; break;
case fom::read | fom::append: access |= GENERIC_READ; break;
case fom::write: access |= GENERIC_WRITE; break;
case fom::write | fom::append: access |= FILE_APPEND_DATA; break;
case fom::read | fom::write: access |= GENERIC_READ | GENERIC_WRITE; break;
case fom::read | fom::write | fom::append: access |= GENERIC_READ | FILE_APPEND_DATA; break;
2015-04-19 16:02:35 +00:00
}
2015-04-19 13:19:24 +00:00
DWORD disp = 0;
switch (mode & (fom::create | fom::trunc | fom::excl))
2015-04-19 13:19:24 +00:00
{
case 0: disp = OPEN_EXISTING; break;
case fom::create: disp = OPEN_ALWAYS; break;
case fom::trunc: disp = TRUNCATE_EXISTING; break;
case fom::create | fom::trunc: disp = CREATE_ALWAYS; break;
case fom::create | fom::excl: disp = CREATE_NEW; break;
case fom::create | fom::excl | fom::trunc: disp = CREATE_NEW; break;
2016-01-05 23:52:48 +00:00
default:
errno = EINVAL;
return false;
2015-04-19 16:02:35 +00:00
}
2016-01-05 23:52:48 +00:00
m_fd = (std::intptr_t)CreateFileW(to_wchar(path).get(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
if (m_fd == null)
2015-04-19 13:19:24 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
case ERROR_FILE_EXISTS: errno = EEXIST; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x (%s).", error, path);
}
2015-04-19 13:19:24 +00:00
return false;
}
2016-01-05 23:52:48 +00:00
return true;
2015-04-19 13:19:24 +00:00
#else
int flags = 0;
2015-04-19 16:02:35 +00:00
switch (mode & (fom::read | fom::write))
2015-04-19 16:02:35 +00:00
{
case fom::read: flags |= O_RDONLY; break;
case fom::write: flags |= O_WRONLY; break;
case fom::read | fom::write: flags |= O_RDWR; break;
2015-04-19 16:02:35 +00:00
}
if (mode & fom::append) flags |= O_APPEND;
if (mode & fom::create) flags |= O_CREAT;
if (mode & fom::trunc) flags |= O_TRUNC;
if (mode & fom::excl) flags |= O_EXCL;
2015-04-19 13:19:24 +00:00
2016-01-05 23:52:48 +00:00
m_fd = ::open(path.c_str(), flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
2015-04-19 16:02:35 +00:00
2016-01-05 23:52:48 +00:00
return m_fd != null;
2015-04-19 13:19:24 +00:00
#endif
}
2015-04-24 21:38:11 +00:00
bool fs::file::trunc(u64 size) const
{
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
LARGE_INTEGER old, pos;
2015-04-19 13:19:24 +00:00
pos.QuadPart = 0;
2016-01-05 23:52:48 +00:00
if (!SetFilePointerEx((HANDLE)m_fd, pos, &old, FILE_CURRENT)) // get old position
{
switch (DWORD error = GetLastError())
{
case ERROR_INVALID_HANDLE: errno = EBADF; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
return false;
}
2015-04-19 13:19:24 +00:00
pos.QuadPart = size;
2016-01-05 23:52:48 +00:00
if (!SetFilePointerEx((HANDLE)m_fd, pos, NULL, FILE_BEGIN)) // set new position
{
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
return false;
}
2015-04-19 13:19:24 +00:00
2016-01-05 23:52:48 +00:00
const BOOL result = SetEndOfFile((HANDLE)m_fd); // change file size
if (!result)
{
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case 0:
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
}
2015-04-19 17:57:04 +00:00
2016-01-05 23:52:48 +00:00
if (!SetFilePointerEx((HANDLE)m_fd, old, NULL, FILE_BEGIN) && result) // restore position
{
if (DWORD error = GetLastError())
{
throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
}
2015-04-19 13:19:24 +00:00
2016-01-05 23:52:48 +00:00
return result != FALSE;
2015-04-19 13:19:24 +00:00
#else
2015-05-27 09:51:25 +00:00
return !::ftruncate(m_fd, size);
2015-04-19 13:19:24 +00:00
#endif
}
2015-04-24 21:38:11 +00:00
bool fs::file::stat(stat_t& info) const
2015-04-19 17:57:04 +00:00
{
#ifdef _WIN32
FILE_BASIC_INFO basic_info;
2015-04-25 19:15:53 +00:00
if (!GetFileInformationByHandleEx((HANDLE)m_fd, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO)))
2015-04-19 17:57:04 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_INVALID_HANDLE: errno = EBADF; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
2015-04-19 17:57:04 +00:00
return false;
}
2015-04-24 21:38:11 +00:00
info.is_directory = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.is_writable = (basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
2015-04-19 17:57:04 +00:00
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);
2015-04-19 17:57:04 +00:00
#else
2016-01-05 23:52:48 +00:00
struct ::stat file_info;
if (::fstat(m_fd, &file_info) < 0)
2015-04-19 17:57:04 +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-19 17:57:04 +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;
#endif
2015-04-25 19:15:53 +00:00
2015-04-19 17:57:04 +00:00
return true;
}
2016-01-05 23:52:48 +00:00
void fs::file::close()
{
2015-04-25 19:15:53 +00:00
if (m_fd == null)
2015-04-19 13:19:24 +00:00
{
2016-01-05 23:52:48 +00:00
return /*true*/;
2015-04-19 13:19:24 +00:00
}
2015-04-24 14:06:30 +00:00
2016-01-05 23:52:48 +00:00
const auto fd = m_fd;
2015-04-25 19:15:53 +00:00
m_fd = null;
2015-04-24 14:06:30 +00:00
2015-04-25 19:15:53 +00:00
#ifdef _WIN32
2016-01-05 23:52:48 +00:00
if (!CloseHandle((HANDLE)fd))
{
throw EXCEPTION("CloseHandle() failed (fd=0x%llx, 0x%x)", fd, GetLastError());
}
2015-04-25 19:15:53 +00:00
#else
2016-01-05 23:52:48 +00:00
if (::close(fd) != 0)
{
throw EXCEPTION("close() failed (fd=0x%llx, errno=%d)", fd, errno);
}
2015-04-19 13:19:24 +00:00
#endif
}
2015-04-24 21:38:11 +00:00
u64 fs::file::read(void* buffer, u64 count) const
{
// TODO (call ReadFile multiple times if count is too big)
2015-07-01 17:09:26 +00:00
const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count);
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
DWORD nread;
if (!ReadFile((HANDLE)m_fd, buffer, size, &nread, NULL))
2015-04-19 13:19:24 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_INVALID_HANDLE: errno = EBADF; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
2015-04-19 13:19:24 +00:00
return -1;
}
return nread;
#else
return ::read(m_fd, buffer, size);
2015-04-19 13:19:24 +00:00
#endif
}
2015-04-24 21:38:11 +00:00
u64 fs::file::write(const void* buffer, u64 count) const
{
// TODO (call WriteFile multiple times if count is too big)
2015-07-01 17:09:26 +00:00
const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count);
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
DWORD nwritten;
if (!WriteFile((HANDLE)m_fd, buffer, size, &nwritten, NULL))
2015-04-19 13:19:24 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_INVALID_HANDLE: errno = EBADF; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
2015-04-19 13:19:24 +00:00
return -1;
}
return nwritten;
#else
return ::write(m_fd, buffer, size);
2015-04-19 13:19:24 +00:00
#endif
}
2016-01-05 23:52:48 +00:00
u64 fs::file::seek(s64 offset, seek_mode whence) const
{
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
LARGE_INTEGER pos;
pos.QuadPart = offset;
2016-01-05 23:52:48 +00:00
DWORD mode;
switch (whence)
{
case seek_set: mode = FILE_BEGIN; break;
case seek_cur: mode = FILE_CURRENT; break;
case seek_end: mode = FILE_END; break;
default:
{
errno = EINVAL;
return -1;
}
}
2015-04-25 19:15:53 +00:00
if (!SetFilePointerEx((HANDLE)m_fd, pos, &pos, mode))
2015-04-19 13:19:24 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_INVALID_HANDLE: errno = EBADF; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
2015-04-19 13:19:24 +00:00
return -1;
}
return pos.QuadPart;
#else
2016-01-05 23:52:48 +00:00
int mode;
switch (whence)
{
case seek_set: mode = SEEK_SET; break;
case seek_cur: mode = SEEK_CUR; break;
case seek_end: mode = SEEK_END; break;
default:
{
errno = EINVAL;
return -1;
}
}
2016-01-05 23:52:48 +00:00
return ::lseek(m_fd, offset, mode);
2015-04-19 13:19:24 +00:00
#endif
}
2015-04-24 21:38:11 +00:00
u64 fs::file::size() const
{
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
LARGE_INTEGER size;
2015-04-25 19:15:53 +00:00
if (!GetFileSizeEx((HANDLE)m_fd, &size))
2015-04-19 13:19:24 +00:00
{
2016-01-05 23:52:48 +00:00
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_INVALID_HANDLE: errno = EBADF; break;
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
2015-04-19 13:19:24 +00:00
return -1;
}
return size.QuadPart;
#else
2016-01-05 23:52:48 +00:00
struct ::stat file_info;
if (::fstat(m_fd, &file_info) != 0)
2015-04-19 13:19:24 +00:00
{
return -1;
}
return file_info.st_size;
#endif
}
2015-04-25 19:15:53 +00:00
fs::dir::~dir()
{
if (m_path)
2015-04-25 19:15:53 +00:00
{
#ifdef _WIN32
if (m_dd != -1) FindClose((HANDLE)m_dd);
2015-04-25 19:15:53 +00:00
#else
2015-04-25 20:10:39 +00:00
::closedir((DIR*)m_dd);
2015-04-25 19:15:53 +00:00
#endif
}
}
2016-01-05 23:52:48 +00:00
void fs::file_read_map::reset(const file& f)
{
reset();
if (f)
{
#ifdef _WIN32
const HANDLE handle = ::CreateFileMapping((HANDLE)f.m_fd, NULL, PAGE_READONLY, 0, 0, NULL);
m_ptr = (char*)::MapViewOfFile(handle, FILE_MAP_READ, 0, 0, 0);
m_size = f.size();
::CloseHandle(handle);
#else
m_ptr = (char*)::mmap(nullptr, m_size = f.size(), PROT_READ, MAP_SHARED, f.m_fd, 0);
if (m_ptr == (void*)-1) m_ptr = nullptr;
#endif
}
}
2016-01-05 23:52:48 +00:00
void fs::file_read_map::reset()
{
if (m_ptr)
{
#ifdef _WIN32
::UnmapViewOfFile(m_ptr);
#else
::munmap(m_ptr, m_size);
#endif
}
}
2015-04-25 19:15:53 +00:00
bool fs::dir::open(const std::string& dirname)
{
this->close();
2015-04-25 19:15:53 +00:00
2016-01-05 23:52:48 +00:00
#ifdef _WIN32
2015-04-25 19:15:53 +00:00
if (!is_dir(dirname))
{
return false;
}
m_dd = -1;
2015-04-25 19:15:53 +00:00
#else
2016-02-26 14:22:57 +00:00
const auto ptr = ::opendir(dirname.c_str());
2016-01-05 23:52:48 +00:00
if (!ptr)
{
return false;
}
m_dd = reinterpret_cast<std::intptr_t>(ptr);
2015-04-25 19:15:53 +00:00
#endif
2016-01-05 23:52:48 +00:00
m_path.reset(new char[dirname.size() + 1]);
std::memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1);
2015-04-25 19:15:53 +00:00
return true;
}
2016-01-05 23:52:48 +00:00
void fs::dir::close()
2014-08-05 22:34:26 +00:00
{
if (!m_path)
2015-04-25 19:15:53 +00:00
{
2016-01-05 23:52:48 +00:00
return /*true*/;
2015-04-25 19:15:53 +00:00
}
m_path.reset();
#ifdef _WIN32
CHECK_ASSERTION(m_dd == -1 || FindClose((HANDLE)m_dd));
2015-04-25 19:15:53 +00:00
#else
CHECK_ASSERTION(!::closedir((DIR*)m_dd));
2015-04-25 19:15:53 +00:00
#endif
2014-08-05 22:34:26 +00:00
}
bool fs::dir::read(std::string& name, stat_t& info)
{
2015-04-25 19:15:53 +00:00
if (!m_path)
{
2016-01-05 23:52:48 +00:00
errno = EINVAL;
2015-04-25 19:15:53 +00:00
return false;
}
#ifdef _WIN32
2016-01-05 23:52:48 +00:00
const bool is_first = m_dd == -1;
2015-04-25 19:15:53 +00:00
WIN32_FIND_DATAW found;
2016-01-05 23:52:48 +00:00
if (is_first)
2015-04-25 20:55:15 +00:00
{
m_dd = (std::intptr_t)FindFirstFileW(to_wchar(m_path.get() + "/*"s).get(), &found);
2016-01-05 23:52:48 +00:00
}
2016-01-05 23:52:48 +00:00
if (is_first && m_dd == -1 || !is_first && !FindNextFileW((HANDLE)m_dd, &found))
{
// TODO: convert Win32 error code to errno
switch (DWORD error = GetLastError())
{
case ERROR_NO_MORE_FILES:
{
2016-01-05 23:52:48 +00:00
name.clear();
return true;
}
2016-01-05 23:52:48 +00:00
default: throw EXCEPTION("Unknown Win32 error: 0x%x.", error);
}
2015-04-25 19:15:53 +00:00
return false;
}
to_utf8(name, found.cFileName);
2015-04-25 19:15:53 +00:00
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);
2015-04-25 19:15:53 +00:00
#else
2015-04-25 20:10:39 +00:00
const auto found = ::readdir((DIR*)m_dd);
2016-01-05 23:52:48 +00:00
if (!found)
{
name.clear();
return true;
}
2015-04-25 19:15:53 +00:00
2016-01-05 23:52:48 +00:00
struct ::stat file_info;
if (::fstatat(::dirfd((DIR*)m_dd), found->d_name, &file_info, 0) != 0)
2015-04-25 19:15:53 +00:00
{
return false;
}
2015-04-25 20:24:45 +00:00
name = found->d_name;
2015-04-25 19:15:53 +00:00
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;
#endif
return true;
}
bool fs::dir::first(std::string& name, stat_t& info)
{
#ifdef _WIN32
if (m_path && m_dd != -1)
{
CHECK_ASSERTION(FindClose((HANDLE)m_dd));
m_dd = -1;
}
#else
if (m_path)
{
::rewinddir((DIR*)m_dd);
}
#endif
return read(name, info);
}
2016-01-05 23:52:48 +00:00
const std::string& fs::get_config_dir()
{
// Use magic static for dir initialization
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/";
if (!is_dir(dir) && !create_path(dir))
{
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();
}
return dir;
#endif
}();
return s_dir;
}
2016-01-05 23:52:48 +00:00
const std::string& fs::get_executable_dir()
{
// Use magic static for dir initialization
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)
{
2015-12-18 11:11:18 +00:00
MessageBoxA(0, fmt::format("GetModuleFileName() failed (0x%x).", GetLastError()).c_str(), "fs::get_config_dir()", MB_ICONERROR);
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(), '\\', '/');
#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);
#endif
2016-01-05 23:52:48 +00:00
// Leave only path
dir.resize(dir.rfind('/') + 1);
return dir;
}();
return s_dir;
}