--- a/src/sysfs.c Sat Apr 26 23:59:30 2008 +0200
+++ b/src/sysfs.c Sun Apr 27 00:57:37 2008 +0200
@@ -38,12 +38,46 @@
#include <limits.h>
#include <linux/types.h>
#include <linux/input.h>
+#include <regex.h>
#include "trace.h"
#include "device.h"
#include "sysfs.h"
+
+
+/*
+ * Small helper around regcomp/regexec to see if the string is match by the regexp
+ *
+ * @param string: search is done in this buffer
+ * @param regex_string: POSIX regular expression
+ *
+ * @return TRUE if the regex match, else FALSE
+ */
+static gboolean
+string_match_regexp(const char *string, const char *regex_string)
+{
+ regex_t r;
+ regmatch_t rm[0];
+ int status;
+
+ if (string == NULL)
+ return FALSE;
+
+ status = regcomp(&r, regex_string, REG_EXTENDED);
+ if (status)
+ return FALSE;
+
+ status = regexec(&r, string, sizeof(rm)/sizeof(regmatch_t), rm, 0);
+
+ regfree(&r);
+ if (status == REG_NOMATCH)
+ return FALSE;
+ else
+ return TRUE;
+}
+
/*
* Return the content of a file in the sysfs device path
@@ -142,7 +176,35 @@ sysfs_get_bus_type(const char *sysfs_pat
return bus;
}
-
+/*
+ * from a sysfs_path return TRUE if the device is connected physically on the
+ * usb bus
+ *
+ * @param sysfs_path a full path like /sys/block/sda
+ */
+static int
+sysfs_device_is_an_usb_device(const char *sysfs_path)
+{
+ char *sys_device_link, *device_filename;
+ gboolean connected_to_usb_device = FALSE;
+
+ device_filename = g_strdup_printf("%s/device", sysfs_path);
+ sys_device_link = g_file_read_link(device_filename, NULL);
+ if (sys_device_link == NULL)
+ {
+ error("%s not found or readable\n", device_filename);
+ g_free(device_filename);
+ return connected_to_usb_device;
+ }
+ g_free(device_filename);
+
+ /* ok i known it a bit strange ... but it works (tm) */
+ if (string_match_regexp(sys_device_link, "/usb\\d+/\\d+-\\d+/"))
+ connected_to_usb_device = TRUE;
+
+ g_free(sys_device_link);
+ return connected_to_usb_device;
+}
/*
* Take a sysfs_path like (/sys/block/sda) and return the /dev/xxx to use the
@@ -492,6 +554,11 @@ sysfs_coldplug_scan_block_devices(void)
if (sysfs_get_block_device_type(sysfs_path, &device_type, &bus_type) == FALSE)
continue;
+ /* Note: SATA drive is connected on the scsi bus, and we don't want to
+ * mark it as removable. So filter only usb device */
+ if (sysfs_device_is_an_usb_device(sysfs_path) == FALSE)
+ continue;
+
/* Only keep removable device, some scsi device doesn't see has a
* removable device (my LACIE usb disk) */
if (bus_type == DEVICE_BUS_SCSI || sysfs_is_removable(sysfs_path))