Browse Source

Bugfix + tests for ttail_search_files functions

Yann Weber 7 years ago
parent
commit
5e5f730815
3 changed files with 59 additions and 16 deletions
  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 View File

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

+ 27
- 16
src/ttail_search_files.c View File

@@ -60,8 +60,12 @@ File sorting not implemented yet\n");
60 60
 	}
61 61
 	/* TODO begining binary search of date_min */
62 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 69
 	goto _ttail_search_closest_files_err;
66 70
 
67 71
 	_ttail_search_closest_files_err:
@@ -111,7 +115,7 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
111 115
 			/* found at the begining of the file */
112 116
 			return 0;
113 117
 		}
114
-		else if (!cmax)
118
+		else if (cmax <= 0)
115 119
 		{
116 120
 			/* found at EOF */
117 121
 			*off = _ttail_from_search_from_end(t, *id, in);
@@ -490,8 +494,10 @@ inline off_t _ttail_from_search_from_end(ttail_t* t , size_t id ,
490 494
 	FILE *f;
491 495
 	struct tm curtm;
492 496
 	off_t last;
493
-	int ret;
497
+	int ret, cmpret;
498
+	off_t result;
494 499
 
500
+	result = -1;
495 501
 	f = t->logfile[id];
496 502
 	if(fseek(f, -1, SEEK_END) < 0)
497 503
 	{
@@ -502,36 +508,41 @@ inline off_t _ttail_from_search_from_end(ttail_t* t , size_t id ,
502 508
 		last = _ttail_file_start_line(f);
503 509
 		if(last < 0)
504 510
 		{
505
-			goto _ttail_from_search_from_end_err;
511
+			break;
506 512
 		}
507 513
 		if(ttail_getline(t, id) < 0)
508 514
 		{
509
-			goto _ttail_from_search_from_end_err;
515
+			break;
510 516
 		}
511 517
 		ret = ttail_logline2date(t, ttail_getline_buf(t), &curtm);
512 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 535
 		if(last == 0)
522 536
 		{
523 537
 			/* considere the begining of the file as the answer */
524 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 548
 inline int _ttail_file_off_cmp(ttail_t* t, size_t id, off_t off,

+ 31
- 0
tests/ttail_search_check.c View File

@@ -320,9 +320,39 @@ START_TEST (test_search_files1)
320 320
 	ttail->prefix_sz = 0;
321 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 352
 	ret = _ttail_search_closest_files(ttail, &tm);
324 353
 	ck_assert_int_eq(ret, 0);
325 354
 	ck_assert(ttail->session->file.id == 0);
355
+	ck_assert_int_eq(ttail->session->file.off, 221);
326 356
 }
327 357
 END_TEST
328 358
 
@@ -376,6 +406,7 @@ ttail_logline_closest_files_init() checks");
376 406
 	tcase_add_checked_fixture(tc_search_files,
377 407
 		setup_closest_fileinit, teardown_closest_fileinit);
378 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 412
 	suite_add_tcase(s, tc_search_closest_fileinit);

Loading…
Cancel
Save