sr_load.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <faux/faux.h>
  4. #include <faux/str.h>
  5. #include <faux/file.h>
  6. #include <faux/argv.h>
  7. #include <sysrepo.h>
  8. #include <sysrepo/xpath.h>
  9. #include <sysrepo/netconf_acm.h>
  10. #include "pline.h"
  11. int srp_mass_set(int fd, pline_opts_t *opts, const char *user, bool_t stop_on_error);
  12. int main(int argc, char **argv)
  13. {
  14. int ret = -1;
  15. pline_opts_t opts;
  16. bool_t stop_on_error = BOOL_TRUE;
  17. const char *user = "root";
  18. pline_opts_init(&opts);
  19. ret = srp_mass_set(STDIN_FILENO, &opts, user, stop_on_error);
  20. argc = argc;
  21. argv = argv;
  22. return ret;
  23. }
  24. int srp_mass_set(int fd, pline_opts_t *opts, const char *user, bool_t stop_on_error)
  25. {
  26. int ret = -1;
  27. int err = SR_ERR_OK;
  28. sr_conn_ctx_t *conn = NULL;
  29. sr_session_ctx_t *sess = NULL;
  30. faux_file_t *file = NULL;
  31. char *line = NULL;
  32. size_t err_num = 0;
  33. sr_subscription_ctx_t *nacm_sub = NULL;
  34. err = sr_connect(SR_CONN_DEFAULT, &conn);
  35. if (err) {
  36. fprintf(stderr, "Error: Can't connect to sysrepo\n");
  37. goto out;
  38. }
  39. err = sr_session_start(conn, SR_DS_CANDIDATE, &sess);
  40. if (err) {
  41. fprintf(stderr, "Error: Can't start session\n");
  42. goto out;
  43. }
  44. sr_session_set_orig_name(sess, user);
  45. // Init NACM session
  46. if (opts->enable_nacm) {
  47. if (sr_nacm_init(sess, 0, &nacm_sub) != SR_ERR_OK) {
  48. fprintf(stderr, "Error: Can't init NACM\n");
  49. goto out;
  50. }
  51. sr_nacm_set_user(sess, user);
  52. }
  53. file = faux_file_fdopen(fd);
  54. if (!file) {
  55. fprintf(stderr, "Error: Can't open input stream\n");
  56. goto out;
  57. }
  58. while ((line = faux_file_getline(file))) {
  59. pline_t *pline = NULL;
  60. faux_argv_t *args = NULL;
  61. args = faux_argv_new();
  62. faux_argv_parse(args, line);
  63. pline = pline_parse(sess, args, opts);
  64. faux_argv_free(args);
  65. if (!pline || pline->invalid) {
  66. err_num++;
  67. fprintf(stderr, "Error: Illegal: %s\n", line);
  68. } else {
  69. faux_list_node_t *iter = NULL;
  70. pexpr_t *expr = NULL;
  71. iter = faux_list_head(pline->exprs);
  72. while ((expr = (pexpr_t *)faux_list_each(&iter))) {
  73. if (!(expr->pat & PT_SET)) {
  74. err_num++;
  75. fprintf(stderr, "Error: Illegal expression for set operation\n");
  76. break;
  77. }
  78. if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
  79. SR_ERR_OK) {
  80. err_num++;
  81. fprintf(stderr, "Error: Can't set data\n");
  82. break;
  83. }
  84. }
  85. }
  86. if (stop_on_error && (err_num > 0)) {
  87. sr_discard_changes(sess);
  88. goto out;
  89. }
  90. pline_free(pline);
  91. faux_str_free(line);
  92. }
  93. if (sr_has_changes(sess)) {
  94. if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
  95. sr_discard_changes(sess);
  96. fprintf(stderr, "Error: Can't apply changes\n");
  97. goto out;
  98. }
  99. }
  100. ret = 0;
  101. out:
  102. faux_file_close(file);
  103. if (opts->enable_nacm) {
  104. sr_unsubscribe(nacm_sub);
  105. sr_nacm_destroy();
  106. }
  107. sr_disconnect(conn);
  108. return ret;
  109. }