GithubHelp home page GithubHelp logo

samuelcampos / usbdrivedetector Goto Github PK

View Code? Open in Web Editor NEW
129.0 9.0 47.0 174 KB

A Java library to get a list of all usb storage devices connected to the computer.

License: MIT License

Java 100.00%
java-library usb-drive java

usbdrivedetector's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

usbdrivedetector's Issues

Quick Question

Does your Library has a callback method that informs the Java Application a new USB inserted?
Or it has to be done manually using some of thread?

Can your Library be adapted for other drives such C: or D:?

Abnormal command termination

I'm running your lib on
Linux 4.14.98 aarch64

I'm getting this on, what I assume to be, every listener interval
2020-10-14 16:09:48.483 WARN 2774 --- [ Thread-11] n.s.u.process.CommandExecutor : Abnormal command 'df -l' terminantion. Exit value: 1

This is the DF usage as printed by the OS. As you can see, there is no -l switch available.
Usage: df [-PkmhT] [FILESYSTEM]...

usbdrivedetector library returns empty value with the getRemovableDevices() function with MAC OS with JRE version 1.8.0_341-b10

usbdrivedetector library is not detecting the USB drive(FAT32) in the MAC OS (Monterey) with the latest JRE update 1.8.0_341.
In fact Library sends the connected event but driveDetector.getRemovableDevices() returns empty value. I even tried to run the Application with sudo mode and still same issue persists. Can you please help me on this.

Failure logs:
[Thread-14] ERROR net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager - An IUSBDriveListener threw an exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.sendEventToListeners(USBDeviceDetectorManager.java:221)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.lambda$updateConnectedDevices$0(USBDeviceDetectorManager.java:202)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.updateConnectedDevices(USBDeviceDetectorManager.java:201)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.access$100(USBDeviceDetectorManager.java:32)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager$ListenerTask.run(USBDeviceDetectorManager.java:247)\

IllegalArgumentException that occurs when the usb drive is removed.

Actual occurs an IllegalArgumentException when a connected USB Drive is removed, to resolved this i added the "if" code as follow :
if (root != null && root.isDirectory())
{
USBStorageDevice device = new USBStorageDevice(root);
listDevices.add(device);
}
in the addUSBDevice method of the AbstractStorageDeviceDetector.

Cannot set polling interval

 if (pollingInterval >= 0) {
            throw new IllegalArgumentException("'pollingInterval' must be greater than 0");
        }

should depend on logging api and not logging implementation

Since this is a library you should only depend on a logging API and not its implementation.
This will allow users of the library to choose their logging implementation (for instance I prefer logback).

More specifically, you should depend on org.slf4j:slf4j-api in the maven compile scope (default scope) and NOT org.slf4j:slf4j-simple.
In the test runtime scope you can depend on org.slf4j:slf4j-simple if you want so that you see the logs in your tests.

Windows 10 is not supported

Hi,

When I'm trying to do it on windows 10 I'm getting:

"no instances available" when trying it from CMD.

Any idea?

Bug in AbstractStorageDeviceDetector

Hello

in the Function addUSBDevice is a try catch block missing

try {
USBStorageDevice device = new USBStorageDevice(root);
listDevices.add(device);
} catch (IllegalArgumentException e) {
logger.debug("Could not add Device - IllegalArgumentException: "
+ e.getMessage());
}

Errors in the workspace after checkout master

I get lots of errors when I check out master (53 to be exact).

e.g.

log cannot be resolved AbstractStorageDeviceDetector.java /usbdrivedetector/src/main/java/net/samuelcampos/usbdrivedetector/detectors line 83 Java Problem

there are lots of these on log lines.
also other errors.

[feature request] Auto-mount USB

This is a feature request for when the Library runs on Linux.

It would be nice if this library would detect new USB device that are connected but not mounted and automatically mount them if possible.

I think this should not be enabled by default, and you would have to specify a directory where to mount new USBs. Also, I assume only programs that run as root could potentially do that.

