libdm: Add a helper function for GetState+DeleteDevice checks.
Bug: N/A
Test: manual test
Change-Id: Ie97b63b53f548af1e82b73893fa491fac42b32ea
diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp
index a4e0d76..e7a3ff2 100644
--- a/fs_mgr/libdm/dm.cpp
+++ b/fs_mgr/libdm/dm.cpp
@@ -79,6 +79,13 @@
return true;
}
+bool DeviceMapper::DeleteDeviceIfExists(const std::string& name) {
+ if (GetState(name) == DmDeviceState::INVALID) {
+ return true;
+ }
+ return DeleteDevice(name);
+}
+
bool DeviceMapper::DeleteDevice(const std::string& name) {
struct dm_ioctl io;
InitIo(&io, name);
diff --git a/fs_mgr/libdm/include/libdm/dm.h b/fs_mgr/libdm/include/libdm/dm.h
index cf306f3..e25ce7f 100644
--- a/fs_mgr/libdm/include/libdm/dm.h
+++ b/fs_mgr/libdm/include/libdm/dm.h
@@ -89,6 +89,7 @@
// Removes a device mapper device with the given name.
// Returns 'true' on success, false otherwise.
bool DeleteDevice(const std::string& name);
+ bool DeleteDeviceIfExists(const std::string& name);
// Fetches and returns the complete state of the underlying device mapper
// device with given name.