From cc978da33e8b56d9901d1bec26c6bb4cece9bd69 Mon Sep 17 00:00:00 2001 From: Pawel Szewczyk Date: Wed, 8 Jul 2015 15:24:13 +0200 Subject: [PATCH] gadget: Add gadget save at libusbg backend Signed-off-by: Pawel Szewczyk Use 0 instead of NULL to check for stat() result. Change-Id: I7d3c0fc942df05bd642bc6287c3d6a83f0140f6f Signed-off-by: Krzysztof Opasiak --- source/gadget/src/gadget.c | 15 +++++++- source/gadget/src/gadget_libusbg.c | 72 +++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/source/gadget/src/gadget.c b/source/gadget/src/gadget.c index d346fd7..ac536bd 100644 --- a/source/gadget/src/gadget.c +++ b/source/gadget/src/gadget.c @@ -617,7 +617,17 @@ static void gt_gadget_save_destructor(void *data) static int gt_gadget_save_help(void *data) { - printf("Gadget save help.\n"); + printf("usage: %s save [name]\n" + "Stores the gadget configuration in system templates as name. If name\n" + "not specified gadget's name is used. Sets the template attributes to\n" + "given values.\n" + "Options:\n" + " -f --force\t\toverride gadget template if exist\n" + " --file=\tstores in file\n" + " --stdout\t\tprints the configuration to standard output\n" + " --path=\t\tstores gadget in given path instead of default\n" + " -h, --help\t\tPrint this help\n", + program_name); return -1; } @@ -688,6 +698,9 @@ static void gt_parse_gadget_save(const Command *cmd, int argc, if (c < 0) goto out; + if (dt->name == NULL) + dt->name = dt->gadget; + executable_command_set(exec, GET_EXECUTABLE(save), (void *)dt, gt_gadget_save_destructor); diff --git a/source/gadget/src/gadget_libusbg.c b/source/gadget/src/gadget_libusbg.c index 030ba70..4094d0e 100644 --- a/source/gadget/src/gadget_libusbg.c +++ b/source/gadget/src/gadget_libusbg.c @@ -446,6 +446,76 @@ out: return ret; } +static int save_func(void *data) +{ + struct gt_gadget_save_data *dt; + const char *filename = NULL; + FILE *fp = NULL; + char buf[PATH_MAX]; + usbg_gadget *g; + struct stat st; + int ret; + + dt = (struct gt_gadget_save_data *)data; + + if (dt->opts & GT_STDOUT) { + fp = stdout; + } else if (dt->file) { + filename = dt->file; + } else if (dt->path) { + ret = snprintf(buf, sizeof(buf), "%s/%s", dt->path, dt->name); + if (ret >= sizeof(buf)) { + fprintf(stderr, "path too long\n"); + return -1; + } + + filename = buf; + } else if (gt_settings.default_template_path) { + ret = snprintf(buf, sizeof(buf), "%s/%s", + gt_settings.default_template_path, dt->name); + if (ret >= sizeof(buf)) { + fprintf(stderr, "path too long\n"); + return -1; + } + + filename = buf; + } else { + fprintf(stderr, "Path not specified\n"); + return -1; + } + + g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget); + if (!g) { + fprintf(stderr, "Error on get gadget\n"); + return -1; + } + + if (fp == NULL) { + if (!(dt->opts & GT_FORCE) && stat(filename, &st) == 0) { + fprintf(stderr, "File exists.\n"); + return -1; + } + + fp = fopen(filename, "w"); + if (fp == NULL) { + perror("Error opening file\n"); + return -1; + } + } + + ret = usbg_export_gadget(g, fp); + if (ret != USBG_SUCCESS) { + fprintf(stderr, "Error on export gadget\n"); + fprintf(stderr, "Error: %s : %s\n", usbg_error_name(ret), + usbg_strerror(ret)); + goto out; + } + +out: + fclose(fp); + return ret; +} + struct gt_gadget_backend gt_gadget_backend_libusbg = { .create = create_func, .rm = rm_func, @@ -455,7 +525,7 @@ struct gt_gadget_backend gt_gadget_backend_libusbg = { .disable = disable_func, .gadget = gadget_func, .load = load_func, - .save = NULL, + .save = save_func, .template_default = NULL, .template_get = NULL, .template_set = NULL, -- 2.7.4