1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 18:53:28 +01:00
rpcs3/Utilities/rFile.cpp

510 lines
11 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-08-22 16:21:55 +02:00
#include "Log.h"
2015-01-25 17:23:24 +01:00
#pragma warning(disable : 4996)
2014-07-11 19:06:59 +02:00
#include <wx/dir.h>
#include <wx/file.h>
#include <wx/filename.h>
#include "rFile.h"
2014-07-11 19:06:59 +02:00
#ifdef _WIN32
#include <Windows.h>
2015-04-15 16:27:37 +02:00
#define GET_API_ERROR static_cast<u64>(GetLastError())
std::unique_ptr<wchar_t> ConvertUTF8ToWChar(const std::string& source)
2015-04-13 20:10:31 +02:00
{
2015-04-15 16:27:37 +02:00
const size_t length = source.size() + 1; // size + null terminator
const int size = length && length <= INT_MAX ? static_cast<int>(length) : throw std::length_error(__FUNCTION__);
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))
{
LOG_ERROR(GENERAL, "ConvertUTF8ToWChar(source='%s') failed: 0x%llx", source.c_str(), GET_API_ERROR);
2014-07-11 19:06:59 +02:00
}
2015-04-15 16:27:37 +02:00
return buffer;
}
time_t to_time_t(const FILETIME& ft)
{
ULARGE_INTEGER v;
v.LowPart = ft.dwLowDateTime;
v.HighPart = ft.dwHighDateTime;
return v.QuadPart / 10000000ULL - 11644473600ULL;
2014-07-11 19:06:59 +02:00
}
2015-04-15 16:27:37 +02:00
2015-04-13 16:46:10 +02:00
#else
#include <fcntl.h>
#include <unistd.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <copyfile.h>
#else
#include <sys/sendfile.h>
#endif
2015-04-15 16:27:37 +02:00
#include "errno.h"
2015-04-13 16:46:10 +02:00
#define GET_API_ERROR static_cast<u64>(errno)
2015-04-15 16:27:37 +02:00
2014-10-24 15:24:09 +02:00
#endif
2015-04-15 16:27:37 +02:00
bool get_file_info(const std::string& path, FileInfo& info)
2015-04-13 20:10:31 +02:00
{
// TODO: Expand relative paths?
2015-04-15 16:27:37 +02:00
info.fullName = path;
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
2015-04-15 16:27:37 +02:00
if (!GetFileAttributesExW(ConvertUTF8ToWChar(path).get(), GetFileExInfoStandard, &attrs))
{
info.exists = false;
info.isDirectory = false;
info.isWritable = false;
info.size = 0;
return false;
}
2015-04-15 16:27:37 +02: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);
#else
struct stat64 file_info;
2015-04-15 16:27:37 +02:00
if (stat64(path, &file_info) < 0)
{
info.exists = false;
info.isDirectory = false;
info.isWritable = false;
info.size = 0;
return false;
}
2015-04-15 16:27:37 +02: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;
#endif
return true;
}
2015-04-15 16:27:37 +02: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;
if (stat64(path, &file_info) < 0)
{
return false;
}
return S_ISDIR(file_info.st_mode);
#endif
}
2015-04-15 16:27:37 +02:00
bool rMkdir(const std::string& dir)
{
2014-08-29 15:06:58 +02:00
#ifdef _WIN32
return !_mkdir(dir.c_str());
#else
return !mkdir(dir.c_str(), 0777);
#endif
}
2015-04-15 16:27:37 +02:00
bool rMkpath(const std::string& path)
{
size_t start=0, pos;
std::string dir;
bool ret;
while (true) {
2014-08-05 14:14:08 +02:00
if ((pos = path.find_first_of("/\\", start)) == std::string::npos)
pos = path.length();
dir = path.substr(0,pos++);
start = pos;
if(dir.size() == 0)
continue;
2014-08-29 15:06:58 +02:00
#ifdef _WIN32
2014-09-15 00:17:24 +02:00
if((ret = _mkdir(dir.c_str()) != 0) && errno != EEXIST){
2014-08-29 15:06:58 +02:00
#else
2014-09-15 00:17:24 +02:00
if((ret = mkdir(dir.c_str(), 0777) != 0) && errno != EEXIST){
2014-08-29 15:06:58 +02:00
#endif
return !ret;
}
2014-08-05 14:14:08 +02:00
if (pos >= path.length())
return true;
}
return true;
}
2015-04-15 16:27:37 +02:00
bool rRmdir(const std::string& dir)
{
#ifdef _WIN32
2015-04-15 16:27:37 +02:00
if (!RemoveDirectory(ConvertUTF8ToWChar(dir).get()))
2014-10-24 15:24:09 +02:00
#else
2015-03-31 11:20:25 +02:00
if (rmdir(dir.c_str()))
2014-10-24 15:24:09 +02:00
#endif
{
2015-04-13 16:46:10 +02:00
LOG_ERROR(GENERAL, "Error deleting directory %s: 0x%llx", dir.c_str(), GET_API_ERROR);
return false;
}
2015-04-13 16:46:10 +02:00
return true;
}
2015-04-15 16:27:37 +02:00
bool rRename(const std::string& from, const std::string& to)
{
// TODO: Deal with case-sensitivity
#ifdef _WIN32
2015-04-15 16:27:37 +02:00
if (!MoveFile(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get()))
#else
2015-04-13 16:46:10 +02:00
if (rename(from.c_str(), to.c_str()))
#endif
{
LOG_ERROR(GENERAL, "Error renaming '%s' to '%s': 0x%llx", from.c_str(), to.c_str(), GET_API_ERROR);
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 21:06:01 +02:00
if ((output = open(destination, O_WRONLY | O_CREAT | (overwrite ? O_TRUNC : O_EXCL), 0666)) == -1)
2015-04-13 16:46:10 +02: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 20:35:44 +02:00
int result = sendfile(output, input, &bytesCopied, fileinfo.st_size) == -1 ? -1 : 0;
2015-04-13 16:46:10 +02: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-15 16:27:37 +02:00
if (!CopyFile(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get(), !overwrite))
2015-04-13 16:46:10 +02:00
#else
if (OSCopyFile(from.c_str(), to.c_str(), overwrite))
#endif
2015-03-16 17:20:02 +01:00
{
2015-04-13 16:46:10 +02:00
LOG_ERROR(GENERAL, "Error copying '%s' to '%s': 0x%llx", from.c_str(), to.c_str(), GET_API_ERROR);
2015-03-16 17:20:02 +01:00
return false;
}
2015-04-13 16:46:10 +02:00
2015-03-16 17:20:02 +01:00
return true;
}
2015-04-15 16:27:37 +02:00
bool rExists(const std::string& file)
{
#ifdef _WIN32
2015-04-15 16:27:37 +02:00
return GetFileAttributes(ConvertUTF8ToWChar(file).get()) != 0xFFFFFFFF;
#else
struct stat buffer;
2015-04-15 16:27:37 +02:00
return stat(file.c_str(), &buffer) == 0;
#endif
}
2015-04-15 16:27:37 +02:00
bool rRemoveFile(const std::string& file)
{
#ifdef _WIN32
2015-04-15 16:27:37 +02:00
if (!DeleteFile(ConvertUTF8ToWChar(file).get()))
#else
2015-03-31 11:20:25 +02:00
if (unlink(file.c_str()))
2014-10-24 15:24:09 +02:00
#endif
{
2015-04-13 16:46:10 +02:00
LOG_ERROR(GENERAL, "Error deleting file %s: 0x%llx", file.c_str(), GET_API_ERROR);
2014-08-04 01:52:36 +02:00
return false;
}
2014-08-04 01:52:36 +02:00
return true;
}
wxFile::OpenMode convertOpenMode(rFile::OpenMode open)
{
wxFile::OpenMode mode;
switch (open)
{
case rFile::read:
mode = wxFile::read;
break;
case rFile::write:
mode = wxFile::write;
break;
case rFile::read_write:
mode = wxFile::read_write;
break;
case rFile::write_append:
mode = wxFile::write_append;
break;
case rFile::write_excl:
mode = wxFile::write_excl;
break;
}
return mode;
}
rFile::OpenMode rConvertOpenMode(wxFile::OpenMode open)
{
rFile::OpenMode mode;
switch (open)
{
case wxFile::read:
mode = rFile::read;
break;
case wxFile::write:
mode = rFile::write;
break;
case wxFile::read_write:
mode = rFile::read_write;
break;
case wxFile::write_append:
mode = rFile::write_append;
break;
case wxFile::write_excl:
mode = rFile::write_excl;
break;
}
return mode;
}
wxSeekMode convertSeekMode(rSeekMode mode)
{
wxSeekMode ret;
switch (mode)
{
case rFromStart:
ret = wxFromStart;
break;
case rFromCurrent:
ret = wxFromCurrent;
break;
case rFromEnd:
ret = wxFromEnd;
break;
}
return ret;
}
rSeekMode rConvertSeekMode(wxSeekMode mode)
{
rSeekMode ret;
switch (mode)
{
case wxFromStart:
ret = rFromStart;
break;
case wxFromCurrent:
ret = rFromCurrent;
break;
case wxFromEnd:
ret = rFromEnd;
break;
}
return ret;
}
rFile::rFile()
{
handle = reinterpret_cast<void*>(new wxFile());
}
rFile::rFile(const std::string& filename, rFile::OpenMode open)
{
handle = reinterpret_cast<void*>(new wxFile(fmt::FromUTF8(filename), convertOpenMode(open)));
}
rFile::rFile(int fd)
{
handle = reinterpret_cast<void*>(new wxFile(fd));
}
rFile::~rFile()
{
delete reinterpret_cast<wxFile*>(handle);
}
bool rFile::Access(const std::string &filename, rFile::OpenMode mode)
{
return wxFile::Access(fmt::FromUTF8(filename), convertOpenMode(mode));
}
size_t rFile::Write(const void *buffer, size_t count)
{
return reinterpret_cast<wxFile*>(handle)->Write(buffer,count);
}
bool rFile::Write(const std::string &text)
{
2014-09-15 00:17:24 +02:00
return reinterpret_cast<wxFile*>(handle)->Write(reinterpret_cast<const void*>(text.c_str()),text.size()) != 0;
}
bool rFile::Close()
{
return reinterpret_cast<wxFile*>(handle)->Close();
}
bool rFile::Create(const std::string &filename, bool overwrite, int access)
{
return reinterpret_cast<wxFile*>(handle)->Create(fmt::FromUTF8(filename), overwrite, access);
}
bool rFile::Open(const std::string &filename, rFile::OpenMode mode, int access)
{
return reinterpret_cast<wxFile*>(handle)->Open(fmt::FromUTF8(filename), convertOpenMode(mode), access);
}
bool rFile::IsOpened() const
{
return reinterpret_cast<wxFile*>(handle)->IsOpened();
}
size_t rFile::Length() const
{
return reinterpret_cast<wxFile*>(handle)->Length();
}
size_t rFile::Read(void *buffer, size_t count)
{
return reinterpret_cast<wxFile*>(handle)->Read(buffer,count);
}
size_t rFile::Seek(size_t ofs, rSeekMode mode)
{
return reinterpret_cast<wxFile*>(handle)->Seek(ofs, convertSeekMode(mode));
}
size_t rFile::Tell() const
{
return reinterpret_cast<wxFile*>(handle)->Tell();
}
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-06 00:34:26 +02:00
bool rDir::IsOpened() const
{
return reinterpret_cast<wxDir*>(handle)->IsOpened();
}
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();
}