libusbgx: Add remove configuration functionality.
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Thu, 3 Apr 2014 14:52:39 +0000 (16:52 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 22 Dec 2015 19:39:43 +0000 (20:39 +0100)
Add function which allow to remove configuration.
This functions also remove binding from internal
library structures what means that after this
operation all pointers to removed config are invalid.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
[Port from libusbg and update description]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
include/usbg/usbg.h
src/usbg.c

index d4435fd..ddec06f 100644 (file)
 #define USBG_MAX_PATH_LENGTH PATH_MAX
 #define USBG_MAX_NAME_LENGTH 40
 
+/**
+ * @brief Additional option for usbg_rm_* functions.
+ * @details This option allows to remove all content
+ * of gadget/config/function recursive.
+ */
+#define USBG_RM_RECURSE 1
+
 /*
  * Internal structures
  */
@@ -287,6 +294,15 @@ extern usbg_config *usbg_get_config(usbg_gadget *g, int id, const char *label);
 extern int usbg_rm_binding(usbg_binding *b);
 
 /**
+ * @brief Remove configuration
+ * @details This function frees also the memory allocated for configuration
+ * @param c Configuration to be removed
+ * @param opts Additional options for configuration removal.
+ * @return 0 on success, usbg_error if error occurred
+ */
+extern int usbg_rm_config(usbg_config *c, int opts);
+
+/**
  * @brief Remove configuration strings for given language
  * @param c Pointer to configuration
  * @param lang Language of strings which should be deleted
index f1cbb76..f558c16 100644 (file)
@@ -714,6 +714,28 @@ static int usbg_rm_dir(char *path, char *name)
        return ret;
 }
 
+static int usbg_rm_all_dirs(char *path)
+{
+       int ret = USBG_SUCCESS;
+       int n, i;
+       struct dirent **dent;
+
+       n = scandir(path, &dent, file_select, alphasort);
+       if (n >= 0) {
+               for (i = 0; i < n; ++i) {
+                       if (ret == USBG_SUCCESS)
+                               ret = usbg_rm_dir(path, dent[i]->d_name);
+
+                       free(dent[i]);
+               }
+               free(dent);
+       } else {
+               ret = usbg_translate_error(errno);
+       }
+
+       return ret;
+}
+
 static int usbg_parse_function_net_attrs(usbg_function *f,
                usbg_function_attrs *f_attrs)
 {
@@ -1319,6 +1341,52 @@ int usbg_rm_binding(usbg_binding *b)
        return ret;
 }
 
+int usbg_rm_config(usbg_config *c, int opts)
+{
+       int ret = USBG_ERROR_INVALID_PARAM;
+       usbg_gadget *g;
+
+       if (!c)
+               return ret;
+
+       g = c->parent;
+
+       if (opts & USBG_RM_RECURSE) {
+               /* Recursive flag was given
+                * so remove all bindings and strings */
+               char spath[USBG_MAX_PATH_LENGTH];
+               int nmb;
+               usbg_binding *b;
+
+               while (!TAILQ_EMPTY(&c->bindings)) {
+                       b = TAILQ_FIRST(&c->bindings);
+                       ret = usbg_rm_binding(b);
+                       if (ret != USBG_SUCCESS)
+                               goto out;
+               }
+
+               nmb = snprintf(spath, sizeof(spath), "%s/%s/%s", c->path,
+                               c->name, STRINGS_DIR);
+               if (nmb >= sizeof(spath)) {
+                       ret = USBG_ERROR_PATH_TOO_LONG;
+                       goto out;
+               }
+
+               ret = usbg_rm_all_dirs(spath);
+               if (ret != USBG_SUCCESS)
+                       goto out;
+       }
+
+       ret = usbg_rm_dir(c->path, c->name);
+       if (ret == USBG_SUCCESS) {
+               TAILQ_REMOVE(&(g->configs), c, cnode);
+               usbg_free_config(c);
+       }
+
+out:
+       return ret;
+}
+
 int usbg_rm_config_strs(usbg_config *c, int lang)
 {
        int ret = USBG_SUCCESS;