From 729496b60a3152266fea4281d76a0938d66136a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 20 Jul 2021 12:55:33 +0200 Subject: [PATCH] Error code support preparations --- Common/File/AndroidStorage.cpp | 21 +++++++++++++++++++ Common/File/AndroidStorage.h | 20 ++++++++++++++++++ Common/File/FileUtil.cpp | 4 ++-- Core/FileLoaders/LocalFileLoader.cpp | 2 +- .../src/org/ppsspp/ppsspp/PpssppActivity.java | 6 ++++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Common/File/AndroidStorage.cpp b/Common/File/AndroidStorage.cpp index 35cfcee09c..980bfaa9e2 100644 --- a/Common/File/AndroidStorage.cpp +++ b/Common/File/AndroidStorage.cpp @@ -18,6 +18,7 @@ static jmethodID contentUriGetFileInfo; static jmethodID contentUriFileExists; static jmethodID contentUriGetFreeStorageSpace; static jmethodID filePathGetFreeStorageSpace; +static jmethodID isExternalStoragePreservedLegacy; static jobject g_nativeActivity; @@ -48,6 +49,8 @@ void Android_RegisterStorageCallbacks(JNIEnv * env, jobject obj) { _dbg_assert_(contentUriGetFreeStorageSpace); filePathGetFreeStorageSpace = env->GetMethodID(env->GetObjectClass(obj), "filePathGetFreeStorageSpace", "(Ljava/lang/String;)J"); _dbg_assert_(filePathGetFreeStorageSpace); + isExternalStoragePreservedLegacy = env->GetMethodID(env->GetObjectClass(obj), "isExternalStoragePreservedLegacy", "()Z"); + _dbg_assert_(isExternalStoragePreservedLegacy); } bool Android_IsContentUri(const std::string &filename) { @@ -241,4 +244,22 @@ int64_t Android_GetFreeSpaceByFilePath(const std::string &filePath) { return env->CallLongMethod(g_nativeActivity, filePathGetFreeStorageSpace, param); } +bool Android_IsExternalStoragePreservedLegacy() { + if (!g_nativeActivity) { + return false; + } + auto env = getEnv(); + return env->CallBooleanMethod(g_nativeActivity, isExternalStoragePreservedLegacy); +} + +const char *Android_ErrorToString(ContentError error) { + switch (error) { + case ContentError::SUCCESS: return "SUCCESS"; + case ContentError::OTHER: return "OTHER"; + case ContentError::NOT_FOUND: return "NOT_FOUND"; + case ContentError::DISK_FULL: return "DISK_FULL"; + default: return "(UNKNOWN)"; + } +} + #endif diff --git a/Common/File/AndroidStorage.h b/Common/File/AndroidStorage.h index a217b3d4e0..55d49da478 100644 --- a/Common/File/AndroidStorage.h +++ b/Common/File/AndroidStorage.h @@ -13,6 +13,22 @@ enum class Android_OpenContentUriMode { READ_WRITE_TRUNCATE = 2, // "rwt" }; +// Matches the constants in PpssppActivity.java. +enum class ContentError { + SUCCESS = 0, + OTHER = -1, + NOT_FOUND = -2, + DISK_FULL = -3, +}; + +inline ContentError ContentErrorFromInt(int ival) { + if (ival >= 0) { + return ContentError::SUCCESS; + } else { + return (ContentError)ival; + } +} + #if PPSSPP_PLATFORM(ANDROID) && !defined(__LIBRETRO__) #include @@ -32,6 +48,8 @@ bool Android_GetFileInfo(const std::string &fileUri, File::FileInfo *info); bool Android_FileExists(const std::string &fileUri); int64_t Android_GetFreeSpaceByContentUri(const std::string &uri); int64_t Android_GetFreeSpaceByFilePath(const std::string &filePath); +bool Android_IsExternalStoragePreservedLegacy(); +const char *Android_ErrorToString(ContentError error); std::vector Android_ListContentUri(const std::string &uri); @@ -52,6 +70,8 @@ inline bool Android_GetFileInfo(const std::string &fileUri, File::FileInfo *info inline bool Android_FileExists(const std::string &fileUri) { return false; } inline int64_t Android_GetFreeSpaceByContentUri(const std::string &uri) { return -1; } inline int64_t Android_GetFreeSpaceByFilePath(const std::string &filePath) { return -1; } +inline bool Android_IsExternalStoragePreservedLegacy() { return false; } +inline const char *Android_ErrorToString(ContentError error) { return ""; } inline std::vector Android_ListContentUri(const std::string &uri) { return std::vector(); } diff --git a/Common/File/FileUtil.cpp b/Common/File/FileUtil.cpp index 14ffec9aa4..f43e443c21 100644 --- a/Common/File/FileUtil.cpp +++ b/Common/File/FileUtil.cpp @@ -107,7 +107,7 @@ FILE *OpenCFile(const Path &path, const char *mode) { INFO_LOG(COMMON, "Opening content file for read: '%s'", path.c_str()); // Read, let's support this - easy one. int descriptor = Android_OpenContentUriFd(path.ToString(), Android_OpenContentUriMode::READ); - if (descriptor == -1) { + if (descriptor < 0) { return nullptr; } return fdopen(descriptor, "rb"); @@ -133,7 +133,7 @@ FILE *OpenCFile(const Path &path, const char *mode) { // TODO: Support append modes and stuff... For now let's go with the most common one. int descriptor = Android_OpenContentUriFd(path.ToString(), Android_OpenContentUriMode::READ_WRITE_TRUNCATE); - if (descriptor == -1) { + if (descriptor < 0) { INFO_LOG(COMMON, "Opening '%s' for write failed", path.ToString().c_str()); return nullptr; } diff --git a/Core/FileLoaders/LocalFileLoader.cpp b/Core/FileLoaders/LocalFileLoader.cpp index d0c4c6828c..96d3412cf6 100644 --- a/Core/FileLoaders/LocalFileLoader.cpp +++ b/Core/FileLoaders/LocalFileLoader.cpp @@ -61,7 +61,7 @@ LocalFileLoader::LocalFileLoader(const Path &filename) if (filename.Type() == PathType::CONTENT_URI) { int fd = Android_OpenContentUriFd(filename.ToString(), Android_OpenContentUriMode::READ); INFO_LOG(SYSTEM, "Fd %d for content URI: '%s'", fd, filename.c_str()); - if (fd == -1) { + if (fd < 0) { ERROR_LOG(FILESYS, "LoadFileLoader failed to open content URI: '%s'", filename.c_str()); return; } diff --git a/android/src/org/ppsspp/ppsspp/PpssppActivity.java b/android/src/org/ppsspp/ppsspp/PpssppActivity.java index aba075a2f7..02c733ec5c 100644 --- a/android/src/org/ppsspp/ppsspp/PpssppActivity.java +++ b/android/src/org/ppsspp/ppsspp/PpssppActivity.java @@ -31,6 +31,12 @@ public class PpssppActivity extends NativeActivity { public static boolean libraryLoaded = false; + // Matches the enum in AndroidStorage.h. + private static final int CONTENT_ERROR_SUCCESS = 0; + private static final int CONTENT_ERROR_OTHER = -1; + private static final int CONTENT_ERROR_NOT_FOUND = -2; + private static final int CONTENT_ERROR_DISK_FULL = -3; + @SuppressWarnings("deprecation") public static void CheckABIAndLoadLibrary() { if (Build.CPU_ABI.equals("armeabi")) {