Merge branch 'master' of github.com:reverbrain/elliptics
[elliptics.git] / example / meta_update_groups.c
blob7ab3d17eef60685175f7018299547f0ce3759b1e
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_id raw;
73 struct dnet_meta_container mc;
74 struct dnet_metadata_control mctl;
75 struct dnet_meta *m;
77 memset(&cfg, 0, sizeof(cfg));
79 cfg.sock_type = SOCK_STREAM;
80 cfg.proto = IPPROTO_TCP;
81 cfg.wait_timeout = 10;
82 meta_logger.log_level = DNET_LOG_INFO;
84 memcpy(&rem, &cfg, sizeof(struct dnet_config));
86 while ((ch = getopt(argc, argv, "N:g:G:w:I:n:r:m:l:h")) != -1) {
87 switch (ch) {
88 case 'N':
89 cfg.ns = optarg;
90 cfg.nsize = strlen(optarg);
91 break;
92 case 'r':
93 err = dnet_parse_addr(optarg, &rem);
94 if (err)
95 return err;
96 break;
97 case 'w':
98 cfg.wait_timeout = atoi(optarg);
99 break;
100 case 'l':
101 logfile = optarg;
102 break;
103 case 'm':
104 meta_logger.log_level = strtoul(optarg, NULL, 0);
105 break;
106 case 'I':
107 err = dnet_parse_numeric_id(optarg, trans_id);
108 if (err)
109 return err;
110 id = trans_id;
111 break;
112 case 'g':
113 group_num = dnet_parse_groups(optarg, &groups);
114 if (group_num <= 0)
115 return -1;
116 break;
117 case 'G':
118 newgroup_num = dnet_parse_groups(optarg, &newgroups);
119 if (newgroup_num <= 0)
120 return -1;
121 break;
122 case 'n':
123 name = optarg;
124 break;
125 case 'h':
126 default:
127 meta_usage(argv[0]);
128 /* not reached */
132 if (!name && !id) {
133 fprintf(stderr, "You must specify name or id\n");
134 meta_usage(argv[0]);
137 if (id && group_num <= 0) {
138 fprintf(stderr, "You must specify groups\n");
139 meta_usage(argv[0]);
142 if (newgroup_num <= 0) {
143 fprintf(stderr, "You must specify new groups\n");
144 meta_usage(argv[0]);
147 log = fopen(logfile, "a");
148 if (!log) {
149 err = -errno;
150 fprintf(stderr, "Failed to open log file %s: %s.\n", logfile, strerror(errno));
151 goto err_out_exit;
154 meta_logger.log_private = log;
155 meta_logger.log = dnet_common_log;
156 cfg.log = &meta_logger;
158 n = dnet_node_create(&cfg);
159 if (!n)
160 goto err_out_exit;
162 err = dnet_add_state(n, &rem);
163 if (err)
164 goto err_out_destroy;
166 dnet_node_set_groups(n, groups, group_num);
168 /* Read meta */
169 memset(&raw, 0, sizeof(struct dnet_id));
170 if (!name) {
171 dnet_setup_id(&raw, groups[0], id);
172 err = dnet_read_meta(n, &mc, NULL, 0, &raw);
173 } else {
174 err = dnet_transform(n, name, strlen(name), &raw);
175 if (err) {
176 fprintf(stderr, "dnet_transform failed, err=%d\n", err);
177 goto err_out_destroy;
179 err = dnet_read_meta(n, &mc, name, strlen(name), NULL);
182 if (err < 0) {
183 fprintf(stderr, "Error during reading meta: %d\n", err);
184 goto err_out_destroy;
187 /* Prepare control structure for dnet_create_write_metadata */
188 memset(&mctl, 0, sizeof(mctl));
189 if (!name) {
190 m = dnet_meta_search_cust(&mc, DNET_META_PARENT_OBJECT);
191 dnet_convert_meta(m);
192 mctl.obj = (char *)m->data;
193 mctl.len = m->size;
194 fprintf(stderr, "File name from meta: %.*s\n", mctl.len, mctl.obj);
195 } else {
196 mctl.obj = name;
197 mctl.len = strlen(name);
200 mctl.groups = newgroups;
201 mctl.group_num = newgroup_num;
202 dnet_setup_id(&mctl.id, newgroups[0], (unsigned char *)&raw.id);
204 /* Write new meta */
205 dnet_node_set_groups(n, newgroups, newgroup_num);
206 err = dnet_create_write_metadata(n, &mctl);
207 if (err < 0) {
208 fprintf(stderr, "Meta update failed, err=%d\n", err);
209 } else {
210 fprintf(stderr, "Meta was successfully updated in %d groups\n", err);
214 err_out_destroy:
215 dnet_node_destroy(n);
216 err_out_exit:
217 return err;