[parted-devel] [PATCH 1/2] arch: allow to open devices in read-only mode

Brian C. Lane bcl at redhat.com
Sat Feb 4 00:20:07 GMT 2023


From: Konstantin Kharlamov <Hi-Angel at yandex.ru>

Before this patch API like `ped_disk_new()` were unconditionally opening
the disk in O_RDWR mode. Now it may be opened in readonly mode by
setting `read_only = 1` field of `PedDevice` before calling the
function.

This allows to avoid kernel/udev events like "remove/add" and "change"
in cases, where users are only interested in read-only functional, like
reading a partitions list.

Signed-off-by: Konstantin Kharlamov <Hi-Angel at yandex.ru>
Signed-off-by: Brian C. Lane <bcl at redhat.com>
---
 libparted/arch/beos.c  | 2 +-
 libparted/arch/linux.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/libparted/arch/beos.c b/libparted/arch/beos.c
index 1a762fe..497eb9c 100644
--- a/libparted/arch/beos.c
+++ b/libparted/arch/beos.c
@@ -335,7 +335,7 @@ beos_open (PedDevice* dev)
 	BEOSSpecific* arch_specific = BEOS_SPECIFIC(dev);
 
 retry:
-	arch_specific->fd = open(dev->path, O_RDWR);
+	arch_specific->fd = open(dev->path, dev->read_only ? RD_MODE : RW_MODE);
 	if (arch_specific->fd == -1) {
 		char* rw_error_msg = strerror(errno);
 
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index f3bf14d..e90d5b3 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -1709,7 +1709,7 @@ _device_open_ro (PedDevice* dev)
 static int
 linux_open (PedDevice* dev)
 {
-    return _device_open (dev, RW_MODE);
+    return _device_open (dev, dev->read_only ? RD_MODE : RW_MODE);
 }
 
 static int
-- 
2.39.1




More information about the parted-devel mailing list