Elliptics version update: 2.19.0.0
[elliptics.git] / example / meta_update_groups.c
blob2cdf4d4acfb64dee6a773b43f3fc487b1fbb2208
1 /*
2 * 2011+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/socket.h>
19 #include <sys/time.h>
20 #include <sys/syscall.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
32 #include <netinet/in.h>
34 #include "elliptics/packet.h"
35 #include "elliptics/interface.h"
37 #include "backends.h"
38 #include "common.h"
40 #ifndef __unused
41 #define __unused __attribute__ ((unused))
42 #endif
44 static struct dnet_log meta_logger;
46 static void meta_usage(char *p)
48 fprintf(stderr, "Usage: %s <options>\n"
49 " -I id - use this ID to get metadata\n"
50 " -n name - use this name to get metadata\n"
51 " -g group:group... - groups to get matadata from\n"
52 " -G group:group... - groups to upload matadata to\n"
53 " -w timeout - wait timeout in seconds\n"
54 " -r addr:port:family - connect to this remote node\n"
55 " -m level - log level\n"
56 " -l log - log file\n"
57 , p);
58 exit(-1);
61 int main(int argc, char *argv[])
63 int ch, err;
64 char *logfile = "/dev/stderr";
65 char *name = NULL;
66 FILE *log;
67 int *groups, group_num = 0;
68 int *newgroups, newgroup_num = 0;
69 unsigned char trans_id[DNET_ID_SIZE], *id = NULL;
70 struct dnet_config cfg, rem;
71 struct dnet_node *n;
72 struct dnet_session *s;
73 struct dnet_id raw;
74 struct dnet_meta_container mc;
75 struct dnet_metadata_control mctl;
76 struct dnet_meta *m;
78 memset(&cfg, 0, sizeof(cfg));
80 cfg.sock_type = SOCK_STREAM;
81 cfg.proto = IPPROTO_TCP;
82 cfg.wait_timeout = 10;
83 meta_logger.log_level = DNET_LOG_INFO;
85 memcpy(&rem, &cfg, sizeof(struct dnet_config));
87 while ((ch = getopt(argc, argv, "N:g:G:w:I:n:r:m:l:h")) != -1) {
88 switch (ch) {
89 case 'N':
90 cfg.ns = optarg;
91 cfg.nsize = strlen(optarg);
92 break;
93 case 'r':
94 err = dnet_parse_addr(optarg, &rem);
95 if (err)
96 return err;
97 break;
98 case 'w':
99 cfg.wait_timeout = atoi(optarg);
100 break;
101 case 'l':
102 logfile = optarg;
103 break;
104 case 'm':
105 meta_logger.log_level = strtoul(optarg, NULL, 0);
106 break;
107 case 'I':
108 err = dnet_parse_numeric_id(optarg, trans_id);
109 if (err)
110 return err;
111 id = trans_id;
112 break;
113 case 'g':
114 group_num = dnet_parse_groups(optarg, &groups);
115 if (group_num <= 0)
116 return -1;
117 break;
118 case 'G':
119 newgroup_num = dnet_parse_groups(optarg, &newgroups);
120 if (newgroup_num <= 0)
121 return -1;
122 break;
123 case 'n':
124 name = optarg;
125 break;
126 case 'h':
127 default:
128 meta_usage(argv[0]);
129 /* not reached */
133 if (!name && !id) {
134 fprintf(stderr, "You must specify name or id\n");
135 meta_usage(argv[0]);
138 if (id && group_num <= 0) {
139 fprintf(stderr, "You must specify groups\n");
140 meta_usage(argv[0]);
143 if (newgroup_num <= 0) {
144 fprintf(stderr, "You must specify new groups\n");
145 meta_usage(argv[0]);
148 log = fopen(logfile, "a");
149 if (!log) {
150 err = -errno;
151 fprintf(stderr, "Failed to open log file %s: %s.\n", logfile, strerror(errno));
152 goto err_out_exit;
155 meta_logger.log_private = log;
156 meta_logger.log = dnet_common_log;
157 cfg.log = &meta_logger;
159 n = dnet_node_create(&cfg);
160 if (!n)
161 goto err_out_exit;
163 s = dnet_session_create(n);
164 if (!s)
165 goto err_out_destroy_node;
167 err = dnet_add_state(n, &rem);
168 if (err)
169 goto err_out_destroy;
171 dnet_session_set_groups(s, groups, group_num);
173 /* Read meta */
174 memset(&raw, 0, sizeof(struct dnet_id));
175 if (!name) {
176 dnet_setup_id(&raw, groups[0], id);
177 err = dnet_read_meta(s, &mc, NULL, 0, &raw);
178 } else {
179 err = dnet_transform(n, name, strlen(name), &raw);
180 if (err) {
181 fprintf(stderr, "dnet_transform failed, err=%d\n", err);
182 goto err_out_destroy;
184 err = dnet_read_meta(s, &mc, name, strlen(name), NULL);
187 if (err < 0) {
188 fprintf(stderr, "Error during reading meta: %d\n", err);
189 goto err_out_destroy;
192 /* Prepare control structure for dnet_create_write_metadata */
193 memset(&mctl, 0, sizeof(mctl));
194 if (!name) {
195 m = dnet_meta_search_cust(&mc, DNET_META_PARENT_OBJECT);
196 dnet_convert_meta(m);
197 mctl.obj = (char *)m->data;
198 mctl.len = m->size;
199 fprintf(stderr, "File name from meta: %.*s\n", mctl.len, mctl.obj);
200 } else {
201 mctl.obj = name;
202 mctl.len = strlen(name);
205 mctl.groups = newgroups;
206 mctl.group_num = newgroup_num;
207 dnet_setup_id(&mctl.id, newgroups[0], (unsigned char *)&raw.id);
209 /* Write new meta */
210 dnet_session_set_groups(s, newgroups, newgroup_num);
211 err = dnet_create_write_metadata(s, &mctl);
212 if (err < 0) {
213 fprintf(stderr, "Meta update failed, err=%d\n", err);
214 } else {
215 fprintf(stderr, "Meta was successfully updated in %d groups\n", err);
219 err_out_destroy:
220 dnet_session_destroy(s);
221 err_out_destroy_node:
222 dnet_node_destroy(n);
223 err_out_exit:
224 return err;