summaryrefslogtreecommitdiffstats
path: root/libfdisk/src/context.c
diff options
context:
space:
mode:
authorKarel Zak2018-08-30 14:01:46 +0200
committerKarel Zak2018-08-30 14:01:46 +0200
commit745801e44ab01eef759b99372e5792d0eadf19ab (patch)
treef97bf31e5daedbc0bd55784352eab78a9c5a1591 /libfdisk/src/context.c
parentdocs: add note about lsblk usage/free cols (diff)
downloadkernel-qcow2-util-linux-745801e44ab01eef759b99372e5792d0eadf19ab.tar.gz
kernel-qcow2-util-linux-745801e44ab01eef759b99372e5792d0eadf19ab.tar.xz
kernel-qcow2-util-linux-745801e44ab01eef759b99372e5792d0eadf19ab.zip
libfdisk: add fdisk_get_devmodel() and fdisk_get_devno()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src/context.c')
-rw-r--r--libfdisk/src/context.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 779a9a889..ad5f99de4 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -112,6 +112,10 @@ static int init_nested_from_parent(struct fdisk_context *cxt, int isnew)
cxt->protect_bootbits = parent->protect_bootbits;
}
+ free(cxt->dev_model);
+ cxt->dev_model = NULL;
+ cxt->dev_model_probed = 0;
+
free(cxt->dev_path);
cxt->dev_path = NULL;
@@ -541,6 +545,10 @@ static void reset_context(struct fdisk_context *cxt)
free(cxt->dev_path);
cxt->dev_path = NULL;
+ free(cxt->dev_model);
+ cxt->dev_model = NULL;
+ cxt->dev_model_probed = 0;
+
free(cxt->collision);
cxt->collision = NULL;
@@ -1326,6 +1334,48 @@ const char *fdisk_get_devname(struct fdisk_context *cxt)
}
/**
+ * fdisk_get_devno:
+ * @cxt: context
+ *
+ * Returns: device number or zero for non-block devices
+ */
+dev_t fdisk_get_devno(struct fdisk_context *cxt)
+{
+ assert(cxt);
+ return S_ISBLK(cxt->dev_st.st_mode) ? cxt->dev_st.st_rdev : 0;
+}
+
+/**
+ * fdisk_get_devmodel:
+ * @cxt: context
+ *
+ * Returns: device model string or NULL.
+ */
+const char *fdisk_get_devmodel(struct fdisk_context *cxt)
+{
+#ifdef __linux__
+ assert(cxt);
+
+ if (cxt->dev_model_probed)
+ return cxt->dev_model;
+
+ if (fdisk_get_devno(cxt)) {
+ struct path_cxt *pc = ul_new_sysfs_path(fdisk_get_devno(cxt), NULL, NULL);
+
+ if (pc) {
+ ul_path_read_string(pc, &cxt->dev_model, "device/model");
+ ul_unref_path(pc);
+ }
+ }
+ cxt->dev_model_probed = 1;
+ return cxt->dev_model;
+#else
+ return NULL;
+#endif
+
+}
+
+/**
* fdisk_get_devfd:
* @cxt: context
*