]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add fdisk_device_is_used()
authorKarel Zak <kzak@redhat.com>
Fri, 30 Jun 2017 09:49:12 +0000 (11:49 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 14 Jul 2017 09:34:55 +0000 (11:34 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/docs/libfdisk-sections.txt
libfdisk/src/context.c
libfdisk/src/libfdisk.h.in
libfdisk/src/libfdisk.sym

index 7f9b5e51b81e5897285d5f761f7e4fce6fbedaee..337e0a643fb54e06fc1d89fabe2b991e9edc7e8e 100644 (file)
@@ -287,6 +287,7 @@ fdisk_context
 fdisk_assign_device
 fdisk_deassign_device
 fdisk_reassign_device
+fdisk_device_is_used
 fdisk_enable_bootbits_protection
 fdisk_enable_details
 fdisk_enable_listonly
index e8dfa0e691471b823fe330d74e6fe0fc4d7c316c..1ebc1b981a9927d4668368ebddde362453551a50 100644 (file)
@@ -2,6 +2,8 @@
 # include <blkid.h>
 #endif
 
+#include "blkdev.h"
+#include "loopdev.h"
 #include "fdiskP.h"
 
 
@@ -733,6 +735,38 @@ int fdisk_reread_partition_table(struct fdisk_context *cxt)
 }
 
 
+/**
+ * fdisk_device_is_used:
+ * @cxt: context
+ *
+ * On systems where is no BLKRRPART ioctl the function returns zero and
+ * sets errno to ENOSYS.
+ *
+ * Returns: 1 if the device assigned to the context is used by system, or 0.
+ */
+int fdisk_device_is_used(struct fdisk_context *cxt)
+{
+       int rc = 0;
+
+       assert(cxt);
+       assert(cxt->dev_fd >= 0);
+
+       errno = 0;
+
+#ifdef BLKRRPART
+       /* it seems kernel always return EINVAL for BLKRRPART on loopdevices */
+       if (S_ISBLK(cxt->dev_st.st_mode)
+           && major(cxt->dev_st.st_rdev) != LOOPDEV_MAJOR) {
+               DBG(CXT, ul_debugobj(cxt, "calling re-read ioctl"));
+               rc = ioctl(cxt->dev_fd, BLKRRPART) != 0;
+       }
+#else
+       errno = ENOSYS;
+#endif
+       DBG(CXT, ul_debugobj(cxt, "device used: %s [errno=%d]", rc ? "TRUE" : "FALSE", errno));
+       return rc;
+}
+
 /**
  * fdisk_is_readonly:
  * @cxt: context
index 11b025c324582e0df7d0ffd6d1ae62001a0aa74a..c00e1c072f7ce308956063b5470245cf92630451 100644 (file)
@@ -185,6 +185,7 @@ int fdisk_reassign_device(struct fdisk_context *cxt);
 
 int fdisk_is_readonly(struct fdisk_context *cxt);
 int fdisk_is_regfile(struct fdisk_context *cxt);
+int fdisk_device_is_used(struct fdisk_context *cxt);
 
 int fdisk_enable_details(struct fdisk_context *cxt, int enable);
 int fdisk_is_details(struct fdisk_context *cxt);
index 7063649b8013a0643c2172837f1351581b83d3a5..7b43f320e73530d54e75466fc3f2f46f2ec825d1 100644 (file)
@@ -285,4 +285,5 @@ FDISK_2.30 {
 
 FDISK_2.31 {
        fdisk_reassign_device;
+       fdisk_device_is_used;
 } FDISK_2.30;