Просмотр исходного кода

Bugfix + tests for ttail_search_files functions

Yann Weber 7 лет назад
Родитель
Сommit
5e5f730815
3 измененных файлов: 59 добавлений и 16 удалений
  1. 1
    0
      src/include/ttail_search_files.h
  2. 27
    16
      src/ttail_search_files.c
  3. 31
    0
      tests/ttail_search_check.c

+ 1
- 0
src/include/ttail_search_files.h Просмотреть файл

122
  *Set f pos to line begining and return the position
122
  *Set f pos to line begining and return the position
123
  *@param FILE* f 
123
  *@param FILE* f 
124
  *@return -1 on error else return the next line position
124
  *@return -1 on error else return the next line position
125
+ *@todo recheck
125
  */
126
  */
126
 inline long _ttail_file_start_line(FILE*);
127
 inline long _ttail_file_start_line(FILE*);
127
 
128
 

+ 27
- 16
src/ttail_search_files.c Просмотреть файл

60
 	}
60
 	}
61
 	/* TODO begining binary search of date_min */
61
 	/* TODO begining binary search of date_min */
62
 	ret = _ttail_search_files_binary_search(t, tm, (const struct tm**)ftm);
62
 	ret = _ttail_search_files_binary_search(t, tm, (const struct tm**)ftm);
63
-
64
-	return 0;
63
+	for(i=0; i<t->logfile_sz; i++)
64
+	{
65
+		if(ftm) { free(ftm[i]); }
66
+	}
67
+	free(ftm);
68
+	return ret;
65
 	goto _ttail_search_closest_files_err;
69
 	goto _ttail_search_closest_files_err;
66
 
70
 
67
 	_ttail_search_closest_files_err:
71
 	_ttail_search_closest_files_err:
111
 			/* found at the begining of the file */
115
 			/* found at the begining of the file */
112
 			return 0;
116
 			return 0;
113
 		}
117
 		}
114
-		else if (!cmax)
118
+		else if (cmax <= 0)
115
 		{
119
 		{
116
 			/* found at EOF */
120
 			/* found at EOF */
117
 			*off = _ttail_from_search_from_end(t, *id, in);
121
 			*off = _ttail_from_search_from_end(t, *id, in);
490
 	FILE *f;
494
 	FILE *f;
491
 	struct tm curtm;
495
 	struct tm curtm;
492
 	off_t last;
496
 	off_t last;
493
-	int ret;
497
+	int ret, cmpret;
498
+	off_t result;
494
 
499
 
500
+	result = -1;
495
 	f = t->logfile[id];
501
 	f = t->logfile[id];
496
 	if(fseek(f, -1, SEEK_END) < 0)
502
 	if(fseek(f, -1, SEEK_END) < 0)
497
 	{
503
 	{
502
 		last = _ttail_file_start_line(f);
508
 		last = _ttail_file_start_line(f);
503
 		if(last < 0)
509
 		if(last < 0)
504
 		{
510
 		{
505
-			goto _ttail_from_search_from_end_err;
511
+			break;
506
 		}
512
 		}
507
 		if(ttail_getline(t, id) < 0)
513
 		if(ttail_getline(t, id) < 0)
508
 		{
514
 		{
509
-			goto _ttail_from_search_from_end_err;
515
+			break;
510
 		}
516
 		}
511
 		ret = ttail_logline2date(t, ttail_getline_buf(t), &curtm);
517
 		ret = ttail_logline2date(t, ttail_getline_buf(t), &curtm);
512
 		if(ret < 0)
518
 		if(ret < 0)
513
 		{
519
 		{
514
-			goto _ttail_from_search_from_end_err;
520
+			break;
515
 		}
521
 		}
516
-		if(!ret && !ttail_tm_cmp(&curtm, tm))
522
+		cmpret = ttail_tm_cmp(&curtm, tm);
523
+		if(!ret)
517
 		{
524
 		{
518
-			/* found */
519
-			break;
525
+			if(cmpret >= 0)
526
+			{
527
+				/* found but continue to search the first one*/
528
+				result = last;
529
+			}
530
+			else
531
+			{
532
+				break;
533
+			}
520
 		}
534
 		}
521
 		if(last == 0)
535
 		if(last == 0)
522
 		{
536
 		{
523
 			/* considere the begining of the file as the answer */
537
 			/* considere the begining of the file as the answer */
524
 			return 0;
538
 			return 0;
525
 		}
539
 		}
526
-		if(fseek(f, last-1, SEEK_CUR))
540
+		if(fseek(f, last-1, SEEK_SET))
527
 		{
541
 		{
528
-			goto _ttail_from_search_from_end_err;
542
+			return -1;
529
 		}
543
 		}
530
 	}
544
 	}
531
-	return last;
532
-	
533
-	_ttail_from_search_from_end_err:
534
-	return -1;
545
+	return result;
535
 }
546
 }
