Skip to content

Commit

Permalink
BraveNewPipeLegacy: (Kitkat) fix getFreeStorageSpace()
Browse files Browse the repository at this point in the history
use available methods on Kitkat to getFreeStorageSpace() or in
case of using SAF just assume there is enough space as the Os.fstatvfs()
is not available and there is no quick solution.
  • Loading branch information
evermind-zz committed Apr 25, 2024
1 parent ffded76 commit 12eca05
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.schabi.newpipe.streams.io;

import android.os.Build;
import android.os.StatFs;
import android.system.ErrnoException;
import android.system.Os;
import android.system.StructStatVfs;

import java.io.FileDescriptor;

public final class BraveStoredDirectoryHelper {

private BraveStoredDirectoryHelper() {

}

public static BraveStructStatVfs statvfs(final String path) throws ErrnoException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final StructStatVfs stat = Os.statvfs(path);
return new BraveStructStatVfs(stat.f_bavail, stat.f_frsize);
} else {
final StatFs stat = new StatFs(path);
return new BraveStructStatVfs(stat.getAvailableBlocksLong(),
stat.getBlockSizeLong()
);
}
}

public static BraveStructStatVfs fstatvfs(final FileDescriptor fd) throws ErrnoException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final StructStatVfs stat = Os.fstatvfs(fd);
return new BraveStructStatVfs(stat.f_bavail, stat.f_frsize);
} else {
// TODO find a real solution for KitKat to determine the free fs on FileDescriptor
// return just a fake value. '1' is just used to not exceed Long.MAX_VALUE as both
// values are multiplied by the caller
return new BraveStructStatVfs(Long.MAX_VALUE, 1);
}
}

public static class BraveStructStatVfs {
/**
* @noinspection checkstyle:MemberName
*/
public final long f_bavail;
/**
* @noinspection checkstyle:MemberName
*/
public final long f_frsize;

/**
* @noinspection checkstyle:ParameterName
*/
BraveStructStatVfs(final long f_bavail,
final long f_frsize) {
this.f_bavail = f_bavail;
this.f_frsize = f_frsize;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.system.Os;
import android.system.StructStatVfs;
import android.util.Log;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -192,12 +190,12 @@ public boolean isDirect() {
*/
public long getFreeStorageSpace() {
try {
final StructStatVfs stat;
final BraveStoredDirectoryHelper.BraveStructStatVfs stat;

if (ioTree != null) {
// non-SAF file, use statvfs with the path directly (also, `context` would be null
// for non-SAF files, so we wouldn't be able to call `getContentResolver` anyway)
stat = Os.statvfs(ioTree.toString());
stat = BraveStoredDirectoryHelper.statvfs(ioTree.toString());

} else {
// SAF file, we can't get a path directly, so obtain a file descriptor first
Expand All @@ -208,7 +206,7 @@ public long getFreeStorageSpace() {
return Long.MAX_VALUE;
}
final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
stat = Os.fstatvfs(fileDescriptor);
stat = BraveStoredDirectoryHelper.fstatvfs(fileDescriptor);
}
}

Expand Down

0 comments on commit 12eca05

Please sign in to comment.