rpcs3/Utilities/File.cpp

849 lines
17 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-08-22 14:21:55 +00:00
#include "Log.h"
2015-04-24 21:38:11 +00:00
#include "File.h"
2014-07-11 17:06:59 +00:00
#ifdef _WIN32
#include <Windows.h>
2015-04-15 14:27:37 +00:00
#define GET_API_ERROR static_cast<u64>(GetLastError())
2015-04-25 19:15:53 +00:00
static_assert(fs::file::null == intptr_t(INVALID_HANDLE_VALUE) && fs::dir::null == fs::file::null, "Check fs::file::null definition");
std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
2015-04-13 18:10:31 +00:00
{
const auto length = source.size() + 1; // size + null terminator
2015-04-15 14:27:37 +00:00
2015-07-01 17:09:26 +00:00
const int size = source.size() < INT_MAX ? static_cast<int>(length) : throw EXCEPTION("Invalid source length (0x%llx)", source.size());
2015-04-15 14:27:37 +00:00
2015-04-21 19:35:11 +00:00
std::unique_ptr<wchar_t[]> buffer(new wchar_t[length]); // 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))
{
2015-07-06 19:35:34 +00:00
throw EXCEPTION("System error 0x%x", GetLastError());
2014-07-11 17:06:59 +00:00
}
2015-04-15 14:27:37 +00:00
return buffer;
}
void to_utf8(std::string& result, const wchar_t* source)
2015-04-25 19:15:53 +00:00
{
2015-05-08 09:45:21 +00:00
const int length = lstrlenW(source); // source length
2015-04-25 19:15:53 +00:00
2015-05-08 09:45:21 +00:00
if (length == 0)
{
return result.clear();
2015-05-08 09:45:21 +00:00
}
const int size = WideCharToMultiByte(CP_UTF8, 0, source, length, NULL, 0, NULL, NULL); // output size
if (size <= 0)
{
2015-07-06 19:35:34 +00:00
throw EXCEPTION("System error 0x%x", GetLastError());
2015-05-08 09:45:21 +00:00
}
result.resize(size);
2015-04-25 19:15:53 +00:00
2015-05-08 09:45:21 +00:00
if (!WideCharToMultiByte(CP_UTF8, 0, source, length, &result.front(), size, NULL, NULL))
2015-04-25 19:15:53 +00:00
{
2015-07-06 19:35:34 +00:00
throw EXCEPTION("System error 0x%x", GetLastError());
2015-04-25 19:15:53 +00:00
}
}
2015-04-19 19:25:04 +00:00
time_t to_time_t(const ULARGE_INTEGER& ft)
2015-04-19 17:57:04 +00:00
{
return ft.QuadPart / 10000000ULL - 11644473600ULL;
}
2015-04-19 19:25:04 +00:00
time_t to_time_t(const LARGE_INTEGER& ft)
{
ULARGE_INTEGER v;
v.LowPart = ft.LowPart;
v.HighPart = ft.HighPart;
return to_time_t(v);
}
2015-04-15 14:27:37 +00:00
time_t to_time_t(const FILETIME& ft)
{
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-04-19 17:57:04 +00:00
return to_time_t(v);
2014-07-11 17:06:59 +00:00
}
2015-04-15 14:27:37 +00:00
2015-04-25 19:15:53 +00:00
bool truncate_file(const std::string& file, u64 length)
2015-04-18 13:38:42 +00:00
{
2015-04-19 13:19:24 +00:00
// open the file
const auto handle = CreateFileW(to_wchar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2015-04-18 13:38:42 +00:00
if (handle == INVALID_HANDLE_VALUE)
{
return false;
}
LARGE_INTEGER distance;
distance.QuadPart = length;
2015-04-19 13:19:24 +00:00
// seek and truncate
if (!SetFilePointerEx(handle, distance, NULL, FILE_BEGIN) || !SetEndOfFile(handle))
2015-04-18 13:38:42 +00:00
{
2015-04-19 13:19:24 +00:00
const auto error = GetLastError();
2015-04-18 13:38:42 +00:00
CloseHandle(handle);
2015-04-19 13:19:24 +00:00
SetLastError(error);
2015-04-18 13:38:42 +00:00
return false;
}
return CloseHandle(handle);
}
2015-04-13 14:46:10 +00:00
#else
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 <unistd.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <copyfile.h>
#else
#include <sys/sendfile.h>
#endif
2015-04-15 14:27:37 +00:00
#include "errno.h"
2015-04-13 14:46:10 +00:00
#define GET_API_ERROR static_cast<u64>(errno)
2015-04-15 14:27:37 +00:00
2014-10-24 13:24:09 +00:00
#endif
thread_local fse fs::g_tls_error = fse::ok;
2015-04-24 21:38:11 +00:00
bool fs::stat(const std::string& path, stat_t& info)
2015-04-13 18:10:31 +00:00
{
g_tls_error = fse::ok;
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs))
2015-04-15 14:27:37 +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-04-15 14:27:37 +00:00
info.atime = to_time_t(attrs.ftLastAccessTime);
info.mtime = to_time_t(attrs.ftLastWriteTime);
info.ctime = to_time_t(attrs.ftCreationTime);
#else
2015-05-27 09:51:25 +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
{
g_tls_error = fse::ok;
2015-04-20 15:53:31 +00:00
#ifdef _WIN32
return GetFileAttributesW(to_wchar(path).get()) != 0xFFFFFFFF;
2015-04-20 15:53:31 +00:00
#else
struct stat buffer;
return stat(path.c_str(), &buffer) == 0;
#endif
}
2015-04-24 21:38:11 +00:00
bool fs::is_file(const std::string& file)
2015-04-20 15:53:31 +00:00
{
g_tls_error = fse::ok;
2015-04-20 15:53:31 +00:00
#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(to_wchar(file).get())) == INVALID_FILE_ATTRIBUTES)
2015-04-20 15:53:31 +00:00
{
return false;
}
return (attrs & FILE_ATTRIBUTE_DIRECTORY) == 0;
#else
2015-05-27 09:51:25 +00:00
struct stat file_info;
if (stat(file.c_str(), &file_info) < 0)
2015-04-20 15:53:31 +00:00
{
return false;
}
return !S_ISDIR(file_info.st_mode);
#endif
}
2015-04-24 21:38:11 +00:00
bool fs::is_dir(const std::string& dir)
2015-04-15 14:27:37 +00:00
{
g_tls_error = fse::ok;
2015-04-15 14:27:37 +00:00
#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(to_wchar(dir).get())) == INVALID_FILE_ATTRIBUTES)
2015-04-15 14:27:37 +00:00
{
return false;
}
return (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
2015-05-27 09:51:25 +00:00
struct stat file_info;
if (stat(dir.c_str(), &file_info) < 0)
2015-04-15 14:27:37 +00:00
{
return false;
}
return S_ISDIR(file_info.st_mode);
#endif
}
2015-04-24 21:38:11 +00:00
bool fs::create_dir(const std::string& dir)
{
g_tls_error = fse::ok;
2014-08-29 13:06:58 +00:00
#ifdef _WIN32
if (!CreateDirectoryW(to_wchar(dir).get(), NULL))
2014-08-29 13:06:58 +00:00
#else
2015-04-18 13:38:42 +00:00
if (mkdir(dir.c_str(), 0777))
2014-08-29 13:06:58 +00:00
#endif
2015-04-18 13:38:42 +00:00
{
2015-04-20 01:54:19 +00:00
LOG_WARNING(GENERAL, "Error creating directory '%s': 0x%llx", dir, GET_API_ERROR);
2015-04-18 13:38:42 +00:00
return false;
}
return true;
}
2015-04-24 21:38:11 +00:00
bool fs::create_path(const std::string& path)
{
g_tls_error = fse::ok;
2015-04-20 15:53:31 +00:00
size_t start = 0;
while (true)
{
// maybe it could be more optimal if goes from the end recursively
size_t pos = path.find_first_of("/\\", start);
2015-04-20 15:53:31 +00:00
if (pos == std::string::npos)
{
pos = path.length();
2015-04-20 15:53:31 +00:00
}
std::string dir = path.substr(0, pos);
start = ++pos;
2015-04-20 15:53:31 +00:00
if (dir.size() == 0)
{
continue;
}
2015-04-20 15:53:31 +00:00
2015-04-24 21:38:11 +00:00
if (!is_dir(dir))
2015-04-20 15:53:31 +00:00
{
// if doesn't exist or not a dir
2015-04-24 21:38:11 +00:00
if (!create_dir(dir))
2015-04-20 15:53:31 +00:00
{
// if creating failed
return false;
}
}
2014-08-05 12:14:08 +00:00
if (pos >= path.length())
2015-04-20 15:53:31 +00:00
{
break;
}
}
2015-04-20 15:53:31 +00:00
return true;
}
2015-04-24 21:38:11 +00:00
bool fs::remove_dir(const std::string& dir)
{
g_tls_error = fse::ok;
#ifdef _WIN32
if (!RemoveDirectoryW(to_wchar(dir).get()))
2014-10-24 13:24:09 +00:00
#else
2015-03-31 09:20:25 +00:00
if (rmdir(dir.c_str()))
2014-10-24 13:24:09 +00:00
#endif
{
2015-04-20 01:54:19 +00:00
LOG_WARNING(GENERAL, "Error deleting directory '%s': 0x%llx", dir, GET_API_ERROR);
return false;
}
2015-04-13 14:46:10 +00:00
return true;
}
2015-04-24 21:38:11 +00:00
bool fs::rename(const std::string& from, const std::string& to)
{
g_tls_error = fse::ok;
#ifdef _WIN32
if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get()))
#else
2015-04-13 14:46:10 +00:00
if (rename(from.c_str(), to.c_str()))
#endif
{
2015-04-20 01:54:19 +00:00
LOG_WARNING(GENERAL, "Error renaming '%s' to '%s': 0x%llx", from, to, GET_API_ERROR);
2015-04-13 14:46:10 +00:00
return false;
}
return true;
}
#ifndef _WIN32
int OSCopyFile(const char* source, const char* destination, bool overwrite)
{
/* This function was taken from http://stackoverflow.com/questions/2180079/how-can-i-copy-a-file-on-unix-using-c */
int input, output;
if ((input = open(source, O_RDONLY)) == -1)
{
return -1;
}
2015-04-13 19:06:01 +00:00
if ((output = open(destination, O_WRONLY | O_CREAT | (overwrite ? O_TRUNC : O_EXCL), 0666)) == -1)
2015-04-13 14:46:10 +00:00
{
close(input);
return -1;
}
//Here we use kernel-space copying for performance reasons
#if defined(__APPLE__) || defined(__FreeBSD__)
//fcopyfile works on FreeBSD and OS X 10.5+
int result = fcopyfile(input, output, 0, COPYFILE_ALL);
#else
//sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+
off_t bytesCopied = 0;
struct stat fileinfo = { 0 };
fstat(input, &fileinfo);
2015-04-13 18:35:44 +00:00
int result = sendfile(output, input, &bytesCopied, fileinfo.st_size) == -1 ? -1 : 0;
2015-04-13 14:46:10 +00:00
#endif
close(input);
close(output);
return result;
}
#endif
2015-04-24 21:38:11 +00:00
bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite)
2015-04-13 14:46:10 +00:00
{
g_tls_error = fse::ok;
2015-04-13 14:46:10 +00:00
#ifdef _WIN32
if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite))
2015-04-13 14:46:10 +00:00
#else
if (OSCopyFile(from.c_str(), to.c_str(), overwrite))
#endif
2015-03-16 16:20:02 +00:00
{
2015-04-20 01:54:19 +00:00
LOG_WARNING(GENERAL, "Error copying '%s' to '%s': 0x%llx", from, to, GET_API_ERROR);
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;
}
2015-04-24 21:38:11 +00:00
bool fs::remove_file(const std::string& file)
{
g_tls_error = fse::ok;
#ifdef _WIN32
if (!DeleteFileW(to_wchar(file).get()))
#else
2015-03-31 09:20:25 +00:00
if (unlink(file.c_str()))
2014-10-24 13:24:09 +00:00
#endif
{
2015-04-20 01:54:19 +00:00
LOG_WARNING(GENERAL, "Error deleting file '%s': 0x%llx", file, GET_API_ERROR);
2014-08-03 23:52:36 +00:00
return false;
}
2015-04-18 13:38:42 +00:00
return true;
}
2015-04-25 19:15:53 +00:00
bool fs::truncate_file(const std::string& file, u64 length)
2015-04-18 13:38:42 +00:00
{
g_tls_error = fse::ok;
2015-04-18 13:38:42 +00:00
#ifdef _WIN32
2015-04-24 21:38:11 +00:00
if (!::truncate_file(file, length))
2015-04-18 13:38:42 +00:00
#else
2015-05-27 09:51:25 +00:00
if (::truncate(file.c_str(), length))
2015-04-18 13:38:42 +00:00
#endif
{
2015-04-20 01:54:19 +00:00
LOG_WARNING(GENERAL, "Error resizing file '%s' to 0x%llx: 0x%llx", file, length, GET_API_ERROR);
2015-04-18 13:38:42 +00:00
return false;
}
2014-08-03 23:52:36 +00:00
return true;
}
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
}
2015-04-24 21:38:11 +00:00
bool fs::file::open(const std::string& filename, u32 mode)
{
this->close();
g_tls_error = fse::ok;
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
default:
{
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither fom::read nor fom::write specified (0x%x)", filename, mode);
2015-04-19 16:02:35 +00:00
return false;
}
}
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;
2015-04-19 16:02:35 +00:00
}
if (!disp || (mode & ~(fom::read | fom::write | fom::append | fom::create | fom::trunc | fom::excl)))
2015-04-19 13:19:24 +00:00
{
2015-04-24 21:38:11 +00:00
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
2015-04-19 13:19:24 +00:00
return false;
}
m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
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
default:
{
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither fom::read nor fom::write specified (0x%x)", filename, mode);
2015-04-19 16:02:35 +00:00
return false;
}
}
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
if (((mode & fom::excl) && !(mode & fom::create)) || (mode & ~(fom::read | fom::write | fom::append | fom::create | fom::trunc | fom::excl)))
2015-04-19 16:02:35 +00:00
{
2015-04-24 21:38:11 +00:00
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
2015-04-19 16:02:35 +00:00
return false;
}
2015-04-25 19:15:53 +00:00
m_fd = ::open(filename.c_str(), flags, 0666);
2015-04-19 13:19:24 +00:00
#endif
2015-04-25 19:15:53 +00:00
if (m_fd == null)
2015-04-20 01:54:19 +00:00
{
2015-04-24 21:38:11 +00:00
LOG_WARNING(GENERAL, "fs::file::open('%s', 0x%x) failed: error 0x%llx", filename, mode, GET_API_ERROR);
2015-04-20 01:54:19 +00:00
return false;
}
2015-04-20 01:54:19 +00:00
return true;
}
2015-04-24 21:38:11 +00:00
bool fs::file::trunc(u64 size) const
{
g_tls_error = fse::ok;
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;
2015-04-25 19:15:53 +00:00
SetFilePointerEx((HANDLE)m_fd, pos, &old, FILE_CURRENT); // get old position
2015-04-19 13:19:24 +00:00
pos.QuadPart = size;
2015-04-25 19:15:53 +00:00
SetFilePointerEx((HANDLE)m_fd, pos, NULL, FILE_BEGIN); // set new position
2015-04-19 13:19:24 +00:00
2015-04-25 19:15:53 +00:00
SetEndOfFile((HANDLE)m_fd); // change file size
2015-04-19 17:57:04 +00:00
2015-04-25 19:15:53 +00:00
SetFilePointerEx((HANDLE)m_fd, old, NULL, FILE_BEGIN); // restore position
2015-04-19 13:19:24 +00:00
return true; // TODO
#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
{
g_tls_error = fse::ok;
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
{
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_t(basic_info.LastAccessTime);
info.mtime = to_time_t(basic_info.ChangeTime);
info.ctime = to_time_t(basic_info.CreationTime);
#else
2015-05-27 09:51:25 +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;
}
2015-04-24 21:38:11 +00:00
bool fs::file::close()
{
g_tls_error = fse::ok;
2015-04-25 19:15:53 +00:00
if (m_fd == null)
2015-04-19 13:19:24 +00:00
{
2015-04-25 19:15:53 +00:00
return false;
2015-04-19 13:19:24 +00:00
}
2015-04-24 14:06:30 +00:00
2015-04-25 19:15:53 +00:00
auto fd = m_fd;
m_fd = null;
2015-04-24 14:06:30 +00:00
2015-04-25 19:15:53 +00:00
#ifdef _WIN32
return CloseHandle((HANDLE)fd);
#else
return !::close(fd);
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
{
g_tls_error = fse::ok;
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
{
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
{
g_tls_error = fse::ok;
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
{
return -1;
}
return nwritten;
#else
return ::write(m_fd, buffer, size);
2015-04-19 13:19:24 +00:00
#endif
}
u64 fs::file::write(const std::string &string) const
{
return write(string.data(), string.size());
}
u64 fs::file::seek(s64 offset, fsm seek_mode) const
{
g_tls_error = fse::ok;
2015-04-19 13:19:24 +00:00
#ifdef _WIN32
LARGE_INTEGER pos;
pos.QuadPart = offset;
const DWORD mode =
seek_mode == fsm::begin ? FILE_BEGIN :
seek_mode == fsm::cur ? FILE_CURRENT :
seek_mode == fsm::end ? FILE_END :
throw EXCEPTION("Unknown seek_mode (0x%x)", seek_mode);
2015-04-25 19:15:53 +00:00
if (!SetFilePointerEx((HANDLE)m_fd, pos, &pos, mode))
2015-04-19 13:19:24 +00:00
{
return -1;
}
return pos.QuadPart;
#else
const int whence =
seek_mode == fsm::begin ? SEEK_SET :
seek_mode == fsm::cur ? SEEK_CUR :
seek_mode == fsm::end ? SEEK_END :
throw EXCEPTION("Unknown seek_mode (0x%x)", seek_mode);
return ::lseek(m_fd, offset, whence);
2015-04-19 13:19:24 +00:00
#endif
}
2015-04-24 21:38:11 +00:00
u64 fs::file::size() const
{
g_tls_error = fse::ok;
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
{
return -1;
}
return size.QuadPart;
#else
2015-05-27 09:51:25 +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()
{
2015-04-25 19:15:53 +00:00
if (m_dd != null)
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 20:10:39 +00:00
::closedir((DIR*)m_dd);
2015-04-25 19:15:53 +00:00
#endif
}
}
2015-04-25 19:15:53 +00:00
void fs::dir::import(handle_type dd, const std::string& path)
{
2015-04-25 19:15:53 +00:00
if (m_dd != null)
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 20:10:39 +00:00
::closedir((DIR*)m_dd);
2015-04-25 19:15:53 +00:00
#endif
}
g_tls_error = fse::ok;
2015-04-25 19:15:53 +00:00
m_dd = dd;
#ifdef _WIN32
m_path = to_wchar(path);
2015-04-25 19:15:53 +00:00
#else
m_path.reset(new char[path.size() + 1]);
memcpy(m_path.get(), path.c_str(), path.size() + 1);
#endif
}
2015-04-25 19:15:53 +00:00
bool fs::dir::open(const std::string& dirname)
{
2015-04-25 19:15:53 +00:00
if (m_dd != null)
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 20:10:39 +00:00
::closedir((DIR*)m_dd);
2015-04-25 19:15:53 +00:00
#endif
}
g_tls_error = fse::ok;
2015-04-25 19:15:53 +00:00
m_dd = null;
m_path.reset();
if (!is_dir(dirname))
{
return false;
}
#ifdef _WIN32
m_path = to_wchar(dirname + "/*");
2015-04-25 19:15:53 +00:00
#else
m_path.reset(new char[dirname.size() + 1]);
memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1);
#endif
return true;
}
2015-04-25 19:15:53 +00:00
bool fs::dir::close()
2014-08-05 22:34:26 +00:00
{
g_tls_error = fse::ok;
2015-04-25 19:15:53 +00:00
if (m_dd == null)
{
if (m_path)
{
m_path.reset();
return true;
}
else
{
return false;
}
}
auto dd = m_dd;
m_dd = null;
m_path.reset();
#ifdef _WIN32
return FindClose((HANDLE)dd);
#else
2015-04-25 20:10:39 +00:00
return !::closedir((DIR*)dd);
2015-04-25 19:15:53 +00:00
#endif
2014-08-05 22:34:26 +00:00
}
2015-04-25 19:15:53 +00:00
bool fs::dir::get_first(std::string& name, stat_t& info)
{
2015-04-25 19:15:53 +00:00
if (m_dd != null) // close previous handle
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 20:10:39 +00:00
::closedir((DIR*)m_dd);
2015-04-25 19:15:53 +00:00
#endif
}
g_tls_error = fse::ok;
2015-04-25 19:15:53 +00:00
m_dd = null;
if (!m_path)
{
return false;
}
#ifdef _WIN32
WIN32_FIND_DATAW found;
m_dd = (intptr_t)FindFirstFileW(m_path.get(), &found);
2015-04-25 20:55:15 +00:00
if (m_dd == null)
{
return false;
}
to_utf8(name, found.cFileName);
2015-04-25 20:55:15 +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_t(found.ftLastAccessTime);
info.mtime = to_time_t(found.ftLastWriteTime);
info.ctime = to_time_t(found.ftCreationTime);
return true;
2015-04-25 19:15:53 +00:00
#else
2015-04-25 20:24:45 +00:00
m_dd = (intptr_t)::opendir(m_path.get());
2015-04-25 19:15:53 +00:00
return get_next(name, info);
2015-04-25 20:55:15 +00:00
#endif
}
2015-04-25 19:15:53 +00:00
bool fs::dir::get_next(std::string& name, stat_t& info)
{
g_tls_error = fse::ok;
2015-04-25 19:15:53 +00:00
if (m_dd == null)
{
return false;
}
#ifdef _WIN32
WIN32_FIND_DATAW found;
if (!FindNextFileW((HANDLE)m_dd, &found))
{
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_t(found.ftLastAccessTime);
info.mtime = to_time_t(found.ftLastWriteTime);
info.ctime = to_time_t(found.ftCreationTime);
#else
2015-04-25 20:10:39 +00:00
const auto found = ::readdir((DIR*)m_dd);
2015-04-25 19:15:53 +00:00
2015-05-27 09:51:25 +00:00
struct stat file_info;
if (!found || ::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;
}