diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-10-14 15:28:51 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-10-14 17:54:41 +0300 |
commit | f035194d7d1b6cc0846ad7a5d86e0d6fa9463c67 (patch) | |
tree | 494d180cee9a078727c8d4965caa65cb2052f33d | |
parent | 58f7dbc0658b3d73816a1bc91b75c2bdac733510 (diff) | |
download | vmod-binlog-f035194d7d1b6cc0846ad7a5d86e0d6fa9463c67.tar.gz vmod-binlog-f035194d7d1b6cc0846ad7a5d86e0d6fa9463c67.tar.bz2 |
New utility binlogsel
* configure.ac: Check for yacc.
* src/.gitignore: Update.
* src/Makefile.am (libbinlog_a_SOURCES): Add new files.
Build binlogsel.
* src/binlogcat.c: Use xmalloc.
* src/binlogsel.c: New file.
* src/parse-datetime.h: New file.
* src/parse-datetime.y: New file.
* src/xalloc.c: New file.
* src/xalloc.h: New file.
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/.gitignore | 2 | ||||
-rw-r--r-- | src/Makefile.am | 9 | ||||
-rw-r--r-- | src/binlogcat.c | 17 | ||||
-rw-r--r-- | src/binlogsel.c | 474 | ||||
-rw-r--r-- | src/parse-datetime.h | 3 | ||||
-rw-r--r-- | src/parse-datetime.y | 1598 | ||||
-rw-r--r-- | src/xalloc.c | 53 | ||||
-rw-r--r-- | src/xalloc.h | 3 |
9 files changed, 2145 insertions, 16 deletions
diff --git a/configure.ac b/configure.ac index ea7c061..2bb8f38 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -32,7 +32,7 @@ if test "x$ac_cv_prog_cc_c99" = xno; then | |||
32 | AC_MSG_ERROR([could not find a C99 compatible compiler]) | 32 | AC_MSG_ERROR([could not find a C99 compatible compiler]) |
33 | fi | 33 | fi |
34 | AC_PROG_CPP | 34 | AC_PROG_CPP |
35 | 35 | AC_PROG_YACC | |
36 | AC_PROG_INSTALL | 36 | AC_PROG_INSTALL |
37 | AC_PROG_LIBTOOL | 37 | AC_PROG_LIBTOOL |
38 | AC_PROG_MAKE_SET | 38 | AC_PROG_MAKE_SET |
diff --git a/src/.gitignore b/src/.gitignore index a75fe6d..8f5a6ab 100644 --- a/src/.gitignore +++ b/src/.gitignore | |||
@@ -1,3 +1,5 @@ | |||
1 | binlogcat | 1 | binlogcat |
2 | binlogsel | ||
3 | parse-datetime.c | ||
2 | vcc_if.c | 4 | vcc_if.c |
3 | vcc_if.h | 5 | vcc_if.h |
diff --git a/src/Makefile.am b/src/Makefile.am index 4e31e89..565444a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -17,13 +17,16 @@ | |||
17 | AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) | 17 | AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) |
18 | 18 | ||
19 | noinst_LIBRARIES = libbinlog.a | 19 | noinst_LIBRARIES = libbinlog.a |
20 | libbinlog_a_SOURCES = pack.c err.c | 20 | libbinlog_a_SOURCES = pack.c err.c parse-datetime.y xalloc.c |
21 | libbinlog_a_CFLAGS = $(AM_CFLAGS) | 21 | libbinlog_a_CFLAGS = $(AM_CFLAGS) |
22 | 22 | ||
23 | bin_PROGRAMS = binlogcat | 23 | bin_PROGRAMS = binlogcat binlogsel |
24 | binlogcat_SOURCES = binlogcat.c | 24 | binlogcat_SOURCES = binlogcat.c |
25 | binlogcat_LDADD = ./libbinlog.a | 25 | binlogcat_LDADD = ./libbinlog.a |
26 | 26 | ||
27 | binlogsel_SOURCES = binlogsel.c | ||
28 | binlogsel_LDADD = ./libbinlog.a | ||
29 | |||
27 | vmoddir = $(VMODDIR) | 30 | vmoddir = $(VMODDIR) |
28 | vmod_LTLIBRARIES = libvmod_binlog.la | 31 | vmod_LTLIBRARIES = libvmod_binlog.la |
29 | 32 | ||
@@ -36,7 +39,7 @@ libvmod_binlog_la_SOURCES = \ | |||
36 | vmod-binlog.h\ | 39 | vmod-binlog.h\ |
37 | vcc_if.c vcc_if.h | 40 | vcc_if.c vcc_if.h |
38 | 41 | ||
39 | noinst_HEADERS = pack.h err.h | 42 | noinst_HEADERS = pack.h err.h parse-datetime.h xalloc.h |
40 | 43 | ||
41 | BUILT_SOURCES = vcc_if.c vcc_if.h | 44 | BUILT_SOURCES = vcc_if.c vcc_if.h |
42 | 45 | ||
diff --git a/src/binlogcat.c b/src/binlogcat.c index 505bbc2..656d8f2 100644 --- a/src/binlogcat.c +++ b/src/binlogcat.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include "vmod-binlog.h" | 27 | #include "vmod-binlog.h" |
28 | #include "pack.h" | 28 | #include "pack.h" |
29 | #include "err.h" | 29 | #include "err.h" |
30 | #include "xalloc.h" | ||
30 | 31 | ||
31 | char *timefmt = "%c"; | 32 | char *timefmt = "%c"; |
32 | int number_option; | 33 | int number_option; |
@@ -75,11 +76,7 @@ catlog(const char *fname) | |||
75 | } | 76 | } |
76 | 77 | ||
77 | size = header.hdrsize - sizeof(header); | 78 | size = header.hdrsize - sizeof(header); |
78 | dataspec = malloc(size); | 79 | dataspec = xmalloc(size); |
79 | if (!dataspec) { | ||
80 | error("not enough memory"); | ||
81 | abort(); | ||
82 | } | ||
83 | 80 | ||
84 | if (fread(dataspec, size, 1, fp) != 1) { | 81 | if (fread(dataspec, size, 1, fp) != 1) { |
85 | error("error reading header of %s: %s", | 82 | error("error reading header of %s: %s", |
@@ -94,7 +91,7 @@ catlog(const char *fname) | |||
94 | inst = packcomp(dataspec, &p); | 91 | inst = packcomp(dataspec, &p); |
95 | if (!inst) { | 92 | if (!inst) { |
96 | if (errno == EINVAL) { | 93 | if (errno == EINVAL) { |
97 | error("%s: bad dataspec near %s", dataspec, p); | 94 | error("%s: %s: bad dataspec near %s", fname, dataspec, p); |
98 | exit(1); | 95 | exit(1); |
99 | } | 96 | } |
100 | 97 | ||
@@ -103,11 +100,7 @@ catlog(const char *fname) | |||
103 | } | 100 | } |
104 | free(dataspec); | 101 | free(dataspec); |
105 | 102 | ||
106 | rec = malloc(header.recsize); | 103 | rec = xmalloc(header.recsize); |
107 | if (!rec) { | ||
108 | error("not enough memory"); | ||
109 | exit(1); | ||
110 | } | ||
111 | env = packenv_create(header.recsize - | 104 | env = packenv_create(header.recsize - |
112 | offsetof(struct binlog_record,data)); | 105 | offsetof(struct binlog_record,data)); |
113 | env->fp = stdout; | 106 | env->fp = stdout; |
@@ -147,7 +140,7 @@ catlog(const char *fname) | |||
147 | void | 140 | void |
148 | help() | 141 | help() |
149 | { | 142 | { |
150 | printf("usage: %s [-dhnv] [t FORMAT] [FILE...]\n", progname); | 143 | printf("usage: %s [-dhnv] [-t FORMAT] [FILE...]\n", progname); |
151 | } | 144 | } |
152 | 145 | ||
153 | int | 146 | int |
diff --git a/src/binlogsel.c b/src/binlogsel.c new file mode 100644 index 0000000..62299e5 --- /dev/null +++ b/src/binlogsel.c | |||
@@ -0,0 +1,474 @@ | |||
1 | /* This file is part of vmod-binlog | ||
2 | Copyright (C) 2013 Sergey Poznyakoff | ||
3 | |||
4 | Vmod-binlog is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | Vmod-binlog is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | |||
18 | #include <config.h> | ||
19 | #include <unistd.h> | ||
20 | #include <sys/stat.h> | ||
21 | #include <sys/mman.h> | ||
22 | #include <fcntl.h> | ||
23 | #include <stddef.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <stdarg.h> | ||
27 | #include <errno.h> | ||
28 | #include <time.h> | ||
29 | #include <string.h> | ||
30 | #include <glob.h> | ||
31 | #include "vmod-binlog.h" | ||
32 | #include "pack.h" | ||
33 | #include "err.h" | ||
34 | #include "xalloc.h" | ||
35 | #include "parse-datetime.h" | ||
36 | |||
37 | char *timefmt = "%c"; | ||
38 | int number_option; | ||
39 | int verbose_option; | ||
40 | int timediff_option; | ||
41 | char *pattern; | ||
42 | |||
43 | #define FROM_TIME 0x01 | ||
44 | #define TO_TIME 0x02 | ||
45 | int timemask; | ||
46 | time_t from_time, to_time; | ||
47 | |||
48 | void | ||
49 | help() | ||
50 | { | ||
51 | printf("usage: %s [-dhnv] [-t FORMAT] [-F FROMTIME] [-T TOTIME] [-p PATTERN] [-D DIR] [FILE...]\n", progname); | ||
52 | } | ||
53 | |||
54 | /* Convert strftime-like pattern into globbing pattern */ | ||
55 | void | ||
56 | convpattern(const char *dir) | ||
57 | { | ||
58 | char *p, *q; | ||
59 | char *newpat; | ||
60 | size_t size = strlen(pattern) + 1; | ||
61 | |||
62 | if (dir) | ||
63 | size += strlen(dir) + 1; | ||
64 | |||
65 | newpat = xmalloc(size); | ||
66 | |||
67 | p = newpat; | ||
68 | if (dir) { | ||
69 | strcpy(p, dir); | ||
70 | p += strlen(dir); | ||
71 | *p++ = '/'; | ||
72 | } | ||
73 | |||
74 | for (q = pattern; *q; ) { | ||
75 | if (*q == '%') { | ||
76 | *p++ = '*'; | ||
77 | q += 2; | ||
78 | } else | ||
79 | *p++ = *q++; | ||
80 | } | ||
81 | *p = 0; | ||
82 | pattern = newpat; | ||
83 | } | ||
84 | |||
85 | #define getrec(base, recsize, n) \ | ||
86 | ((struct binlog_record*)((char *)(base) + (n) * (recsize))) | ||
87 | |||
88 | int | ||
89 | searchts(void *base, size_t recsize, size_t from, size_t to, time_t ts, | ||
90 | size_t *ret) | ||
91 | { | ||
92 | struct binlog_record *rec; | ||
93 | size_t middle; | ||
94 | |||
95 | rec = getrec(base, recsize, from); | ||
96 | |||
97 | if (ts < rec->ts) { | ||
98 | *ret = from; | ||
99 | return -1; | ||
100 | } | ||
101 | |||
102 | if (to == from) { | ||