Control: tag -1 +patch

Here's a tested patch. Linux lookups on VFAT are not as intelligent as
you'd hope in terms of matching filenames despite case changes.
Instead,  I've added more code to do directory lookups and do
case-insensitive matching by hand. This will now work regardless of
the silliness of any existing filenames, and will default to all-caps
on creation if they don't already exist.

-- 
Steve McIntyre, Cambridge, UK.                                st...@einval.com
Google-bait:       http://www.debian.org/CD/free-linux-cd
  Debian does NOT ship free CDs. Please do NOT contact the mailing
  lists asking us to send them to you.
>From 9774672cc1b9ab3d5b30cdbeaa3cad635ef9393b Mon Sep 17 00:00:00 2001
From: Steve McIntyre <st...@einval.com>
Date: Sat, 20 Dec 2014 01:02:34 +0000
Subject: [PATCH 2/2] V2 of the grub-install-extra-removable patch

Deal with case-insensitive filesystem issues: maybe the directories
and file names we're using don't match what's already there. Check
each path component as we go, and use existing ones if we can.

Fixed #773092
---
 debian/patches/grub-install-extra-removable.patch | 99 ++++++++++++++++++-----
 util/grub-install.c                               | 80 ++++++++++++++++--
 2 files changed, 153 insertions(+), 26 deletions(-)

diff --git a/debian/patches/grub-install-extra-removable.patch b/debian/patches/grub-install-extra-removable.patch
index cbaa47a..2a7c972 100644
--- a/debian/patches/grub-install-extra-removable.patch
+++ b/debian/patches/grub-install-extra-removable.patch
@@ -12,9 +12,9 @@ with new boot paths.
 
 Signed-off-by: Steve McIntyre <93...@debian.org>
 
-Bug-Debian: https://bugs.debian.org/767037
+Bug-Debian: https://bugs.debian.org/767037 https://bugs.debian.org/773092
 Forwarded: Not yet
-Last-Update: 2014-12-03
+Last-Update: 2014-12-20
 
 Patch-Name: grub-install-extra-removable.patch
 ---
@@ -22,7 +22,6 @@ Patch-Name: grub-install-extra-removable.patch
  1 file changed, 44 insertions(+), 2 deletions(-)
 
 diff --git a/util/grub-install.c b/util/grub-install.c
-index 7a7734e..18015da 100644
 --- a/util/grub-install.c
 +++ b/util/grub-install.c
 @@ -56,6 +56,7 @@
@@ -33,7 +32,7 @@ index 7a7734e..18015da 100644
  static int recheck = 0;
  static int update_nvram = 1;
  static char *install_device = NULL;
-@@ -114,7 +115,8 @@ enum
+@@ -114,7 +115,8 @@
      OPTION_LABEL_BGCOLOR,
      OPTION_PRODUCT_VERSION,
      OPTION_UEFI_SECURE_BOOT,
@@ -43,7 +42,7 @@ index 7a7734e..18015da 100644
    };
  
  static int fs_probe = 1;
-@@ -217,6 +219,10 @@ argp_parser (int key, char *arg, struct argp_state *state)
+@@ -217,6 +219,10 @@
        removable = 1;
        return 0;
  
@@ -54,7 +53,7 @@ index 7a7734e..18015da 100644
      case OPTION_ALLOW_FLOPPY:
        allow_floppy = 1;
        return 0;
-@@ -323,6 +329,9 @@ static struct argp_option options[] = {
+@@ -323,6 +329,9 @@
     N_("do not install an image usable with UEFI Secure Boot, even if the "
        "system was currently started using it. "
        "This option is only available on EFI."), 2},
@@ -64,35 +63,99 @@ index 7a7734e..18015da 100644
    {0, 0, 0, 0, 0, 0}
  };
  
-@@ -829,6 +838,27 @@ fill_core_services (const char *core_services)
+@@ -829,6 +838,91 @@
    free (sysv_plist);
  }
  
