[Xen-devel] [XEN PATCH 2/3] read a grubenv file if it is next to the grub.cfg file

YOUNG, MICHAEL A. posted 3 patches 6 years ago
[Xen-devel] [XEN PATCH 2/3] read a grubenv file if it is next to the grub.cfg file
Posted by YOUNG, MICHAEL A. 6 years ago
When a grub.cfg file is found this patch checks if there is grubenv
file in the same directory as the grub.cfg file. If there is it
passes the contents to parse().

Signed-off-by: Michael Young <m.a.young@durham.ac.uk>
---
 tools/pygrub/src/pygrub | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/tools/pygrub/src/pygrub b/tools/pygrub/src/pygrub
index ce7ab0eb8c..53a0803817 100755
--- a/tools/pygrub/src/pygrub
+++ b/tools/pygrub/src/pygrub
@@ -457,10 +457,25 @@ class Grub:
         # limit read size to avoid pathological cases
         buf = f.read(FS_READ_MAX)
         del f
-        if sys.version_info[0] < 3:
-            self.cf.parse(buf)
+        # check for a grubenv file next to the grub.cfg file
+        (fdir, fsep, ffile) = self.cf.filename.rpartition("/")
+        if fdir != "" and ffile == "grub.cfg":
+            fenv = fdir + "/grubenv"
         else:
-            self.cf.parse(buf.decode())
+            fenv = ""
+        if fenv != "" and fs.file_exists(fenv):
+            fenvf = fs.open_file(fenv)
+            grubenv = fenvf.read(FS_READ_MAX)
+            del fenvf
+            if sys.version_info[0] < 3:
+                self.cf.parse(buf, grubenv)
+            else:
+                self.cf.parse(buf.decode(), grubenv.decode())
+        else:
+            if sys.version_info[0] < 3:
+                self.cf.parse(buf)
+            else:
+                self.cf.parse(buf.decode())
 
     def image_index(self):
         if isinstance(self.cf.default, int):
-- 
2.21.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [XEN PATCH 2/3] read a grubenv file if it is next to the grub.cfg file
Posted by Ian Jackson 6 years ago
YOUNG, MICHAEL A. writes ("[XEN PATCH 2/3] read a grubenv file if it is next to the grub.cfg file"):
> When a grub.cfg file is found this patch checks if there is grubenv
> file in the same directory as the grub.cfg file. If there is it
> passes the contents to parse().

I am happy with the semantics of this patch.  But I am not really
happy with the duplication of the code to call self.cf.parse, so that
it is now quadriplicated.

> +        if fenv != "" and fs.file_exists(fenv):
> +            fenvf = fs.open_file(fenv)
> +            grubenv = fenvf.read(FS_READ_MAX)
> +            del fenvf
> +            if sys.version_info[0] < 3:
> +                self.cf.parse(buf, grubenv)
> +            else:
> +                self.cf.parse(buf.decode(), grubenv.decode())
> +        else:
> +            if sys.version_info[0] < 3:
> +                self.cf.parse(buf)
> +            else:
> +                self.cf.parse(buf.decode())

Can you please do something like

   grubenv = None
   if fenv ...
       ...

   if grubenv is not None and sys.version_info[0] < 3:
       grubenv = grubenv.decode()

and then you could at least avoid further multiplications of the call
to .parse...

Thanks,
Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel