From 63c5b2ab321b299e602a4d2eb58ab09a82d07ead Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Thu, 18 Jun 2015 14:55:11 +0300 Subject: [PATCH] sys_fs_open() O_APPEND support Added vm::static_ptr_cast, vm::const_ptr_cast, vm::reinterpret_ptr_cast Added pointer comparison with vm::null --- Utilities/File.cpp | 57 ++++++++-------- rpcs3/Emu/Memory/vm_ptr.h | 107 ++++++++++++++++++++++++++---- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 7 +- 3 files changed, 127 insertions(+), 44 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index d79cdb2718..ba91e15f87 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -9,9 +9,9 @@ static_assert(fs::file::null == intptr_t(INVALID_HANDLE_VALUE) && fs::dir::null == fs::file::null, "Check fs::file::null definition"); -std::unique_ptr ConvertUTF8ToWChar(const std::string& source) +std::unique_ptr to_wchar(const std::string& source) { - const size_t length = source.size() + 1; // size + null terminator + const auto length = source.size() + 1; // size + null terminator const int size = source.size() < INT_MAX ? static_cast(length) : throw std::length_error(__FUNCTION__); @@ -26,15 +26,13 @@ std::unique_ptr ConvertUTF8ToWChar(const std::string& source) return buffer; } -std::string ConvertWCharToUTF8(const wchar_t* source) +void to_utf8(std::string& result, const wchar_t* source) { const int length = lstrlenW(source); // source length - std::string result; - if (length == 0) { - return result; + return result.clear(); } const int size = WideCharToMultiByte(CP_UTF8, 0, source, length, NULL, 0, NULL, NULL); // output size @@ -51,8 +49,6 @@ std::string ConvertWCharToUTF8(const wchar_t* source) { throw __FUNCTION__; } - - return result; } time_t to_time_t(const ULARGE_INTEGER& ft) @@ -81,7 +77,7 @@ time_t to_time_t(const FILETIME& ft) bool truncate_file(const std::string& file, u64 length) { // open the file - const auto handle = CreateFileW(ConvertUTF8ToWChar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + const auto handle = CreateFileW(to_wchar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) { @@ -124,7 +120,7 @@ bool fs::stat(const std::string& path, stat_t& info) { #ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA attrs; - if (!GetFileAttributesExW(ConvertUTF8ToWChar(path).get(), GetFileExInfoStandard, &attrs)) + if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs)) { return false; } @@ -156,7 +152,7 @@ bool fs::stat(const std::string& path, stat_t& info) bool fs::exists(const std::string& path) { #ifdef _WIN32 - return GetFileAttributesW(ConvertUTF8ToWChar(path).get()) != 0xFFFFFFFF; + return GetFileAttributesW(to_wchar(path).get()) != 0xFFFFFFFF; #else struct stat buffer; return stat(path.c_str(), &buffer) == 0; @@ -167,7 +163,7 @@ bool fs::is_file(const std::string& file) { #ifdef _WIN32 DWORD attrs; - if ((attrs = GetFileAttributesW(ConvertUTF8ToWChar(file).get())) == INVALID_FILE_ATTRIBUTES) + if ((attrs = GetFileAttributesW(to_wchar(file).get())) == INVALID_FILE_ATTRIBUTES) { return false; } @@ -188,7 +184,7 @@ bool fs::is_dir(const std::string& dir) { #ifdef _WIN32 DWORD attrs; - if ((attrs = GetFileAttributesW(ConvertUTF8ToWChar(dir).get())) == INVALID_FILE_ATTRIBUTES) + if ((attrs = GetFileAttributesW(to_wchar(dir).get())) == INVALID_FILE_ATTRIBUTES) { return false; } @@ -208,7 +204,7 @@ bool fs::is_dir(const std::string& dir) bool fs::create_dir(const std::string& dir) { #ifdef _WIN32 - if (!CreateDirectoryW(ConvertUTF8ToWChar(dir).get(), NULL)) + if (!CreateDirectoryW(to_wchar(dir).get(), NULL)) #else if (mkdir(dir.c_str(), 0777)) #endif @@ -265,7 +261,7 @@ bool fs::create_path(const std::string& path) bool fs::remove_dir(const std::string& dir) { #ifdef _WIN32 - if (!RemoveDirectoryW(ConvertUTF8ToWChar(dir).get())) + if (!RemoveDirectoryW(to_wchar(dir).get())) #else if (rmdir(dir.c_str())) #endif @@ -279,9 +275,8 @@ bool fs::remove_dir(const std::string& dir) bool fs::rename(const std::string& from, const std::string& to) { - // TODO: Deal with case-sensitivity #ifdef _WIN32 - if (!MoveFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get())) + if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get())) #else if (rename(from.c_str(), to.c_str())) #endif @@ -332,7 +327,7 @@ int OSCopyFile(const char* source, const char* destination, bool overwrite) bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite) { #ifdef _WIN32 - if (!CopyFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get(), !overwrite)) + if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite)) #else if (OSCopyFile(from.c_str(), to.c_str(), overwrite)) #endif @@ -347,7 +342,7 @@ bool fs::copy_file(const std::string& from, const std::string& to, bool overwrit bool fs::remove_file(const std::string& file) { #ifdef _WIN32 - if (!DeleteFileW(ConvertUTF8ToWChar(file).get())) + if (!DeleteFileW(to_wchar(file).get())) #else if (unlink(file.c_str())) #endif @@ -388,7 +383,7 @@ fs::file::~file() bool fs::file::open(const std::string& filename, u32 mode) { - this->~file(); + this->close(); #ifdef _WIN32 DWORD access = 0; @@ -424,7 +419,7 @@ bool fs::file::open(const std::string& filename, u32 mode) return false; } - m_fd = (intptr_t)CreateFileW(ConvertUTF8ToWChar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL); + m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL); #else int flags = 0; @@ -537,31 +532,35 @@ bool fs::file::close() u64 fs::file::read(void* buffer, u64 count) const { + const int size = count <= INT_MAX ? static_cast(count) : throw std::length_error(__FUNCTION__); + #ifdef _WIN32 DWORD nread; - if (!ReadFile((HANDLE)m_fd, buffer, count, &nread, NULL)) + if (!ReadFile((HANDLE)m_fd, buffer, size, &nread, NULL)) { return -1; } return nread; #else - return ::read(m_fd, buffer, count); + return ::read(m_fd, buffer, size); #endif } u64 fs::file::write(const void* buffer, u64 count) const { + const int size = count <= INT_MAX ? static_cast(count) : throw std::length_error(__FUNCTION__); + #ifdef _WIN32 DWORD nwritten; - if (!WriteFile((HANDLE)m_fd, buffer, count, &nwritten, NULL)) + if (!WriteFile((HANDLE)m_fd, buffer, size, &nwritten, NULL)) { return -1; } return nwritten; #else - return ::write(m_fd, buffer, count); + return ::write(m_fd, buffer, size); #endif } @@ -631,7 +630,7 @@ void fs::dir::import(handle_type dd, const std::string& path) m_dd = dd; #ifdef _WIN32 - m_path = ConvertUTF8ToWChar(path); + m_path = to_wchar(path); #else m_path.reset(new char[path.size() + 1]); memcpy(m_path.get(), path.c_str(), path.size() + 1); @@ -659,7 +658,7 @@ bool fs::dir::open(const std::string& dirname) } #ifdef _WIN32 - m_path = ConvertUTF8ToWChar(dirname + "/*"); + m_path = to_wchar(dirname + "/*"); #else m_path.reset(new char[dirname.size() + 1]); memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1); @@ -723,7 +722,7 @@ bool fs::dir::get_first(std::string& name, stat_t& info) return false; } - name = ConvertWCharToUTF8(found.cFileName); + to_utf8(name, found.cFileName); info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0; @@ -755,7 +754,7 @@ bool fs::dir::get_next(std::string& name, stat_t& info) return false; } - name = ConvertWCharToUTF8(found.cFileName); + to_utf8(name, found.cFileName); info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0; diff --git a/rpcs3/Emu/Memory/vm_ptr.h b/rpcs3/Emu/Memory/vm_ptr.h index 61c828f57b..f3afcc6277 100644 --- a/rpcs3/Emu/Memory/vm_ptr.h +++ b/rpcs3/Emu/Memory/vm_ptr.h @@ -53,12 +53,12 @@ namespace vm } // enable only the conversions which are originally possible between pointer types - template::value>> operator _ptr_base() const + template::value>> operator _ptr_base() const { return{ convert_le_be(vm::cast(m_addr)) }; } - template::value>> explicit operator T2*() const + template::value>> explicit operator T2*() const { return get_ptr(); } @@ -205,9 +205,28 @@ namespace vm RT>; // helper SFINAE type for vm::_ptr_base pointer arithmetic operators and indirection (disabled for void and function pointers) - template using if_arithmetical_t = std::enable_if_t< - !std::is_void::value && !std::is_function::value, + template using if_arithmetical_ptr_t = std::enable_if_t< + !std::is_void::value && + !std::is_function::value, RT>; + + // perform static_cast (for example, vm::ptr to vm::ptr) + template(std::declval()))> inline _ptr_base static_ptr_cast(const _ptr_base& other) + { + return{ other.m_addr }; + } + + // perform const_cast (for example, vm::ptr to vm::ptr) + template(std::declval()))> inline _ptr_base const_ptr_cast(const _ptr_base& other) + { + return{ other.m_addr }; + } + + // perform reinterpret_cast (for example, vm::ptr to vm::ptr) + template(std::declval()))> inline _ptr_base reinterpret_ptr_cast(const _ptr_base& other) + { + return{ other.m_addr }; + } } // unary plus operator for vm::_ptr_base (always available) @@ -217,13 +236,13 @@ template inline vm::_ptr_base operator +(const v } // indirection operator for vm::_ptr_base -template inline vm::if_arithmetical_t operator *(const vm::_ptr_base& ptr) +template inline vm::if_arithmetical_ptr_t operator *(const vm::_ptr_base& ptr) { return vm::get_ref(vm::cast(ptr.m_addr)); } // postfix increment operator for vm::_ptr_base -template inline vm::if_arithmetical_t> operator ++(vm::_ptr_base& ptr, int) +template inline vm::if_arithmetical_ptr_t> operator ++(vm::_ptr_base& ptr, int) { const AT result = ptr.m_addr; ptr.m_addr += sizeof32(T); @@ -231,14 +250,14 @@ template inline vm::if_arithmetical_t inline vm::if_arithmetical_t&> operator ++(vm::_ptr_base& ptr) +template inline vm::if_arithmetical_ptr_t&> operator ++(vm::_ptr_base& ptr) { ptr.m_addr += sizeof32(T); return ptr; } // postfix decrement operator for vm::_ptr_base -template inline vm::if_arithmetical_t> operator --(vm::_ptr_base& ptr, int) +template inline vm::if_arithmetical_ptr_t> operator --(vm::_ptr_base& ptr, int) { const AT result = ptr.m_addr; ptr.m_addr -= sizeof32(T); @@ -246,40 +265,40 @@ template inline vm::if_arithmetical_t inline vm::if_arithmetical_t&> operator --(vm::_ptr_base& ptr) +template inline vm::if_arithmetical_ptr_t&> operator --(vm::_ptr_base& ptr) { ptr.m_addr -= sizeof32(T); return ptr; } // addition assignment operator for vm::_ptr_base (pointer += integer) -template inline vm::if_arithmetical_t&> operator +=(vm::_ptr_base& ptr, to_ne_t count) +template inline vm::if_arithmetical_ptr_t&> operator +=(vm::_ptr_base& ptr, to_ne_t count) { ptr.m_addr += count * sizeof32(T); return ptr; } // subtraction assignment operator for vm::_ptr_base (pointer -= integer) -template inline vm::if_arithmetical_t&> operator -=(vm::_ptr_base& ptr, to_ne_t count) +template inline vm::if_arithmetical_ptr_t&> operator -=(vm::_ptr_base& ptr, to_ne_t count) { ptr.m_addr -= count * sizeof32(T); return ptr; } // addition operator for vm::_ptr_base (pointer + integer) -template inline vm::if_arithmetical_t> operator +(const vm::_ptr_base& ptr, to_ne_t count) +template inline vm::if_arithmetical_ptr_t> operator +(const vm::_ptr_base& ptr, to_ne_t count) { return{ convert_le_be(ptr.m_addr + count * sizeof32(T)) }; } // addition operator for vm::_ptr_base (integer + pointer) -template inline vm::if_arithmetical_t> operator +(to_ne_t count, const vm::_ptr_base& ptr) +template inline vm::if_arithmetical_ptr_t> operator +(to_ne_t count, const vm::_ptr_base& ptr) { return{ convert_le_be(ptr.m_addr + count * sizeof32(T)) }; } // subtraction operator for vm::_ptr_base (pointer - integer) -template inline vm::if_arithmetical_t> operator -(const vm::_ptr_base& ptr, to_ne_t count) +template inline vm::if_arithmetical_ptr_t> operator -(const vm::_ptr_base& ptr, to_ne_t count) { return{ convert_le_be(ptr.m_addr - count * sizeof32(T)) }; } @@ -302,36 +321,96 @@ template vm::if_comparable return left.m_addr == right.m_addr; } +template bool operator ==(const vm::null_t&, const vm::_ptr_base& ptr) +{ + return ptr.m_addr == 0; +} + +template bool operator ==(const vm::_ptr_base& ptr, const vm::null_t&) +{ + return ptr.m_addr == 0; +} + // comparison operator for vm::_ptr_base (pointer1 != pointer2) template vm::if_comparable_t operator !=(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.m_addr != right.m_addr; } +template bool operator !=(const vm::null_t&, const vm::_ptr_base& ptr) +{ + return ptr.m_addr != 0; +} + +template bool operator !=(const vm::_ptr_base& ptr, const vm::null_t&) +{ + return ptr.m_addr != 0; +} + // comparison operator for vm::_ptr_base (pointer1 < pointer2) template vm::if_comparable_t operator <(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.m_addr < right.m_addr; } +template bool operator <(const vm::null_t&, const vm::_ptr_base& ptr) +{ + return ptr.m_addr != 0; +} + +template bool operator <(const vm::_ptr_base&, const vm::null_t&) +{ + return false; +} + // comparison operator for vm::_ptr_base (pointer1 <= pointer2) template vm::if_comparable_t operator <=(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.m_addr <= right.m_addr; } +template bool operator <=(const vm::null_t&, const vm::_ptr_base&) +{ + return true; +} + +template bool operator <=(const vm::_ptr_base& ptr, const vm::null_t&) +{ + return ptr.m_addr == 0; +} + // comparison operator for vm::_ptr_base (pointer1 > pointer2) template vm::if_comparable_t operator >(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.m_addr > right.m_addr; } +template bool operator >(const vm::null_t&, const vm::_ptr_base&) +{ + return false; +} + +template bool operator >(const vm::_ptr_base& ptr, const vm::null_t&) +{ + return ptr.m_addr != 0; +} + // comparison operator for vm::_ptr_base (pointer1 >= pointer2) template vm::if_comparable_t operator >=(const vm::_ptr_base& left, const vm::_ptr_base& right) { return left.m_addr >= right.m_addr; } +template bool operator >=(const vm::null_t&, const vm::_ptr_base& ptr) +{ + return ptr.m_addr == 0; +} + +template bool operator >=(const vm::_ptr_base&, const vm::null_t&) +{ + return true; +} + // external specialization for is_be_t<> (true if AT is be_t<>) template diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index fad6fe9c17..c420585167 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -68,6 +68,11 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, open_mode |= o_trunc; } + if (flags & CELL_FS_O_APPEND) + { + open_mode |= o_append; + } + if (flags & CELL_FS_O_EXCL) { if (flags & CELL_FS_O_CREAT) @@ -80,7 +85,7 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, } } - if (flags & ~(CELL_FS_O_ACCMODE | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_EXCL)) + if (flags & ~(CELL_FS_O_ACCMODE | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_APPEND | CELL_FS_O_EXCL)) { open_mode = 0; // error }