linux_usbfs: libusb_init() should succeed if no devices are present
authorChris Dickens <christopher.a.dickens@gmail.com>
Thu, 28 Dec 2017 08:40:14 +0000 (00:40 -0800)
committerChris Dickens <christopher.a.dickens@gmail.com>
Thu, 28 Dec 2017 08:40:14 +0000 (00:40 -0800)
When using sysfs to scan for devices, libusb_init() will fail if there
are no USB devices present. There is no reason for this behavior, so
this commit modifies the logic to only return an error if one or more
devices are present but none could be successfully enumerated.

Closes #301

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/os/linux_usbfs.c
libusb/version_nano.h

index 96fe07c..31ff219 100644 (file)
@@ -1299,11 +1299,12 @@ static int sysfs_get_device_list(struct libusb_context *ctx)
 {
        DIR *devices = opendir(SYSFS_DEVICE_PATH);
        struct dirent *entry;
-       int r = LIBUSB_ERROR_IO;
+       int num_devices = 0;
+       int num_enumerated = 0;
 
        if (!devices) {
                usbi_err(ctx, "opendir devices failed errno=%d", errno);
-               return r;
+               return LIBUSB_ERROR_IO;
        }
 
        while ((entry = readdir(devices))) {
@@ -1311,16 +1312,23 @@ static int sysfs_get_device_list(struct libusb_context *ctx)
                                || strchr(entry->d_name, ':'))
                        continue;
 
+               num_devices++;
+
                if (sysfs_scan_device(ctx, entry->d_name)) {
                        usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
                        continue;
                }
 
-               r = 0;
+               num_enumerated++;
        }
 
        closedir(devices);
-       return r;
+
+       /* successful if at least one device was enumerated or no devices were found */
+       if (num_enumerated || !num_devices)
+               return LIBUSB_SUCCESS;
+       else
+               return LIBUSB_ERROR_IO;
 }
 
 static int linux_default_scan_devices (struct libusb_context *ctx)
index 423c3df..61232e8 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11232
+#define LIBUSB_NANO 11233