chiark / gitweb /
gpt-auto-generator: use _cleanup_ for blkid_free_probe
[elogind.git] / src / gpt-auto-generator / gpt-auto-generator.c
index 81d692c51368c034ff14ad51932c1fbfcffbbd4b..222d6a0da7959ad7fdd54a0b7a70ada87cb7d859 100644 (file)
 #include <unistd.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <fcntl.h>
-#include <linux/btrfs.h>
 #include <sys/ioctl.h>
 #include <sys/statfs.h>
 #include <blkid.h>
 
 #include <sys/ioctl.h>
 #include <sys/statfs.h>
 #include <blkid.h>
 
+#ifdef HAVE_LINUX_BTRFS_H
+#include <linux/btrfs.h>
+#endif
+
 #include "path-util.h"
 #include "util.h"
 #include "mkdir.h"
 #include "path-util.h"
 #include "util.h"
 #include "mkdir.h"
 
 static const char *arg_dest = "/tmp";
 
 
 static const char *arg_dest = "/tmp";
 
+static inline void blkid_free_probep(blkid_probe *b) {
+        if (*b)
+                blkid_free_probe(*b);
+}
+#define _cleanup_blkid_freep_probe_ _cleanup_(blkid_free_probep)
+
 static int verify_gpt_partition(dev_t dev, sd_id128_t *type, unsigned *nr, char **fstype) {
         _cleanup_free_ char *t = NULL;
 static int verify_gpt_partition(dev_t dev, sd_id128_t *type, unsigned *nr, char **fstype) {
         _cleanup_free_ char *t = NULL;
-        blkid_probe b = NULL;
+        _cleanup_blkid_freep_probe_ blkid_probe b = NULL;
         const char *v;
         int r;
 
         const char *v;
         int r;
 
@@ -58,12 +67,8 @@ static int verify_gpt_partition(dev_t dev, sd_id128_t *type, unsigned *nr, char
 
         errno = 0;
         b = blkid_new_probe_from_filename(t);
 
         errno = 0;
         b = blkid_new_probe_from_filename(t);
-        if (!b) {
-                if (errno != 0)
-                        return -errno;
-
-                return -ENOMEM;
-        }
+        if (!b)
+                return errno != 0 ? -errno : -ENOMEM;
 
         blkid_probe_enable_superblocks(b, 1);
         blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
 
         blkid_probe_enable_superblocks(b, 1);
         blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
@@ -72,36 +77,26 @@ static int verify_gpt_partition(dev_t dev, sd_id128_t *type, unsigned *nr, char
 
         errno = 0;
         r = blkid_do_safeprobe(b);
 
         errno = 0;
         r = blkid_do_safeprobe(b);
-        if (r == -2) {
-                r = -ENODEV;
-                goto finish;
-        } else if (r == 1) {
-                r = -ENODEV;
-                goto finish;
-        } else if (r != 0) {
-                r = errno ? -errno : -EIO;
-                goto finish;
-        }
+        if (r == -2)
+                return -ENODEV;
+        else if (r == 1)
+                return -ENODEV;
+        else if (r != 0)
+                return errno ? -errno : -EIO;
 
         errno = 0;
         r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
 
         errno = 0;
         r = blkid_probe_lookup_value(b, "PART_ENTRY_SCHEME", &v, NULL);
-        if (r != 0) {
-                r = errno ? -errno : -EIO;
-                goto finish;
-        }
+        if (r != 0)
+                return errno ? -errno : -EIO;
 
 
-        if (strcmp(v, "gpt") != 0) {
-                r = 0;
-                goto finish;
-        }
+        if (strcmp(v, "gpt") != 0)
+                return 0;
 
         if (type) {
                 errno = 0;
                 r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
 
         if (type) {
                 errno = 0;
                 r = blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, NULL);
-                if (r != 0) {
-                        r = errno ? -errno : -EIO;
-                        goto finish;
-                }
+                if (r != 0)
+                        return errno ? -errno : -EIO;
 
                 r = sd_id128_from_string(v, type);
                 if (r < 0)
 
                 r = sd_id128_from_string(v, type);
                 if (r < 0)
@@ -111,10 +106,8 @@ static int verify_gpt_partition(dev_t dev, sd_id128_t *type, unsigned *nr, char
         if (nr) {
                 errno = 0;
                 r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
         if (nr) {
                 errno = 0;
                 r = blkid_probe_lookup_value(b, "PART_ENTRY_NUMBER", &v, NULL);
-                if (r != 0) {
-                        r = errno ? -errno : -EIO;
-                        goto finish;
-                }
+                if (r != 0)
+                        return errno ? -errno : -EIO;
 
                 r = safe_atou(v, nr);
                 if (r < 0)
 
                 r = safe_atou(v, nr);
                 if (r < 0)
@@ -131,22 +124,14 @@ static int verify_gpt_partition(dev_t dev, sd_id128_t *type, unsigned *nr, char
                         *fstype = NULL;
                 else {
                         fst = strdup(v);
                         *fstype = NULL;
                 else {
                         fst = strdup(v);
-                        if (!fst) {
-                                r = -ENOMEM;
-                                goto finish;
-                        }
+                        if (!fst)
+                                return -ENOMEM;
 
                         *fstype = fst;
                 }
         }
 
         return 1;
 
                         *fstype = fst;
                 }
         }
 
         return 1;
-
-finish:
-        if (b)
-                blkid_free_probe(b);
-
-        return r;
 }
 
 static int add_swap(const char *path, const char *fstype) {
 }
 
 static int add_swap(const char *path, const char *fstype) {
@@ -296,7 +281,8 @@ static int enumerate_partitions(dev_t dev) {
 
         r = udev_enumerate_scan_devices(e);
         if (r < 0) {
 
         r = udev_enumerate_scan_devices(e);
         if (r < 0) {
-                log_error("Failed to enumerate partitions: %s", strerror(-r));
+                log_error("Failed to enumerate partitions on /dev/block/%u:%u: %s",
+                          major(dev), minor(dev), strerror(-r));
                 goto finish;
         }
 
                 goto finish;
         }
 
@@ -307,6 +293,7 @@ static int enumerate_partitions(dev_t dev) {
                 struct udev_device *q;
                 sd_id128_t type_id;
                 unsigned nr;
                 struct udev_device *q;
                 sd_id128_t type_id;
                 unsigned nr;
+                dev_t sub;
 
                 q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
                 if (!q) {
 
                 q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
                 if (!q) {
@@ -326,9 +313,12 @@ static int enumerate_partitions(dev_t dev) {
                         goto finish;
                 }
 
                         goto finish;
                 }
 
-                r = verify_gpt_partition(udev_device_get_devnum(q), &type_id, &nr, &fstype);
+                sub = udev_device_get_devnum(q);
+
+                r = verify_gpt_partition(sub, &type_id, &nr, &fstype);
                 if (r < 0) {
                 if (r < 0) {
-                        log_error("Failed to verify GPT partition: %s", strerror(-r));
+                        log_error("Failed to verify GPT partition /dev/block/%u:%u: %s",
+                                  major(sub), minor(sub), strerror(-r));
                         udev_device_unref(q);
                         goto finish;
                 }
                         udev_device_unref(q);
                         goto finish;
                 }
@@ -480,11 +470,12 @@ int main(int argc, char *argv[]) {
                 return EXIT_SUCCESS;
         }
 
                 return EXIT_SUCCESS;
         }
 
-        log_debug("Root device %u:%u.", major(dev), minor(dev));
+        log_debug("Root device /dev/block/%u:%u.", major(dev), minor(dev));
 
         r = verify_gpt_partition(dev, NULL, NULL, NULL);
         if (r < 0) {
 
         r = verify_gpt_partition(dev, NULL, NULL, NULL);
         if (r < 0) {
-                log_error("Failed to verify GPT partition: %s", strerror(-r));
+                log_error("Failed to verify GPT partition /dev/block/%u:%u: %s",
+                          major(dev), minor(dev), strerror(-r));
                 return EXIT_FAILURE;
         }
         if (r == 0)
                 return EXIT_FAILURE;
         }
         if (r == 0)