chiark / gitweb /
Introduce udev object cleanup functions
[elogind.git] / src / readahead / readahead-common.c
index a234a89954de129413631212d7ee59a190d713c2..aea1fbeea4ff8fe02e8f226f7b5c5f25ecb65363 100644 (file)
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <unistd.h>
-#include <libudev.h>
 
 #include "log.h"
 #include "readahead-common.h"
 #include "util.h"
 #include "missing.h"
 #include "fileio.h"
+#include "libudev.h"
+#include "udev-util.h"
 
 int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
         assert(fd >= 0);
@@ -60,9 +61,9 @@ int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
 
 int fs_on_ssd(const char *p) {
         struct stat st;
-        struct udev *udev = NULL;
-        struct udev_device *udev_device = NULL, *look_at = NULL;
-        bool b = false;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
+        struct udev_device *look_at = NULL;
         const char *devtype, *rotational, *model, *id;
         int r;
 
@@ -128,7 +129,7 @@ int fs_on_ssd(const char *p) {
 
         udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
         if (!udev_device)
-                goto finish;
+                return false;
 
         devtype = udev_device_get_property_value(udev_device, "DEVTYPE");
         if (devtype && streq(devtype, "partition"))
@@ -137,46 +138,34 @@ int fs_on_ssd(const char *p) {
                 look_at = udev_device;
 
         if (!look_at)
-                goto finish;
+                return false;
 
         /* First, try high-level property */
         id = udev_device_get_property_value(look_at, "ID_SSD");
-        if (id) {
-                b = streq(id, "1");
-                goto finish;
-        }
+        if (id)
+                return streq(id, "1");
 
         /* Second, try kernel attribute */
         rotational = udev_device_get_sysattr_value(look_at, "queue/rotational");
-        if (rotational) {
-                b = streq(rotational, "0");
-                goto finish;
-        }
+        if (rotational)
+                return streq(rotational, "0");
 
         /* Finally, fallback to heuristics */
         look_at = udev_device_get_parent(look_at);
         if (!look_at)
-                goto finish;
+                return false;
 
         model = udev_device_get_sysattr_value(look_at, "model");
         if (model)
-                b = !!strstr(model, "SSD");
-
-finish:
-        if (udev_device)
-                udev_device_unref(udev_device);
-
-        if (udev)
-                udev_unref(udev);
+                return !!strstr(model, "SSD");
 
-        return b;
+        return false;
 }
 
 int fs_on_read_only(const char *p) {
         struct stat st;
-        struct udev *udev = NULL;
-        struct udev_device *udev_device = NULL;
-        bool b = false;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
         const char *read_only;
 
         assert(p);
@@ -187,24 +176,19 @@ int fs_on_read_only(const char *p) {
         if (major(st.st_dev) == 0)
                 return false;
 
-        if (!(udev = udev_new()))
+        udev = udev_new();
+        if (!udev)
                 return -ENOMEM;
 
-        if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
-                goto finish;
-
-        if ((read_only = udev_device_get_sysattr_value(udev_device, "ro")))
-                if ((b = streq(read_only, "1")))
-                        goto finish;
-
-finish:
-        if (udev_device)
-                udev_device_unref(udev_device);
+        udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
+        if (!udev_device)
+                return false;
 
-        if (udev)
-                udev_unref(udev);
+        read_only = udev_device_get_sysattr_value(udev_device, "ro");
+        if (read_only)
+                return streq(read_only, "1");
 
-        return b;
+        return false;
 }
 
 bool enough_ram(void) {