It would be possible to detect USB that are connected but not mounted by keeping track of added/removed devices in /dev

Typo in API?

AbstractStorageDeviceDetector.getStorageDevicesDevices()

Seems like a failed copy/paste to me.

Leaks native memory under Windows

This library appears to leak native memory under Windows 10.

After a night of running USBDriveDetector, with a polling interval of 500ms, my Windows machine has ran totally out of memory. Heap of the Java application has not increased at all, so it must be native memory that exhausts the system.

.unmount() doesn't work at all for Windows 10

Unmounting devices doesn't work for Windows 10 and Java 11.

Steps to reproduce:

  1. insert any USB. I have SSD + HDD, so I have two roots on Windows, C and D. USB is named E when inserted.
  2. close any applications using E. It is legal to be unmounted and is not blocked by any read/write process.
  3. run code:
import java.io.IOException;
import java.nio.file.FileSystems;

import net.samuelcampos.usbdrivedetector.detectors.AbstractStorageDeviceDetector;
import net.samuelcampos.usbdrivedetector.unmounters.AbstractStorageDeviceUnmounter;

public class Test {

    public static void main(String[] args) throws IOException {
        FileSystems
                .getDefault()
                .getRootDirectories()
                .forEach(path -> System.out.println(path.toString()));

        var deviceDetector = AbstractStorageDeviceDetector.getInstance();
        var driveUnmounter = AbstractStorageDeviceUnmounter.getInstance();

        var usb = deviceDetector
                .getStorageDevices()
                .get(0); // I have 1 usb flash connected.
        
        driveUnmounter.unmount(usb);

        FileSystems
                .getDefault()
                .getRootDirectories()
                .forEach(path -> System.out.println(path.toString()));
    }
}
  1. output:
C:\
D:\
E:\
 TRACE [net.samuelcampos.usbdrivedetector.process.CommandExecutor] [main] - Running command: C:\WINDOWS\System32\wbem\wmic.exe logicaldisk where drivetype=2 get DeviceID,VolumeSerialNumber,VolumeName
TRACE [net.samuelcampos.usbdrivedetector.detectors.AbstractStorageDeviceDetector] [main] - Device found: E:\
C:\
D:\
E:\
  1. it's expected that root E will be unmounted and second .getRootDirectories() will not list it. Instead, E is still present, connected and is accessable by OS and/or application.

  2. conclusion - AbstractStorageDeviceUnmounter.unmount is not working at all, at least for Windows 10 + Java 11.

Windows DriveType 3 not picked up

I am running Windows 7 (yea I know...its a bummer but I have to wait a bit longer). My NTFS hard disk DriveType 3 is not picked up. We use USB external hard disk to transfer stuff to this Windows machine. However while doing some testing the api picks up USB memory sticks DriveType 2 just fine. Our NTFS formatted USB hard disk DriveType 3 however is not picked up at all.

Running Linux, DriveType 3 is found. Is Windows (7 and maybe also newer?) DriveType 3 just not supported?

I have mentioned this problem here also.
Originally posted by @Avec112 in #37 (comment)

Access to device

Hi there.

Could you implement a getDevice() in USBStorageDevice (for ex: /dev/sdc1) ? I need it because I'm having some problems while umount devices with "space" in its name, such as "Ubuntu 18.04 Install".

USBDriveListener settings ?

I use code:
driveDetector.getRemovableDevices().forEach(System.out::println);
driveDetector.addDriveListener (System.out :: System.out.println)
 It just writes the USB drives connected to the computer and the program stops.
I try to set the poolin interval but without success.
How to permanently monitor the connection of a new USB flash?

Windows 10
Thanks

Licensing

Hello,

I would be very interested in including your library for a work project but I could not find any mentioning of a licence. Could I use you licence in a commercial product ?

Would you release your project with a licence such as the MIT Licence (https://choosealicense.com/licenses/mit/) ?

Thanks,
Gwendal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.