------------------------------------------------------------ revno: 3 committer: Andres Salomon timestamp: Tue 2005-11-08 19:24:51 -0500 message: Instead of using block devices from /proc/partitions (which tends to be unreliable), if /sys/block is available (>= linux 2.6.x), use that instead. We're guaranteed that all block devices within this directory are complete devices, not just partitions. === modified file 'libparted/linux.c' --- libparted/linux.c +++ libparted/linux.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -1463,6 +1464,64 @@ } static int +_skip_entry (const char *name) +{ + unsigned int i; + static const char *entries[] = { + ".", + "..", + "dm-", + "loop", + "ram", + }; + + for (i = 0; i < sizeof(entries)/sizeof(*entries); i++) { + if (strncmp (name, entries[i], sizeof (entries[i]) - 1) == 0) + return 1; + } + + return 0; +} + +static int +_probe_sys_block () +{ + DIR *blockdir; + struct dirent *dirent; + char dev_name [256]; + char *ptr; + +#if 0 + struct stat buf; + if ((stat ("/sys/block", &buf) != 0) + || !S_ISDIR (buf.st_mode)) + return 0; +#endif + + if (!(blockdir = opendir ("/sys/block"))) + return 0; + while ((dirent = readdir (blockdir))) { + if (_skip_entry (dirent->d_name)) + continue; + + if (strlen (dirent->d_name) > sizeof (dev_name) - 6) + continue; /* device name too long! */ + + strcpy (dev_name, "/dev/"); + strcat (dev_name, dirent->d_name); + /* in /sys/block, '/'s are replaced with '!' or '.' */ + for (ptr = dev_name; *ptr != '\0'; ptr++) { + if (*ptr == '!' || *ptr == '.') + *ptr = '/'; + } + _ped_device_probe (dev_name); + } + + closedir (blockdir); + return 1; +} + +static int _probe_standard_devices () { _ped_device_probe ("/dev/sda"); @@ -1487,7 +1546,11 @@ static void linux_probe_all () { - _probe_proc_partitions (); + /* /sys/block is more reliable and consistent; fall back to using + * /proc/partitions if the former is unavailable, however. + */ + if (!_probe_sys_block ()) + _probe_proc_partitions (); /* we should probe the standard devs too, even with /proc/partitions, * because /proc/partitions might return devfs stuff, and we might not