Use a simpler method that actually works to get free storage space from content URI.

This commit is contained in:
Henrik Rydgård 2021-07-20 11:57:19 +02:00
parent 14e24b28ad
commit 6b0851cb73
2 changed files with 15 additions and 15 deletions

View file

@ -203,7 +203,6 @@ bool DirectoryFileHandle::Open(const Path &basePath, std::string &fileName, File
#endif
Path fullName = GetLocalPath(basePath, fileName);
INFO_LOG(FILESYS, "Actually opening %s", fullName.c_str());
// On the PSP, truncating doesn't lose data. If you seek later, you'll recover it.
// This is abnormal, so we deviate from the PSP's behavior and truncate on write/close.
@ -302,8 +301,6 @@ bool DirectoryFileHandle::Open(const Path &basePath, std::string &fileName, File
}
}
INFO_LOG(FILESYS, "Opening '%s' straight", fullName.c_str());
int flags = 0;
if (access & FILEACCESS_APPEND) {
flags |= O_APPEND;

View file

@ -8,6 +8,8 @@ import android.os.Bundle;
import android.os.Looper;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.system.StructStatVfs;
import android.system.Os;
import android.os.storage.StorageManager;
import android.content.ContentResolver;
import android.database.Cursor;
@ -314,22 +316,23 @@ public class PpssppActivity extends NativeActivity {
// The example in Android documentation uses this.getFilesDir for path.
// There's also a way to beg the OS for more space, which might clear caches, but
// let's just not bother with that for now.
public long contentUriGetFreeStorageSpace(String uriString) {
public long contentUriGetFreeStorageSpace(String fileName) {
try {
Uri uri = Uri.parse(fileName);
StorageManager storageManager = getApplicationContext().getSystemService(StorageManager.class);
// In 29 and later, we can directly get the UUID for the storage volume
// through the URI.
UUID volumeUUID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Uri uri = Uri.parse(uriString);
volumeUUID = UUID.fromString(storageManager.getStorageVolume(uri).getUuid());
} else {
volumeUUID = storageManager.getUuidForPath(this.getFilesDir());
ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");
if (pfd == null) {
Log.w(TAG, "Failed to get free storage space from URI: " + fileName);
return -1;
}
long availableBytes = storageManager.getAllocatableBytes(volumeUUID);
return availableBytes;
} catch (Exception e) {
StructStatVfs stats = Os.fstatvfs(pfd.getFileDescriptor());
long freeSpace = stats.f_bavail * stats.f_bsize;
pfd.close();
return freeSpace;
} catch (Exception e) {
// FileNotFoundException | ErrnoException e
// Log.getStackTraceString(e)
Log.e(TAG, "contentUriGetFreeStorageSpace exception: " + e.toString());
return -1;
}