*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <dirent.h>
#include <sys/stat.h>
+#include "c-ctype.h"
+
#include "daemon.h"
#include "actions.h"
char dev_path[256];
snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
+ /* Ignore the root device. */
+ if (is_root_device (dev_path))
+ continue;
+
/* RHBZ#514505: Some versions of qemu <= 0.10 add a
* CD-ROM device even though we didn't request it. Try to
* detect this by seeing if the device contains media.
if(0 != errno) {
reply_with_perror ("readdir: /sys/block");
free_stringslen(r, size);
+ closedir (dir);
return NULL;
}
return NULL;
}
- /* Sort the devices */
- sort_strings (r, size);
+ /* Sort the devices. Note that r might be NULL if there are no devices. */
+ if (r != NULL)
+ sort_strings (r, size);
/* NULL terminate the list */
if (add_string (&r, &size, &alloc, NULL) == -1) {
if(0 != errno) {
reply_with_perror ("readdir: %s", devdir);
free_stringslen(*r, *size);
+ closedir (dir);
return -1;
}
{
return foreach_block_device(add_partitions);
}
+
+char *
+do_part_to_dev (const char *part)
+{
+ int err = 1;
+ size_t n = strlen (part);
+
+ while (n >= 1 && c_isdigit (part[n-1])) {
+ err = 0;
+ n--;
+ }
+
+ if (err) {
+ reply_with_error ("device name is not a partition");
+ return NULL;
+ }
+
+ char *r = strndup (part, n);
+ if (r == NULL) {
+ reply_with_perror ("strdup");
+ return NULL;
+ }
+
+ return r;
+}
+
+int
+do_part_to_partnum (const char *part)
+{
+ int err = 1;
+ size_t n = strlen (part);
+
+ while (n >= 1 && c_isdigit (part[n-1])) {
+ err = 0;
+ n--;
+ }
+
+ if (err) {
+ reply_with_error ("device name is not a partition");
+ return -1;
+ }
+
+ int r;
+ if (sscanf (&part[n], "%d", &r) != 1) {
+ reply_with_error ("could not parse number");
+ return -1;
+ }
+
+ return r;
+}