Move AndroidCanUseRawFS and AndroidTranslateFilename into AndroidStorage namespace

This commit is contained in:
OpenSauce04 2026-03-07 23:38:46 +00:00 committed by OpenSauce
parent e87635095a
commit c71b2dc822
3 changed files with 51 additions and 55 deletions

View File

@ -293,6 +293,18 @@ bool MoveAndRenameFile(const std::string& src_full_path, const std::string& dest
return result; return result;
} }
std::string TranslateFilePath(const std::string& filepath) {
std::optional<std::string> userDirLocation = GetUserDirectory();
if (userDirLocation) {
return *userDirLocation + filepath;
}
return "";
}
bool CanUseRawFS() {
return AndroidStorage::GetBuildFlavor() != AndroidBuildFlavors::GOOGLEPLAY;
}
#define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) \ #define FR(FunctionName, ReturnValue, JMethodID, Caller, JMethodName, Signature) \
F(FunctionName, ReturnValue, JMethodID, Caller) F(FunctionName, ReturnValue, JMethodID, Caller)
#define F(FunctionName, ReturnValue, JMethodID, Caller) \ #define F(FunctionName, ReturnValue, JMethodID, Caller) \

View File

@ -56,7 +56,9 @@ ANDROID_STORAGE_FUNCTIONS(FS)
#undef F #undef F
#undef FS #undef FS
#undef FR #undef FR
bool CanUseRawFS();
bool MoveAndRenameFile(const std::string& src_full_path, const std::string& dest_full_path); bool MoveAndRenameFile(const std::string& src_full_path, const std::string& dest_full_path);
std::string TranslateFilePath(const std::string& filepath);
// Reference: // Reference:
// https://developer.android.com/reference/android/os/ParcelFileDescriptor#parseMode(java.lang.String) // https://developer.android.com/reference/android/os/ParcelFileDescriptor#parseMode(java.lang.String)
enum class AndroidOpenMode { enum class AndroidOpenMode {

View File

@ -145,23 +145,6 @@ static void StripTailDirSlashes(std::string& fname) {
fname.resize(i); fname.resize(i);
} }
#if defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
namespace {
std::string AndroidTranslateFilename(const std::string& file) {
std::optional<std::string> userDirLocation = AndroidStorage::GetUserDirectory();
if (userDirLocation) {
return *userDirLocation + file;
}
return "";
}
bool AndroidCanUseRawFS() {
return AndroidStorage::GetBuildFlavor() != AndroidStorage::AndroidBuildFlavors::GOOGLEPLAY;
}
} // anonymous namespace
#endif
bool Exists(const std::string& filename) { bool Exists(const std::string& filename) {
std::string copy(filename); std::string copy(filename);
StripTailDirSlashes(copy); StripTailDirSlashes(copy);
@ -175,9 +158,9 @@ bool Exists(const std::string& filename) {
int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info);
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
int result; int result;
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
struct stat file_info; struct stat file_info;
result = stat(AndroidTranslateFilename(copy).c_str(), &file_info); result = stat(AndroidStorage::TranslateFilePath(copy).c_str(), &file_info);
} else { } else {
result = AndroidStorage::FileExists(filename) ? 0 : -1; result = AndroidStorage::FileExists(filename) ? 0 : -1;
} }
@ -204,8 +187,8 @@ bool IsDirectory(const std::string& filename) {
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
struct stat file_info; struct stat file_info;
int result; int result;
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
result = stat(AndroidTranslateFilename(copy).c_str(), &file_info); result = stat(AndroidStorage::TranslateFilePath(copy).c_str(), &file_info);
} else { } else {
return AndroidStorage::IsDirectory(filename); return AndroidStorage::IsDirectory(filename);
} }
@ -222,19 +205,19 @@ bool IsDirectory(const std::string& filename) {
return S_ISDIR(file_info.st_mode); return S_ISDIR(file_info.st_mode);
} }
bool Delete(const std::string& filename) { bool Delete(const std::string& filepath) {
LOG_TRACE(Common_Filesystem, "file {}", filename); LOG_TRACE(Common_Filesystem, "file {}", filepath);
// Return true because we care about the file no // Return true because we care about the file no
// being there, not the actual delete. // being there, not the actual delete.
if (!Exists(filename)) { if (!Exists(filepath)) {
LOG_DEBUG(Common_Filesystem, "{} does not exist", filename); LOG_DEBUG(Common_Filesystem, "{} does not exist", filepath);
return true; return true;
} }
// We can't delete a directory // We can't delete a directory
if (IsDirectory(filename)) { if (IsDirectory(filepath)) {
LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filename); LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filepath);
return false; return false;
} }
@ -248,39 +231,39 @@ bool Delete(const std::string& filename) {
// finally deleted. // finally deleted.
static std::atomic<uint64_t> counter{0}; static std::atomic<uint64_t> counter{0};
const std::wstring wfilename = Common::UTF8ToUTF16W(filename); const std::wstring wfilepath = Common::UTF8ToUTF16W(filepath);
const DWORD pid = GetCurrentProcessId(); const DWORD pid = GetCurrentProcessId();
const uint64_t id = counter++; const uint64_t id = counter++;
std::wstring deleted_path = std::wstring deleted_path =
wfilename + L".deleted." + std::to_wstring(pid) + L"." + std::to_wstring(id); wfilepath + L".deleted." + std::to_wstring(pid) + L"." + std::to_wstring(id);
// Rename first // Rename first
if (MoveFileExW(wfilename.c_str(), deleted_path.c_str(), MOVEFILE_REPLACE_EXISTING)) { if (MoveFileExW(wfilepath.c_str(), deleted_path.c_str(), MOVEFILE_REPLACE_EXISTING)) {
// Then mark file for deletion // Then mark file for deletion
DeleteFileW(deleted_path.c_str()); DeleteFileW(deleted_path.c_str());
return true; return true;
} }
LOG_ERROR(Common_Filesystem, "Rename to deleted path failed on {}: {}", filename, LOG_ERROR(Common_Filesystem, "Rename to deleted path failed on {}: {}", filepath,
GetLastErrorMsg()); GetLastErrorMsg());
return false; return false;
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
if (unlink(AndroidTranslateFilename(filename).c_str()) == -1) { if (unlink(AndroidStorage::TranslateFilePath(filepath).c_str()) == -1) {
LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg()); LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filepath, GetLastErrorMsg());
return false; return false;
} }
} else { } else {
if (!AndroidStorage::DeleteDocument(filename)) { if (!AndroidStorage::DeleteDocument(filepath)) {
LOG_ERROR(Common_Filesystem, "unlink failed on {}", filename); LOG_ERROR(Common_Filesystem, "unlink failed on {}", filepath);
return false; return false;
} }
} }
#else #else
if (unlink(filename.c_str()) == -1) { if (unlink(filepath.c_str()) == -1) {
LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg()); LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filepath, GetLastErrorMsg());
return false; return false;
} }
#endif #endif
@ -301,8 +284,8 @@ bool CreateDir(const std::string& path) {
LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error); LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error);
return false; return false;
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
if (mkdir(AndroidTranslateFilename(path).c_str(), 0755) == 0) if (mkdir(AndroidStorage::TranslateFilePath(path).c_str(), 0755) == 0)
return true; return true;
int err = errno; int err = errno;
@ -404,8 +387,8 @@ bool DeleteDir(const std::string& filename) {
if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str())) if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str()))
return true; return true;
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
if (rmdir(AndroidTranslateFilename(filename).c_str()) == 0) if (rmdir(AndroidStorage::TranslateFilePath(filename).c_str()) == 0)
return true; return true;
} else { } else {
if (AndroidStorage::DeleteDocument(filename)) if (AndroidStorage::DeleteDocument(filename))
@ -428,10 +411,9 @@ bool Rename(const std::string& srcFullPath, const std::string& destFullPath) {
return true; return true;
} }
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
if (rename(AndroidTranslateFilename(srcFullPath).c_str(), if (rename(AndroidStorage::TranslateFilePath(srcFullPath).c_str(),
AndroidTranslateFilename(destFullPath).c_str()) == 0) { AndroidStorage::TranslateFilePath(destFullPath).c_str()) == 0) {
return true; return true;
} }
} else { } else {
@ -504,9 +486,9 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename) {
}; };
#if defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #if defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
return copy_files(AndroidTranslateFilename(srcFilename), return copy_files(AndroidStorage::TranslateFilePath(srcFilename),
AndroidTranslateFilename(destFilename)); AndroidStorage::TranslateFilePath(destFilename));
} else { } else {
return AndroidStorage::CopyFile(srcFilename, std::string(GetParentPath(destFilename)), return AndroidStorage::CopyFile(srcFilename, std::string(GetParentPath(destFilename)),
std::string(GetFilename(destFilename))); std::string(GetFilename(destFilename)));
@ -534,8 +516,8 @@ u64 GetSize(const std::string& filename) {
struct _stat64 buf; struct _stat64 buf;
if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0) if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0)
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
if (stat(AndroidTranslateFilename(filename).c_str(), &buf) == 0) { if (stat(AndroidStorage::TranslateFilePath(filename).c_str(), &buf) == 0) {
return buf.st_size; return buf.st_size;
} }
} else { } else {
@ -616,10 +598,10 @@ std::optional<std::vector<std::string>> ListDirectoryEntries(const std::string&
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
std::optional<std::vector<std::string>> ListDirectoryEntries(const std::string& directory) { std::optional<std::vector<std::string>> ListDirectoryEntries(const std::string& directory) {
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
std::vector<std::string> entries; std::vector<std::string> entries;
DIR* dirp = opendir(AndroidTranslateFilename(directory).c_str()); DIR* dirp = opendir(AndroidStorage::TranslateFilePath(directory).c_str());
if (!dirp) if (!dirp)
return std::nullopt; return std::nullopt;
@ -1324,8 +1306,8 @@ bool IOFile::Open() {
m_good = m_file != nullptr; m_good = m_file != nullptr;
#elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS) #elif defined(ANDROID) && !defined(HAVE_LIBRETRO_VFS)
if (AndroidCanUseRawFS()) { if (AndroidStorage::CanUseRawFS()) {
m_file = FOPEN(AndroidTranslateFilename(filename).c_str(), openmode.c_str()); m_file = FOPEN(AndroidStorage::TranslateFilePath(filename).c_str(), openmode.c_str());
} else { } else {
// Check whether filepath is startsWith content // Check whether filepath is startsWith content
AndroidStorage::AndroidOpenMode android_open_mode = AndroidStorage::ParseOpenmode(openmode); AndroidStorage::AndroidOpenMode android_open_mode = AndroidStorage::ParseOpenmode(openmode);