-
Notifications
You must be signed in to change notification settings - Fork 1
/
br.cpp
260 lines (217 loc) · 5.45 KB
/
br.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (C) 2006 Folkert van Heusden <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define _LARGEFILE64_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <syslog.h>
#include "br.h"
buffered_reader::buffered_reader(int cur_fd, int cur_block_size)
{
#ifdef USE_MMAP
struct stat64 finfo;
#endif
fd = cur_fd;
block_size = cur_block_size;
buffer = NULL;
buffer_length = buffer_pointer = 0;
mmap_addr = NULL;
/* try do mmap */
#ifdef USE_MMAP
if (fstat64(cur_fd, &finfo) == 0)
{
if (!S_ISFIFO(finfo.st_mode))
{
/* mmap */
size_of_file = finfo.st_size;
cur_offset = mmap_addr = (char *)mmap64(NULL, size_of_file, PROT_READ, MAP_SHARED, cur_fd, 0);
if (!mmap_addr)
{
fprintf(stderr, "mmap64 failed: %d/%s\n", errno, strerror(errno));
}
/* advise the kernel how to treat the mmaped region */
/* FIXME: change to madvise64 as soon as it comes available */
(void)madvise(mmap_addr, size_of_file, MADV_SEQUENTIAL);
// fprintf(stderr, "*using mmap*\n");
}
}
else
{
fprintf(stderr, "Error obtaining information on fd %d: %d/%s\n", cur_fd, errno, strerror(errno));
}
#endif
if (!mmap_addr)
{
#if (_XOPEN_VERSION >= 600)
(void)posix_fadvise(cur_fd, 0, 0, POSIX_FADV_SEQUENTIAL); // or POSIX_FADV_NOREUSE?
#endif
}
}
buffered_reader::~buffered_reader()
{
free(buffer);
if (mmap_addr)
munmap(mmap_addr, size_of_file);
}
int buffered_reader::number_of_bytes_in_buffer(void)
{
return buffer_length - buffer_pointer;
}
int buffered_reader::garbage_collect(char shrink_buffer)
{
if (buffer_pointer)
{
int n_to_move = number_of_bytes_in_buffer();
if (n_to_move > 0)
{
memmove(&buffer[0], &buffer[buffer_pointer], n_to_move);
buffer_length -= buffer_pointer;
buffer_pointer = 0;
if (shrink_buffer)
{
char *dummy = (char *)realloc(buffer, buffer_length + 1);
if (!dummy)
{
fprintf(stderr, "buffered_reader::garbage_collect: realloc failed\n");
syslog(LOG_EMERG, "buffered_reader::garbage_collect: realloc failed");
exit(1);
}
buffer = dummy;
}
buffer[buffer_length] = 0x00;
}
}
return 0;
}
int buffered_reader::read_into_buffer(void)
{
char *dummy;
int n_read = 0;
garbage_collect();
dummy = (char *)realloc(buffer, buffer_length + block_size + 1);
if (!dummy)
{
fprintf(stderr, "buffered_reader::read_into_buffer: realloc failed\n");
syslog(LOG_EMERG, "buffered_reader::read_into_buffer: realloc failed");
exit(1);
}
buffer = dummy;
for(;;)
{
n_read = read(fd, &buffer[buffer_length], block_size);
if (n_read == -1)
{
if (errno == EINTR || errno == EAGAIN)
continue;
fprintf(stderr, "buffered_reader::read_into_buffer: read failed (%s)\n", strerror(errno));
syslog(LOG_EMERG, "buffered_reader::read_into_buffer: read failed: %m");
exit(1);
}
buffer_length += n_read;
break;
}
buffer[buffer_length] = 0x00;
return n_read;
}
char * buffered_reader::read_line(void)
{
char *out = NULL;
#ifdef USE_MMAP
if (mmap_addr)
{
long long int n_bytes;
char *lf;
char *virtual_0x00 = &mmap_addr[size_of_file];
/* EOF reached? */
if (!cur_offset)
return NULL;
/* determine length of current line */
lf = (char *)memchr(cur_offset, '\n', (virtual_0x00 - cur_offset));
if (lf)
n_bytes = lf - cur_offset;
else
n_bytes = virtual_0x00 - cur_offset;
/* allocate memory & copy string */
out = (char *)malloc(n_bytes + 1);
if (!out)
{
fprintf(stderr, "buffered_reader::read_line: malloc(%lld) failed\n", n_bytes + 1);
syslog(LOG_EMERG, "buffered_reader::read_line: malloc(%lld) failed", n_bytes + 1);
exit(1);
}
memcpy(out, cur_offset, n_bytes);
out[n_bytes] = 0x00;
if (lf)
cur_offset = lf + 1;
else
cur_offset = NULL;
}
else
#endif
{
long long int lf_offset = -1;
long long int n_bytes, search_start;
if (number_of_bytes_in_buffer() <= 0)
{
garbage_collect();
if (read_into_buffer() == 0)
{
// EOF
return NULL;
}
}
search_start = buffer_pointer;
for(;;)
{
char *dummy = strchr(&buffer[buffer_pointer], '\n');
if (dummy)
lf_offset = (long long int)(dummy - buffer);
if (lf_offset != -1)
break;
if (read_into_buffer() == 0)
{
lf_offset = buffer_length;
break;
}
}
n_bytes = lf_offset - buffer_pointer;
out = strndup(&buffer[buffer_pointer], n_bytes);
if (!out)
{
fprintf(stderr, "buffered_reader::read_line: malloc(%lld) failed\n", n_bytes + 1);
syslog(LOG_EMERG, "buffered_reader::read_line: malloc(%lld) failed", n_bytes + 1);
exit(1);
}
buffer_pointer = lf_offset + 1;
}
return out;
}
off64_t buffered_reader::file_offset(void)
{
if (mmap_addr)
return cur_offset - mmap_addr;
else
return lseek64(fd, 0, SEEK_CUR);
}