536
 
547
 
537
 inline int _ttail_file_off_cmp(ttail_t* t, size_t id, off_t off,
548
 inline int _ttail_file_off_cmp(ttail_t* t, size_t id, off_t off,

+ 31
- 0
tests/ttail_search_check.c Просмотреть файл

320
 	ttail->prefix_sz = 0;
320
 	ttail->prefix_sz = 0;
321
 	ttail_set_fmt(ttail, "%B%n%d %H:%M");
321
 	ttail_set_fmt(ttail, "%B%n%d %H:%M");
322
 
322
 
323
+	ret = _ttail_search_closest_files(ttail, &tm);
324
+	ck_assert_int_eq(ret, 1);
325
+	ck_assert(ttail->session->file.id == 0);
326
+	ck_assert(ttail->session->file.off == 0);
327
+}
328
+END_TEST
329
+
330
+START_TEST (test_search_files2)
331
+{
332
+	int ret;
333
+	size_t i;
334
+	struct tm tm;
335
+	memset(&tm, 0, sizeof(tm));
336
+	for(i=1;i<ttail->logfile_sz;i++)
337
+	{
338
+		fclose(ttail->logfile[i]);
339
+		ttail->logfile[i] = NULL;
340
+	}
341
+	ttail->flag |= TTAIL_FLAG_PREFIX;
342
+	ttail->prefix_sz = 0;
343
+	ttail_set_fmt(ttail, "%B%n%d %H:%M");
344
+
345
+	tm.tm_year = -1;
346
+	tm.tm_mon = 2;
347
+	tm.tm_mday = 6;
348
+	tm.tm_hour = 0;
349
+	tm.tm_min = 29;
350
+	tm.tm_sec = -1;
351
+	
323
 	ret = _ttail_search_closest_files(ttail, &tm);
352
 	ret = _ttail_search_closest_files(ttail, &tm);
324
 	ck_assert_int_eq(ret, 0);
353
 	ck_assert_int_eq(ret, 0);
325
 	ck_assert(ttail->session->file.id == 0);
354
 	ck_assert(ttail->session->file.id == 0);
355
+	ck_assert_int_eq(ttail->session->file.off, 221);
326
 }
356
 }
327
 END_TEST
357
 END_TEST
328
 
358
 
376
 	tcase_add_checked_fixture(tc_search_files,
406
 	tcase_add_checked_fixture(tc_search_files,
377
 		setup_closest_fileinit, teardown_closest_fileinit);
407
 		setup_closest_fileinit, teardown_closest_fileinit);
378
 	tcase_add_test(tc_search_files, test_search_files1);
408
 	tcase_add_test(tc_search_files, test_search_files1);
409
+	tcase_add_test(tc_search_files, test_search_files2);
379
 	
410
 	
380
 
411
 
381
 	suite_add_tcase(s, tc_search_closest_fileinit);
412
 	suite_add_tcase(s, tc_search_closest_fileinit);

Загрузка…
Отмена
Сохранить