segfault/memleak on incorrect data fixed
[elliptics.git] / example / hparser.c
blob973157d229e82e3609aac066fc833b78d762c36c
1 /*
2 * 2008+ 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/mman.h>
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
31 #include "elliptics/packet.h"
32 #include "elliptics/interface.h"
34 #include "common.h"
36 static int hparser_region_match(struct dnet_history_entry *e,
37 unsigned long long offset, unsigned long long size)
39 if ((e->offset > offset) && (e->offset < offset + size))
40 return 1;
42 if ((e->offset < offset) && (e->offset + e->size > offset))
43 return 1;
45 return 0;
48 static void hparser_usage(const char *p)
50 fprintf(stderr, "Usage: %s args\n", p);
51 fprintf(stderr, " -f file - history file to parse\n"
52 " -d - history database to parse\n"
53 " -o offset - offset of the region to highlight\n"
54 " -s size - size of the region to highlight\n"
55 " -h - this help\n");
56 exit(-1);
59 static void hparser_dump_history(struct dnet_history_map *m, unsigned long long offset,
60 unsigned long long size)
62 long i;
63 struct tm tm;
64 char str[64];
65 char id_str[DNET_ID_SIZE*2 + 1];
67 for (i=m->num-1; i>=0; --i) {
68 struct dnet_history_entry e = m->ent[i];
69 time_t t;
71 dnet_convert_history_entry(&e);
73 t = e.tsec;
74 localtime_r(&t, &tm);
75 strftime(str, sizeof(str), "%F %R:%S", &tm);
77 printf("%s.%09llu: %s: flags: %08x [removed: %s], offset: %8llu, size: %8llu: %c\n",
78 str, (unsigned long long)e.tnsec,
79 dnet_dump_id_len_raw(e.id, DNET_ID_SIZE, id_str), e.flags,
80 (e.flags & DNET_IO_FLAGS_REMOVED) ? "yes" : "no",
81 (unsigned long long)e.offset, (unsigned long long)e.size,
82 hparser_region_match(&e, offset, size) ? '+' : '-');
84 return;
87 int main(int argc, char *argv[])
89 struct dnet_history_map m;
90 int err, ch;
91 char *file = NULL, *database = NULL;
92 unsigned long long offset, size;
94 size = offset = 0;
96 while ((ch = getopt(argc, argv, "s:o:f:d:h")) != -1) {
97 switch (ch) {
98 case 's':
99 size = strtoull(optarg, NULL, 0);
100 break;
101 case 'o':
102 offset = strtoull(optarg, NULL, 0);
103 break;
104 case 'f':
105 file = optarg;
106 break;
107 case 'd':
108 database = optarg;
109 break;
110 case 'h':
111 hparser_usage(argv[0]);
115 if (!file && !database) {
116 fprintf(stderr, "You have to provide history file or database to parse.\n");
117 hparser_usage(argv[0]);
120 if (file) {
121 err = dnet_map_history(NULL, file, &m);
122 if (err) {
123 fprintf(stderr, "Failed to map history file '%s': %d.\n", file, err);
124 goto err_out_exit;
127 printf("%s: objects: %ld, range: %llu-%llu, counting from the most recent (nanoseconds resolution).\n",
128 file, m.num, offset, offset+size);
130 hparser_dump_history(&m, offset, size);
132 dnet_unmap_history(NULL, &m);
135 if (database) {
136 printf("not yet supported\n");
139 err_out_exit:
140 return err;