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          |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index b496787..9188a7b 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -269,6 +269,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 be83407..5e39adb 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:
@@ -568,6 +569,25 @@ static usbg_binding *usbg_allocate_binding(char *path, 
char *name,
        return b;
 }
 
+static int usbg_remove_file(char *path, char *name)
+{
+       int ret = USBG_SUCCESS;
+       int nmb;
+       char buf[USBG_MAX_PATH_LENGTH];
+
+       nmb = snprintf(buf, sizeof(buf), "%s/%s", path, name);
+       if (nmb < sizeof(buf)) {
+               nmb = unlink(buf);
+               if (nmb != 0)
+                       ret = usbg_translate_error(errno);
+       } else {
+               ret = USBG_ERROR_PATH_TOO_LONG;
+       }
+
+       return ret;
+}
+
+
 static int usbg_parse_function_net_attrs(usbg_function *f,
                usbg_function_attrs *f_attrs)
 {
@@ -1102,6 +1122,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