summaryrefslogtreecommitdiffstats
path: root/discover
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2014-01-24 14:39:37 +0800
committerJeremy Kerr <jk@ozlabs.org>2014-01-30 21:59:10 +0800
commit4896183708855fbfd0aa892537fbcc17ed7eb971 (patch)
tree5bbd851636871982cc26ca59ff1aa0970f6d8116 /discover
parenteea9a9fa697654ef26d7e2c1cee3b4ac610db643 (diff)
downloadtalos-petitboot-4896183708855fbfd0aa892537fbcc17ed7eb971.tar.gz
talos-petitboot-4896183708855fbfd0aa892537fbcc17ed7eb971.zip
discover: Add support for checking directories in parser API
This change adds a function to the parser API: int parser_check_dir(struct discover_context *ctx, struct discover_device *dev, const char *dirname) - which allows parsers to check for the presence of a directory (path of 'dirname') on the device ('dev'). We use this in the GRUB2 parser to implement the `test -d` check. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'discover')
-rw-r--r--discover/grub2/builtins.c14
-rw-r--r--discover/parser.c19
-rw-r--r--discover/parser.h2
3 files changed, 35 insertions, 0 deletions
diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c
index 7511076..6ada2a6 100644
--- a/discover/grub2/builtins.c
+++ b/discover/grub2/builtins.c
@@ -154,6 +154,15 @@ static bool builtin_test_op_file(struct grub2_script *script, char op,
return result;
}
+static bool builtin_test_op_dir(struct grub2_script *script, char op,
+ const char *dir)
+{
+ if (op != 'd')
+ return false;
+
+ return parser_check_dir(script->ctx, script->ctx->device, dir) == 0;
+}
+
static bool builtin_test_op(struct grub2_script *script,
int argc, char **argv, int *consumed)
{
@@ -207,6 +216,11 @@ static bool builtin_test_op(struct grub2_script *script,
*consumed = 2;
return builtin_test_op_file(script, op[1], a1);
}
+
+ if (!strcmp(op, "-d")) {
+ *consumed = 2;
+ return builtin_test_op_dir(script, op[1], a1);
+ }
}
op = argv[0];
diff --git a/discover/parser.c b/discover/parser.c
index 21b48de..74b2559 100644
--- a/discover/parser.c
+++ b/discover/parser.c
@@ -49,6 +49,25 @@ int parser_request_file(struct discover_context *ctx,
return rc;
}
+int parser_check_dir(struct discover_context *ctx,
+ struct discover_device *dev, const char *dirname)
+{
+ struct stat statbuf;
+ char *path;
+ int rc;
+
+ if (!dev->mount_path)
+ return -1;
+
+ path = local_path(ctx, dev, dirname);
+
+ rc = stat(path, &statbuf);
+ if (!rc)
+ return -1;
+
+ return S_ISDIR(statbuf.st_mode) ? 0 : -1;
+}
+
int parser_replace_file(struct discover_context *ctx,
struct discover_device *dev, const char *filename,
char *buf, int len)
diff --git a/discover/parser.h b/discover/parser.h
index 970d72c..e0e8dc6 100644
--- a/discover/parser.h
+++ b/discover/parser.h
@@ -62,5 +62,7 @@ int parser_replace_file(struct discover_context *ctx,
char *buf, int len);
int parser_request_url(struct discover_context *ctx, struct pb_url *url,
char **buf, int *len);
+int parser_check_dir(struct discover_context *ctx,
+ struct discover_device *dev, const char *dirname);
#endif /* _PARSER_H */
OpenPOWER on IntegriCloud