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)
{
*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];
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)
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 */
void test_add_file_data(struct parser_test *test, struct discover_device *dev,
const char *filename, const void *data, int size);
+void test_add_dir(struct parser_test *test, struct discover_device *dev,
+ const char *dirname);
void test_set_event_source(struct parser_test *test);
void test_set_event_param(struct event *event, const char *name,
const char *value);
struct test_file {
struct discover_device *dev;
+ enum {
+ TEST_FILE,
+ TEST_DIR,
+ } type;
const char *name;
void *data;
int size;
struct test_file *file;
file = talloc_zero(test, struct test_file);
+ file->type = TEST_FILE;
file->dev = dev;
file->name = filename;
file->data = talloc_memdup(test, data, size);
list_add(&test->files, &file->list);
}
+void test_add_dir(struct parser_test *test, struct discover_device *dev,
+ const char *dirname)
+{
+ struct test_file *file;
+
+ file = talloc_zero(test, struct test_file);
+ file->type = TEST_DIR;
+ file->dev = dev;
+ file->name = dirname;
+ list_add(&test->files, &file->list);
+}
+
void test_set_event_source(struct parser_test *test)
{
test->ctx->event = talloc_zero(test->ctx, struct event);
continue;
if (strcmp(file->name, filename))
continue;
+ if (file->type != TEST_FILE)
+ continue;
/* the read_file() interface always adds a trailing null
* for string-safety; do the same here */
return -1;
}
+int parser_check_dir(struct discover_context *ctx,
+ struct discover_device *dev, const char *dirname)
+{
+ struct parser_test *test = ctx->test_data;
+ struct test_file *file;
+
+ printf("%s: %s\n", __func__, dirname);
+
+ list_for_each_entry(&test->files, file, list) {
+ if (file->dev != dev)
+ continue;
+ if (strcmp(file->name, dirname))
+ continue;
+ return file->type == TEST_DIR ? 0 : -1;
+ }
+
+ return -1;
+}
+
int parser_replace_file(struct discover_context *ctx,
struct discover_device *dev, const char *filename,
char *buf, int len)