[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 3/7] Factor out envblk buffer creation for reuse
From: |
Paul Dagnelie |
Subject: |
[PATCH 3/7] Factor out envblk buffer creation for reuse |
Date: |
Wed, 11 Mar 2020 10:37:11 -0700 |
This patch factors out the filling of the grubenv buffer into a separate
function for reuse.
Signed-off-by: Paul Dagnelie <address@hidden>
---
include/grub/util/install.h | 3 +++
util/editenv.c | 26 +++++++++++++++++---------
2 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index 2631b1074..2ca349503 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -246,6 +246,9 @@ grub_install_get_blocklist (grub_device_t root_dev,
void *data),
void *hook_data);
+void
+grub_util_create_envblk_buffer (char *, size_t);
+
void
grub_util_create_envblk_file (const char *name);
diff --git a/util/editenv.c b/util/editenv.c
index 81f68bd10..45aeba259 100644
--- a/util/editenv.c
+++ b/util/editenv.c
@@ -32,13 +32,29 @@
#define DEFAULT_ENVBLK_SIZE 1024
#define GRUB_ENVBLK_MESSAGE "# WARNING: Do not edit this file by tools
other than "PACKAGE"-editenv!!!\n"
+void
+grub_util_create_envblk_buffer (char *buf, size_t size)
+{
+ if (size < DEFAULT_ENVBLK_SIZE)
+ grub_util_error (_("Envblock buffer too small"));
+ char *pbuf;
+ pbuf = buf;
+ memcpy (pbuf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
+ pbuf += sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
+ memcpy (pbuf, GRUB_ENVBLK_MESSAGE, sizeof (GRUB_ENVBLK_MESSAGE) - 1);
+ pbuf += sizeof (GRUB_ENVBLK_MESSAGE) - 1;
+ memset (pbuf , '#',
+ size - sizeof (GRUB_ENVBLK_SIGNATURE) - sizeof (GRUB_ENVBLK_MESSAGE)
+ 2);
+}
+
void
grub_util_create_envblk_file (const char *name)
{
FILE *fp;
- char *buf, *pbuf, *namenew;
+ char *buf, *namenew;
buf = xmalloc (DEFAULT_ENVBLK_SIZE);
+ grub_util_create_envblk_buffer(buf, DEFAULT_ENVBLK_SIZE);
namenew = xasprintf ("%s.new", name);
fp = grub_util_fopen (namenew, "wb");
@@ -46,14 +62,6 @@ grub_util_create_envblk_file (const char *name)
grub_util_error (_("cannot open `%s': %s"), namenew,
strerror (errno));
- pbuf = buf;
- memcpy (pbuf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
- pbuf += sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
- memcpy (pbuf, GRUB_ENVBLK_MESSAGE, sizeof (GRUB_ENVBLK_MESSAGE) - 1);
- pbuf += sizeof (GRUB_ENVBLK_MESSAGE) - 1;
- memset (pbuf , '#',
- DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) - sizeof
(GRUB_ENVBLK_MESSAGE) + 2);
-
if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
grub_util_error (_("cannot write to `%s': %s"), namenew,
strerror (errno));
--
2.19.0
- [PATCH 0/7] ZFS/other CoW FS save_env support, Paul Dagnelie, 2020/03/11
- [PATCH 1/7] Factor out file filter function for reuse, Paul Dagnelie, 2020/03/11
- [PATCH 2/7] Add support for special envblk functions in GRUB, Paul Dagnelie, 2020/03/11
- [PATCH 3/7] Factor out envblk buffer creation for reuse,
Paul Dagnelie <=
- [PATCH 4/7] Use envblk file and fs functions to implement reading the grubenv file from special FS regions, Paul Dagnelie, 2020/03/11
- [PATCH 5/7] Add ZFS envblock functions, Paul Dagnelie, 2020/03/11
- [PATCH 6/7] Refactor default envblk size out for reuse, Paul Dagnelie, 2020/03/11
- [PATCH 7/7] Update editenv to support editing zfs envblock, fix whitespace, and autodetect bootenv support in libzfs, Paul Dagnelie, 2020/03/11
- Re: [PATCH 0/7] ZFS/other CoW FS save_env support, Daniel Kiper, 2020/03/25