]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
Split make_system_path_relative_to_its_root into separate file
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 19 Oct 2013 14:21:08 +0000 (16:21 +0200)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 19 Oct 2013 14:21:08 +0000 (16:21 +0200)
relpath.c from getroot.c as it's common between unix and haiku
but otherwise haiku doesn't use any functions from unix getroot.c.

ChangeLog
Makefile.util.def
grub-core/osdep/aros/getroot.c
grub-core/osdep/aros/relpath.c [new file with mode: 0644]
grub-core/osdep/relpath.c [new file with mode: 0644]
grub-core/osdep/unix/getroot.c
grub-core/osdep/unix/relpath.c [new file with mode: 0644]
grub-core/osdep/windows/getroot.c
grub-core/osdep/windows/relpath.c [new file with mode: 0644]

index 80f9a8669f60b7b03b1d53508a3b83d35c2df848..57f33c912c19f8164381a9f64de7a80e1d4e1cb5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2013-10-19  Vladimir Serbinenko  <phcoder@gmail.com>
+
+       Split make_system_path_relative_to_its_root into separate file
+       relpath.c from getroot.c as it's common between unix and haiku
+       but otherwise haiku doesn't use any functions from unix getroot.c.
+
 2013-10-19  Vladimir Serbinenko  <phcoder@gmail.com>
 
        * grub-core/osdep/aros/hostdisk.c (grub_util_is_directory):
index 225b82d1346c2a7d95b835455b852bc3ec72be29..efb0d8786add426104d53364e37d98cf7675f9b1 100644 (file)
@@ -13,6 +13,10 @@ library = {
   common = grub-core/osdep/unix/getroot.c;
   common = grub-core/osdep/getroot.c;
   common = grub-core/osdep/devmapper/getroot.c;
+  common = grub-core/osdep/relpath.c;
+  extra_dist = grub-core/osdep/unix/relpath.c;
+  extra_dist = grub-core/osdep/aros/relpath.c;
+  extra_dist = grub-core/osdep/windows/relpath.c;
   common = grub-core/kern/emu/hostdisk.c;
   common = grub-core/osdep/devmapper/hostdisk.c;
   common = grub-core/osdep/hostdisk.c;
index f1d66c1c3d9afc80ddc9e7b3f9c335d123acad42..a27df9eb9a53c1b90912b8932e4dcd5112cc88ee 100644 (file)
@@ -130,45 +130,6 @@ grub_util_find_partition_start_os (const char *dev)
          * (grub_uint64_t) envec->de_SizeBlock) >> 7;
 }
 
-char *
-grub_make_system_path_relative_to_its_root (const char *path)
-{
-  char *p;
-  unsigned char *tmp;
-  char *ret;
-  BPTR lck;
-
-  if (path[0] == '/' && path[1] == '/' && path[2] == ':')
-    return xstrdup (path);
-
-  tmp = xmalloc (2048);
-
-  lck = Lock ((const unsigned char *) path, SHARED_LOCK);
-  if (!lck || !NameFromLock (lck, tmp, 2040))
-    {
-      free (tmp);
-      tmp = (unsigned char *) xstrdup (path);
-    }
-  if (lck)
-    UnLock (lck);
-  p = strchr ((char *) tmp, ':');
-  if (!p)
-    return (char *) tmp;
-  if (p[1] == '/' || p[1] == '\0')
-    {
-      ret = xstrdup (p + 1);
-    }
-  else
-    {
-      ret = xmalloc (strlen (p + 1) + 2);
-      ret[0] = '/';
-      strcpy (ret + 1, p + 1);
-    }
-
-  free (tmp);
-  return ret;
-}
-
 char **
 grub_guess_root_devices (const char *path)
 {
diff --git a/grub-core/osdep/aros/relpath.c b/grub-core/osdep/aros/relpath.c
new file mode 100644 (file)
index 0000000..9c2328d
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2013  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config-util.h>
+#include <config.h>
+
+#include <grub/util/misc.h>
+
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/emu/misc.h>
+#include <grub/emu/hostdisk.h>
+#include <grub/emu/getroot.h>
+
+#include <string.h>
+#include <dos/dos.h>
+#include <dos/filesystem.h>
+#include <dos/exall.h>
+#include <proto/dos.h>
+#include <proto/exec.h>
+#include <devices/trackdisk.h>
+
+char *
+grub_make_system_path_relative_to_its_root (const char *path)
+{
+  char *p;
+  unsigned char *tmp;
+  char *ret;
+  BPTR lck;
+
+  if (path[0] == '/' && path[1] == '/' && path[2] == ':')
+    return xstrdup (path);
+
+  tmp = xmalloc (2048);
+
+  lck = Lock ((const unsigned char *) path, SHARED_LOCK);
+  if (!lck || !NameFromLock (lck, tmp, 2040))
+    {
+      free (tmp);
+      tmp = (unsigned char *) xstrdup (path);
+    }
+  if (lck)
+    UnLock (lck);
+  p = strchr ((char *) tmp, ':');
+  if (!p)
+    return (char *) tmp;
+  if (p[1] == '/' || p[1] == '\0')
+    {
+      ret = xstrdup (p + 1);
+    }
+  else
+    {
+      ret = xmalloc (strlen (p + 1) + 2);
+      ret[0] = '/';
+      strcpy (ret + 1, p + 1);
+    }
+
+  free (tmp);
+  return ret;
+}
diff --git a/grub-core/osdep/relpath.c b/grub-core/osdep/relpath.c
new file mode 100644 (file)
index 0000000..4e10d8a
--- /dev/null
@@ -0,0 +1,7 @@
+#if defined (__MINGW32__) || defined (__CYGWIN__)
+#include "windows/relpath.c"
+#elif defined (__AROS__)
+#include "aros/relpath.c"
+#else
+#include "unix/relpath.c"
+#endif
index 2552e536e26d0f81cc716d147b67bebcabad93e6..410d58ddd214b8e2565657b4f45c56fdc4cc4f8f 100644 (file)
@@ -710,142 +710,6 @@ grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs)
     *poolfs = xstrdup ("");
 }
 
