Add function which allow to remove binding between function
and configuration. This functions also remove binding from
internal library structures wht means that after this
operation all pointers to removed binding are invalid.

Signed-off-by: Krzysztof Opasiak <k.opas...@samsung.com>
---
 include/usbg/usbg.h |   10 ++++++++++
 src/usbg.c          |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 354ba38..aa8ebc9 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -267,6 +267,16 @@ extern usbg_function *usbg_get_function(usbg_gadget *g, 
const char *name);
  */
 extern usbg_config *usbg_get_config(usbg_gadget *g, const char *name);
 
+/* USB gadget/config/function/binding removal */
+
+/**
+ * @brief Remove binding between configuration and function
+ * @details This function frees also the memory allocated for binding
+ * @param b Binding to be removed
+ * @return 0 on success, usbg_error if error occurred
+ */
+extern int usbg_remove_binding(usbg_binding *b);
+
 /* USB gadget allocation and configuration */
 
 /**
diff --git a/src/usbg.c b/src/usbg.c
index f655675..e82658c 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -142,6 +142,7 @@ static int usbg_translate_error(int error)
                break;
        case EACCES:
        case EROFS:
+       case EPERM:
                ret = USBG_ERROR_NO_ACCESS;
                break;
        case ENOENT:
@@ -450,6 +451,22 @@ static void usbg_free_state(usbg_state *s)
        free(s);
 }
 
+static int usbg_remove_file(char *path, char *name)
+{
+       int ret;
+       char buf[USBG_MAX_PATH_LENGTH];
+
+       sprintf(buf, "%s/%s", path, name);
+
+       ret = unlink(buf);
+       if (ret != 0)
+               ret = usbg_translate_error(errno);
+       else
+               ret = USBG_SUCCESS;
+
+       return ret;
+}
+
 static int usbg_parse_function_net_attrs(usbg_function *f,
                usbg_function_attrs *f_attrs)
 {
@@ -957,6 +974,25 @@ usbg_binding *usbg_get_link_binding(usbg_config *c, 
usbg_function *f)
        return NULL;
 }
 
+int usbg_remove_binding(usbg_binding *b)
+{
+       int ret = USBG_SUCCESS;
+       usbg_config *c;
+
+       if (!b)
+               return USBG_ERROR_INVALID_PARAM;
+
+       c = b->parent;
+
+       ret = usbg_remove_file(b->path, b->name);
+       if (ret == USBG_SUCCESS) {
+               TAILQ_REMOVE(&(c->bindings), b, bnode);
+               usbg_free_binding(b);
+       }
+
+       return ret;
+}
+
 static int usbg_create_empty_gadget(usbg_state *s, char *name, usbg_gadget **g)
 {
        char gpath[USBG_MAX_PATH_LENGTH];
-- 
1.7.9.5

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

Reply via email to