summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libfdisk/docs/libfdisk-sections.txt1
-rw-r--r--libfdisk/src/libfdisk.h.in1
-rw-r--r--libfdisk/src/libfdisk.sym4
-rw-r--r--libfdisk/src/script.c76
4 files changed, 69 insertions, 13 deletions
diff --git a/libfdisk/docs/libfdisk-sections.txt b/libfdisk/docs/libfdisk-sections.txt
index f6675fe46..d0d362f60 100644
--- a/libfdisk/docs/libfdisk-sections.txt
+++ b/libfdisk/docs/libfdisk-sections.txt
@@ -132,6 +132,7 @@ fdisk_ref_script
fdisk_script_enable_json
fdisk_script_get_header
fdisk_script_get_nlines
+fdisk_script_set_table
fdisk_script_get_table
fdisk_script_has_force_label
fdisk_script_read_context
diff --git a/libfdisk/src/libfdisk.h.in b/libfdisk/src/libfdisk.h.in
index 47e778a67..69c6fd820 100644
--- a/libfdisk/src/libfdisk.h.in
+++ b/libfdisk/src/libfdisk.h.in
@@ -757,6 +757,7 @@ void fdisk_unref_script(struct fdisk_script *dp);
const char *fdisk_script_get_header(struct fdisk_script *dp, const char *name);
int fdisk_script_set_header(struct fdisk_script *dp, const char *name, const char *data);
struct fdisk_table *fdisk_script_get_table(struct fdisk_script *dp);
+int fdisk_script_set_table(struct fdisk_script *dp, struct fdisk_table *tb);
int fdisk_script_get_nlines(struct fdisk_script *dp);
int fdisk_script_has_force_label(struct fdisk_script *dp);
diff --git a/libfdisk/src/libfdisk.sym b/libfdisk/src/libfdisk.sym
index 8f80d7964..8ce943acc 100644
--- a/libfdisk/src/libfdisk.sym
+++ b/libfdisk/src/libfdisk.sym
@@ -303,3 +303,7 @@ FDISK_2.33 {
fdisk_get_devmodel;
fdisk_get_devno;
} FDISK_2.32;
+
+FDISK_2.35 {
+ fdisk_script_set_table;
+} FDISK_2.33;
diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c
index 66be4f08c..0a165aad4 100644
--- a/libfdisk/src/script.c
+++ b/libfdisk/src/script.c
@@ -7,13 +7,33 @@
/**
* SECTION: script
* @title: Script
- * @short_description: text based sfdisk compatible description of partition table
+ * @short_description: complex way to create and dump partition table
*
- * The libfdisk scripts are based on original sfdisk script (dumps). Each
+ * This interface allows to compose in-memory partition table with all details,
+ * write all partition table description to human readable text file, read it
+ * from the file, and apply the script to on-disk label.
+ *
+ * The libfdisk scripts are based on original sfdisk script (dumps). Each
* script has two parts: script headers and partition table entries
- * (partitions).
+ * (partitions). The script is possible to dump in JSON too (read JSON is not
+ * implemented yet).
*
* For more details about script format see sfdisk man page.
+ *
+ * There are four ways how to build the script:
+ *
+ * - read the current on-disk partition table by fdisk_script_read_context())
+ * - read it from text file by fdisk_script_read_file()
+ * - read it interactively from user by fdisk_script_read_line() and fdisk_script_set_fgets()
+ * - manually in code by fdisk_script_set_header() and fdisk_script_set_table()
+ *
+ * The read functions fdisk_script_read_context() and fdisk_script_read_file()
+ * creates always a new script partition table. The table (see
+ * fdisk_script_get_table()) is possible to modify by standard
+ * fdisk_table_...() functions and than apply by fdisk_apply_script().
+ *
+ * Note that script API is fully non-interactive and forces libfdisk to not use
+ * standard dialog driven partitioning as we have in fdisk(8).
*/
/* script header (e.g. unit: sectors) */
@@ -76,12 +96,6 @@ struct fdisk_script *fdisk_new_script(struct fdisk_context *cxt)
dp->cxt = cxt;
fdisk_ref_context(cxt);
- dp->table = fdisk_new_table();
- if (!dp->table) {
- fdisk_unref_script(dp);
- return NULL;
- }
-
INIT_LIST_HEAD(&dp->headers);
return dp;
}
@@ -317,11 +331,11 @@ int fdisk_script_set_header(struct fdisk_script *dp,
* fdisk_script_get_table:
* @dp: script
*
- * The table (container with partitions) is possible to create by
- * fdisk_script_read_context() or fdisk_script_read_file(), otherwise
- * this function returns NULL.
+ * The table represents partitions holded by the script. The table is possible to
+ * fill by fdisk_script_read_context() or fdisk_script_read_file(). All the "read"
+ * functions reset the table. See also fdisk_script_set_table().
*
- * Returns: NULL or script.
+ * Returns: NULL or script table.
*/
struct fdisk_table *fdisk_script_get_table(struct fdisk_script *dp)
{
@@ -329,6 +343,40 @@ struct fdisk_table *fdisk_script_get_table(struct fdisk_script *dp)
return dp ? dp->table : NULL;
}
+/**
+ * fdisk_script_set_table:
+ * @dp: script
+ * @tb: table
+ *
+ * Replaces table used by script and creates a new reference to @tb. This
+ * function allows to generate a new script table independently on the current
+ * context and without any file reading.
+ *
+ * This is useful for example to create partition table with the same basic
+ * settings (e.g. label-id, ...) but with different partitions -- just call
+ * fdisk_script_read_context() to get current settings and than
+ * fdisk_script_set_table() to set a different layout.
+ *
+ * If @tb is NULL than the current script table is unreferenced.
+ *
+ * Note that script read_ functions (e.g. fdisk_script_read_context()) create
+ * always a new script table.
+ *
+ * Returns: 0 on success, <0 on error
+ */
+int fdisk_script_set_table(struct fdisk_script *dp, struct fdisk_table *tb)
+{
+ if (!dp)
+ return -EINVAL;
+
+ fdisk_ref_table(tb);
+ fdisk_unref_table(dp->table);
+ dp->table = tb;
+
+ DBG(SCRIPT, ul_debugobj(dp, "table replaced"));
+ return 0;
+}
+
static struct fdisk_label *script_get_label(struct fdisk_script *dp)
{
assert(dp);
@@ -911,6 +959,7 @@ static int parse_line_nameval(struct fdisk_script *dp, char *s)
assert(dp);
assert(s);
+ assert(dp->table);
DBG(SCRIPT, ul_debugobj(dp, " parse script line: '%s'", s));
@@ -1106,6 +1155,7 @@ static int parse_line_valcommas(struct fdisk_script *dp, char *s)
assert(dp);
assert(s);
+ assert(dp->table);
pa = fdisk_new_partition();
if (!pa)