Only work with 0.9.4 cocaine
[elliptics.git] / example / meta.c
blob648283f6554cdb1c6f56428a5f6d3de4741e570f
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_id raw;
81 struct dnet_meta_container mc;
83 memset(&mc, 0, sizeof(mc));
84 memset(&cfg, 0, sizeof(cfg));
86 cfg.sock_type = SOCK_STREAM;
87 cfg.proto = IPPROTO_TCP;
88 cfg.wait_timeout = 10;
89 meta_logger.log_level = DNET_LOG_ERROR;
91 memcpy(&rem, &cfg, sizeof(struct dnet_config));
93 while ((ch = getopt(argc, argv, "f:N:g:w:I:n:r:m:l:h")) != -1) {
94 switch (ch) {
95 case 'f':
96 fd = open(optarg, O_RDONLY | O_CLOEXEC);
97 if (fd < 0) {
98 fprintf(stderr, "Could not open file '%s': %s\n", optarg, strerror(errno));
99 return -errno;
101 case 'N':
102 cfg.ns = optarg;
103 cfg.nsize = strlen(optarg);
104 break;
105 case 'r':
106 err = dnet_parse_addr(optarg, &rem);
107 if (err)
108 return err;
109 break;
110 case 'w':
111 cfg.wait_timeout = atoi(optarg);
112 break;
113 case 'l':
114 logfile = optarg;
115 break;
116 case 'm':
117 meta_logger.log_level = strtoul(optarg, NULL, 0);
118 break;
119 case 'I':
120 err = dnet_parse_numeric_id(optarg, trans_id);
121 if (err)
122 return err;
123 id = trans_id;
124 break;
125 case 'g':
126 group_num = dnet_parse_groups(optarg, &groups);
127 if (group_num <= 0)
128 return -1;
129 break;
130 case 'n':
131 name = optarg;
132 break;
133 case 'h':
134 default:
135 meta_usage(argv[0]);
136 /* not reached */
140 if (!name && !id) {
141 fprintf(stderr, "You must specify name or id\n");
142 meta_usage(argv[0]);
145 if (id && group_num <= 0) {
146 fprintf(stderr, "You must specify groups\n");
147 meta_usage(argv[0]);
150 log = fopen(logfile, "a");
151 if (!log) {
152 err = -errno;
153 fprintf(stderr, "Failed to open log file %s: %s.\n", logfile, strerror(-err));
154 goto err_out_exit;
157 meta_logger.log_private = log;
158 meta_logger.log = dnet_common_log;
159 cfg.log = &meta_logger;
161 n = dnet_node_create(&cfg);
162 if (!n) {
163 err = -EINVAL;
164 goto err_out_exit;
167 if (fd == -1) {
168 err = dnet_add_state(n, &rem);
169 if (err)
170 goto err_out_destroy;
172 dnet_node_set_groups(n, groups, group_num);
174 if (id) {
175 int i;
177 for (i=0; i<group_num; ++i) {
178 dnet_setup_id(&raw, groups[i], id);
179 err = dnet_read_meta(n, &mc, NULL, 0, &raw);
180 if (!err)
181 dnet_meta_print(n, &mc);
182 else
183 dnet_log_raw(n, DNET_LOG_ERROR, "%s: could not read metadata\n", dnet_dump_id(&raw));
185 } else {
186 err = dnet_read_meta(n, &mc, name, strlen(name), NULL);
187 if (!err)
188 dnet_meta_print(n, &mc);
190 } else {
191 struct stat st;
192 struct dnet_map_fd m;
194 fstat(fd, &st);
196 memset(&m, 0, sizeof(m));
197 m.size = st.st_size - 96;
198 m.offset = 96;
199 m.fd = fd;
201 err = dnet_data_map(&m);
202 if (err) {
203 dnet_log_raw(n, DNET_LOG_ERROR, "Could not map metadata: %s\n", strerror(-err));
204 goto err_out_destroy;
207 mc.size = m.size;
208 mc.data = m.data;
210 dnet_meta_print(n, &mc);
212 dnet_data_unmap(&m);
215 err_out_destroy:
216 dnet_node_destroy(n);
217 err_out_exit:
218 return err;