grub-devel
[Top][All Lists]
Advanced

[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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]