2 * 2011+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
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>
18 #include <sys/socket.h>
20 #include <sys/syscall.h>
32 #include <netinet/in.h>
34 #include "elliptics/packet.h"
35 #include "elliptics/interface.h"
41 #define __unused __attribute__ ((unused))
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"
61 int main(int argc
, char *argv
[])
64 char *logfile
= "/dev/stderr";
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
;
72 struct dnet_session
*s
;
74 struct dnet_meta_container mc
;
75 struct dnet_metadata_control mctl
;
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) {
91 cfg
.nsize
= strlen(optarg
);
94 err
= dnet_parse_addr(optarg
, &rem
);
99 cfg
.wait_timeout
= atoi(optarg
);
105 meta_logger
.log_level
= strtoul(optarg
, NULL
, 0);
108 err
= dnet_parse_numeric_id(optarg
, trans_id
);
114 group_num
= dnet_parse_groups(optarg
, &groups
);
119 newgroup_num
= dnet_parse_groups(optarg
, &newgroups
);
120 if (newgroup_num
<= 0)
134 fprintf(stderr
, "You must specify name or id\n");
138 if (id
&& group_num
<= 0) {
139 fprintf(stderr
, "You must specify groups\n");
143 if (newgroup_num
<= 0) {
144 fprintf(stderr
, "You must specify new groups\n");
148 log
= fopen(logfile
, "a");
151 fprintf(stderr
, "Failed to open log file %s: %s.\n", logfile
, strerror(errno
));
155 meta_logger
.log_private
= log
;
156 meta_logger
.log
= dnet_common_log
;
157 cfg
.log
= &meta_logger
;
159 n
= dnet_node_create(&cfg
);
163 s
= dnet_session_create(n
);
165 goto err_out_destroy_node
;
167 err
= dnet_add_state(n
, &rem
);
169 goto err_out_destroy
;
171 dnet_session_set_groups(s
, groups
, group_num
);
174 memset(&raw
, 0, sizeof(struct dnet_id
));
176 dnet_setup_id(&raw
, groups
[0], id
);
177 err
= dnet_read_meta(s
, &mc
, NULL
, 0, &raw
);
179 err
= dnet_transform(n
, name
, strlen(name
), &raw
);
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
);
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
));
195 m
= dnet_meta_search_cust(&mc
, DNET_META_PARENT_OBJECT
);
196 dnet_convert_meta(m
);
197 mctl
.obj
= (char *)m
->data
;
199 fprintf(stderr
, "File name from meta: %.*s\n", mctl
.len
, mctl
.obj
);
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
);
210 dnet_session_set_groups(s
, newgroups
, newgroup_num
);
211 err
= dnet_create_write_metadata(s
, &mctl
);
213 fprintf(stderr
, "Meta update failed, err=%d\n", err
);
215 fprintf(stderr
, "Meta was successfully updated in %d groups\n", err
);
220 dnet_session_destroy(s
);
221 err_out_destroy_node
:
222 dnet_node_destroy(n
);