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"
39 * THIS IS REALLY BAD IDEA !!11
43 #include "../library/elliptics.h"
50 #define __unused __attribute__ ((unused))
53 static struct dnet_log meta_logger
;
55 static void meta_usage(char *p
)
57 fprintf(stderr
, "Usage: %s <options>\n"
58 " -I id - use this ID to get metadata\n"
59 " -n name - use this name to get metadata\n"
60 " -g group:group... - groups to get matadata from\n"
61 " -w timeout - wait timeout in seconds\n"
62 " -r addr:port:family - connect to this remote node\n"
63 " -m level - log level\n"
64 " -l log - log file\n"
65 " -f file - read metadata from file\n"
70 int main(int argc
, char *argv
[])
73 char *logfile
= "/dev/stderr";
76 int *groups
, group_num
= 0;
77 unsigned char trans_id
[DNET_ID_SIZE
], *id
= NULL
;
78 struct dnet_config cfg
, rem
;
80 struct dnet_session
*s
;
82 struct dnet_meta_container mc
;
84 memset(&mc
, 0, sizeof(mc
));
85 memset(&cfg
, 0, sizeof(cfg
));
87 cfg
.sock_type
= SOCK_STREAM
;
88 cfg
.proto
= IPPROTO_TCP
;
89 cfg
.wait_timeout
= 10;
90 meta_logger
.log_level
= DNET_LOG_ERROR
;
92 memcpy(&rem
, &cfg
, sizeof(struct dnet_config
));
94 while ((ch
= getopt(argc
, argv
, "f:N:g:w:I:n:r:m:l:h")) != -1) {
97 fd
= open(optarg
, O_RDONLY
| O_CLOEXEC
);
99 fprintf(stderr
, "Could not open file '%s': %s\n", optarg
, strerror(errno
));
104 cfg
.nsize
= strlen(optarg
);
107 err
= dnet_parse_addr(optarg
, &rem
);
112 cfg
.wait_timeout
= atoi(optarg
);
118 meta_logger
.log_level
= strtoul(optarg
, NULL
, 0);
121 err
= dnet_parse_numeric_id(optarg
, trans_id
);
127 group_num
= dnet_parse_groups(optarg
, &groups
);
142 fprintf(stderr
, "You must specify name or id\n");
146 if (id
&& group_num
<= 0) {
147 fprintf(stderr
, "You must specify groups\n");
151 log
= fopen(logfile
, "a");
154 fprintf(stderr
, "Failed to open log file %s: %s.\n", logfile
, strerror(-err
));
158 meta_logger
.log_private
= log
;
159 meta_logger
.log
= dnet_common_log
;
160 cfg
.log
= &meta_logger
;
162 n
= dnet_node_create(&cfg
);
168 s
= dnet_session_create(n
);
173 err
= dnet_add_state(n
, &rem
);
175 goto err_out_destroy
;
177 dnet_session_set_groups(s
, groups
, group_num
);
182 for (i
=0; i
<group_num
; ++i
) {
183 dnet_setup_id(&raw
, groups
[i
], id
);
184 err
= dnet_read_meta(s
, &mc
, NULL
, 0, &raw
);
186 dnet_meta_print(s
, &mc
);
188 dnet_log_raw(n
, DNET_LOG_ERROR
, "%s: could not read metadata\n", dnet_dump_id(&raw
));
191 err
= dnet_read_meta(s
, &mc
, name
, strlen(name
), NULL
);
193 dnet_meta_print(s
, &mc
);
197 struct dnet_map_fd m
;
201 memset(&m
, 0, sizeof(m
));
202 m
.size
= st
.st_size
- 96;
206 err
= dnet_data_map(&m
);
208 dnet_log_raw(n
, DNET_LOG_ERROR
, "Could not map metadata: %s\n", strerror(-err
));
209 goto err_out_destroy
;
215 dnet_meta_print(s
, &mc
);
220 dnet_session_destroy(s
);
222 dnet_node_destroy(n
);