Android: Make the handling of SAF open modes more robust

This commit is contained in:
JosJuice
2020-11-04 20:59:39 +01:00
parent 6a4ac74ec4
commit 70df5446d3
4 changed files with 43 additions and 25 deletions

View File

@ -18,7 +18,6 @@
#ifdef ANDROID
#include <algorithm>
#include "Common/StringUtil.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#endif
@ -66,24 +65,17 @@ void IOFile::Swap(IOFile& other) noexcept
bool IOFile::Open(const std::string& filename, const char openmode[])
{
Close();
#ifdef _WIN32
m_good = _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()) == 0;
#else
#ifdef ANDROID
if (StringBeginsWith(filename, "content://"))
{
// The Java method which OpenAndroidContent passes the mode to does not support the b specifier.
// Since we're on POSIX, it's fine to just remove the b.
std::string mode_without_b(openmode);
mode_without_b.erase(std::remove(mode_without_b.begin(), mode_without_b.end(), 'b'),
mode_without_b.end());
m_file = fdopen(OpenAndroidContent(filename, mode_without_b), mode_without_b.c_str());
}
if (IsPathAndroidContent(filename))
m_file = fdopen(OpenAndroidContent(filename, OpenModeToAndroid(openmode)), openmode);
else
#endif
{
m_file = std::fopen(filename.c_str(), openmode);
}
m_good = m_file != nullptr;
#endif