question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Libaums just to measure free drive space and then close it

See original GitHub issue

Libaums has very fast methods for measuring free drive space and drive capacity, whereas Android 12 has crippled our ability to measure these through normal means. My goal is to copy files to the USB drive using the standard Android API (by getting a Uri from SAF), and then when I’m done I want to use Libaums to measure the drive space. But I’m running into a problem with this, as it seems that SAF won’t fully release control of the USB drive. In fact, trying to init the drive using Libaums corrupts many of the files that were just copied to the USB drive. Is there some way to use Libaums side-by-side with the standard Android APIs?

Here’s my method for measuring space:

fun getDriveInfoFromLibaums(): Boolean {
    val deviceListFromLibaums = UsbMassStorageDevice.getMassStorageDevices(appContext)
    if (deviceListFromLibaums.isNullOrEmpty()) return false

    var usbMassStorageDevice: UsbMassStorageDevice? = null
    try {
        usbMassStorageDevice = deviceListFromLibaums[0]
        val tempUsbDevice = usbMassStorageDevice.usbDevice
        usbMassStorageDevice.init()
        val currentFileSystem = usbMassStorageDevice.partitions[0].fileSystem

        prefs.lastRecordedDriveSize = currentFileSystem.capacity
        prefs.lastRecordedDriveFreeSpace = currentFileSystem.freeSpace
        usbMassStorageDevice.close()

        return true
    } catch (throwable: Throwable) {
        usbMassStorageDevice?.close()
        return false
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
depaucommented, Nov 19, 2021

Is there perhaps a way to use the Libaums approach within the standard Android APIs?

No. Libaums directly accesses the USB drive by issuing SCSI commands directly, bypassing the Linux kernel’s block device subsystem. Android does not, and it should never, since it is not in the condition of a regular unprivileged app that needs to work around limitations.

You should probably look for the source code of ContentResolver.openAssetFileDescriptor(), see what it does and see what changed in the latest release.

0reactions
depaucommented, Nov 20, 2021

It does not. However, yours doesn’t look like a great idea: there could be pending writes before the user launches your app and you’re likely to corrupt their filesystem.

How about tweaking the user experience so the user doesn’t get bored while you’re loading the disk space? Add a spinner and wait for the file descriptor in a background thread.

Read more comments on GitHub >

github_iconTop Results From Across the Web

magnusja/libaums - GitHub
A library to access USB mass storage devices (pen drives, external HDDs, card readers) using the Android USB Host API. Currently it supports...
Read more >
libaums - can a file copied to or from USB drive be corrupt if ...
The title is the question. I'm using libaums to transfer files both ways using BufferedInput/OutputStream, calling close() at the end. And then ......
Read more >
Free up drive space in Windows - Microsoft Support
Learn how you can free up drive space in Windows. Keep your PC running smoothly and up to date by increasing the disk...
Read more >
Free up space on your Chromebook - Google Support
Check how much storage is being used. At the bottom right, select the time. Select Settings . In the "Device" section, select Storage...
Read more >
Troubleshoot Disk Space Usage on Tableau Server Nodes
Note: Disk space monitoring measures free disk space on each server node. ... in the Tableau Knowledge Base: After Running Out of Hard...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found