Elliptics version update: 2.19.2.8
[elliptics.git] / example / meta.c
blob8bd8ee7c22b3b9facda49c4d974ee4eaea5157fa
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"
39 * THIS IS REALLY BAD IDEA !!11
40 * DO NOT DO THIS
43 #include "../library/elliptics.h"
46 #include "backends.h"
47 #include "common.h"
49 #ifndef __unused
50 #define __unused __attribute__ ((unused))
51 #endif
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"
66 , p);
67 exit(-1);
70 int main(int argc, char *argv[])
72 int ch, err, fd = -1;
73 char *logfile = "/dev/stderr";
74 char *name = NULL;
75 FILE *log;
76 int *groups, group_num = 0;
77 unsigned char trans_id[DNET_ID_SIZE], *id = NULL;
78 struct dnet_config cfg, rem;
79 struct dnet_node *n;
80 struct dnet_session *s;
81 struct dnet_id raw;
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) {
95 switch (ch) {
96 case 'f':
97 fd = open(optarg, O_RDONLY | O_CLOEXEC);
98 if (fd < 0) {
99 fprintf(stderr, "Could not open file '%s': %s\n", optarg, strerror(errno));
100 return -errno;
102 case 'N':
103 cfg.ns = optarg;
104 cfg.nsize = strlen(optarg);
105 break;
106 case 'r':
107 err = dnet_parse_addr(optarg, &rem);
108 if (err)
109 return err;
110 break;
111 case 'w':
112 cfg.wait_timeout = atoi(optarg);
113 break;
114 case 'l':
115 logfile = optarg;
116 break;
117 case 'm':
118 meta_logger.log_level = strtoul(optarg, NULL, 0);
119 break;
120 case 'I':
121 err = dnet_parse_numeric_id(optarg, trans_id);
122 if (err)
123 return err;
124 id = trans_id;
125 break;
126 case 'g':
127 group_num = dnet_parse_groups(optarg, &groups);
128 if (group_num <= 0)
129 return -1;
130 break;
131 case 'n':
132 name = optarg;
133 break;
134 case 'h':
135 default:
136 meta_usage(argv[0]);
137 /* not reached */
141 if (!name && !id) {
142 fprintf(stderr, "You must specify name or id\n");
143 meta_usage(argv[0]);
146 if (id && group_num <= 0) {
147 fprintf(stderr, "You must specify groups\n");
148 meta_usage(argv[0]);
151 log = fopen(logfile, "a");
152 if (!log) {
153 err = -errno;
154 fprintf(stderr, "Failed to open log file %s: %s.\n", logfile, strerror(-err));
155 goto err_out_exit;
158 meta_logger.log_private = log;
159 meta_logger.log = dnet_common_log;
160 cfg.log = &meta_logger;
162 n = dnet_node_create(&cfg);
163 if (!n) {
164 err = -EINVAL;
165 goto err_out_exit;
168 s = dnet_session_create(n);
169 if (!s)
170 return -ENOMEM;
172 if (fd == -1) {
173 err = dnet_add_state(n, &rem);
174 if (err)
175 goto err_out_destroy;
177 dnet_session_set_groups(s, groups, group_num);
179 if (id) {
180 int i;
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);
185 if (!err)
186 dnet_meta_print(s, &mc);
187 else
188 dnet_log_raw(n, DNET_LOG_ERROR, "%s: could not read metadata\n", dnet_dump_id(&raw));
190 } else {
191 err = dnet_read_meta(s, &mc, name, strlen(name), NULL);
192 if (!err)
193 dnet_meta_print(s, &mc);
195 } else {
196 struct stat st;
197 struct dnet_map_fd m;
199 fstat(fd, &st);
201 memset(&m, 0, sizeof(m));
202 m.size = st.st_size - 96;
203 m.offset = 96;
204 m.fd = fd;
206 err = dnet_data_map(&m);
207 if (err) {
208 dnet_log_raw(n, DNET_LOG_ERROR, "Could not map metadata: %s\n", strerror(-err));
209 goto err_out_destroy;
212 mc.size = m.size;
213 mc.data = m.data;
215 dnet_meta_print(s, &mc);
217 dnet_data_unmap(&m);
220 dnet_session_destroy(s);
221 err_out_destroy:
222 dnet_node_destroy(n);
223 err_out_exit:
224 return err;