-/* This function never prints trailing slashes (so that its output
-   can be appended a slash unconditionally).  */
-char *
-grub_make_system_path_relative_to_its_root (const char *path)
-{
-  struct stat st;
-  char *p, *buf, *buf2, *buf3, *ret;
-  uintptr_t offset = 0;
-  dev_t num;
-  size_t len;
-  char *poolfs = NULL;
-
-  /* canonicalize.  */
-  p = canonicalize_file_name (path);
-  if (p == NULL)
-    grub_util_error (_("failed to get canonical path of `%s'"), path);
-
-  /* For ZFS sub-pool filesystems, could be extended to others (btrfs?).  */
-  {
-    char *dummy;
-    grub_find_zpool_from_dir (p, &dummy, &poolfs);
-  }
-
-  len = strlen (p) + 1;
-  buf = xstrdup (p);
-  free (p);
-
-  if (stat (buf, &st) < 0)
-    grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
-
-  buf2 = xstrdup (buf);
-  num = st.st_dev;
-
-  /* This loop sets offset to the number of chars of the root
-     directory we're inspecting.  */
-  while (1)
-    {
-      p = strrchr (buf, '/');
-      if (p == NULL)
-       /* This should never happen.  */
-       grub_util_error ("%s",
-                        /* TRANSLATORS: canonical pathname is the
-                           complete one e.g. /etc/fstab. It has
-                           to contain `/' normally, if it doesn't
-                           we're in trouble and throw this error.  */
-                        _("no `/' in canonical filename"));
-      if (p != buf)
-       *p = 0;
-      else
-       *++p = 0;
-
-      if (stat (buf, &st) < 0)
-       grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
-
-      /* buf is another filesystem; we found it.  */
-      if (st.st_dev != num)
-       {
-         /* offset == 0 means path given is the mount point.
-            This works around special-casing of "/" in Un*x.  This function never
-            prints trailing slashes (so that its output can be appended a slash
-            unconditionally).  Each slash in is considered a preceding slash, and
-            therefore the root directory is an empty string.  */
-         if (offset == 0)
-           {
-             free (buf);
-#ifdef __linux__
-             {
-               char *bind;
-               grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind));
-               if (bind && bind[0] && bind[1])
-                 {
-                   buf3 = bind;
-                   goto parsedir;
-                 }
-               grub_free (bind);
-             }
-#endif
-             free (buf2);
-             if (poolfs)
-               return xasprintf ("/%s/@", poolfs);
-             return xstrdup ("");
-           }
-         else
-           break;
-       }
-
-      offset = p - buf;
-      /* offset == 1 means root directory.  */
-      if (offset == 1)
-       {
-         /* Include leading slash.  */
-         offset = 0;
-         break;
-       }
-    }
-  free (buf);
-  buf3 = xstrdup (buf2 + offset);
-  buf2[offset] = 0;
-#ifdef __linux__
-  {
-    char *bind;
-    grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind));
-    if (bind && bind[0] && bind[1])
-      {
-       char *temp = buf3;
-       buf3 = grub_xasprintf ("%s%s%s", bind, buf3[0] == '/' ?"":"/", buf3);
-       grub_free (temp);
-      }
-    grub_free (bind);
-  }
-#endif
-  
-  free (buf2);
-
-#ifdef __linux__
- parsedir:
-#endif
-  /* Remove trailing slashes, return empty string if root directory.  */
-  len = strlen (buf3);
-  while (len > 0 && buf3[len - 1] == '/')
-    {
-      buf3[len - 1] = '\0';
-      len--;
-    }
-
-  if (poolfs)
-    {
-      ret = xasprintf ("/%s/@%s", poolfs, buf3);
-      free (buf3);
-    }
-  else
-    ret = buf3;
-
-  return ret;
-}
-
 int
 grub_util_biosdisk_is_floppy (grub_disk_t disk)
 {
diff --git a/grub-core/osdep/unix/relpath.c b/grub-core/osdep/unix/relpath.c
new file mode 100644 (file)
index 0000000..8489a16
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config-util.h>
+#include <config.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+
+#include <grub/util/misc.h>
+#include <grub/emu/hostdisk.h>
+#include <grub/emu/getroot.h>
+#include <grub/mm.h>
+
+/* This function never prints trailing slashes (so that its output
+   can be appended a slash unconditionally).  */
+char *
+grub_make_system_path_relative_to_its_root (const char *path)
+{
+  struct stat st;
+  char *p, *buf, *buf2, *buf3, *ret;
+  uintptr_t offset = 0;
+  dev_t num;
+  size_t len;
+  char *poolfs = NULL;
+
+  /* canonicalize.  */
+  p = canonicalize_file_name (path);
+  if (p == NULL)
+    grub_util_error (_("failed to get canonical path of `%s'"), path);
+
+  /* For ZFS sub-pool filesystems, could be extended to others (btrfs?).  */
+  {
+    char *dummy;
+    grub_find_zpool_from_dir (p, &dummy, &poolfs);
+  }
+
+  len = strlen (p) + 1;
+  buf = xstrdup (p);
+  free (p);
+
+  if (stat (buf, &st) < 0)
+    grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
+
+  buf2 = xstrdup (buf);
+  num = st.st_dev;
+
+  /* This loop sets offset to the number of chars of the root
+     directory we're inspecting.  */
+  while (1)
+    {
+      p = strrchr (buf, '/');
+      if (p == NULL)
+       /* This should never happen.  */
+       grub_util_error ("%s",
+                        /* TRANSLATORS: canonical pathname is the
+                           complete one e.g. /etc/fstab. It has
+                           to contain `/' normally, if it doesn't
+                           we're in trouble and throw this error.  */
+                        _("no `/' in canonical filename"));
+      if (p != buf)
+       *p = 0;
+      else
+       *++p = 0;
+
+      if (stat (buf, &st) < 0)
+       grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
+
+      /* buf is another filesystem; we found it.  */
+      if (st.st_dev != num)
+       {
+         /* offset == 0 means path given is the mount point.
+            This works around special-casing of "/" in Un*x.  This function never
+            prints trailing slashes (so that its output can be appended a slash
+            unconditionally).  Each slash in is considered a preceding slash, and
+            therefore the root directory is an empty string.  */
+         if (offset == 0)
+           {
+             free (buf);
+#ifdef __linux__
+             {
+               char *bind;
+               grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind));
+               if (bind && bind[0] && bind[1])
+                 {
+                   buf3 = bind;
+                   goto parsedir;
+                 }
+               grub_free (bind);
+             }
+#endif
+             free (buf2);
+             if (poolfs)
+               return xasprintf ("/%s/@", poolfs);
+             return xstrdup ("");
+           }
+         else
+           break;
+       }
+
+      offset = p - buf;
+      /* offset == 1 means root directory.  */
+      if (offset == 1)
+       {
+         /* Include leading slash.  */
+         offset = 0;
+         break;
+       }
+    }
+  free (buf);
+  buf3 = xstrdup (buf2 + offset);
+  buf2[offset] = 0;
+#ifdef __linux__
+  {
+    char *bind;
+    grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind));
+    if (bind && bind[0] && bind[1])
+      {
+       char *temp = buf3;
+       buf3 = grub_xasprintf ("%s%s%s", bind, buf3[0] == '/' ?"":"/", buf3);
+       grub_free (temp);
+      }
+    grub_free (bind);
+  }
+#endif
+  
+  free (buf2);
+
+#ifdef __linux__
+ parsedir:
+#endif
+  /* Remove trailing slashes, return empty string if root directory.  */
+  len = strlen (buf3);
+  while (len > 0 && buf3[len - 1] == '/')
+    {
+      buf3[len - 1] = '\0';
+      len--;
+    }
+
+  if (poolfs)
+    {
+      ret = xasprintf ("/%s/@%s", poolfs, buf3);
+      free (buf3);
+    }
+  else
+    ret = buf3;
+
+  return ret;
+}
index d86d0774d9f11feabd4ca4e2127f49d27e15765b..160e8c92c92f25d0d82f450b49d69077473f88d8 100644 (file)
 
 #if SIZEOF_TCHAR == 1
 #define tcsnicmp strncasecmp
