2014-05-02 06:30:32 +00:00
|
|
|
#include "stdafx.h"
|
2014-08-22 14:21:55 +00:00
|
|
|
#include "Log.h"
|
2015-01-25 16:23:24 +00:00
|
|
|
#pragma warning(disable : 4996)
|
2014-07-11 17:06:59 +00:00
|
|
|
#include <wx/dir.h>
|
2014-08-28 22:49:26 +00:00
|
|
|
#include <wx/filename.h>
|
2014-08-25 14:56:13 +00:00
|
|
|
#include "rFile.h"
|
2014-07-11 17:06:59 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2014-08-28 22:49:26 +00:00
|
|
|
#include <Windows.h>
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
#define GET_API_ERROR static_cast<u64>(GetLastError())
|
|
|
|
|
|
|
|
std::unique_ptr<wchar_t> ConvertUTF8ToWChar(const std::string& source)
|
2015-04-13 18:10:31 +00:00
|
|
|
{
|
2015-04-15 14:27:37 +00:00
|
|
|
const size_t length = source.size() + 1; // size + null terminator
|
|
|
|
|
2015-04-15 18:33:44 +00:00
|
|
|
const int size = source.size() < INT_MAX ? static_cast<int>(length) : throw std::length_error(__FUNCTION__);
|
2015-04-15 14:27:37 +00:00
|
|
|
|
|
|
|
std::unique_ptr<wchar_t> buffer(new wchar_t[length]); // allocate buffer assuming that length is the max possible size
|
|
|
|
|
|
|
|
if (!MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.get(), size))
|
|
|
|
{
|
2015-04-18 13:38:42 +00:00
|
|
|
LOG_ERROR(GENERAL, "ConvertUTF8ToWChar(source='%s') failed: 0x%llx", source, GET_API_ERROR);
|
2014-07-11 17:06:59 +00:00
|
|
|
}
|
2015-04-15 14:27:37 +00:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-04-19 17:57:04 +00:00
|
|
|
time_t to_time_t(const LARGE_INTEGER& ft)
|
|
|
|
{
|
|
|
|
return ft.QuadPart / 10000000ULL - 11644473600ULL;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
time_t to_time_t(const FILETIME& ft)
|
|
|
|
{
|
2015-04-19 17:57:04 +00:00
|
|
|
LARGE_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-18 13:38:42 +00:00
|
|
|
bool truncate_file(const std::string& file, uint64_t length)
|
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
// open the file
|
2015-04-18 13:38:42 +00:00
|
|
|
const auto handle = CreateFileW(ConvertUTF8ToWChar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
|
|
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-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"
|
2014-05-02 06:30:32 +00:00
|
|
|
|
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
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool get_file_info(const std::string& path, FileInfo& info)
|
2015-04-13 18:10:31 +00:00
|
|
|
{
|
2015-04-19 17:57:04 +00:00
|
|
|
info.name = path;
|
|
|
|
|
|
|
|
info.exists = false;
|
|
|
|
info.isDirectory = false;
|
|
|
|
info.isWritable = false;
|
|
|
|
info.size = 0;
|
2014-07-31 18:20:00 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA attrs;
|
2015-04-15 14:27:37 +00:00
|
|
|
if (!GetFileAttributesExW(ConvertUTF8ToWChar(path).get(), GetFileExInfoStandard, &attrs))
|
|
|
|
{
|
2014-07-31 18:20:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-15 14:27:37 +00:00
|
|
|
|
|
|
|
info.exists = true;
|
|
|
|
info.isDirectory = (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
info.isWritable = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
|
|
|
|
info.size = (uint64_t)attrs.nFileSizeLow | ((uint64_t)attrs.nFileSizeHigh << 32);
|
|
|
|
info.atime = to_time_t(attrs.ftLastAccessTime);
|
|
|
|
info.mtime = to_time_t(attrs.ftLastWriteTime);
|
|
|
|
info.ctime = to_time_t(attrs.ftCreationTime);
|
2014-07-31 18:20:00 +00:00
|
|
|
#else
|
|
|
|
struct stat64 file_info;
|
2015-04-15 15:12:10 +00:00
|
|
|
if (stat64(path.c_str(), &file_info) < 0)
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
2014-07-31 18:20:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
info.exists = true;
|
|
|
|
info.isDirectory = S_ISDIR(file_info.st_mode);
|
|
|
|
info.isWritable = 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;
|
2014-07-31 18:20:00 +00:00
|
|
|
#endif
|
2015-04-19 17:57:04 +00:00
|
|
|
|
2014-07-31 18:20:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool rIsDir(const std::string& dir)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD attrs;
|
|
|
|
if ((attrs = GetFileAttributesW(ConvertUTF8ToWChar(dir).get())) == INVALID_FILE_ATTRIBUTES)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
#else
|
|
|
|
struct stat64 file_info;
|
2015-04-15 15:12:10 +00:00
|
|
|
if (stat64(dir.c_str(), &file_info) < 0)
|
2015-04-15 14:27:37 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_ISDIR(file_info.st_mode);
|
|
|
|
#endif
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool rMkdir(const std::string& dir)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
2014-08-29 13:06:58 +00:00
|
|
|
#ifdef _WIN32
|
2015-04-18 13:38:42 +00:00
|
|
|
if (!CreateDirectoryW(ConvertUTF8ToWChar(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
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "Error creating directory '%s': 0x%llx", dir, GET_API_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool rMkpath(const std::string& path)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
|
|
|
size_t start=0, pos;
|
|
|
|
std::string dir;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
while (true) {
|
2014-08-05 12:14:08 +00:00
|
|
|
if ((pos = path.find_first_of("/\\", start)) == std::string::npos)
|
2014-07-31 18:20:00 +00:00
|
|
|
pos = path.length();
|
|
|
|
|
|
|
|
dir = path.substr(0,pos++);
|
|
|
|
start = pos;
|
|
|
|
if(dir.size() == 0)
|
|
|
|
continue;
|
2014-08-29 13:06:58 +00:00
|
|
|
#ifdef _WIN32
|
2014-09-14 22:17:24 +00:00
|
|
|
if((ret = _mkdir(dir.c_str()) != 0) && errno != EEXIST){
|
2014-08-29 13:06:58 +00:00
|
|
|
#else
|
2014-09-14 22:17:24 +00:00
|
|
|
if((ret = mkdir(dir.c_str(), 0777) != 0) && errno != EEXIST){
|
2014-08-29 13:06:58 +00:00
|
|
|
#endif
|
2014-07-31 18:20:00 +00:00
|
|
|
return !ret;
|
|
|
|
}
|
2014-08-05 12:14:08 +00:00
|
|
|
if (pos >= path.length())
|
2014-07-31 18:20:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool rRmdir(const std::string& dir)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2015-04-17 04:37:13 +00:00
|
|
|
if (!RemoveDirectoryW(ConvertUTF8ToWChar(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-18 13:38:42 +00:00
|
|
|
LOG_ERROR(GENERAL, "Error deleting directory '%s': 0x%llx", dir, GET_API_ERROR);
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool rRename(const std::string& from, const std::string& to)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
|
|
|
// TODO: Deal with case-sensitivity
|
|
|
|
#ifdef _WIN32
|
2015-04-17 04:37:13 +00:00
|
|
|
if (!MoveFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get()))
|
2014-07-31 18:20:00 +00:00
|
|
|
#else
|
2015-04-13 14:46:10 +00:00
|
|
|
if (rename(from.c_str(), to.c_str()))
|
|
|
|
#endif
|
|
|
|
{
|
2015-04-18 13:38:42 +00:00
|
|
|
LOG_ERROR(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
|
|
|
|
|
|
|
|
bool rCopy(const std::string& from, const std::string& to, bool overwrite)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2015-04-17 04:37:13 +00:00
|
|
|
if (!CopyFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get(), !overwrite))
|
2015-04-13 14:46:10 +00:00
|
|
|
#else
|
|
|
|
if (OSCopyFile(from.c_str(), to.c_str(), overwrite))
|
2014-07-31 18:20:00 +00:00
|
|
|
#endif
|
2015-03-16 16:20:02 +00:00
|
|
|
{
|
2015-04-18 13:38:42 +00:00
|
|
|
LOG_ERROR(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;
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool rExists(const std::string& file)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2015-04-17 04:37:13 +00:00
|
|
|
return GetFileAttributesW(ConvertUTF8ToWChar(file).get()) != 0xFFFFFFFF;
|
2014-07-31 18:20:00 +00:00
|
|
|
#else
|
|
|
|
struct stat buffer;
|
2015-04-15 14:27:37 +00:00
|
|
|
return stat(file.c_str(), &buffer) == 0;
|
2014-07-31 18:20:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:27:37 +00:00
|
|
|
bool rRemoveFile(const std::string& file)
|
2014-07-31 18:20:00 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2015-04-17 04:37:13 +00:00
|
|
|
if (!DeleteFileW(ConvertUTF8ToWChar(file).get()))
|
2014-07-31 18:20:00 +00:00
|
|
|
#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-18 13:38:42 +00:00
|
|
|
LOG_ERROR(GENERAL, "Error deleting file '%s': 0x%llx", file, GET_API_ERROR);
|
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
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rTruncate(const std::string& file, uint64_t length)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (!truncate_file(file, length))
|
|
|
|
#else
|
2015-04-19 13:19:24 +00:00
|
|
|
if (truncate64(file.c_str(), length))
|
2015-04-18 13:38:42 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "Error resizing file '%s' to 0x%llx: 0x%llx", file, length, GET_API_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-03 23:52:36 +00:00
|
|
|
return true;
|
2014-07-31 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
rfile_t::rfile_t()
|
|
|
|
#ifdef _WIN32
|
|
|
|
: fd(INVALID_HANDLE_VALUE)
|
|
|
|
#else
|
|
|
|
: fd(-1)
|
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
rfile_t::~rfile_t()
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (fd != INVALID_HANDLE_VALUE)
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
CloseHandle(fd);
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
#else
|
|
|
|
if (fd != -1)
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 16:02:35 +00:00
|
|
|
::close(fd);
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
rfile_t::rfile_t(const std::string& filename, u32 mode)
|
|
|
|
#ifdef _WIN32
|
|
|
|
: fd(INVALID_HANDLE_VALUE)
|
|
|
|
#else
|
|
|
|
: fd(-1)
|
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
open(filename, mode);
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
rfile_t::operator bool() const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return fd != INVALID_HANDLE_VALUE;
|
|
|
|
#else
|
|
|
|
return fd != -1;
|
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 16:02:35 +00:00
|
|
|
void rfile_t::import(handle_type handle)
|
|
|
|
{
|
|
|
|
this->~rfile_t();
|
|
|
|
fd = handle;
|
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
bool rfile_t::open(const std::string& filename, u32 mode)
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
this->~rfile_t();
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD access = 0;
|
2015-04-19 16:02:35 +00:00
|
|
|
switch (mode & (o_read | o_write))
|
|
|
|
{
|
|
|
|
case o_read: access |= GENERIC_READ; break;
|
|
|
|
case o_write: access |= GENERIC_WRITE; break;
|
|
|
|
case o_read | o_write: access |= GENERIC_READ | GENERIC_WRITE; break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "rfile_t::open(): neither o_read nor o_write specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
DWORD disp = 0;
|
|
|
|
switch (mode & (o_create | o_trunc | o_excl))
|
|
|
|
{
|
|
|
|
case 0: disp = OPEN_EXISTING; break;
|
|
|
|
case o_create: disp = OPEN_ALWAYS; break;
|
|
|
|
case o_trunc: disp = TRUNCATE_EXISTING; break;
|
|
|
|
case o_create | o_trunc: disp = CREATE_ALWAYS; break;
|
|
|
|
case o_create | o_excl: disp = CREATE_NEW; break;
|
2015-04-19 16:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!disp || (mode & ~(o_read | o_write | o_create | o_trunc | o_excl)))
|
2015-04-19 13:19:24 +00:00
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "rfile_t::open(): unknown mode specified (0x%x)", mode);
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
fd = CreateFileW(ConvertUTF8ToWChar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
#else
|
|
|
|
int flags = 0;
|
2015-04-19 16:02:35 +00:00
|
|
|
|
|
|
|
switch (mode & (o_read | o_write))
|
|
|
|
{
|
2015-04-19 17:14:16 +00:00
|
|
|
case o_read: flags |= O_RDONLY; break;
|
|
|
|
case o_write: flags |= O_WRONLY; break;
|
2015-04-19 16:02:35 +00:00
|
|
|
case o_read | o_write: flags |= O_RDWR; break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "rfile_t::open(): neither o_read nor o_write specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
if (mode & o_create) flags |= O_CREAT;
|
|
|
|
if (mode & o_trunc) flags |= O_TRUNC;
|
|
|
|
if (mode & o_excl) flags |= O_EXCL;
|
|
|
|
|
2015-04-19 16:02:35 +00:00
|
|
|
if (((mode & o_excl) && (!(mode & o_create) || (mode & o_trunc))) || (mode & ~(o_read | o_write | o_create | o_trunc | o_excl)))
|
|
|
|
{
|
|
|
|
LOG_ERROR(GENERAL, "rfile_t::open(): unknown mode specified (0x%x)", mode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = ::open(filename.c_str(), flags, 0666);
|
2015-04-19 13:19:24 +00:00
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
return is_opened();
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
bool rfile_t::is_opened() const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
return *this;
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
bool rfile_t::trunc(u64 size) const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER old, pos;
|
2014-05-02 06:30:32 +00:00
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
pos.QuadPart = 0;
|
|
|
|
SetFilePointerEx(fd, pos, &old, FILE_CURRENT); // get old position
|
|
|
|
|
|
|
|
pos.QuadPart = size;
|
|
|
|
SetFilePointerEx(fd, pos, NULL, FILE_BEGIN); // set new position
|
|
|
|
|
|
|
|
SetEndOfFile(fd); // change file size
|
2015-04-19 17:57:04 +00:00
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
SetFilePointerEx(fd, old, NULL, FILE_BEGIN); // restore position
|
|
|
|
|
|
|
|
return true; // TODO
|
|
|
|
#else
|
|
|
|
return !ftruncate64(fd, size);
|
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 17:57:04 +00:00
|
|
|
bool rfile_t::stat(FileInfo& info) const
|
|
|
|
{
|
|
|
|
info.name.clear(); // possibly, TODO
|
|
|
|
|
|
|
|
info.exists = false;
|
|
|
|
info.isDirectory = false;
|
|
|
|
info.isWritable = false;
|
|
|
|
info.size = 0;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
FILE_BASIC_INFO basic_info;
|
|
|
|
//FILE_NAME_INFO name_info;
|
|
|
|
|
|
|
|
if (!GetFileInformationByHandleEx(fd, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.exists = true;
|
|
|
|
info.isDirectory = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
info.isWritable = (basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
|
|
|
|
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
|
|
|
|
struct stat64 file_info;
|
|
|
|
if (fstat64(fd, &file_info) < 0)
|
|
|
|
{
|
|
|
|
info.exists = false;
|
|
|
|
info.isDirectory = false;
|
|
|
|
info.isWritable = false;
|
|
|
|
info.size = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.exists = true;
|
|
|
|
info.isDirectory = S_ISDIR(file_info.st_mode);
|
|
|
|
info.isWritable = 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;
|
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
bool rfile_t::close()
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (CloseHandle(fd))
|
|
|
|
{
|
|
|
|
fd = INVALID_HANDLE_VALUE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
2015-04-19 16:02:35 +00:00
|
|
|
if (!::close(fd))
|
2015-04-19 13:19:24 +00:00
|
|
|
{
|
|
|
|
fd = -1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
u64 rfile_t::read(void* buffer, u64 count) const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD nread;
|
|
|
|
if (!ReadFile(fd, buffer, count, &nread, NULL))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
#else
|
2015-04-19 17:14:16 +00:00
|
|
|
return ::read(fd, buffer, count);
|
2015-04-19 13:19:24 +00:00
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
u64 rfile_t::write(const void* buffer, u64 count) const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD nwritten;
|
|
|
|
if (!WriteFile(fd, buffer, count, &nwritten, NULL))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nwritten;
|
|
|
|
#else
|
2015-04-19 17:14:16 +00:00
|
|
|
return ::write(fd, buffer, count);
|
2015-04-19 13:19:24 +00:00
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
u64 rfile_t::seek(u64 offset, u32 mode) const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
assert(mode < 3);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER pos;
|
|
|
|
pos.QuadPart = offset;
|
|
|
|
|
|
|
|
if (!SetFilePointerEx(fd, pos, &pos, mode))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos.QuadPart;
|
|
|
|
#else
|
|
|
|
return lseek64(fd, offset, mode);
|
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 13:19:24 +00:00
|
|
|
u64 rfile_t::size() const
|
2014-05-02 06:30:32 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER size;
|
|
|
|
if (!GetFileSizeEx(fd, &size))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size.QuadPart;
|
|
|
|
#else
|
|
|
|
struct stat64 file_info;
|
|
|
|
if (fstat64(fd, &file_info) < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_info.st_size;
|
|
|
|
#endif
|
2014-05-02 06:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rDir::rDir()
|
|
|
|
{
|
|
|
|
handle = reinterpret_cast<void*>(new wxDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
rDir::~rDir()
|
|
|
|
{
|
|
|
|
delete reinterpret_cast<wxDir*>(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
rDir::rDir(const std::string &path)
|
|
|
|
{
|
|
|
|
handle = reinterpret_cast<void*>(new wxDir(fmt::FromUTF8(path)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rDir::Open(const std::string& path)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<wxDir*>(handle)->Open(fmt::FromUTF8(path));
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:34:26 +00:00
|
|
|
bool rDir::IsOpened() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<wxDir*>(handle)->IsOpened();
|
|
|
|
}
|
|
|
|
|
2014-05-02 06:30:32 +00:00
|
|
|
bool rDir::GetFirst(std::string *filename) const
|
|
|
|
{
|
|
|
|
wxString str;
|
|
|
|
bool res;
|
|
|
|
res = reinterpret_cast<wxDir*>(handle)->GetFirst(&str);
|
|
|
|
*filename = str.ToStdString();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rDir::GetNext(std::string *filename) const
|
|
|
|
{
|
|
|
|
wxString str;
|
|
|
|
bool res;
|
|
|
|
res = reinterpret_cast<wxDir*>(handle)->GetNext(&str);
|
|
|
|
*filename = str.ToStdString();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
rFileName::rFileName()
|
|
|
|
{
|
|
|
|
handle = reinterpret_cast<void*>(new wxFileName());
|
|
|
|
}
|
|
|
|
|
|
|
|
rFileName::~rFileName()
|
|
|
|
{
|
|
|
|
delete reinterpret_cast<wxFileName*>(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
rFileName::rFileName(const rFileName& filename)
|
|
|
|
{
|
|
|
|
handle = reinterpret_cast<void*>(new wxFileName(*reinterpret_cast<wxFileName*>(filename.handle)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rFileName::rFileName(const std::string& name)
|
|
|
|
{
|
|
|
|
handle = reinterpret_cast<void*>(new wxFileName(fmt::FromUTF8(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rFileName::GetFullPath()
|
|
|
|
{
|
|
|
|
return fmt::ToUTF8(reinterpret_cast<wxFileName*>(handle)->GetFullPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rFileName::GetPath()
|
|
|
|
{
|
|
|
|
return fmt::ToUTF8(reinterpret_cast<wxFileName*>(handle)->GetPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rFileName::GetName()
|
|
|
|
{
|
|
|
|
return fmt::ToUTF8(reinterpret_cast<wxFileName*>(handle)->GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rFileName::GetFullName()
|
|
|
|
{
|
|
|
|
return fmt::ToUTF8(reinterpret_cast<wxFileName*>(handle)->GetFullName());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rFileName::Normalize()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<wxFileName*>(handle)->Normalize();
|
|
|
|
}
|