Skip to content

Commit

Permalink
linux_usbfs: libusb_init() should succeed if no devices are present
Browse files Browse the repository at this point in the history
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 libusb#301

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
  • Loading branch information
dickens authored and seanm committed Dec 29, 2017
1 parent 65bd650 commit 4e79922
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions libusb/os/linux_usbfs.c
Expand Up @@ -1299,28 +1299,36 @@ 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))) {
if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
|| 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)
Expand Down
2 changes: 1 addition & 1 deletion libusb/version_nano.h
@@ -1 +1 @@
#define LIBUSB_NANO 11232
#define LIBUSB_NANO 11233

0 comments on commit 4e79922

Please sign in to comment.