log: Allow runtime selection of 'debug' log level
[petitboot] / discover / grub2 / builtins.c
index c218bc7d07155bd6426d630f26bf623f0bcb4bb6..6ada2a64c130ae05c66c12c15753e84e0af684e2 100644 (file)
@@ -5,8 +5,9 @@
 #include <log/log.h>
 #include <types/types.h>
 #include <talloc/talloc.h>
 #include <log/log.h>
 #include <types/types.h>
 #include <talloc/talloc.h>
-#include <array-size/array-size.h>
+#include <util/util.h>
 
 
+#include "discover/parser.h"
 #include "grub2.h"
 
 
 #include "grub2.h"
 
 
@@ -123,7 +124,47 @@ static int builtin_search(struct grub2_script *script,
        return 0;
 }
 
        return 0;
 }
 
-static bool builtin_test_op(int argc, char **argv, int *consumed)
+static bool builtin_test_op_file(struct grub2_script *script, char op,
+               const char *file)
+{
+       bool result;
+       int len, rc;
+       char *buf;
+
+       rc = parser_request_file(script->ctx, script->ctx->device,
+                       file, &buf, &len);
+       if (rc)
+               return false;
+
+       switch (op) {
+       case 's':
+               /* -s: return true if file exists and has non-zero size */
+               result = len > 0;
+               break;
+       case 'f':
+               /* -f: return true if file exists */
+               result = true;
+               break;
+       default:
+               result = false;
+
+       }
+
+       talloc_free(buf);
+       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)
 {
        char *op;
 
 {
        char *op;
 
@@ -171,10 +212,14 @@ static bool builtin_test_op(int argc, char **argv, int *consumed)
                        return strlen(a1) != 0;
                }
 
                        return strlen(a1) != 0;
                }
 
-               /* todo: implement file checks */
                if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
                        *consumed = 2;
                if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
                        *consumed = 2;
-                       return false;
+                       return builtin_test_op_file(script, op[1], a1);
+               }
+
+               if (!strcmp(op, "-d")) {
+                       *consumed = 2;
+                       return builtin_test_op_dir(script, op[1], a1);
                }
        }
 
                }
        }
 
@@ -183,7 +228,7 @@ static bool builtin_test_op(int argc, char **argv, int *consumed)
        return strlen(op) > 0;
 }
 
        return strlen(op) > 0;
 }
 
-static int builtin_test(struct grub2_script *script __attribute__((unused)),
+static int builtin_test(struct grub2_script *script,
                void *data __attribute__((unused)),
                int argc, char *argv[])
 {
                void *data __attribute__((unused)),
                int argc, char *argv[])
 {
@@ -222,7 +267,7 @@ static int builtin_test(struct grub2_script *script __attribute__((unused)),
                        continue;
                }
 
                        continue;
                }
 
-               rc = builtin_test_op(argc, argv, &consumed);
+               rc = builtin_test_op(script, argc, argv, &consumed);
                if (not) {
                        rc = !rc;
                        not = false;
                if (not) {
                        rc = !rc;
                        not = false;