Skip to content
This repository
Browse code

Add del_snap command to example C client

  • Loading branch information...
commit 4da918e3802c70de0a17dbbc29409569565b6f6a 1 parent 872e94f
David Disseldorp ddiss authored

Showing 1 changed file with 134 additions and 3 deletions. Show diff stats Hide diff stats

  1. +134 3 examples/C/snapper_dbus_cli.c
137 examples/C/snapper_dbus_cli.c
@@ -30,6 +30,7 @@
30 30 #define CDBUS_SIG_LIST_SNAPS_RSP "a(uqutussa{ss})"
31 31 #define CDBUS_SIG_LIST_CONFS_RSP "a(ssa{ss})"
32 32 #define CDBUS_SIG_CREATE_SNAP_RSP "u"
  33 +#define CDBUS_SIG_DEL_SNAPS_RSP ""
33 34 #define CDBUS_SIG_STRING_DICT "{ss}"
34 35
35 36 struct dict {
@@ -892,13 +893,137 @@ static int cdbus_create_snap_call(DBusConnection *conn)
892 893 return 0;
893 894 }
894 895
  896 +static int cdbus_del_snap_pack(char *snapper_conf,
  897 + uint32_t snap_id,
  898 + DBusMessage **req_msg_out)
  899 +{
  900 + DBusMessage *msg;
  901 + DBusMessageIter args;
  902 + DBusMessageIter array_iter;
  903 + bool ret;
  904 +
  905 + msg = dbus_message_new_method_call("org.opensuse.Snapper",
  906 + "/org/opensuse/Snapper",
  907 + "org.opensuse.Snapper",
  908 + "DeleteSnapshots");
  909 + if (msg == NULL) {
  910 + fprintf(stderr, "failed to create req msg\n");
  911 + return -ENOMEM;
  912 + }
  913 +
  914 + /* append arguments */
  915 + dbus_message_iter_init_append(msg, &args);
  916 + if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
  917 + &snapper_conf)) {
  918 + fprintf(stderr, "Out Of Memory!\n");
  919 + return -ENOMEM;
  920 + }
  921 +
  922 + ret = dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY,
  923 + DBUS_TYPE_UINT32_AS_STRING,
  924 + &array_iter);
  925 + if (!ret) {
  926 + fprintf(stderr, "failed to open array container\n");
  927 + return -ENOMEM;
  928 + }
  929 +
  930 + if (!dbus_message_iter_append_basic(&array_iter,
  931 + DBUS_TYPE_UINT32,
  932 + &snap_id)) {
  933 + fprintf(stderr, "Out Of Memory!\n");
  934 + return -ENOMEM;
  935 + }
  936 +
  937 + dbus_message_iter_close_container(&args, &array_iter);
  938 +
  939 + *req_msg_out = msg;
  940 +
  941 + return 0;
  942 +}
  943 +
  944 +static int cdbus_del_snap_unpack(DBusConnection *conn,
  945 + DBusMessage *rsp_msg)
  946 +{
  947 + DBusMessageIter iter;
  948 + int msg_type;
  949 + const char *sig;
  950 +
  951 + msg_type = dbus_message_get_type(rsp_msg);
  952 + if (msg_type == DBUS_MESSAGE_TYPE_ERROR) {
  953 + fprintf(stderr, "del snap error response: %s\n",
  954 + dbus_message_get_error_name(rsp_msg));
  955 + return -EINVAL;
  956 + }
  957 +
  958 + if (msg_type != DBUS_MESSAGE_TYPE_METHOD_RETURN) {
  959 + fprintf(stderr, "unexpected del snap ret type: %d\n",
  960 + msg_type);
  961 + return -EINVAL;
  962 + }
  963 +
  964 + sig = dbus_message_get_signature(rsp_msg);
  965 + if ((sig == NULL)
  966 + || (strcmp(sig, CDBUS_SIG_DEL_SNAPS_RSP) != 0)) {
  967 + fprintf(stderr, "bad del snap response sig: %s, "
  968 + "expected: %s\n",
  969 + (sig ? sig : "NULL"), CDBUS_SIG_DEL_SNAPS_RSP);
  970 + return -EINVAL;
  971 + }
  972 +
  973 + /* no parameters in response */
  974 +
  975 + return 0;
  976 +}
  977 +static int cdbus_del_snap_call(DBusConnection *conn,
  978 + uint32_t snap_id)
  979 +{
  980 + int ret;
  981 + DBusMessage *req_msg;
  982 + DBusMessage *rsp_msg;
  983 + DBusPendingCall *pending;
  984 +
  985 + ret = cdbus_del_snap_pack("root", snap_id, &req_msg);
  986 + if (ret < 0) {
  987 + fprintf(stderr, "failed to pack del snap request\n");
  988 + return ret;
  989 + }
  990 +
  991 + ret = cdbus_msg_send(conn, req_msg, &pending);
  992 + if (ret < 0) {
  993 + dbus_message_unref(req_msg);
  994 + return ret;
  995 + }
  996 +
  997 + ret = cdbus_msg_recv(conn, pending, &rsp_msg);
  998 + if (ret < 0) {
  999 + dbus_message_unref(req_msg);
  1000 + dbus_pending_call_unref(pending);
  1001 + return ret;
  1002 + }
  1003 +
  1004 + ret = cdbus_del_snap_unpack(conn, rsp_msg);
  1005 + if (ret < 0) {
  1006 + fprintf(stderr, "failed to unpack del snap response\n");
  1007 + dbus_message_unref(req_msg);
  1008 + dbus_message_unref(rsp_msg);
  1009 + return ret;
  1010 + }
  1011 +
  1012 + printf("deleted snapshot %u\n", snap_id);
  1013 +
  1014 + dbus_message_unref(req_msg);
  1015 + dbus_message_unref(rsp_msg);
  1016 +
  1017 + return 0;
  1018 +}
  1019 +
895 1020 int main(int argc, char **argv)
896 1021 {
897 1022 DBusConnection *conn;
898 1023 int ret = -EINVAL;
899 1024
900   - if (argc != 2) {
901   - fprintf(stderr, "Syntax: %s <list_confs | list_snaps | create_snap>\n",
  1025 + if (argc < 2) {
  1026 + fprintf(stderr, "Syntax: %s <list_confs | list_snaps | create_snap | del_snap> <arg>\n",
902 1027 argv[0]);
903 1028 return -EINVAL;
904 1029 }
@@ -915,8 +1040,14 @@ int main(int argc, char **argv)
915 1040 ret = cdbus_list_snaps_call(conn);
916 1041 } else if (!strcmp(argv[1], "create_snap")) {
917 1042 ret = cdbus_create_snap_call(conn);
  1043 + } else if (!strcmp(argv[1], "del_snap")) {
  1044 + if (argc < 3) {
  1045 + fprintf(stderr, "del_snap requires a snapshot_id argument\n");
  1046 + return -EINVAL;
  1047 + }
  1048 + ret = cdbus_del_snap_call(conn, atoi(argv[2]));
918 1049 } else {
919   - fprintf(stderr, "Syntax: %s <list_confs | list_snaps | create_snap>\n",
  1050 + fprintf(stderr, "Syntax: %s <list_confs | list_snaps | create_snap> | del_snap> <arg>\n",
920 1051 argv[0]);
921 1052 ret = -EINVAL;
922 1053 goto err_conn_close;

0 comments on commit 4da918e

Please sign in to comment.
Something went wrong with that request. Please try again.