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

Konstantin Kharlamov Hi-Angel at yandex.ru
Fri Jan 13 08:51:45 GMT 2023


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>
---
 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 91664ad..a2d94db 100644
--- a/libparted/arch/beos.c
+++ b/libparted/arch/beos.c
@@ -334,7 +334,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 09ec781..1010c70 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -1708,7 +1708,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.0




More information about the parted-devel mailing list