From 0fb2ef3aaecbde97459ec6d94d73d22718e62aae Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Thu, 4 Nov 2021 14:45:31 +0000 Subject: [PATCH] trivial: Add fu_udev_device_seek() for future code --- libfwupdplugin/fu-udev-device.c | 51 +++++++++++++++++++++++++++++++++ libfwupdplugin/fu-udev-device.h | 2 ++ libfwupdplugin/fwupdplugin.map | 1 + 3 files changed, 54 insertions(+) diff --git a/libfwupdplugin/fu-udev-device.c b/libfwupdplugin/fu-udev-device.c index 16b591c38..a0c891e8e 100644 --- a/libfwupdplugin/fu-udev-device.c +++ b/libfwupdplugin/fu-udev-device.c @@ -1447,6 +1447,57 @@ fu_udev_device_pread_full(FuUdevDevice *self, #endif } +/** + * fu_udev_device_seek: + * @self: a #FuUdevDevice + * @offset: offset address + * @error: (nullable): optional return location for an error + * + * Seeks a file descriptor to a given offset. + * + * Returns: %TRUE for success + * + * Since: 1.7.2 + **/ +gboolean +fu_udev_device_seek(FuUdevDevice *self, goffset offset, GError **error) +{ + FuUdevDevicePrivate *priv = GET_PRIVATE(self); + + g_return_val_if_fail(FU_IS_UDEV_DEVICE(self), FALSE); + g_return_val_if_fail(error == NULL || *error == NULL, FALSE); + + /* not open! */ + if (priv->fd == 0) { + g_set_error(error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "%s [%s] has not been opened", + fu_device_get_id(FU_DEVICE(self)), + fu_device_get_name(FU_DEVICE(self))); + return FALSE; + } + +#ifdef HAVE_PWRITE + if (lseek(priv->fd, offset, SEEK_SET) < 0) { + g_set_error(error, + G_IO_ERROR, + G_IO_ERROR_FAILED, + "failed to seek to 0x%04x: %s", + (guint)offset, + strerror(errno)); + return FALSE; + } + return TRUE; +#else + g_set_error_literal(error, + FWUPD_ERROR, + FWUPD_ERROR_NOT_SUPPORTED, + "Not supported as lseek() is unavailable"); + return FALSE; +#endif +} + /** * fu_udev_device_pwrite_full: * @self: a #FuUdevDevice diff --git a/libfwupdplugin/fu-udev-device.h b/libfwupdplugin/fu-udev-device.h index 106a97cb5..d930674b9 100644 --- a/libfwupdplugin/fu-udev-device.h +++ b/libfwupdplugin/fu-udev-device.h @@ -113,6 +113,8 @@ fu_udev_device_pread_full(FuUdevDevice *self, guint8 *buf, gsize bufsz, GError **error) G_GNUC_WARN_UNUSED_RESULT; +gboolean +fu_udev_device_seek(FuUdevDevice *self, goffset offset, GError **error) G_GNUC_WARN_UNUSED_RESULT; const gchar * fu_udev_device_get_sysfs_attr(FuUdevDevice *self, const gchar *attr, GError **error); gboolean diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 543a81736..8ee52fd80 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -950,5 +950,6 @@ LIBFWUPDPLUGIN_1.7.2 { global: fu_context_has_hwid_flag; fu_udev_device_get_sysfs_attr_uint64; + fu_udev_device_seek; local: *; } LIBFWUPDPLUGIN_1.7.1;