Remove nested functions from filesystem directory iterators.
* include/grub/fs.h (grub_fs_dir_hook_t): New type. (struct grub_fs.dir): Add hook_data argument. Update all implementations and callers.
This commit is contained in:
parent
53d3e4e3df
commit
fc524edf65
35 changed files with 1723 additions and 1451 deletions
|
@ -85,6 +85,119 @@ grub_ls_list_devices (int longlist)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Context for grub_ls_list_files. */
|
||||
struct grub_ls_list_files_ctx
|
||||
{
|
||||
char *dirname;
|
||||
int all;
|
||||
int human;
|
||||
};
|
||||
|
||||
/* Helper for grub_ls_list_files. */
|
||||
static int
|
||||
print_files (const char *filename, const struct grub_dirhook_info *info,
|
||||
void *data)
|
||||
{
|
||||
struct grub_ls_list_files_ctx *ctx = data;
|
||||
|
||||
if (ctx->all || filename[0] != '.')
|
||||
grub_printf ("%s%s ", filename, info->dir ? "/" : "");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Helper for grub_ls_list_files. */
|
||||
static int
|
||||
print_files_long (const char *filename, const struct grub_dirhook_info *info,
|
||||
void *data)
|
||||
{
|
||||
struct grub_ls_list_files_ctx *ctx = data;
|
||||
|
||||
if ((! ctx->all) && (filename[0] == '.'))
|
||||
return 0;
|
||||
|
||||
if (! info->dir)
|
||||
{
|
||||
grub_file_t file;
|
||||
char *pathname;
|
||||
|
||||
if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/')
|
||||
pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
|
||||
else
|
||||
pathname = grub_xasprintf ("%s/%s", ctx->dirname, filename);
|
||||
|
||||
if (!pathname)
|
||||
return 1;
|
||||
|
||||
/* XXX: For ext2fs symlinks are detected as files while they
|
||||
should be reported as directories. */
|
||||
grub_file_filter_disable_compression ();
|
||||
file = grub_file_open (pathname);
|
||||
if (! file)
|
||||
{
|
||||
grub_errno = 0;
|
||||
grub_free (pathname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (! ctx->human)
|
||||
grub_printf ("%-12llu", (unsigned long long) file->size);
|
||||
else
|
||||
{
|
||||
grub_uint64_t fsize = file->size * 100ULL;
|
||||
grub_uint64_t fsz = file->size;
|
||||
int units = 0;
|
||||
char buf[20];
|
||||
|
||||
while (fsz / 1024)
|
||||
{
|
||||
fsize = (fsize + 512) / 1024;
|
||||
fsz /= 1024;
|
||||
units++;
|
||||
}
|
||||
|
||||
if (units)
|
||||
{
|
||||
grub_uint64_t whole, fraction;
|
||||
|
||||
whole = grub_divmod64 (fsize, 100, &fraction);
|
||||
grub_snprintf (buf, sizeof (buf),
|
||||
"%" PRIuGRUB_UINT64_T
|
||||
".%02" PRIuGRUB_UINT64_T "%c", whole, fraction,
|
||||
grub_human_sizes[units]);
|
||||
grub_printf ("%-12s", buf);
|
||||
}
|
||||
else
|
||||
grub_printf ("%-12llu", (unsigned long long) file->size);
|
||||
|
||||
}
|
||||
grub_file_close (file);
|
||||
grub_free (pathname);
|
||||
}
|
||||
else
|
||||
grub_printf ("%-12s", _("DIR"));
|
||||
|
||||
if (info->mtimeset)
|
||||
{
|
||||
struct grub_datetime datetime;
|
||||
grub_unixtime2datetime (info->mtime, &datetime);
|
||||
if (ctx->human)
|
||||
grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
|
||||
datetime.year, datetime.month, datetime.day,
|
||||
datetime.hour, datetime.minute,
|
||||
datetime.second,
|
||||
grub_get_weekday_name (&datetime));
|
||||
else
|
||||
grub_printf (" %04d%02d%02d%02d%02d%02d ",
|
||||
datetime.year, datetime.month,
|
||||
datetime.day, datetime.hour,
|
||||
datetime.minute, datetime.second);
|
||||
}
|
||||
grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_ls_list_files (char *dirname, int longlist, int all, int human)
|
||||
{
|
||||
|
@ -93,107 +206,6 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
|
|||
const char *path;
|
||||
grub_device_t dev;
|
||||
|
||||
auto int print_files (const char *filename,
|
||||
const struct grub_dirhook_info *info);
|
||||
auto int print_files_long (const char *filename,
|
||||
const struct grub_dirhook_info *info);
|
||||
|
||||
int print_files (const char *filename, const struct grub_dirhook_info *info)
|
||||
{
|
||||
if (all || filename[0] != '.')
|
||||
grub_printf ("%s%s ", filename, info->dir ? "/" : "");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int print_files_long (const char *filename,
|
||||
const struct grub_dirhook_info *info)
|
||||
{
|
||||
if ((! all) && (filename[0] == '.'))
|
||||
return 0;
|
||||
|
||||
if (! info->dir)
|
||||
{
|
||||
grub_file_t file;
|
||||
char *pathname;
|
||||
|
||||
if (dirname[grub_strlen (dirname) - 1] == '/')
|
||||
pathname = grub_xasprintf ("%s%s", dirname, filename);
|
||||
else
|
||||
pathname = grub_xasprintf ("%s/%s", dirname, filename);
|
||||
|
||||
if (!pathname)
|
||||
return 1;
|
||||
|
||||
/* XXX: For ext2fs symlinks are detected as files while they
|
||||
should be reported as directories. */
|
||||
grub_file_filter_disable_compression ();
|
||||
file = grub_file_open (pathname);
|
||||
if (! file)
|
||||
{
|
||||
grub_errno = 0;
|
||||
grub_free (pathname);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (! human)
|
||||
grub_printf ("%-12llu", (unsigned long long) file->size);
|
||||
else
|
||||
{
|
||||
grub_uint64_t fsize = file->size * 100ULL;
|
||||
grub_uint64_t fsz = file->size;
|
||||
int units = 0;
|
||||
char buf[20];
|
||||
|
||||
while (fsz / 1024)
|
||||
{
|
||||
fsize = (fsize + 512) / 1024;
|
||||
fsz /= 1024;
|
||||
units++;
|
||||
}
|
||||
|
||||
if (units)
|
||||
{
|
||||
grub_uint64_t whole, fraction;
|
||||
|
||||
whole = grub_divmod64 (fsize, 100, &fraction);
|
||||
grub_snprintf (buf, sizeof (buf),
|
||||
"%" PRIuGRUB_UINT64_T
|
||||
".%02" PRIuGRUB_UINT64_T "%c", whole, fraction,
|
||||
grub_human_sizes[units]);
|
||||
grub_printf ("%-12s", buf);
|
||||
}
|
||||
else
|
||||
grub_printf ("%-12llu", (unsigned long long) file->size);
|
||||
|
||||
}
|
||||
grub_file_close (file);
|
||||
grub_free (pathname);
|
||||
}
|
||||
else
|
||||
grub_printf ("%-12s", _("DIR"));
|
||||
|
||||
if (info->mtimeset)
|
||||
{
|
||||
struct grub_datetime datetime;
|
||||
grub_unixtime2datetime (info->mtime, &datetime);
|
||||
if (human)
|
||||
grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
|
||||
datetime.year, datetime.month, datetime.day,
|
||||
datetime.hour, datetime.minute,
|
||||
datetime.second,
|
||||
grub_get_weekday_name (&datetime));
|
||||
else
|
||||
grub_printf (" %04d%02d%02d%02d%02d%02d ",
|
||||
datetime.year, datetime.month,
|
||||
datetime.day, datetime.hour,
|
||||
datetime.minute, datetime.second);
|
||||
}
|
||||
grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
device_name = grub_file_get_device_name (dirname);
|
||||
dev = grub_device_open (device_name);
|
||||
if (! dev)
|
||||
|
@ -221,10 +233,16 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
|
|||
}
|
||||
else if (fs)
|
||||
{
|
||||
struct grub_ls_list_files_ctx ctx = {
|
||||
.dirname = dirname,
|
||||
.all = all,
|
||||
.human = human
|
||||
};
|
||||
|
||||
if (longlist)
|
||||
(fs->dir) (dev, path, print_files_long);
|
||||
(fs->dir) (dev, path, print_files_long, &ctx);
|
||||
else
|
||||
(fs->dir) (dev, path, print_files);
|
||||
(fs->dir) (dev, path, print_files, &ctx);
|
||||
|
||||
if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
|
||||
&& path[grub_strlen (path) - 1] != '/')
|
||||
|
@ -250,9 +268,9 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
|
|||
all = 1;
|
||||
grub_memset (&info, 0, sizeof (info));
|
||||
if (longlist)
|
||||
print_files_long (p, &info);
|
||||
print_files_long (p, &info, &ctx);
|
||||
else
|
||||
print_files (p, &info);
|
||||
print_files (p, &info, &ctx);
|
||||
|
||||
grub_free (dirname);
|
||||
}
|
||||
|
|
|
@ -38,114 +38,125 @@ grub_strtosl (char *arg, char **end, int base)
|
|||
return grub_strtoul (arg, end, base);
|
||||
}
|
||||
|
||||
/* Context for test_parse. */
|
||||
struct test_parse_ctx
|
||||
{
|
||||
int ret, discard, invert;
|
||||
int file_exists;
|
||||
struct grub_dirhook_info file_info;
|
||||
char *filename;
|
||||
};
|
||||
|
||||
/* Take care of discarding and inverting. */
|
||||
static void
|
||||
update_val (int val, struct test_parse_ctx *ctx)
|
||||
{
|
||||
if (! ctx->discard)
|
||||
ctx->ret = ctx->invert ? ! val : val;
|
||||
ctx->invert = ctx->discard = 0;
|
||||
}
|
||||
|
||||
/* A hook for iterating directories. */
|
||||
static int
|
||||
find_file (const char *cur_filename, const struct grub_dirhook_info *info,
|
||||
void *data)
|
||||
{
|
||||
struct test_parse_ctx *ctx = data;
|
||||
|
||||
if ((info->case_insensitive ? grub_strcasecmp (cur_filename, ctx->filename)
|
||||
: grub_strcmp (cur_filename, ctx->filename)) == 0)
|
||||
{
|
||||
ctx->file_info = *info;
|
||||
ctx->file_exists = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check if file exists and fetch its information. */
|
||||
static void
|
||||
get_fileinfo (char *path, struct test_parse_ctx *ctx)
|
||||
{
|
||||
char *pathname;
|
||||
char *device_name;
|
||||
grub_fs_t fs;
|
||||
grub_device_t dev;
|
||||
|
||||
ctx->file_exists = 0;
|
||||
device_name = grub_file_get_device_name (path);
|
||||
dev = grub_device_open (device_name);
|
||||
if (! dev)
|
||||
{
|
||||
grub_free (device_name);
|
||||
return;
|
||||
}
|
||||
|
||||
fs = grub_fs_probe (dev);
|
||||
if (! fs)
|
||||
{
|
||||
grub_free (device_name);
|
||||
grub_device_close (dev);
|
||||
return;
|
||||
}
|
||||
|
||||
pathname = grub_strchr (path, ')');
|
||||
if (! pathname)
|
||||
pathname = path;
|
||||
else
|
||||
pathname++;
|
||||
|
||||
/* Remove trailing '/'. */
|
||||
while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
|
||||
pathname[grub_strlen (pathname) - 1] = 0;
|
||||
|
||||
/* Split into path and filename. */
|
||||
ctx->filename = grub_strrchr (pathname, '/');
|
||||
if (! ctx->filename)
|
||||
{
|
||||
path = grub_strdup ("/");
|
||||
ctx->filename = pathname;
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx->filename++;
|
||||
path = grub_strdup (pathname);
|
||||
path[ctx->filename - pathname] = 0;
|
||||
}
|
||||
|
||||
/* It's the whole device. */
|
||||
if (! *pathname)
|
||||
{
|
||||
ctx->file_exists = 1;
|
||||
grub_memset (&ctx->file_info, 0, sizeof (ctx->file_info));
|
||||
/* Root is always a directory. */
|
||||
ctx->file_info.dir = 1;
|
||||
|
||||
/* Fetch writing time. */
|
||||
ctx->file_info.mtimeset = 0;
|
||||
if (fs->mtime)
|
||||
{
|
||||
if (! fs->mtime (dev, &ctx->file_info.mtime))
|
||||
ctx->file_info.mtimeset = 1;
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
}
|
||||
}
|
||||
else
|
||||
(fs->dir) (dev, path, find_file, ctx);
|
||||
|
||||
grub_device_close (dev);
|
||||
grub_free (path);
|
||||
grub_free (device_name);
|
||||
}
|
||||
|
||||
/* Parse a test expression starting from *argn. */
|
||||
static int
|
||||
test_parse (char **args, int *argn, int argc)
|
||||
{
|
||||
int ret = 0, discard = 0, invert = 0;
|
||||
int file_exists;
|
||||
struct grub_dirhook_info file_info;
|
||||
|
||||
auto void update_val (int val);
|
||||
auto void get_fileinfo (char *pathname);
|
||||
|
||||
/* Take care of discarding and inverting. */
|
||||
void update_val (int val)
|
||||
{
|
||||
if (! discard)
|
||||
ret = invert ? ! val : val;
|
||||
invert = discard = 0;
|
||||
}
|
||||
|
||||
/* Check if file exists and fetch its information. */
|
||||
void get_fileinfo (char *path)
|
||||
{
|
||||
char *filename, *pathname;
|
||||
char *device_name;
|
||||
grub_fs_t fs;
|
||||
grub_device_t dev;
|
||||
|
||||
/* A hook for iterating directories. */
|
||||
auto int find_file (const char *cur_filename,
|
||||
const struct grub_dirhook_info *info);
|
||||
int find_file (const char *cur_filename,
|
||||
const struct grub_dirhook_info *info)
|
||||
{
|
||||
if ((info->case_insensitive ? grub_strcasecmp (cur_filename, filename)
|
||||
: grub_strcmp (cur_filename, filename)) == 0)
|
||||
{
|
||||
file_info = *info;
|
||||
file_exists = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
file_exists = 0;
|
||||
device_name = grub_file_get_device_name (path);
|
||||
dev = grub_device_open (device_name);
|
||||
if (! dev)
|
||||
{
|
||||
grub_free (device_name);
|
||||
return;
|
||||
}
|
||||
|
||||
fs = grub_fs_probe (dev);
|
||||
if (! fs)
|
||||
{
|
||||
grub_free (device_name);
|
||||
grub_device_close (dev);
|
||||
return;
|
||||
}
|
||||
|
||||
pathname = grub_strchr (path, ')');
|
||||
if (! pathname)
|
||||
pathname = path;
|
||||
else
|
||||
pathname++;
|
||||
|
||||
/* Remove trailing '/'. */
|
||||
while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
|
||||
pathname[grub_strlen (pathname) - 1] = 0;
|
||||
|
||||
/* Split into path and filename. */
|
||||
filename = grub_strrchr (pathname, '/');
|
||||
if (! filename)
|
||||
{
|
||||
path = grub_strdup ("/");
|
||||
filename = pathname;
|
||||
}
|
||||
else
|
||||
{
|
||||
filename++;
|
||||
path = grub_strdup (pathname);
|
||||
path[filename - pathname] = 0;
|
||||
}
|
||||
|
||||
/* It's the whole device. */
|
||||
if (! *pathname)
|
||||
{
|
||||
file_exists = 1;
|
||||
grub_memset (&file_info, 0, sizeof (file_info));
|
||||
/* Root is always a directory. */
|
||||
file_info.dir = 1;
|
||||
|
||||
/* Fetch writing time. */
|
||||
file_info.mtimeset = 0;
|
||||
if (fs->mtime)
|
||||
{
|
||||
if (! fs->mtime (dev, &file_info.mtime))
|
||||
file_info.mtimeset = 1;
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
}
|
||||
}
|
||||
else
|
||||
(fs->dir) (dev, path, find_file);
|
||||
|
||||
grub_device_close (dev);
|
||||
grub_free (path);
|
||||
grub_free (device_name);
|
||||
}
|
||||
struct test_parse_ctx ctx = {
|
||||
.ret = 0,
|
||||
.discard = 0,
|
||||
.invert = 0
|
||||
};
|
||||
|
||||
/* Here we have the real parsing. */
|
||||
while (*argn < argc)
|
||||
|
@ -157,14 +168,16 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn + 1], "=") == 0
|
||||
|| grub_strcmp (args[*argn + 1], "==") == 0)
|
||||
{
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) == 0);
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) == 0,
|
||||
&ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn + 1], "!=") == 0)
|
||||
{
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) != 0);
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) != 0,
|
||||
&ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -172,28 +185,32 @@ test_parse (char **args, int *argn, int argc)
|
|||
/* GRUB extension: lexicographical sorting. */
|
||||
if (grub_strcmp (args[*argn + 1], "<") == 0)
|
||||
{
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) < 0);
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) < 0,
|
||||
&ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn + 1], "<=") == 0)
|
||||
{
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) <= 0);
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) <= 0,
|
||||
&ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn + 1], ">") == 0)
|
||||
{
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) > 0);
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) > 0,
|
||||
&ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn + 1], ">=") == 0)
|
||||
{
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) >= 0);
|
||||
update_val (grub_strcmp (args[*argn], args[*argn + 2]) >= 0,
|
||||
&ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -202,7 +219,7 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn + 1], "-eq") == 0)
|
||||
{
|
||||
update_val (grub_strtosl (args[*argn], 0, 0)
|
||||
== grub_strtosl (args[*argn + 2], 0, 0));
|
||||
== grub_strtosl (args[*argn + 2], 0, 0), &ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -210,7 +227,7 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn + 1], "-ge") == 0)
|
||||
{
|
||||
update_val (grub_strtosl (args[*argn], 0, 0)
|
||||
>= grub_strtosl (args[*argn + 2], 0, 0));
|
||||
>= grub_strtosl (args[*argn + 2], 0, 0), &ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -218,7 +235,7 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn + 1], "-gt") == 0)
|
||||
{
|
||||
update_val (grub_strtosl (args[*argn], 0, 0)
|
||||
> grub_strtosl (args[*argn + 2], 0, 0));
|
||||
> grub_strtosl (args[*argn + 2], 0, 0), &ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -226,7 +243,7 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn + 1], "-le") == 0)
|
||||
{
|
||||
update_val (grub_strtosl (args[*argn], 0, 0)
|
||||
<= grub_strtosl (args[*argn + 2], 0, 0));
|
||||
<= grub_strtosl (args[*argn + 2], 0, 0), &ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -234,7 +251,7 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn + 1], "-lt") == 0)
|
||||
{
|
||||
update_val (grub_strtosl (args[*argn], 0, 0)
|
||||
< grub_strtosl (args[*argn + 2], 0, 0));
|
||||
< grub_strtosl (args[*argn + 2], 0, 0), &ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -242,7 +259,7 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn + 1], "-ne") == 0)
|
||||
{
|
||||
update_val (grub_strtosl (args[*argn], 0, 0)
|
||||
!= grub_strtosl (args[*argn + 2], 0, 0));
|
||||
!= grub_strtosl (args[*argn + 2], 0, 0), &ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -265,10 +282,10 @@ test_parse (char **args, int *argn, int argc)
|
|||
|
||||
if (grub_strcmp (args[*argn + 1], "-pgt") == 0)
|
||||
update_val (grub_strtoul (args[*argn] + i, 0, 0)
|
||||
> grub_strtoul (args[*argn + 2] + i, 0, 0));
|
||||
> grub_strtoul (args[*argn + 2] + i, 0, 0), &ctx);
|
||||
else
|
||||
update_val (grub_strtoul (args[*argn] + i, 0, 0)
|
||||
< grub_strtoul (args[*argn + 2] + i, 0, 0));
|
||||
< grub_strtoul (args[*argn + 2] + i, 0, 0), &ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -283,22 +300,24 @@ test_parse (char **args, int *argn, int argc)
|
|||
int bias = 0;
|
||||
|
||||
/* Fetch fileinfo. */
|
||||
get_fileinfo (args[*argn]);
|
||||
file1 = file_info;
|
||||
file1exists = file_exists;
|
||||
get_fileinfo (args[*argn + 2]);
|
||||
get_fileinfo (args[*argn], &ctx);
|
||||
file1 = ctx.file_info;
|
||||
file1exists = ctx.file_exists;
|
||||
get_fileinfo (args[*argn + 2], &ctx);
|
||||
|
||||
if (args[*argn + 1][3])
|
||||
bias = grub_strtosl (args[*argn + 1] + 3, 0, 0);
|
||||
|
||||
if (grub_memcmp (args[*argn + 1], "-nt", 3) == 0)
|
||||
update_val ((file1exists && ! file_exists)
|
||||
|| (file1.mtimeset && file_info.mtimeset
|
||||
&& file1.mtime + bias > file_info.mtime));
|
||||
update_val ((file1exists && ! ctx.file_exists)
|
||||
|| (file1.mtimeset && ctx.file_info.mtimeset
|
||||
&& file1.mtime + bias > ctx.file_info.mtime),
|
||||
&ctx);
|
||||
else
|
||||
update_val ((! file1exists && file_exists)
|
||||
|| (file1.mtimeset && file_info.mtimeset
|
||||
&& file1.mtime + bias < file_info.mtime));
|
||||
update_val ((! file1exists && ctx.file_exists)
|
||||
|| (file1.mtimeset && ctx.file_info.mtimeset
|
||||
&& file1.mtime + bias < ctx.file_info.mtime),
|
||||
&ctx);
|
||||
(*argn) += 3;
|
||||
continue;
|
||||
}
|
||||
|
@ -310,27 +329,27 @@ test_parse (char **args, int *argn, int argc)
|
|||
/* File tests. */
|
||||
if (grub_strcmp (args[*argn], "-d") == 0)
|
||||
{
|
||||
get_fileinfo (args[*argn + 1]);
|
||||
update_val (file_exists && file_info.dir);
|
||||
get_fileinfo (args[*argn + 1], &ctx);
|
||||
update_val (ctx.file_exists && ctx.file_info.dir, &ctx);
|
||||
(*argn) += 2;
|
||||
return ret;
|
||||
return ctx.ret;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn], "-e") == 0)
|
||||
{
|
||||
get_fileinfo (args[*argn + 1]);
|
||||
update_val (file_exists);
|
||||
get_fileinfo (args[*argn + 1], &ctx);
|
||||
update_val (ctx.file_exists, &ctx);
|
||||
(*argn) += 2;
|
||||
return ret;
|
||||
return ctx.ret;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn], "-f") == 0)
|
||||
{
|
||||
get_fileinfo (args[*argn + 1]);
|
||||
get_fileinfo (args[*argn + 1], &ctx);
|
||||
/* FIXME: check for other types. */
|
||||
update_val (file_exists && ! file_info.dir);
|
||||
update_val (ctx.file_exists && ! ctx.file_info.dir, &ctx);
|
||||
(*argn) += 2;
|
||||
return ret;
|
||||
return ctx.ret;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn], "-s") == 0)
|
||||
|
@ -338,25 +357,25 @@ test_parse (char **args, int *argn, int argc)
|
|||
grub_file_t file;
|
||||
grub_file_filter_disable_compression ();
|
||||
file = grub_file_open (args[*argn + 1]);
|
||||
update_val (file && (grub_file_size (file) != 0));
|
||||
update_val (file && (grub_file_size (file) != 0), &ctx);
|
||||
if (file)
|
||||
grub_file_close (file);
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
(*argn) += 2;
|
||||
return ret;
|
||||
return ctx.ret;
|
||||
}
|
||||
|
||||
/* String tests. */
|
||||
if (grub_strcmp (args[*argn], "-n") == 0)
|
||||
{
|
||||
update_val (args[*argn + 1][0]);
|
||||
update_val (args[*argn + 1][0], &ctx);
|
||||
|
||||
(*argn) += 2;
|
||||
continue;
|
||||
}
|
||||
if (grub_strcmp (args[*argn], "-z") == 0)
|
||||
{
|
||||
update_val (! args[*argn + 1][0]);
|
||||
update_val (! args[*argn + 1][0], &ctx);
|
||||
(*argn) += 2;
|
||||
continue;
|
||||
}
|
||||
|
@ -368,42 +387,42 @@ test_parse (char **args, int *argn, int argc)
|
|||
if (grub_strcmp (args[*argn], ")") == 0)
|
||||
{
|
||||
(*argn)++;
|
||||
return ret;
|
||||
return ctx.ret;
|
||||
}
|
||||
/* Recursively invoke if parenthesis. */
|
||||
if (grub_strcmp (args[*argn], "(") == 0)
|
||||
{
|
||||
(*argn)++;
|
||||
update_val (test_parse (args, argn, argc));
|
||||
update_val (test_parse (args, argn, argc), &ctx);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (grub_strcmp (args[*argn], "!") == 0)
|
||||
{
|
||||
invert = ! invert;
|
||||
ctx.invert = ! ctx.invert;
|
||||
(*argn)++;
|
||||
continue;
|
||||
}
|
||||
if (grub_strcmp (args[*argn], "-a") == 0)
|
||||
{
|
||||
/* If current value is 0 second value is to be discarded. */
|
||||
discard = ! ret;
|
||||
ctx.discard = ! ctx.ret;
|
||||
(*argn)++;
|
||||
continue;
|
||||
}
|
||||
if (grub_strcmp (args[*argn], "-o") == 0)
|
||||
{
|
||||
/* If current value is 1 second value is to be discarded. */
|
||||
discard = ret;
|
||||
ctx.discard = ctx.ret;
|
||||
(*argn)++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* No test found. Interpret if as just a string. */
|
||||
update_val (args[*argn][0]);
|
||||
update_val (args[*argn][0], &ctx);
|
||||
(*argn)++;
|
||||
}
|
||||
return ret;
|
||||
return ctx.ret;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
|
|
|
@ -279,63 +279,75 @@ match_devices (const regex_t *regexp, int noparts)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Context for match_files. */
|
||||
struct match_files_ctx
|
||||
{
|
||||
const regex_t *regexp;
|
||||
char **files;
|
||||
unsigned nfile;
|
||||
char *dir;
|
||||
};
|
||||
|
||||
/* Helper for match_files. */
|
||||
static int
|
||||
match_files_iter (const char *name, const struct grub_dirhook_info *info,
|
||||
void *data)
|
||||
{
|
||||
struct match_files_ctx *ctx = data;
|
||||
char **t;
|
||||
char *buffer;
|
||||
|
||||
/* skip . and .. names */
|
||||
if (grub_strcmp(".", name) == 0 || grub_strcmp("..", name) == 0)
|
||||
return 0;
|
||||
|
||||
grub_dprintf ("expand", "matching: %s in %s\n", name, ctx->dir);
|
||||
if (regexec (ctx->regexp, name, 0, 0, 0))
|
||||
return 0;
|
||||
|
||||
grub_dprintf ("expand", "matched\n");
|
||||
|
||||
buffer = grub_xasprintf ("%s%s", ctx->dir, name);
|
||||
if (! buffer)
|
||||
return 1;
|
||||
|
||||
t = grub_realloc (ctx->files, sizeof (char*) * (ctx->nfile + 2));
|
||||
if (! t)
|
||||
{
|
||||
grub_free (buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ctx->files = t;
|
||||
ctx->files[ctx->nfile++] = buffer;
|
||||
ctx->files[ctx->nfile] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char **
|
||||
match_files (const char *prefix, const char *suffix, const char *end,
|
||||
const regex_t *regexp)
|
||||
{
|
||||
struct match_files_ctx ctx = {
|
||||
.regexp = regexp,
|
||||
.nfile = 0,
|
||||
.files = 0
|
||||
};
|
||||
int i;
|
||||
char **files;
|
||||
unsigned nfile;
|
||||
char *dir;
|
||||
const char *path;
|
||||
char *device_name;
|
||||
grub_fs_t fs;
|
||||
grub_device_t dev;
|
||||
|
||||
auto int match (const char *name, const struct grub_dirhook_info *info);
|
||||
int match (const char *name, const struct grub_dirhook_info *info)
|
||||
{
|
||||
char **t;
|
||||
char *buffer;
|
||||
|
||||
/* skip . and .. names */
|
||||
if (grub_strcmp(".", name) == 0 || grub_strcmp("..", name) == 0)
|
||||
return 0;
|
||||
|
||||
grub_dprintf ("expand", "matching: %s in %s\n", name, dir);
|
||||
if (regexec (regexp, name, 0, 0, 0))
|
||||
return 0;
|
||||
|
||||
grub_dprintf ("expand", "matched\n");
|
||||
|
||||
buffer = grub_xasprintf ("%s%s", dir, name);
|
||||
if (! buffer)
|
||||
return 1;
|
||||
|
||||
t = grub_realloc (files, sizeof (char*) * (nfile + 2));
|
||||
if (! t)
|
||||
{
|
||||
grub_free (buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
files = t;
|
||||
files[nfile++] = buffer;
|
||||
files[nfile] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
nfile = 0;
|
||||
files = 0;
|
||||
dev = 0;
|
||||
device_name = 0;
|
||||
grub_error_push ();
|
||||
|
||||
dir = make_dir (prefix, suffix, end);
|
||||
if (! dir)
|
||||
ctx.dir = make_dir (prefix, suffix, end);
|
||||
if (! ctx.dir)
|
||||
goto fail;
|
||||
|
||||
device_name = grub_file_get_device_name (dir);
|
||||
device_name = grub_file_get_device_name (ctx.dir);
|
||||
dev = grub_device_open (device_name);
|
||||
if (! dev)
|
||||
goto fail;
|
||||
|
@ -344,33 +356,33 @@ match_files (const char *prefix, const char *suffix, const char *end,
|
|||
if (! fs)
|
||||
goto fail;
|
||||
|
||||
if (dir[0] == '(')
|
||||
if (ctx.dir[0] == '(')
|
||||
{
|
||||
path = grub_strchr (dir, ')');
|
||||
path = grub_strchr (ctx.dir, ')');
|
||||
if (!path)
|
||||
goto fail;
|
||||
path++;
|
||||
}
|
||||
else
|
||||
path = dir;
|
||||
path = ctx.dir;
|
||||
|
||||
if (fs->dir (dev, path, match))
|
||||
if (fs->dir (dev, path, match_files_iter, &ctx))
|
||||
goto fail;
|
||||
|
||||
grub_free (dir);
|
||||
grub_free (ctx.dir);
|
||||
grub_device_close (dev);
|
||||
grub_free (device_name);
|
||||
grub_error_pop ();
|
||||
return files;
|
||||
return ctx.files;
|
||||
|
||||
fail:
|
||||
|
||||
grub_free (dir);
|
||||
grub_free (ctx.dir);
|
||||
|
||||
for (i = 0; files && files[i]; i++)
|
||||
grub_free (files[i]);
|
||||
for (i = 0; ctx.files && ctx.files[i]; i++)
|
||||
grub_free (ctx.files[i]);
|
||||
|
||||
grub_free (files);
|
||||
grub_free (ctx.files);
|
||||
|
||||
if (dev)
|
||||
grub_device_close (dev);
|
||||
|
@ -381,28 +393,42 @@ match_files (const char *prefix, const char *suffix, const char *end,
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Context for check_file. */
|
||||
struct check_file_ctx
|
||||
{
|
||||
const char *basename;
|
||||
int found;
|
||||
};
|
||||
|
||||
/* Helper for check_file. */
|
||||
static int
|
||||
check_file_iter (const char *name, const struct grub_dirhook_info *info,
|
||||
void *data)
|
||||
{
|
||||
struct check_file_ctx *ctx = data;
|
||||
|
||||
if (ctx->basename[0] == 0
|
||||
|| (info->case_insensitive ? grub_strcasecmp (name, ctx->basename) == 0
|
||||
: grub_strcmp (name, ctx->basename) == 0))
|
||||
{
|
||||
ctx->found = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
check_file (const char *dir, const char *basename)
|
||||
{
|
||||
struct check_file_ctx ctx = {
|
||||
.basename = basename,
|
||||
.found = 0
|
||||
};
|
||||
grub_fs_t fs;
|
||||
grub_device_t dev;
|
||||
int found = 0;
|
||||
const char *device_name, *path;
|
||||
|
||||
auto int match (const char *name, const struct grub_dirhook_info *info);
|
||||
int match (const char *name, const struct grub_dirhook_info *info)
|
||||
{
|
||||
if (basename[0] == 0
|
||||
|| (info->case_insensitive ? grub_strcasecmp (name, basename) == 0
|
||||
: grub_strcmp (name, basename) == 0))
|
||||
{
|
||||
found = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
device_name = grub_file_get_device_name (dir);
|
||||
dev = grub_device_open (device_name);
|
||||
if (! dev)
|
||||
|
@ -422,14 +448,14 @@ check_file (const char *dir, const char *basename)
|
|||
else
|
||||
path = dir;
|
||||
|
||||
fs->dir (dev, path[0] ? path : "/", match);
|
||||
fs->dir (dev, path[0] ? path : "/", check_file_iter, &ctx);
|
||||
if (grub_errno == 0 && basename[0] == 0)
|
||||
found = 1;
|
||||
ctx.found = 1;
|
||||
|
||||
fail:
|
||||
grub_errno = 0;
|
||||
|
||||
return found;
|
||||
return ctx.found;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue