From b53333d25be58ca9d9803423f0ab0ec75079d345 Mon Sep 17 00:00:00 2001 From: Eric Caruso Date: Thu, 26 Sep 2019 11:44:14 -0700 Subject: libbrillo: add UdevDevice::Clone UdevDevice does not allow access to the underlying libudev struct, so give another way to copy UdevDevice objects. BUG=chromium:986484 TEST=build, upcoming UdevWatcher changes Change-Id: Ibe6d0ccd23e663e6e893312d42968701d2044d58 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/1827463 Tested-by: Eric Caruso Commit-Queue: Eric Caruso Reviewed-by: Anand Mistry Cr-Mirrored-From: https://chromium.googlesource.com/chromiumos/platform2 Cr-Mirrored-Commit: 4f80108567894cbcab38b877476be041f77f56a0 --- brillo/udev/mock_udev_device.h | 1 + brillo/udev/udev_device.cc | 4 ++++ brillo/udev/udev_device.h | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/brillo/udev/mock_udev_device.h b/brillo/udev/mock_udev_device.h index 9e0c2ef..6e812d1 100644 --- a/brillo/udev/mock_udev_device.h +++ b/brillo/udev/mock_udev_device.h @@ -57,6 +57,7 @@ class BRILLO_EXPORT MockUdevDevice : public UdevDevice { GetSysAttributeValue, (const char*), (const, override)); + MOCK_METHOD(std::unique_ptr, Clone, (), (override)); private: DISALLOW_COPY_AND_ASSIGN(MockUdevDevice); diff --git a/brillo/udev/udev_device.cc b/brillo/udev/udev_device.cc index b84fa36..2251699 100644 --- a/brillo/udev/udev_device.cc +++ b/brillo/udev/udev_device.cc @@ -121,4 +121,8 @@ const char* UdevDevice::GetSysAttributeValue(const char* attribute) const { return udev_device_get_sysattr_value(device_, attribute); } +std::unique_ptr UdevDevice::Clone() { + return std::make_unique(device_); +} + } // namespace brillo diff --git a/brillo/udev/udev_device.h b/brillo/udev/udev_device.h index 1f942a8..2704a22 100644 --- a/brillo/udev/udev_device.h +++ b/brillo/udev/udev_device.h @@ -95,6 +95,10 @@ class BRILLO_EXPORT UdevDevice { // Wraps udev_device_get_sysattr_value(). virtual const char* GetSysAttributeValue(const char* attribute) const; + // Creates a copy of this UdevDevice pointing to the same underlying + // struct udev_device* (increasing its libudev reference count by 1). + virtual std::unique_ptr Clone(); + private: // Allows MockUdevDevice to invoke the private default constructor below. friend class MockUdevDevice; -- cgit v1.2.3