++/* Helper routine for also_install_removable() below. Walk through the
++   specified dir, looking to see if there is a file/dir that matches
++   the search string exactly, but in a case-insensitive manner. If so,
++   return a copy of the exact file/dir that *does* exist. If not,
++   return NULL */
++static char *
++check_component_exists(const char *dir,
++		       const char *search)
++{
++  grub_util_fd_dir_t d;
++  grub_util_fd_dirent_t de;
++  char *found = NULL;
++
++  d = grub_util_fd_opendir (dir);
++  if (!d)
++    grub_util_error (_("cannot open directory `%s': %s"),
++		     dir, grub_util_fd_strerror ());
++
++  while ((de = grub_util_fd_readdir (d)))
++    {
++      if (strcasecmp (de->d_name, search) == 0)
++	{
++	  found = xstrdup (de->d_name);
++	  break;
++	}
++    }
++  grub_util_fd_closedir (d);
++  return found;
++}
++
++/* Some complex directory-handling stuff in here, to cope with
++ * case-insensitive FAT/VFAT filesystem semantics. Ugh. */
 +static void
-+also_install_removable(const char *src, const char *base_efidir, const char *efi_suffix_upper)
++also_install_removable(const char *src,
++		       const char *base_efidir,
++		       const char *efi_suffix_upper)
 +{
 +  char *efi_file = NULL;
 +  char *dst = NULL;
-+  char *dir = NULL;
++  char *cur = NULL;
++  char *found = NULL;
 +
 +  if (!efi_suffix_upper)
 +    grub_util_error ("%s", _("efi_suffix_upper not set"));
 +  efi_file = xasprintf ("BOOT%s.EFI", efi_suffix_upper);
 +
-+  dir = grub_util_path_concat (3, base_efidir, "EFI", "BOOT");
-+  grub_install_mkdir_p (dir);
++  /* We need to install in $base_efidir/EFI/BOOT/$efi_file, but we
++   * need to cope with case-insensitive stuff here. Build the path one
++   * component at a time, checking for existing matches each time. */
 +
-+  dst = grub_util_path_concat (2, dir, efi_file);
-+  grub_install_copy_file (src, dst, 1);
++  /* Look for "EFI" in base_efidir. Make it if it does not exist in
++   * some form. */
++  found = check_component_exists(base_efidir, "EFI");
++  if (found == NULL)
++    found = xstrdup("EFI");
++  dst = grub_util_path_concat (2, base_efidir, found);
++  cur = xstrdup (dst);
 +  free (dst);
++  free (found);
++  grub_install_mkdir_p (cur);
++
++  /* Now BOOT */
++  found = check_component_exists(cur, "BOOT");
++  if (found == NULL)
++    found = xstrdup("BOOT");
++  dst = grub_util_path_concat (2, cur, found);
++  cur = xstrdup (dst);
++  free (dst);
++  free (found);
++  grub_install_mkdir_p (cur);
++
++  /* Now $efi_file */
++  found = check_component_exists(cur, efi_file);
++  if (found == NULL)
++    found = xstrdup(efi_file);
++  dst = grub_util_path_concat (2, cur, found);
++  cur = xstrdup (dst);
++  free (dst);
++  free (found);
++  grub_install_copy_file (src, cur, 1);
++
++  free (cur);
 +  free (efi_file);
-+  free (dir);
 +}
 +
  int
  main (int argc, char *argv[])
  {
-@@ -846,6 +876,7 @@ main (int argc, char *argv[])
+@@ -846,6 +940,7 @@
    char *relative_grubdir;
    char **efidir_device_names = NULL;
    grub_device_t efidir_grub_dev = NULL;
@@ -100,7 +163,7 @@ index 7a7734e..18015da 100644
    char *efidir_grub_devname;
    int efidir_is_mac = 0;
    int is_prep = 0;
-@@ -878,6 +909,9 @@ main (int argc, char *argv[])
+@@ -878,6 +973,9 @@
        bootloader_id = xstrdup ("grub");
      }
  
