Ver código fonte

delete operation

Serj Kalichev 1 ano atrás
pai
commit
8e6eeace67
4 arquivos alterados com 73 adições e 1 exclusões
  1. 1 0
      src/plugin.c
  2. 59 0
      src/syms.c
  3. 4 0
      src/syms.h
  4. 9 1
      xml/sysrepo.xml

+ 1 - 0
src/plugin.c

@@ -50,6 +50,7 @@ int kplugin_sysrepo_init(kcontext_t *context)
 
 	// Operations
 	kplugin_add_syms(plugin, ksym_new("srp_set", srp_set));
+	kplugin_add_syms(plugin, ksym_new("srp_del", srp_del));
 
 	return 0;
 }

+ 59 - 0
src/syms.c

@@ -219,3 +219,62 @@ cleanup:
 
 	return ret;
 }
+
+
+int srp_del(kcontext_t *context)
+{
+	int ret = 0;
+	faux_argv_t *args = NULL;
+	pline_t *pline = NULL;
+	sr_conn_ctx_t *conn = NULL;
+	sr_session_ctx_t *sess = NULL;
+	pexpr_t *expr = NULL;
+	size_t err_num = 0;
+
+	assert(context);
+
+	if (sr_connect(SR_CONN_DEFAULT, &conn))
+		return -1;
+	if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
+		sr_disconnect(conn);
+		return -1;
+	}
+
+	args = param2argv(kcontext_pargv(context), "path");
+	pline = pline_parse(sess, args, 0);
+	faux_argv_free(args);
+
+	if (pline->invalid) {
+		fprintf(stderr, "Invalid 'del' request\n");
+		ret = -1;
+		goto cleanup;
+	}
+
+	if (faux_list_len(pline->exprs) > 1) {
+		fprintf(stderr, "Can't delete more than one object\n");
+		ret = -1;
+		goto cleanup;
+	}
+
+	expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
+
+	if (!(expr->pat & PT_DEL)) {
+		fprintf(stderr, "Illegal expression for 'del' operation\n");
+		ret = -1;
+		goto cleanup;
+	}
+
+	if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
+		fprintf(stderr, "Can't delete data\n");
+		ret = -1;
+		goto cleanup;
+	}
+
+	sr_apply_changes(sess, 0);
+
+cleanup:
+	pline_free(pline);
+	sr_disconnect(conn);
+
+	return ret;
+}

+ 4 - 0
src/syms.h

@@ -11,14 +11,18 @@
 
 C_DECL_BEGIN
 
+// Types
 int srp_PLINE_SET(kcontext_t *context);
 int srp_PLINE_DEL(kcontext_t *context);
 int srp_PLINE_EDIT(kcontext_t *context);
 
+// Completion/Help
 int srp_compl(kcontext_t *context);
 int srp_help(kcontext_t *context);
 
+// Operations
 int srp_set(kcontext_t *context);
+int srp_del(kcontext_t *context);
 
 C_DECL_END
 

+ 9 - 1
xml/sysrepo.xml

@@ -48,7 +48,7 @@
 </ENTRY>
 -->
 
-<ENTRY name="set" help="Clear settings" mode="sequence">
+<ENTRY name="set" help="Set data to database" mode="sequence">
 	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
 	<ENTRY name="path" max="100">
 		<ENTRY name="PLINE_SET" purpose="ptype" ref="/PLINE_SET"/>
@@ -56,6 +56,14 @@
 	<ACTION sym="srp_set@sysrepo"/>
 </ENTRY>
 
+<ENTRY name="del" help="Delete data from database" mode="sequence">
+	<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+	<ENTRY name="path" max="100">
+		<ENTRY name="PLINE_DEL" purpose="ptype" ref="/PLINE_DEL"/>
+	</ENTRY>
+	<ACTION sym="srp_del@sysrepo"/>
+</ENTRY>
+
 </ENTRY>
 
 </KLISH>