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
;
73 struct dnet_meta_container mc
;
74 struct dnet_metadata_control mctl
;
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) {
90 cfg
.nsize
= strlen(optarg
);
93 err
= dnet_parse_addr(optarg
, &rem
);
98 cfg
.wait_timeout
= atoi(optarg
);
104 meta_logger
.log_level
= strtoul(optarg
, NULL
, 0);
107 err
= dnet_parse_numeric_id(optarg
, trans_id
);
113 group_num
= dnet_parse_groups(optarg
, &groups
);
118 newgroup_num
= dnet_parse_groups(optarg
, &newgroups
);
119 if (newgroup_num
<= 0)
133 fprintf(stderr
, "You must specify name or id\n");
137 if (id
&& group_num
<= 0) {
138 fprintf(stderr
, "You must specify groups\n");
142 if (newgroup_num
<= 0) {
143 fprintf(stderr
, "You must specify new groups\n");
147 log
= fopen(logfile
, "a");
150 fprintf(stderr
, "Failed to open log file %s: %s.\n", logfile
, strerror(errno
));
154 meta_logger
.log_private
= log
;
155 meta_logger
.log
= dnet_common_log
;
156 cfg
.log
= &meta_logger
;
158 n
= dnet_node_create(&cfg
);
162 err
= dnet_add_state(n
, &rem
);
164 goto err_out_destroy
;
166 dnet_node_set_groups(n
, groups
, group_num
);
169 memset(&raw
, 0, sizeof(struct dnet_id
));
171 dnet_setup_id(&raw
, groups
[0], id
);
172 err
= dnet_read_meta(n
, &mc
, NULL
, 0, &raw
);
174 err
= dnet_transform(n
, name
, strlen(name
), &raw
);
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
);
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
));
190 m
= dnet_meta_search_cust(&mc
, DNET_META_PARENT_OBJECT
);
191 dnet_convert_meta(m
);
192 mctl
.obj
= (char *)m
->data
;
194 fprintf(stderr
, "File name from meta: %.*s\n", mctl
.len
, mctl
.obj
);
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
);
205 dnet_node_set_groups(n
, newgroups
, newgroup_num
);
206 err
= dnet_create_write_metadata(n
, &mctl
);
208 fprintf(stderr
, "Meta update failed, err=%d\n", err
);
210 fprintf(stderr
, "Meta was successfully updated in %d groups\n", err
);
215 dnet_node_destroy(n
);