Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Win32 file IO with UWP safe variants and add support for getting drives to UWP build #15652

Merged
merged 6 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ PPSSPPDebugARM64.exe.manifest
PPSSPPDebugARM.exe.manifest
assets/flash0
UWP/icph
UWP/*.ipch
UWP/Content/*
!UWP/Content/.empty
UWP/.vs
Expand Down
27 changes: 24 additions & 3 deletions Common/File/DirListing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <direct.h>
#if PPSSPP_PLATFORM(UWP)
#include <fileapifromapp.h>
#endif
#else
#include <strings.h>
#include <dirent.h>
Expand Down Expand Up @@ -62,7 +65,11 @@ bool GetFileInfo(const Path &path, FileInfo * fileInfo) {

#if PPSSPP_PLATFORM(WINDOWS)
WIN32_FILE_ATTRIBUTE_DATA attrs;
#if PPSSPP_PLATFORM(UWP)
if (!GetFileAttributesExFromAppW(path.ToWString().c_str(), GetFileExInfoStandard, &attrs)) {
#else
if (!GetFileAttributesExW(path.ToWString().c_str(), GetFileExInfoStandard, &attrs)) {
#endif
fileInfo->size = 0;
fileInfo->isDirectory = false;
fileInfo->exists = false;
Expand Down Expand Up @@ -206,7 +213,11 @@ bool GetFilesInDir(const Path &directory, std::vector<FileInfo> *files, const ch
}
// Find the first file in the directory.
WIN32_FIND_DATA ffd;
#if PPSSPP_PLATFORM(UWP)
HANDLE hFind = FindFirstFileExFromAppW((directory.ToWString() + L"\\*").c_str(), FindExInfoStandard, &ffd, FindExSearchNameMatch, NULL, 0);
#else
HANDLE hFind = FindFirstFileEx((directory.ToWString() + L"\\*").c_str(), FindExInfoStandard, &ffd, FindExSearchNameMatch, NULL, 0);
#endif
if (hFind == INVALID_HANDLE_VALUE) {
return 0;
}
Expand Down Expand Up @@ -289,11 +300,21 @@ bool GetFilesInDir(const Path &directory, std::vector<FileInfo> *files, const ch
// Returns a vector with the device names
std::vector<std::string> GetWindowsDrives()
{
#if PPSSPP_PLATFORM(UWP)
return std::vector<std::string>(); // TODO UWP http://stackoverflow.com/questions/37404405/how-to-get-logical-drives-names-in-windows-10
#else
std::vector<std::string> drives;

#if PPSSPP_PLATFORM(UWP)
DWORD logicaldrives = GetLogicalDrives();
for (int i = 0; i < 26; i++)
{
if (logicaldrives & (1 << i))
{
CHAR driveName[] = { TEXT('A') + i, TEXT(':'), TEXT('\\'), TEXT('\0') };
std::string str(driveName);
drives.push_back(driveName);
}
}
return drives;
#else
const DWORD buffsize = GetLogicalDriveStrings(0, NULL);
std::vector<wchar_t> buff(buffsize);
if (GetLogicalDriveStrings(buffsize, buff.data()) == buffsize - 1)
Expand Down
40 changes: 37 additions & 3 deletions Common/File/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#include <commdlg.h> // for GetSaveFileName
#include <io.h>
#include <direct.h> // getcwd
#if PPSSPP_PLATFORM(UWP)
#include <fileapifromapp.h>
#endif
#else
#include <sys/param.h>
#include <sys/types.h>
Expand Down Expand Up @@ -253,7 +256,7 @@ static bool ResolvePathVista(const std::wstring &path, wchar_t *buf, DWORD bufSi

if (getFinalPathNameByHandleW) {
#if PPSSPP_PLATFORM(UWP)
HANDLE hFile = CreateFile2(path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
HANDLE hFile = CreateFile2FromAppW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
#else
HANDLE hFile = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
#endif
Expand Down Expand Up @@ -367,9 +370,15 @@ bool Exists(const Path &path) {
int OldMode = SetErrorMode(SEM_FAILCRITICALERRORS);
#endif
WIN32_FILE_ATTRIBUTE_DATA data{};
#if PPSSPP_PLATFORM(UWP)
if (!GetFileAttributesExFromAppW(path.ToWString().c_str(), GetFileExInfoStandard, &data) || data.dwFileAttributes == INVALID_FILE_ATTRIBUTES) {
return false;
}
#else
if (!GetFileAttributesEx(path.ToWString().c_str(), GetFileExInfoStandard, &data) || data.dwFileAttributes == INVALID_FILE_ATTRIBUTES) {
return false;
}
#endif
#if !PPSSPP_PLATFORM(UWP)
SetErrorMode(OldMode);
#endif
Expand Down Expand Up @@ -399,7 +408,11 @@ bool IsDirectory(const Path &filename) {

#if defined(_WIN32)
WIN32_FILE_ATTRIBUTE_DATA data{};
#if PPSSPP_PLATFORM(UWP)
if (!GetFileAttributesExFromAppW(filename.ToWString().c_str(), GetFileExInfoStandard, &data) || data.dwFileAttributes == INVALID_FILE_ATTRIBUTES) {
#else
if (!GetFileAttributesEx(filename.ToWString().c_str(), GetFileExInfoStandard, &data) || data.dwFileAttributes == INVALID_FILE_ATTRIBUTES) {
#endif
auto err = GetLastError();
if (err != ERROR_FILE_NOT_FOUND) {
WARN_LOG(COMMON, "GetFileAttributes failed on %s: %08x %s", filename.ToVisualString().c_str(), (uint32_t)err, GetStringErrorMsg(err).c_str());
Expand Down Expand Up @@ -448,10 +461,17 @@ bool Delete(const Path &filename) {
}

#ifdef _WIN32
#if PPSSPP_PLATFORM(UWP)
if (!DeleteFileFromAppW(filename.ToWString().c_str())) {
WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s", filename.c_str(), GetLastErrorMsg().c_str());
return false;
}
#else
if (!DeleteFile(filename.ToWString().c_str())) {
WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s", filename.c_str(), GetLastErrorMsg().c_str());
return false;
}
#endif
#else
if (unlink(filename.c_str()) == -1) {
WARN_LOG(COMMON, "Delete: unlink failed on %s: %s",
Expand Down Expand Up @@ -496,8 +516,14 @@ bool CreateDir(const Path &path) {

DEBUG_LOG(COMMON, "CreateDir('%s')", path.c_str());
#ifdef _WIN32
#if PPSSPP_PLATFORM(UWP)
if (CreateDirectoryFromAppW(path.ToWString().c_str(), NULL))
return true;
#else
if (::CreateDirectory(path.ToWString().c_str(), NULL))
return true;
#endif

DWORD error = GetLastError();
if (error == ERROR_ALREADY_EXISTS) {
WARN_LOG(COMMON, "CreateDir: CreateDirectory failed on %s: already exists", path.c_str());
Expand Down Expand Up @@ -585,8 +611,13 @@ bool DeleteDir(const Path &path) {
}

#ifdef _WIN32
#if PPSSPP_PLATFORM(UWP)
if (RemoveDirectoryFromAppW(path.ToWString().c_str()))
return true;
#else
if (::RemoveDirectory(path.ToWString().c_str()))
return true;
#endif
#else
if (rmdir(path.c_str()) == 0)
return true;
Expand Down Expand Up @@ -662,9 +693,8 @@ bool Copy(const Path &srcFilename, const Path &destFilename) {
INFO_LOG(COMMON, "Copy: %s --> %s", srcFilename.c_str(), destFilename.c_str());
#ifdef _WIN32
#if PPSSPP_PLATFORM(UWP)
if (CopyFile2(srcFilename.ToWString().c_str(), destFilename.ToWString().c_str(), nullptr))
if (CopyFileFromAppW(srcFilename.ToWString().c_str(), destFilename.ToWString().c_str(), FALSE))
return true;
return false;
#else
if (CopyFile(srcFilename.ToWString().c_str(), destFilename.ToWString().c_str(), FALSE))
return true;
Expand Down Expand Up @@ -796,7 +826,11 @@ uint64_t GetFileSize(const Path &filename) {

#if defined(_WIN32) && defined(UNICODE)
WIN32_FILE_ATTRIBUTE_DATA attr;
#if PPSSPP_PLATFORM(UWP)
if (!GetFileAttributesExFromAppW(filename.ToWString().c_str(), GetFileExInfoStandard, &attr))
#else
if (!GetFileAttributesEx(filename.ToWString().c_str(), GetFileExInfoStandard, &attr))
#endif
return 0;
if (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return 0;
Expand Down
8 changes: 8 additions & 0 deletions Core/FileLoaders/DiskCachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "Core/FileLoaders/DiskCachingFileLoader.h"
#include "Core/System.h"

#if PPSSPP_PLATFORM(UWP)
#include <fileapifromapp.h>
#endif

#if PPSSPP_PLATFORM(SWITCH)
// Far from optimal, but I guess it works...
#define fseeko fseek
Expand Down Expand Up @@ -827,7 +831,11 @@ void DiskCachingFileLoaderCache::GarbageCollectCacheFiles(u64 goalBytes) {

#ifdef _WIN32
const std::wstring w32path = file.fullName.ToWString();
#if PPSSPP_PLATFORM(UWP)
bool success = DeleteFileFromAppW(w32path.c_str()) != 0;
#else
bool success = DeleteFileW(w32path.c_str()) != 0;
#endif
#else
bool success = unlink(file.fullName.c_str()) == 0;
#endif
Expand Down
5 changes: 4 additions & 1 deletion Core/FileLoaders/LocalFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

#ifdef _WIN32
#include "Common/CommonWindows.h"
#if PPSSPP_PLATFORM(UWP)
#include <fileapifromapp.h>
#endif
#else
#include <fcntl.h>
#endif
Expand Down Expand Up @@ -85,7 +88,7 @@ LocalFileLoader::LocalFileLoader(const Path &filename)

const DWORD access = GENERIC_READ, share = FILE_SHARE_READ, mode = OPEN_EXISTING, flags = FILE_ATTRIBUTE_NORMAL;
#if PPSSPP_PLATFORM(UWP)
handle_ = CreateFile2(filename.ToWString().c_str(), access, share, mode, nullptr);
handle_ = CreateFile2FromAppW(filename.ToWString().c_str(), access, share, mode, nullptr);
#else
handle_ = CreateFile(filename.ToWString().c_str(), access, share, nullptr, mode, flags, nullptr);
#endif
Expand Down
7 changes: 5 additions & 2 deletions Core/FileSystems/DirectoryFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
#ifdef _WIN32
#include "Common/CommonWindows.h"
#include <sys/stat.h>
#if PPSSPP_PLATFORM(UWP)
#include <fileapifromapp.h>
#endif
#undef FILE_OPEN
#else
#include <dirent.h>
Expand Down Expand Up @@ -158,7 +161,7 @@ bool DirectoryFileHandle::Open(const Path &basePath, std::string &fileName, File

// Let's do it!
#if PPSSPP_PLATFORM(UWP)
hFile = CreateFile2(fullName.ToWString().c_str(), desired, sharemode, openmode, nullptr);
hFile = CreateFile2FromAppW(fullName.ToWString().c_str(), desired, sharemode, openmode, nullptr);
#else
hFile = CreateFile(fullName.ToWString().c_str(), desired, sharemode, 0, openmode, 0, 0);
#endif
Expand All @@ -170,7 +173,7 @@ bool DirectoryFileHandle::Open(const Path &basePath, std::string &fileName, File
// Sometimes, the file is locked for write, let's try again.
sharemode |= FILE_SHARE_WRITE;
#if PPSSPP_PLATFORM(UWP)
hFile = CreateFile2(fullName.ToWString().c_str(), desired, sharemode, openmode, nullptr);
hFile = CreateFile2FromAppW(fullName.ToWString().c_str(), desired, sharemode, openmode, nullptr);
#else
hFile = CreateFile(fullName.ToWString().c_str(), desired, sharemode, 0, openmode, 0, 0);
#endif
Expand Down
8 changes: 7 additions & 1 deletion Core/FileSystems/VirtualDiscFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#ifdef _WIN32
#include "Common/CommonWindows.h"
#include <sys/stat.h>
#if PPSSPP_PLATFORM(UWP)
#include <fileapifromapp.h>
#endif
#else
#include <dirent.h>
#include <unistd.h>
Expand Down Expand Up @@ -673,8 +676,11 @@ std::vector<PSPFileInfo> VirtualDiscFileSystem::GetDirListing(std::string path)

std::wstring w32path = GetLocalPath(path).ToWString() + L"\\*.*";

#if PPSSPP_PLATFORM(UWP)
hFind = FindFirstFileExFromAppW(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);
#else
hFind = FindFirstFileEx(w32path.c_str(), FindExInfoStandard, &findData, FindExSearchNameMatch, NULL, 0);

#endif
if (hFind == INVALID_HANDLE_VALUE) {
return myVector; //the empty list
}
Expand Down
5 changes: 4 additions & 1 deletion ext/libzip/zip_source_file_win32_utf16.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef MS_UWP
#include <fileapifromapp.h>
#endif

#include "zip_source_file_win32.h"

Expand Down Expand Up @@ -92,7 +95,7 @@ utf16_create_file(const char *name, DWORD access, DWORD share_mode, PSECURITY_AT

return CreateFile2((const wchar_t *)name, access, share_mode, creation_disposition, &extParams);
#else
return CreateFile2((const wchar_t *)name, access, share_mode, creation_disposition, NULL);
return CreateFile2FromAppW((const wchar_t *)name, access, share_mode, creation_disposition, NULL);
#endif
#else
return CreateFileW((const wchar_t *)name, access, share_mode, security_attributes, creation_disposition, file_attributes, template_file);
Expand Down