From 04861318a326c344e2c699abb3b6fd08fcff9b15 Mon Sep 17 00:00:00 2001 From: Pawel Szewczyk Date: Thu, 9 Jul 2015 16:27:17 +0200 Subject: [PATCH] gadget: Add gadget template command at libusbg backend Change-Id: Ic8835d9f3e84c16c39a26b8f9a2e340b1a4b20d9 Signed-off-by: Pawel Szewczyk --- source/gadget/src/gadget_libusbg.c | 50 +++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/source/gadget/src/gadget_libusbg.c b/source/gadget/src/gadget_libusbg.c index c40b101..5b81275 100644 --- a/source/gadget/src/gadget_libusbg.c +++ b/source/gadget/src/gadget_libusbg.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "gadget.h" #include "backend.h" @@ -560,6 +561,53 @@ static int set_func(void *data) return 0; } +int template_filter(const struct dirent *entry) +{ + if (entry->d_name[0] == '.') + return 0; + + return 1; +} + +static int template_func(void *data) +{ + struct gt_gadget_template_data *dt; + struct dirent **dentry; + char buf[PATH_MAX]; + int ret; + const char **ptr; + + dt = (struct gt_gadget_template_data *)data; + + if (gt_settings.lookup_path != NULL) { + ptr = gt_settings.lookup_path; + while (*ptr) { + ret = snprintf(buf, sizeof(buf), "%s/%s", *ptr, dt->name); + if (ret >= sizeof(buf)) { + fprintf(stderr, "path too long\n"); + return -1; + } + + ret = scandir(*ptr, &dentry, template_filter, alphasort); + if (ret < 0) { + perror("Error reading directory"); + return -1; + } + + while (ret--) { + printf("%s\n", dentry[ret]->d_name); + free(dentry[ret]); + } + + free(dentry); + + ptr++; + } + } + + return 0; +} + struct gt_gadget_backend gt_gadget_backend_libusbg = { .create = create_func, .rm = rm_func, @@ -570,7 +618,7 @@ struct gt_gadget_backend gt_gadget_backend_libusbg = { .gadget = gadget_func, .load = load_func, .save = save_func, - .template_default = NULL, + .template_default = template_func, .template_get = NULL, .template_set = NULL, .template_rm = NULL, -- 2.7.4