]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add fdisk_reassign_device()
authorKarel Zak <kzak@redhat.com>
Mon, 10 Jul 2017 10:32:26 +0000 (12:32 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 10 Jul 2017 10:32:26 +0000 (12:32 +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
libfdisk/src/script.c

index ad8f148861fc8de3595defb484f44295e82d33a0..767089a71cc7a968ae76bac58448cf28e1561402 100644 (file)
@@ -287,6 +287,7 @@ fdisk_unref_table
 fdisk_context
 fdisk_assign_device
 fdisk_deassign_device
+fdisk_reassign_device
 fdisk_enable_bootbits_protection
 fdisk_enable_details
 fdisk_enable_listonly
index 951310ffae2a233b716cc5b57cd92e48fa98d9f4..666ebbba058bc0463480609b7cf4b0ffdefc4acc 100644 (file)
@@ -632,6 +632,8 @@ int fdisk_deassign_device(struct fdisk_context *cxt, int nosync)
                return rc;
        }
 
+       DBG(CXT, ul_debugobj(cxt, "de-assigning device %s", cxt->dev_path));
+
        if (cxt->readonly)
                close(cxt->dev_fd);
        else {
@@ -655,6 +657,40 @@ int fdisk_deassign_device(struct fdisk_context *cxt, int nosync)
        return 0;
 }
 
+/**
+ * fdisk_reassign_device:
+ * @cxt: context
+ *
+ * This function is "hard reset" of the context and it does not write anything
+ * to the device. All in-memory changes associated with the context will be
+ * lost. It's recommended to use this function after some fatal problem when the
+ * context (and label specific driver) is in an undefined state.
+ *
+ * Returns: 0 on success, < 0 on error.
+ */
+int fdisk_reassign_device(struct fdisk_context *cxt)
+{
+       char *devname;
+       int rdonly, rc;
+
+       assert(cxt);
+       assert(cxt->dev_fd >= 0);
+
+       DBG(CXT, ul_debugobj(cxt, "re-assigning device %s", cxt->dev_path));
+
+       devname = strdup(cxt->dev_path);
+       if (!devname)
+               return -ENOMEM;
+
+       rdonly = cxt->readonly;
+
+       fdisk_deassign_device(cxt, 1);
+       rc = fdisk_assign_device(cxt, devname, rdonly);
+       free(devname);
+
+       return rc;
+}
+
 /**
  * fdisk_is_readonly:
  * @cxt: context
index 26dfe58c0efbe74e66ae73ce51c17f75b7bd4f73..11b025c324582e0df7d0ffd6d1ae62001a0aa74a 100644 (file)
@@ -181,6 +181,8 @@ int fdisk_is_labeltype(struct fdisk_context *cxt, enum fdisk_labeltype id);
 int fdisk_assign_device(struct fdisk_context *cxt,
                        const char *fname, int readonly);
 int fdisk_deassign_device(struct fdisk_context *cxt, int nosync);
+int fdisk_reassign_device(struct fdisk_context *cxt);
+
 int fdisk_is_readonly(struct fdisk_context *cxt);
 int fdisk_is_regfile(struct fdisk_context *cxt);
 
index c26abf0d0d7c28e5a4163a58dd8a8bad449605a1..7063649b8013a0643c2172837f1351581b83d3a5 100644 (file)
@@ -282,3 +282,7 @@ FDISK_2.30 {
        fdisk_is_ptcollision;
        fdisk_partition_has_wipe;
 } FDISK_2.29;
+
+FDISK_2.31 {
+       fdisk_reassign_device;
+} FDISK_2.30;
index be325bc09b7a93cd4267ac3c659710aac84b947a..b0ac8b2458cb0fecfa724eac6807e3109401d743 100644 (file)
@@ -1373,6 +1373,7 @@ struct fdisk_script *fdisk_get_script(struct fdisk_context *cxt)
  * @dp: script
  *
  * Associate context @cxt with script @dp and creates a new empty disklabel.
+ * The script may be later unreference by fdisk_set_script() with NULL as script.
  *
  * Returns: 0 on success, <0 on error.
  */