malloc->zalloc
[grub2/phcoder.git] / util / grub-mkconfig_lib.in
blob9afbd3b1f1501784c7f836c986abad270c93fd94
1 # Helper library for grub-mkconfig
2 # Copyright (C) 2007,2008,2009  Free Software Foundation, Inc.
4 # GRUB is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # GRUB is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17 transform="@program_transform_name@"
19 prefix=@prefix@
20 exec_prefix=@exec_prefix@
21 datarootdir=@datarootdir@
22 datadir=@datadir@
23 sbindir=@sbindir@
24 pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
26 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
28 grub_warn ()
30   echo "Warning: $@" >&2
33 make_system_path_relative_to_its_root ()
35   path=$1
36   # abort if file doesn't exist
37   if test -e $path ; then : ;else
38     return 1
39   fi
41   # canonicalize
42   if path=`readlink -f $path` ; then : ; else
43     return 1
44   fi
46   # if not a directory, climb up to the directory containing it
47   if test -d $path ; then
48     dir=$path
49   else
50     dir=`echo $path | sed -e "s,/[^/]*$,,g"`
51   fi
53   num=`stat -c %d $dir`
55   # this loop sets $dir to the root directory of the filesystem we're inspecting
56   while : ; do
57     parent=`readlink -f $dir/..`
58     if [ "x`stat -c %d $parent`" = "x$num" ] ; then : ; else
59       # $parent is another filesystem; we found it.
60       break
61     fi
62     if [ "x$dir" = "x/" ] ; then
63       # / is our root.
64       break
65     fi
66     dir=$parent
67   done
69   # This function never prints trailing slashes (so that its output can be
70   # appended a slash unconditionally).  Each slash in $dir is considered a
71   # preceding slash, and therefore the root directory is an empty string.
72   if [ "$dir" = "/" ] ; then
73     dir=""
74   fi
76   # XXX: This fails if $dir contains ','.
77   path=`echo "$path" | sed -e "s,^$dir,,g"` || return 1
79   case "`uname 2>/dev/null`" in
80     CYGWIN*)
81       # Cygwin: Check if regular or emulated mount.
82       if [ -z "$dir" ] || [ "`stat -c %D "$dir/.."`" != 620000 ] ; then
83         # Reached some mount point not below /cygdrive.
84         # GRUB does not know Cygwin's emulated mounts,
85         # convert to Win32 path and remove drive letter.
86         path=`cygpath -m "$path" | sed -n 's,^[A-Za-z]:,,p'`
87         test ! -z "$path" || return 1
88       fi ;;
89   esac
91   echo "$path"
94 is_path_readable_by_grub ()
96   path=$1
98   # abort if path doesn't exist
99   if test -e $path ; then : ;else
100     return 1
101   fi
103   # abort if file is in a filesystem we can't read
104   if ${grub_probe} -t fs $path > /dev/null 2>&1 ; then : ; else
105     return 1
106   fi
108   return 0
111 convert_system_path_to_grub_path ()
113   path=$1
115   grub_warn "convert_system_path_to_grub_path() is deprecated.  Use prepare_grub_to_access_device() instead."
117   # abort if GRUB can't access the path
118   if is_path_readable_by_grub ${path} ; then : ; else
119     return 1
120   fi
122   if drive=`${grub_probe} -t drive $path` ; then : ; else
123     return 1
124   fi
126   if relative_path=`make_system_path_relative_to_its_root $path` ; then : ; else
127     return 1
128   fi
130   echo ${drive}${relative_path}
133 prepare_grub_to_access_device ()
135   device=$1
137   # Abstraction modules aren't auto-loaded.
138   abstraction="`${grub_probe} --device ${device} --target=abstraction`"
139   if [ "x${abstraction}" = "x" ] ; then : ; else
140     echo "insmod ${abstraction}"
141   fi
143   # If there's a filesystem UUID that GRUB is capable of identifying, use it;
144   # otherwise set root as per value in device.map.
145   echo "set root=`${grub_probe} --device ${device} --target=drive`"
146   if fs_uuid="`${grub_probe} --device ${device} --target=fs_uuid 2> /dev/null`" ; then
147     echo "search --no-floppy --fs-uuid --set ${fs_uuid}"
148   fi
151 font_path ()
153   for dir in ${pkgdatadir} /boot/grub /usr/share/grub ; do
154     # FIXME: We prefer ascii because loading complete fonts is too slow (and
155     # we don't yet provide the gettext magic that would make unicode useful).
156     for basename in ascii unicode unifont ; do
157       path="${dir}/${basename}.pf2"
158       if is_path_readable_by_grub ${path} > /dev/null ; then
159         echo "${path}"
160         return 0
161       fi
162     done
163   done
165   return 1
168 grub_file_is_not_garbage ()
170   if test -f "$1" ; then
171     case "$1" in
172       *.dpkg-dist|*.dpkg-old|*.dpkg-tmp) return 1 ;; # debian dpkg
173     esac
174   else
175     return 1
176   fi
177   return 0