From 55c193ce78abcc9bd9802e536ada41f778084e5a Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sat, 24 Jun 2017 21:07:37 +0300 Subject: [PATCH] Remove use of C file api in LocalFileLoader Instead use native system calls. --- Common/CommonFuncs.h | 7 ++-- Core/FileLoaders/LocalFileLoader.cpp | 63 ++++++++++++++++++---------- Core/FileLoaders/LocalFileLoader.h | 6 ++- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/Common/CommonFuncs.h b/Common/CommonFuncs.h index f4a06cfe8b..4d18e2c84f 100644 --- a/Common/CommonFuncs.h +++ b/Common/CommonFuncs.h @@ -59,8 +59,11 @@ inline u64 __rotr64(u64 x, unsigned int shift){ return (x >> n) | (x << (64 - n)); } +#ifndef linux +#define pread64 pread +#endif + #else // WIN32 - #include // Function Cross-Compatibility #define strcasecmp _stricmp @@ -75,8 +78,6 @@ inline u64 __rotr64(u64 x, unsigned int shift){ #define fseeko _fseeki64 #define ftello _ftelli64 #define atoll _atoi64 - #define fileno _fileno - #define fileno_to_handle _get_osfhandle #if _M_IX86 #define Crash() {__asm int 3} #else diff --git a/Core/FileLoaders/LocalFileLoader.cpp b/Core/FileLoaders/LocalFileLoader.cpp index 43f85e31b7..8c63da1654 100644 --- a/Core/FileLoaders/LocalFileLoader.cpp +++ b/Core/FileLoaders/LocalFileLoader.cpp @@ -20,39 +20,64 @@ #include "Core/FileLoaders/LocalFileLoader.h" #ifdef _WIN32 #include "Common/CommonWindows.h" +#else +#include #endif LocalFileLoader::LocalFileLoader(const std::string &filename) - : fd_(0), f_(nullptr), filesize_(0), filename_(filename) { - // FIXME: perhaps at this point we should just use plain open? - f_ = File::OpenCFile(filename, "rb"); - fd_ = fileno(f_); - if (!f_) { + : filesize_(0), filename_(filename) { + +#ifndef _WIN32 + + fd_ = open(filename.c_str(), O_RDONLY | O_CLOEXEC); + if (fd_ == -1) { return; } - -#ifdef __ANDROID__ - // Android NDK does not support 64-bit file I/O using C streams - // so we fall back onto syscalls off64_t off = lseek64(fd_, 0, SEEK_END); filesize_ = off; lseek64(fd_, 0, SEEK_SET); + +#else // !_WIN32 + + const DWORD access = GENERIC_READ, share = FILE_SHARE_READ, mode = OPEN_EXISTING, + const DWORD flags = FILE_ATTRIBUTE_NORMAL; +#if PPSSPP_PLATFORM(UWP) + handle_ = CreateFile2(ConvertUTF8ToWString(filename).c_str(), access, share, mode, nullptr); #else - fseek(f_, 0, SEEK_END); - filesize_ = ftello(f_); - fseek(f_, 0, SEEK_SET); + handle_ = CreateFile(ConvertUTF8ToWString(filename).c_str(), access, share, nullptr, mode, flags, nullptr); #endif + if (handle_ == INVALID_HANDLE_VALUE) { + return; + } + FILE_STANDARD_INFO info; + if (GetFileInformationByHandleEx(handle_, FileStandardInfo, &info, sizeof(info)) == 0) { + return; + } + filesize_ = info.EndOfFile.QuadPart; + +#endif // !_WIN32 + } LocalFileLoader::~LocalFileLoader() { - if (f_) { - fclose(f_); +#ifndef _WIN32 + if (fd_ != -1) { + close(fd_); } +#else + if (handle_ != INVALID_HANDLE_VALUE) { + CloseHandle(_handle); + } +#endif } bool LocalFileLoader::Exists() { // If we couldn't open it for reading, we say it does not exist. - if (f_ || IsDirectory()) { +#ifndef _WIN32 + if (fd_ != -1 || IsDirectory()) { +#else + if (handle_ != INVALID_HANDLE_VALUE || IsDirectory()) { +#endif FileInfo info; return getFileInfo(filename_.c_str(), &info); } @@ -77,17 +102,13 @@ std::string LocalFileLoader::Path() const { size_t LocalFileLoader::ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags) { #ifndef _WIN32 - return pread(fd_, data, bytes * count, absolutePos) / bytes; + return pread64(fd_, data, bytes * count, absolutePos) / bytes; #else DWORD read = -1; - intptr_t handle = fileno_to_handle(fd_); - if (handle == 0) { - return -1; - } OVERLAPPED offset = { 0 }; offset.Offset = (DWORD)(absolutePos & 0xffffffff); offset.OffsetHigh = (DWORD)((absolutePos & 0xffffffff00000000) >> 32); - auto result = ReadFile((HANDLE)handle, data, bytes * count, &read, &offset); + auto result = ReadFile(handle_, data, bytes * count, &read, &offset); return result == TRUE ? (size_t)read / bytes : -1; #endif } diff --git a/Core/FileLoaders/LocalFileLoader.h b/Core/FileLoaders/LocalFileLoader.h index f85b67ecfa..cb88d196b8 100644 --- a/Core/FileLoaders/LocalFileLoader.h +++ b/Core/FileLoaders/LocalFileLoader.h @@ -32,9 +32,11 @@ public: virtual size_t ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags = Flags::NONE) override; private: - // First only used by Android, but we can keep it here for everyone. +#ifndef _WIN32 int fd_; - FILE *f_; +#else + HANDLE handle_; +#endif u64 filesize_; std::string filename_; };