Search code examples
usbstorageunmount

libaums - can a file copied to or from USB drive be corrupt if drive not unmounted?


The title is the question. I'm using libaums to transfer files both ways using BufferedInput/OutputStream, calling close() at the end. And then I'm closing UsbMassStorageDevice and exiting the app.

However, in Settings->Storage I see that USB OTG storage stays mounted. And it can be unmounted only there, in Settings, in a popup. There doesn't seem to be a legal Android API to unmount USB drive from code.

Since I'm building a minimal UI/minimal interaction app, this is a major hassle. However, I've unplugged the stick many times without explicitly unmounting and the copied files were always valid (used diff to check).

So - does copying a file and calling close with libaums guarantee complete transfer of the file? Or is there some underlying OS memory buffer which can't be forced to flush, so unplugging the drive without unmounting can result in corrupt files?


Solution

  • good question! You should be fine because USB communication is always acknowledged. That means if your call to close returns without an exception it is guaranteed that the USB drive got the data replied with an "Ok" to the host. You can see that particular part of the code [1] where I am checking for a successful answer from the drive.

    [1] https://github.com/magnusja/libaums/blob/develop/libaums/src/main/java/com/github/mjdev/libaums/driver/scsi/ScsiBlockDevice.java#L175-L191