/* fshelp.c -- Filesystem helper functions */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2004,2005,2006,2007,2008 Free Software Foundation, Inc.
*
* 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/>.
*/
GRUB_MOD_LICENSE ("GPLv3+");
/* Lookup the node PATH. The node ROOTNODE describes the root of the
directory tree. The node found is returned in FOUNDNODE, which is
either a ROOTNODE or a new malloc'ed node. ITERATE_DIR is used to
iterate over all directory entries in the current node.
READ_SYMLINK is used to read the symlink if a node is a symlink.
EXPECTTYPE is the type node that is expected by the called, an
error is generated if the node is not of the expected type. Make
sure you use the NESTED_FUNC_ATTR macro for HOOK, this is required
because GCC has a nasty bug when using regparm=3. */
int NESTED_FUNC_ATTR (*hook)
(const char *filename,
enum grub_fshelp_filetype filetype,
{
int symlinknest = 0;
{
char *next;
enum grub_fshelp_filetype filetype,
{
}
enum grub_fshelp_filetype filetype,
{
if (filetype == GRUB_FSHELP_UNKNOWN ||
(! (filetype & GRUB_FSHELP_CASE_INSENSITIVE) ||
{
return 0;
}
/* The node is found, stop iterating over the nodes. */
return 1;
}
/* Remove all leading slashes. */
while (*name == '/')
name++;
if (! *name)
{
return 0;
}
for (;;)
{
int found;
/* Extract the actual part from the pathname. */
if (next)
{
/* Remove all leading slashes. */
while (*next == '/')
*(next++) = '\0';
}
/* At this point it is expected that the current node is a
directory, check if this is true. */
if (type != GRUB_FSHELP_DIR)
{
}
/* Iterate over the directory. */
if (! found)
{
if (grub_errno)
return grub_errno;
break;
}
/* Read in the symlink and follow it. */
if (type == GRUB_FSHELP_SYMLINK)
{
char *symlink;
/* Test if the symlink does not loop. */
if (++symlinknest == 8)
{
return grub_error (GRUB_ERR_SYMLINK_LOOP,
"too deep nesting of symlinks");
}
if (!symlink)
{
return grub_errno;
}
/* The symlink is an absolute path, go back to the root inode. */
if (symlink[0] == '/')
{
}
/* Lookup the node the symlink points to. */
if (grub_errno)
{
return grub_errno;
}
}
/* Found the node! */
{
return 0;
}
}
}
{
return grub_errno;
}
if (err)
return err;
/* Check if the node that was found was of the expected type. */
return 0;
}
/* Read LEN bytes from the file NODE on disk DISK into the buffer BUF,
beginning with the block POS. READ_HOOK should be set before
reading a block from the file. GET_BLOCK is used to translate file
blocks to disk blocks. The file is FILESIZE bytes big and the
blocks have a size of LOG2BLOCKSIZE (in log2). */
unsigned offset,
unsigned length),
{
/* Adjust LEN so it we can't read past the end of the file. */
{
int skipfirst = 0;
if (grub_errno)
return -1;
/* Last block. */
if (i == blockcnt - 1)
{
/* The last portion is exactly blocksize. */
if (! blockend)
}
/* First block. */
{
}
/* If the block number is 0 this block is not stored on disk but
is zero filled instead. */
if (blknr)
{
if (grub_errno)
return -1;
}
else
}
return len;
}
unsigned int
{
int mod;
*pow = 0;
while (blksize > 1)
{
blksize >>= 1;
/* Check if it really is a power of two. */
if (mod)
return grub_error (GRUB_ERR_BAD_NUMBER,
"the blocksize is not a power of two");
(*pow)++;
}
return GRUB_ERR_NONE;
}