[PATCH 3/6] libusbg: Add remove configuration functionality.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx>
---
 include/usbg/usbg.h |   10 +++++++++
 src/usbg.c          |   62 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 6c14cbc..ef5aba2 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -278,6 +278,16 @@ extern usbg_config *usbg_get_config(usbg_gadget *g, const char *name);
 extern int usbg_remove_binding(usbg_binding *b);
 
 /**
+ * @brief Remove configuration
+ * @details This function frees also the memory allocated for configuration
+ * @param c Configuration to be removed
+ * @param recursive If different than 0 all bindings and strings will
+ * be recursively removed before removing configuration
+ * @return 0 on success, usbg_error if error occurred
+ */
+extern int usbg_remove_config(usbg_config *c, int recursive);
+
+/**
  * @brief Remove configuration strings for given language
  * @param c Pointer to configuration
  * @param lang Language of strings which should be deleted
diff --git a/src/usbg.c b/src/usbg.c
index 8f18047..55530bb 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -483,6 +483,28 @@ static int usbg_remove_dir(char *path, char *name)
 	return ret;
 }
 
+static int usbg_remove_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_remove_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)
 {
@@ -1009,6 +1031,46 @@ int usbg_remove_binding(usbg_binding *b)
 	return ret;
 }
 
+int usbg_remove_config(usbg_config *c, int recursive)
+{
+	int ret = USBG_ERROR_INVALID_PARAM;
+	usbg_gadget *g;
+
+	if (!c)
+		return ret;
+
+	g = c->parent;
+
+	if (recursive) {
+		/* Recursive flag was given
+		 * so remove all bindings and strings */
+		char spath[USBG_MAX_PATH_LENGTH];
+		usbg_binding *b;
+
+		while (!TAILQ_EMPTY(&c->bindings)) {
+			b = TAILQ_FIRST(&c->bindings);
+			ret = usbg_remove_binding(b);
+			if (ret != USBG_SUCCESS)
+				return ret;
+		}
+
+		sprintf(spath, "%s/%s/%s", c->path, c->name, STRINGS_DIR);
+
+		ret = usbg_remove_all_dirs(spath);
+		if (ret != USBG_SUCCESS)
+			return ret;
+
+	}
+
+	ret = usbg_remove_dir(c->path, c->name);
+	if (ret == USBG_SUCCESS) {
+		TAILQ_REMOVE(&(g->configs), c, cnode);
+		usbg_free_config(c);
+	}
+
+	return ret;
+}
+
 int usbg_remove_config_strs(usbg_config *c, int lang)
 {
 	int ret = USBG_SUCCESS;
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux