|
@@ -64,8 +64,25 @@ File sorting not implemented yet\n");
|
64
|
64
|
prev_found = 1;
|
65
|
65
|
prev = i;
|
66
|
66
|
}
|
67
|
|
- /* TODO begining binary search of date_min */
|
68
|
|
- ret = _ttail_search_files_binary_search(t, tm, (const struct tm**)ftm);
|
|
67
|
+ if(t->flag & TTAIL_FLAG_DATE_MIN)
|
|
68
|
+ {
|
|
69
|
+ ret = _ttail_search_files_binary_search(t, &(t->date_min),
|
|
70
|
+ (const struct tm**)ftm, 1);
|
|
71
|
+ if(ret)
|
|
72
|
+ {
|
|
73
|
+ goto _ttail_search_closest_files_err;
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+ if(t->flag & TTAIL_FLAG_DATE_MAX)
|
|
77
|
+ {
|
|
78
|
+ ret = _ttail_search_files_binary_search(t, &(t->date_max),
|
|
79
|
+ (const struct tm**)ftm, 0);
|
|
80
|
+ if(ret)
|
|
81
|
+ {
|
|
82
|
+ goto _ttail_search_closest_files_err;
|
|
83
|
+ }
|
|
84
|
+ t->session->file.off_max.off++;
|
|
85
|
+ }
|
69
|
86
|
for(i=0; i<t->logfile_sz; i++)
|
70
|
87
|
{
|
71
|
88
|
if(ftm) { free(ftm[i]); }
|
|
@@ -97,51 +114,99 @@ void _ttail_search_print_files(ttail_t* t, int out)
|
97
|
114
|
int fd;
|
98
|
115
|
char buf[8192];
|
99
|
116
|
int r;
|
100
|
|
-
|
101
|
|
- for(i=t->session->file.id; i<t->logfile_sz; i++)
|
|
117
|
+ off_t cur_off;
|
|
118
|
+
|
|
119
|
+ /* if no date_min t->session->file.id == 0 */
|
|
120
|
+ for(i=t->session->file.off_min.id; i<t->logfile_sz; i++)
|
102
|
121
|
{
|
103
|
122
|
if(!t->logfile[i])
|
104
|
123
|
{
|
105
|
124
|
continue;
|
106
|
125
|
}
|
107
|
126
|
fd = fileno(t->logfile[i]);
|
108
|
|
- if(i==t->session->file.id)
|
|
127
|
+ if(t->flag & TTAIL_FLAG_DATE_MIN &&
|
|
128
|
+ i==t->session->file.off_min.id)
|
109
|
129
|
{
|
110
|
|
- lseek(fd, t->session->file.off, SEEK_SET);
|
111
|
|
- } else {
|
112
|
|
- lseek(fd, 0, SEEK_SET);
|
|
130
|
+ if(t->flag & TTAIL_FLAG_DATE_MAX)
|
|
131
|
+ {
|
|
132
|
+ fseek(t->logfile[i],
|
|
133
|
+ t->session->file.off_min.off,
|
|
134
|
+ SEEK_SET);
|
|
135
|
+ }
|
|
136
|
+ else
|
|
137
|
+ {
|
|
138
|
+ lseek(fd, t->session->file.off_min.off,
|
|
139
|
+ SEEK_SET);
|
|
140
|
+ }
|
113
|
141
|
}
|
114
|
|
- while(1)
|
|
142
|
+ else
|
115
|
143
|
{
|
116
|
|
- r = read(fd, buf, sizeof(buf));
|
117
|
|
- if(r == -1)
|
|
144
|
+ if(t->flag & TTAIL_FLAG_DATE_MAX)
|
118
|
145
|
{
|
119
|
|
- perror("unable to read file");
|
120
|
|
- return;
|
|
146
|
+ fseek(t->logfile[i], 0, SEEK_SET);
|
121
|
147
|
}
|
122
|
|
- else if(r == 0)
|
|
148
|
+ else
|
123
|
149
|
{
|
124
|
|
- break;
|
|
150
|
+ lseek(fd, 0, SEEK_SET);
|
125
|
151
|
}
|
126
|
|
- r = write(out, buf, r);
|
127
|
|
- if(r == -1 || r == 0)
|
|
152
|
+ }
|
|
153
|
+ cur_off = 0;
|
|
154
|
+ while(1)
|
|
155
|
+ {
|
|
156
|
+ if(t->flag & TTAIL_FLAG_DATE_MAX)
|
128
|
157
|
{
|
129
|
|
- perror("Unable to write result");
|
130
|
|
- return;
|
|
158
|
+ if(i >= t->session->file.off_max.id &&
|
|
159
|
+ cur_off >= t->session->file.off_max.off)
|
|
160
|
+ {
|
|
161
|
+ return;
|
|
162
|
+ }
|
|
163
|
+ if((r = ttail_getline(t, i)) < 0)
|
|
164
|
+ {
|
|
165
|
+ break;
|
|
166
|
+ }
|
|
167
|
+ cur_off += r;
|
|
168
|
+ write(out, ttail_getline_buf(t),
|
|
169
|
+ strlen(ttail_getline_buf(t)));
|
|
170
|
+ }
|
|
171
|
+ else
|
|
172
|
+ {
|
|
173
|
+ r = read(fd, buf, sizeof(buf));
|
|
174
|
+ if(r == -1)
|
|
175
|
+ {
|
|
176
|
+ perror("unable to read file");
|
|
177
|
+ return;
|
|
178
|
+ }
|
|
179
|
+ else if(r == 0)
|
|
180
|
+ {
|
|
181
|
+ break;
|
|
182
|
+ }
|
|
183
|
+ cur_off += r;
|
|
184
|
+ r = write(out, buf, r);
|
|
185
|
+ if(r == -1 || r == 0)
|
|
186
|
+ {
|
|
187
|
+ perror("Unable to write result");
|
|
188
|
+ return;
|
|
189
|
+ }
|
131
|
190
|
}
|
132
|
191
|
}
|
133
|
192
|
}
|
134
|
193
|
}
|
135
|
194
|
|
136
|
195
|
int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
|
137
|
|
- const struct tm** ftm)
|
|
196
|
+ const struct tm** ftm, short min)
|
138
|
197
|
{
|
139
|
|
- int cmin, cmax;
|
|
198
|
+ int cmin, cmax, ret;
|
140
|
199
|
size_t valid;
|
141
|
|
- off_t *off;
|
|
200
|
+ int cmpres;
|
|
201
|
+ off_t tmpoff;
|
|
202
|
+ ttail_files_off_t *files_off;
|
142
|
203
|
size_t *id;
|
143
|
|
- off = &(t->session->file.off);
|
144
|
|
- id = &(t->session->file.id);
|
|
204
|
+ off_t *off;
|
|
205
|
+
|
|
206
|
+ files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
|
|
207
|
+ id = &(files_off->id),
|
|
208
|
+ off = &(files_off->off);
|
|
209
|
+
|
145
|
210
|
*id = *off = 0;
|
146
|
211
|
valid = 0;
|
147
|
212
|
if(ttail_tm_cmp(&(ftm[0][0]), in) > 0)
|
|
@@ -185,7 +250,40 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
|
185
|
250
|
cmax > 0)
|
186
|
251
|
{
|
187
|
252
|
/* somewhere in current file */
|
188
|
|
- break;
|
|
253
|
+ ret = _ttail_search_file_binary_search(t, in, ftm, min);
|
|
254
|
+ if(ret)
|
|
255
|
+ {
|
|
256
|
+ *id = 0;
|
|
257
|
+ return ret;
|
|
258
|
+ }
|
|
259
|
+ /* searching for equivalent */
|
|
260
|
+ tmpoff = *off;
|
|
261
|
+ while(1)
|
|
262
|
+ {
|
|
263
|
+ if(fseek(t->logfile[*id], tmpoff, SEEK_SET) < 0)
|
|
264
|
+ {
|
|
265
|
+ return -1;
|
|
266
|
+ }
|
|
267
|
+ tmpoff = min?
|
|
268
|
+ _ttail_file_start_line(t->logfile[*id]):
|
|
269
|
+ _ttail_file_next_line(t->logfile[*id]);
|
|
270
|
+ if(tmpoff < 0)
|
|
271
|
+ {
|
|
272
|
+ break;
|
|
273
|
+ }
|
|
274
|
+ cmpres=0;
|
|
275
|
+ ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
|
|
276
|
+ if((min && cmpres < 0) || (!min && cmpres > 0))
|
|
277
|
+ {
|
|
278
|
+ break;
|
|
279
|
+ }
|
|
280
|
+ *off = tmpoff;
|
|
281
|
+ if(min)
|
|
282
|
+ {
|
|
283
|
+ tmpoff--;
|
|
284
|
+ }
|
|
285
|
+ }
|
|
286
|
+ return 0;
|
189
|
287
|
}
|
190
|
288
|
(*id)++;
|
191
|
289
|
}
|
|
@@ -199,14 +297,17 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
|
199
|
297
|
}
|
200
|
298
|
|
201
|
299
|
inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
|
202
|
|
- const struct tm** ftm)
|
|
300
|
+ const struct tm** ftm, short min)
|
203
|
301
|
{
|
204
|
|
- off_t cur, sz, d, prev;
|
|
302
|
+ off_t cur, sz, d, prev, tmp;
|
205
|
303
|
int ret, cmpres;
|
206
|
|
- off_t *off;
|
|
304
|
+ ttail_files_off_t *files_off;
|
207
|
305
|
size_t id;
|
208
|
|
- off = &(t->session->file.off);
|
209
|
|
- id = t->session->file.id;
|
|
306
|
+ off_t *off;
|
|
307
|
+
|
|
308
|
+ files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
|
|
309
|
+ id = files_off->id,
|
|
310
|
+ off = &(files_off->off);
|
210
|
311
|
|
211
|
312
|
sz = t->session->file.file_sz[id];
|
212
|
313
|
d = cur = sz / 2;
|
|
@@ -214,7 +315,19 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
|
214
|
315
|
cmpres = 0;
|
215
|
316
|
while(1)
|
216
|
317
|
{
|
217
|
|
- cur = _ttail_file_next_line(t->logfile[id]);
|
|
318
|
+ if(fseek(t->logfile[id], cur, SEEK_SET) < 0)
|
|
319
|
+ {
|
|
320
|
+ perror("Unable to move in the file");
|
|
321
|
+ return -1;
|
|
322
|
+ }
|
|
323
|
+ if(cur > prev)
|
|
324
|
+ {
|
|
325
|
+ cur = _ttail_file_next_line(t->logfile[id]);
|
|
326
|
+ }
|
|
327
|
+ else
|
|
328
|
+ {
|
|
329
|
+ cur = _ttail_file_start_line(t->logfile[id]);
|
|
330
|
+ }
|
218
|
331
|
if(cur < -1)
|
219
|
332
|
{
|
220
|
333
|
cur = _ttail_file_start_line(t->logfile[id]);
|
|
@@ -241,10 +354,11 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
|
241
|
354
|
}
|
242
|
355
|
else if(cmpres < 0)
|
243
|
356
|
{
|
|
357
|
+ tmp = _ttail_file_next_line(t->logfile[id]);
|
244
|
358
|
ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
|
245
|
359
|
if(cmpres >=0)
|
246
|
360
|
{
|
247
|
|
- *off = cur;
|
|
361
|
+ *off = tmp;
|
248
|
362
|
break;
|
249
|
363
|
}
|
250
|
364
|
d/=2;
|
|
@@ -255,6 +369,10 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
|
255
|
369
|
{
|
256
|
370
|
d/=2;
|
257
|
371
|
cur -= d;
|
|
372
|
+ if(cur < 0)
|
|
373
|
+ {
|
|
374
|
+ cur = 0;
|
|
375
|
+ }
|
258
|
376
|
}
|
259
|
377
|
}
|
260
|
378
|
return 0;
|
|
@@ -616,8 +734,12 @@ inline int _ttail_file_cur_cmp(ttail_t* t, size_t id, const struct tm* tm ,
|
616
|
734
|
{
|
617
|
735
|
int ret;
|
618
|
736
|
struct tm ctm;
|
|
737
|
+ if(ttail_getline(t, id) < 0)
|
|
738
|
+ {
|
|
739
|
+ return -1;
|
|
740
|
+ }
|
619
|
741
|
ret = ttail_logline2date(t, ttail_getline_buf(t), &ctm);
|
620
|
|
- if(ret < 0)
|
|
742
|
+ if(ret)
|
621
|
743
|
{
|
622
|
744
|
return -1;
|
623
|
745
|
}
|