-#define tclen strlen
 #elif SIZEOF_TCHAR == 2
 #define tcsnicmp wcsnicmp
-#define tclen wcslen
 #endif
 
 char **
@@ -244,64 +242,6 @@ grub_util_find_partition_start_os (const char *os_dev)
   return exts.Extents[0].StartingOffset.QuadPart / 512;
 }
 
-char *
-grub_make_system_path_relative_to_its_root (const char *path)
-{
-  TCHAR *dirwindows, *mntpointwindows;
-  TCHAR *ptr;
-  TCHAR volumename[100];
-  size_t mntpointwindows_sz;
-  size_t offset, flen;
-  TCHAR *ret;
-  char *cret;
-
-  dirwindows = grub_util_get_windows_path (path);
-  if (!dirwindows)
-    return xstrdup (path);
-
-  mntpointwindows_sz = strlen (path) * 2 + 1;
-  mntpointwindows = xmalloc ((mntpointwindows_sz + 1) * sizeof (mntpointwindows[0]));
-
-  if (!GetVolumePathName (dirwindows,
-                         mntpointwindows,
-                         mntpointwindows_sz))
-    {
-      offset = 0;
-      if (dirwindows[0] && dirwindows[1] == ':')
-       offset = 2;
-    }
-  offset = tclen (mntpointwindows);
-  free (mntpointwindows);
-  flen = tclen (dirwindows);
-  if (offset > flen)
-    {
-      offset = 0;
-      if (dirwindows[0] && dirwindows[1] == ':')
-       offset = 2;
-    }
-  ret = xmalloc (sizeof (ret[0]) * (flen - offset + 2));
-  if (dirwindows[offset] != '\\'
-      && dirwindows[offset] != '/'
-      && dirwindows[offset])
-    {
-      ret[0] = '\\';
-      memcpy (ret + 1, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
-    }
-  else
-    memcpy (ret, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
-
-  free (dirwindows);
-
-  for (ptr = ret; *ptr; ptr++)
-    if (*ptr == '\\')
-      *ptr = '/';
-
-  cret = grub_util_tchar_to_utf8 (ret);
-  free (ret);
-
-  return cret;
-}
-
 int
 grub_util_biosdisk_is_floppy (grub_disk_t disk)
 {
diff --git a/grub-core/osdep/windows/relpath.c b/grub-core/osdep/windows/relpath.c
new file mode 100644 (file)
index 0000000..731cd0c
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config-util.h>
+#include <config.h>
+
+#include <grub/types.h>
+
+#include <grub/util/misc.h>
+
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/emu/misc.h>
+#include <grub/emu/hostdisk.h>
+#include <grub/emu/getroot.h>
+#include <grub/charset.h>
+#include <grub/util/windows.h>
+#include <windows.h>
+#include <winioctl.h>
+
+#if SIZEOF_TCHAR == 1
+#define tclen strlen
+#elif SIZEOF_TCHAR == 2
+#define tclen wcslen
+#endif
+
+char *
+grub_make_system_path_relative_to_its_root (const char *path)
+{
+  TCHAR *dirwindows, *mntpointwindows;
+  TCHAR *ptr;
+  TCHAR volumename[100];
+  size_t mntpointwindows_sz;
+  size_t offset, flen;
+  TCHAR *ret;
+  char *cret;
+
+  dirwindows = grub_util_get_windows_path (path);
+  if (!dirwindows)
+    return xstrdup (path);
+
+  mntpointwindows_sz = strlen (path) * 2 + 1;
+  mntpointwindows = xmalloc ((mntpointwindows_sz + 1) * sizeof (mntpointwindows[0]));
+
+  if (!GetVolumePathName (dirwindows,
+                         mntpointwindows,
+                         mntpointwindows_sz))
+    {
+      offset = 0;
+      if (dirwindows[0] && dirwindows[1] == ':')
+       offset = 2;
+    }
+  offset = tclen (mntpointwindows);
+  free (mntpointwindows);
+  flen = tclen (dirwindows);
+  if (offset > flen)
+    {
+      offset = 0;
+      if (dirwindows[0] && dirwindows[1] == ':')
+       offset = 2;
+    }
+  ret = xmalloc (sizeof (ret[0]) * (flen - offset + 2));
+  if (dirwindows[offset] != '\\'
+      && dirwindows[offset] != '/'
+      && dirwindows[offset])
+    {
+      ret[0] = '\\';
+      memcpy (ret + 1, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
+    }
+  else
+    memcpy (ret, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
+
+  free (dirwindows);
+
+  for (ptr = ret; *ptr; ptr++)
+    if (*ptr == '\\')
+      *ptr = '/';
+
+  cret = grub_util_tchar_to_utf8 (ret);
+  free (ret);
+
+  return cret;
+}