@@ -110,7 +173,7 @@ index 7a7734e..18015da 100644
    if (!grub_install_source_directory)
      {
        if (!target)
-@@ -1087,6 +1121,8 @@ main (int argc, char *argv[])
+@@ -1087,6 +1185,8 @@
        if (!efidir_is_mac && grub_strcmp (fs->name, "fat") != 0)
  	grub_util_error (_("%s doesn't look like an EFI partition.\n"), efidir);
  
@@ -119,7 +182,7 @@ index 7a7734e..18015da 100644
        /* The EFI specification requires that an EFI System Partition must
  	 contain an "EFI" subdirectory, and that OS loaders are stored in
  	 subdirectories below EFI.  Vendors are expected to pick names that do
-@@ -1949,9 +1985,15 @@ main (int argc, char *argv[])
+@@ -1949,9 +2049,15 @@
  	    fprintf (config_dst_f, "configfile $prefix/grub.cfg\n");
  	    fclose (config_dst_f);
  	    free (config_dst);
diff --git a/util/grub-install.c b/util/grub-install.c
index 18015da..388b26d 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -838,25 +838,89 @@ fill_core_services (const char *core_services)
   free (sysv_plist);
 }
 
+/* Helper routine for also_install_removable() below. Walk through the
+   specified dir, looking to see if there is a file/dir that matches
+   the search string exactly, but in a case-insensitive manner. If so,
+   return a copy of the exact file/dir that *does* exist. If not,
+   return NULL */
+static char *
+check_component_exists(const char *dir,
+		       const char *search)
+{
+  grub_util_fd_dir_t d;
+  grub_util_fd_dirent_t de;
+  char *found = NULL;
+
+  d = grub_util_fd_opendir (dir);
+  if (!d)
+    grub_util_error (_("cannot open directory `%s': %s"),
+		     dir, grub_util_fd_strerror ());
+
+  while ((de = grub_util_fd_readdir (d)))
+    {
+      if (strcasecmp (de->d_name, search) == 0)
+	{
+	  found = xstrdup (de->d_name);
+	  break;
+	}
+    }
+  grub_util_fd_closedir (d);
+  return found;
+}
+        
+/* Some complex directory-handling stuff in here, to cope with
+ * case-insensitive FAT/VFAT filesystem semantics. Ugh. */
 static void
-also_install_removable(const char *src, const char *base_efidir, const char *efi_suffix_upper)
+also_install_removable(const char *src,
+		       const char *base_efidir,
+		       const char *efi_suffix_upper)
 {
   char *efi_file = NULL;
   char *dst = NULL;
-  char *dir = NULL;
+  char *cur = NULL;
+  char *found = NULL;
 
   if (!efi_suffix_upper)
     grub_util_error ("%s", _("efi_suffix_upper not set"));
   efi_file = xasprintf ("BOOT%s.EFI", efi_suffix_upper);
 
-  dir = grub_util_path_concat (3, base_efidir, "EFI", "BOOT");
-  grub_install_mkdir_p (dir);
-
-  dst = grub_util_path_concat (2, dir, efi_file);
-  grub_install_copy_file (src, dst, 1);
+  /* We need to install in $base_efidir/EFI/BOOT/$efi_file, but we
+   * need to cope with case-insensitive stuff here. Build the path one
+   * component at a time, checking for existing matches each time. */
+
+  /* Look for "EFI" in base_efidir. Make it if it does not exist in
+   * some form. */
+  found = check_component_exists(base_efidir, "EFI");
+  if (found == NULL)
+    found = xstrdup("EFI");
+  dst = grub_util_path_concat (2, base_efidir, found);
+  cur = xstrdup (dst);
   free (dst);
+  free (found);
+  grub_install_mkdir_p (cur);
+  
+  /* Now BOOT */
+  found = check_component_exists(cur, "BOOT");
+  if (found == NULL)
+    found = xstrdup("BOOT");
+  dst = grub_util_path_concat (2, cur, found);
+  cur = xstrdup (dst);
+  free (dst);
+  free (found);
+  grub_install_mkdir_p (cur);
+
+  /* Now $efi_file */
+  found = check_component_exists(cur, efi_file);
+  if (found == NULL)
+    found = xstrdup(efi_file);
+  dst = grub_util_path_concat (2, cur, found);
+  cur = xstrdup (dst);
+  free (dst);
+  free (found);
+  grub_install_copy_file (src, cur, 1);
+
+  free (cur);
   free (efi_file);
-  free (dir);
 }
 
 int
-- 
2.1.3

Reply via email to