timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttail_search_files.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * Copyright 2017 Yann Weber
  3. *
  4. * This file is part of Ttail.
  5. *
  6. * Ttail is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * Ttail is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ttail. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "ttail_search_files.h"
  20. int _ttail_search_closest_files(ttail_t* t)
  21. {
  22. int ret;
  23. size_t i, prev;
  24. struct tm **ftm; /* t->logfile_sz len of struct tm[2] */
  25. char prev_found;
  26. /* Storing min & max of each files in ftm and checking that files are
  27. * sorted well */
  28. ftm = malloc(sizeof(struct tm*) * t->logfile_sz);
  29. if(!ftm)
  30. {
  31. perror("Unable to allocate memory");
  32. goto _ttail_search_closest_files_alloc_err;
  33. }
  34. ret = 0; /* to avoid may be used uninitialized error */
  35. prev_found = 0;
  36. prev = 0;
  37. for(i=0; i<t->logfile_sz; i++)
  38. {
  39. if(!t->logfile[i])
  40. {
  41. ftm[i] = NULL;
  42. continue;
  43. }
  44. ftm[i] = malloc(sizeof(struct tm)*2);
  45. if(!ftm[i])
  46. {
  47. perror("Unable to allocate memory for time");
  48. goto _ttail_search_closest_files_alloc_loop_err;
  49. }
  50. ret = _ttail_file_minmax(t, i, ftm[i]);
  51. if(ret < 0)
  52. {
  53. fprintf(stderr, "Minmax error\n");
  54. goto _ttail_search_closest_files_loop_err;
  55. }
  56. else if (ret == 1)
  57. {
  58. fprintf(stderr, "Warning : unable to find a valid date \
  59. in '%s'\n", t->logfile_name[i]);
  60. free(ftm[i]);
  61. ftm[i] = NULL;
  62. fclose(t->logfile[i]);
  63. t->logfile[i] = NULL;
  64. continue;
  65. }
  66. if(i && prev_found &&
  67. ttail_tm_cmp(&(ftm[prev][1]), &(ftm[i][0])) > 0)
  68. {
  69. /* files are not sorted */
  70. fprintf(stderr, "Files do not seems to be sorted. \
  71. File sorting not implemented yet\n");
  72. goto _ttail_search_closest_files_loop_err;
  73. }
  74. prev_found = 1;
  75. prev = i;
  76. }
  77. if(t->flag & TTAIL_FLAG_DATE_MIN)
  78. {
  79. ret = _ttail_search_files_binary_search(t, &(t->date_min),
  80. (const struct tm**)ftm, 1);
  81. if(ret)
  82. {
  83. if(t->verbose > 2)
  84. {
  85. fprintf(stderr, "Error while looking for \
  86. date-max\n");
  87. }
  88. goto _ttail_search_closest_files_err;
  89. }
  90. }
  91. for(i=0; i<t->logfile_sz; i++)
  92. {
  93. if(!t->logfile[i])
  94. {
  95. continue;
  96. }
  97. if(fseek(t->logfile[i], 0, SEEK_SET) < 0)
  98. {
  99. perror("Error setting position in file");
  100. goto _ttail_search_closest_files_err;
  101. }
  102. }
  103. if(t->flag & TTAIL_FLAG_DATE_MAX)
  104. {
  105. ret = _ttail_search_files_binary_search(t, &(t->date_max),
  106. (const struct tm**)ftm, 0);
  107. if(ret)
  108. {
  109. if(t->verbose > 2)
  110. {
  111. fprintf(stderr, "Error while looking for \
  112. date-max\n");
  113. }
  114. goto _ttail_search_closest_files_err;
  115. }
  116. t->session->file.off_max.off++;
  117. }
  118. for(i=0; i<t->logfile_sz; i++)
  119. {
  120. if(ftm) { free(ftm[i]); }
  121. }
  122. free(ftm);
  123. return ret;
  124. goto _ttail_search_closest_files_err;
  125. _ttail_search_closest_files_err:
  126. i = t->logfile_sz;
  127. do
  128. {
  129. _ttail_search_closest_files_alloc_loop_err:
  130. i--;
  131. _ttail_search_closest_files_loop_err:
  132. if(ftm[i])
  133. {
  134. free(ftm[i]);
  135. }
  136. }while(i);
  137. free(ftm);
  138. _ttail_search_closest_files_alloc_err:
  139. return -1;
  140. }
  141. int _ttail_search_fmtdetect_provider_files(ttail_t *t, int *try_left,
  142. size_t* line_sz, char*** lines_ptr)
  143. {
  144. return 1;
  145. }
  146. void _ttail_search_print_files(ttail_t* t, int out)
  147. {
  148. size_t i;
  149. int fd;
  150. char buf[8192];
  151. int r;
  152. off_t cur_off;
  153. /* if no date_min t->session->file.id == 0 */
  154. for(i=t->session->file.off_min.id; i<t->logfile_sz; i++)
  155. {
  156. if(!t->logfile[i])
  157. {
  158. continue;
  159. }
  160. fd = fileno(t->logfile[i]);
  161. if(t->flag & TTAIL_FLAG_DATE_MIN &&
  162. i==t->session->file.off_min.id)
  163. {
  164. if(t->flag & TTAIL_FLAG_DATE_MAX)
  165. {
  166. fseek(t->logfile[i],
  167. t->session->file.off_min.off,
  168. SEEK_SET);
  169. }
  170. else
  171. {
  172. lseek(fd, t->session->file.off_min.off,
  173. SEEK_SET);
  174. }
  175. }
  176. else
  177. {
  178. if(t->flag & TTAIL_FLAG_DATE_MAX)
  179. {
  180. fseek(t->logfile[i], 0, SEEK_SET);
  181. }
  182. else
  183. {
  184. lseek(fd, 0, SEEK_SET);
  185. }
  186. }
  187. cur_off = 0;
  188. while(1)
  189. {
  190. if(t->flag & TTAIL_FLAG_DATE_MAX)
  191. {
  192. if(i >= t->session->file.off_max.id &&
  193. cur_off >= t->session->file.off_max.off)
  194. {
  195. return;
  196. }
  197. if((r = ttail_file_getline(t, i)) < 0)
  198. {
  199. break;
  200. }
  201. cur_off += r;
  202. write(out, ttail_file_getline_buf(t),
  203. strlen(ttail_file_getline_buf(t)));
  204. }
  205. else
  206. {
  207. r = read(fd, buf, sizeof(buf));
  208. if(r == -1)
  209. {
  210. perror("unable to read file");
  211. return;
  212. }
  213. else if(r == 0)
  214. {
  215. break;
  216. }
  217. cur_off += r;
  218. r = write(out, buf, r);
  219. if(r == -1 || r == 0)
  220. {
  221. perror("Unable to write result");
  222. return;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
  229. const struct tm** ftm, short min)
  230. {
  231. int cmin, cmax, ret;
  232. size_t valid;
  233. int cmpres;
  234. off_t tmpoff;
  235. ttail_files_off_t *files_off;
  236. size_t *id;
  237. off_t *off;
  238. files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
  239. id = &(files_off->id),
  240. off = &(files_off->off);
  241. *id = *off = 0;
  242. valid = 0;
  243. while(*id < t->logfile_sz)
  244. {
  245. if(!ftm[*id])
  246. {
  247. (*id)++;
  248. continue;
  249. }
  250. valid++;
  251. cmin = ttail_tm_cmp(&(ftm[*id][0]), in);
  252. cmax = ttail_tm_cmp(&(ftm[*id][1]), in);
  253. if(!cmin)
  254. {
  255. /* found at the begining of the file */
  256. return 0;
  257. }
  258. else if (cmax == 0)
  259. {
  260. /* found at EOF */
  261. if(min)
  262. {
  263. *off = _ttail_file_search_from_end(t, *id, in);
  264. if(*off < 0)
  265. {
  266. *off = 0;
  267. return -1;
  268. }
  269. }
  270. else
  271. {
  272. *off = t->session->file.file_sz[*id];
  273. }
  274. return 0;
  275. }
  276. else if(cmin > 0)
  277. {
  278. /* found at start of file */
  279. off = 0;
  280. return 0;
  281. }
  282. else if(cmax > 0)
  283. {
  284. /* somewhere in current file */
  285. ret = _ttail_search_file_binary_search(t, in, ftm, min);
  286. if(ret)
  287. {
  288. if(t->verbose > 2)
  289. {
  290. fprintf(stderr, "Error while \
  291. running binary search algorithm in '%s'\n", t->logfile_name[*id]);
  292. }
  293. *id = 0;
  294. return ret;
  295. }
  296. /* searching for equivalent */
  297. tmpoff = *off;
  298. while(1)
  299. {
  300. if(fseek(t->logfile[*id], tmpoff, SEEK_SET) < 0)
  301. {
  302. return -1;
  303. }
  304. tmpoff = min?
  305. _ttail_file_start_line(t, *id):
  306. _ttail_file_next_line(t, *id);
  307. if(tmpoff < 0)
  308. {
  309. break;
  310. }
  311. cmpres=0;
  312. ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
  313. if((min && cmpres < 0) || (!min && cmpres > 0))
  314. {
  315. break;
  316. }
  317. *off = tmpoff;
  318. if(min)
  319. {
  320. tmpoff--;
  321. }
  322. }
  323. return 0;
  324. }
  325. else if(*id == t->logfile_sz - 1 && cmax < 0)
  326. {
  327. *off = t->session->file.file_sz[*id];
  328. return 0;
  329. }
  330. (*id)++;
  331. }
  332. if(!valid)
  333. {
  334. fprintf(stderr, "No files to scan\n");
  335. return 0;
  336. }
  337. /* the answer is somewhere in *id file */
  338. *off = _ttail_file_search_from_end(t, *id, in);
  339. return 0;
  340. }
  341. inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
  342. const struct tm** ftm, short min)
  343. {
  344. off_t cur, sz, d, prev, tmp;
  345. int ret, cmpres;
  346. ttail_files_off_t *files_off;
  347. size_t id;
  348. off_t *off;
  349. files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
  350. id = files_off->id,
  351. off = &(files_off->off);
  352. sz = t->session->file.file_sz[id];
  353. d = cur = sz / 2;
  354. prev = 0;
  355. cmpres = 0;
  356. while(1)
  357. {
  358. if(fseek(t->logfile[id], cur, SEEK_SET) < 0)
  359. {
  360. perror("Unable to move in the file");
  361. return -1;
  362. }
  363. if(cur > prev)
  364. {
  365. cur = _ttail_file_next_line(t, id);
  366. if(cur < 0)
  367. {
  368. /* not sure errno is really set */
  369. perror("Error searching previous line");
  370. return -1;
  371. }
  372. }
  373. else
  374. {
  375. cur = _ttail_file_start_line(t, id);
  376. if(cur < 0)
  377. {
  378. /* not sure errno is really set */
  379. perror("Error searching previous line");
  380. return -1;
  381. }
  382. }
  383. if(cur == prev)
  384. {
  385. *off = cur;
  386. return 0;
  387. }
  388. prev = cur;
  389. ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
  390. if(ret < 0)
  391. {
  392. if(t->verbose > 2)
  393. {
  394. fprintf(stderr, "Error comparing a logline \
  395. to a date directly from a file\n");
  396. }
  397. return -1;
  398. }
  399. else if(cmpres == 0)
  400. {
  401. *off = cur;
  402. break;
  403. }
  404. else if(cmpres < 0)
  405. {
  406. tmp = _ttail_file_next_line(t,id);
  407. ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
  408. if(cmpres >=0)
  409. {
  410. *off = tmp;
  411. break;
  412. }
  413. d/=2;
  414. cur += d;
  415. cur %= t->session->file.file_sz[id];
  416. }
  417. else
  418. {
  419. d/=2;
  420. cur -= d;
  421. if(cur < 0)
  422. {
  423. cur = 0;
  424. }
  425. }
  426. }
  427. return 0;
  428. }
  429. int ttail_search_files_init(ttail_t* t)
  430. {
  431. struct stat stat;
  432. FILE *fp;
  433. int fd;
  434. size_t i;
  435. off_t *file_sz;
  436. t->session = (ttail_search_t*)malloc(sizeof(ttail_search_file_t));
  437. if(!t->session)
  438. {
  439. perror("Unable to allocate memory for search session");
  440. goto _ttail_search_closest_files_alloc_session_err;
  441. }
  442. memset(t->session, 0, sizeof(ttail_search_file_t));
  443. file_sz = malloc(sizeof(off_t)*t->logfile_sz);
  444. if(!file_sz)
  445. {
  446. perror("Unable to allocate memory for file sizes");
  447. goto _ttail_search_closest_files_alloc_err;
  448. }
  449. t->session->file.file_sz = file_sz;
  450. #ifdef HUGEFILE
  451. t->session->file.sz_div = 0;
  452. #endif
  453. for(i=0;i<t->logfile_sz;i++)
  454. {
  455. fp = t->logfile[i];
  456. if(!fp)
  457. {
  458. file_sz[i] = 0;
  459. continue;
  460. }
  461. if((fd = fileno(fp)) < 0)
  462. {
  463. perror("Unable to get fp");
  464. goto _ttail_search_closest_files_err;
  465. }
  466. if(fstat(fileno(fp), &stat))
  467. {
  468. perror("Unable to get file size");
  469. goto _ttail_search_closest_files_err;
  470. }
  471. file_sz[i] = stat.st_size;
  472. }
  473. /* we got all real size, determining if we need a divisor */
  474. /*
  475. * not implemented
  476. */
  477. if(_ttail_search_closest_files_set_fsizes(t))
  478. {
  479. goto _ttail_search_closest_files_err;
  480. }
  481. t->session->file.buf_sz = 128;
  482. t->session->file.buf = malloc(t->session->file.buf_sz);
  483. if(!t->session->file.buf)
  484. {
  485. goto _ttail_search_closest_files_err;
  486. }
  487. t->fmtdetect_provider = _ttail_search_fmtdetect_provider_files;
  488. return 0;
  489. _ttail_search_closest_files_err:
  490. free(file_sz);
  491. t->session->file.file_sz = NULL;
  492. _ttail_search_closest_files_alloc_err:
  493. free(t->session);
  494. _ttail_search_closest_files_alloc_session_err:
  495. return -1;
  496. }
  497. int _ttail_search_closest_files_set_fsizes(ttail_t* t)
  498. {
  499. size_t i;
  500. off_t *vfile, *vsz;
  501. vfile = malloc(sizeof(off_t)*t->logfile_sz);
  502. if(!vfile)
  503. {
  504. perror("Unable to allocate memory for file size sum");
  505. return -1;
  506. }
  507. t->session->file.vfile = vfile;
  508. vsz = &(t->session->file.vsz);
  509. *vsz = 0;
  510. for(i=0; i<t->logfile_sz;i++)
  511. {
  512. vfile[i] = *vsz;
  513. #ifdef HUGEFILE
  514. *vsz += t->session->file.file_sz[i] >> t->session->file.sz_div;
  515. #else
  516. *vsz += t->session->file.file_sz[i];
  517. #endif
  518. }
  519. t->session->file.vpos = 0;
  520. return 0;
  521. }
  522. inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
  523. {
  524. FILE *fp;
  525. long cur;
  526. memset(tm, 0, sizeof(struct tm)*2);
  527. fp = t->logfile[id];
  528. if(!fp)
  529. {
  530. return 1;
  531. }
  532. if(fseek(fp, 0, SEEK_SET) < 0)
  533. {
  534. perror("Unable to manipulate fp");
  535. return -1;
  536. }
  537. while(1)
  538. {
  539. if(ttail_file_getline(t, id) < 0)
  540. {
  541. return 1;
  542. }
  543. if(!ttail_logline2date(t, ttail_file_getline_buf(t), tm))
  544. {
  545. break;
  546. }
  547. }
  548. if(fseek(fp, -1, SEEK_END) < 0)
  549. {
  550. perror("Unable to manipulate fp");
  551. return -1;
  552. }
  553. while(1)
  554. {
  555. if((cur = _ttail_file_start_line(t, id)) < 0)
  556. {
  557. return -1;
  558. }
  559. if(ttail_file_getline(t, id) < 0)
  560. {
  561. return 1;
  562. }
  563. if(!ttail_logline2date(t, ttail_file_getline_buf(t), tm+1))
  564. {
  565. break;
  566. }
  567. if(!cur)
  568. {
  569. return 1;
  570. }
  571. if(fseek(fp, cur-1, SEEK_SET) < 0)
  572. {
  573. perror("Unable to manipulate fp");
  574. return -1;
  575. }
  576. }
  577. return 0;
  578. }
  579. int _ttail_file_reopen(ttail_t* t, size_t id)
  580. {
  581. if(t->logfile[id])
  582. {
  583. fclose(t->logfile[id]);
  584. }
  585. t->logfile[id] = fopen(t->logfile_name[id], "r");
  586. if(!t->logfile[id] && t->verbose > 2)
  587. {
  588. fprintf(stderr, "Unable to reopen '%s'\n",
  589. t->logfile_name[id]);
  590. }
  591. return t->logfile[id]?0:-1;
  592. }
  593. inline long _ttail_file_next_line(ttail_t *t, size_t id)
  594. {
  595. FILE *f;
  596. ssize_t s;
  597. size_t r;
  598. char *buff;
  599. long res;
  600. int c;
  601. f = t->logfile[id];
  602. r=0;
  603. buff = NULL;
  604. s = getline(&buff, &r, f);
  605. if(s < 0)
  606. {
  607. goto _ttail_file_next_line_err;
  608. }
  609. while(1)
  610. {
  611. c = getc(f);
  612. if(c == EOF)
  613. {
  614. return 0;
  615. }
  616. else if(c!='\n')
  617. {
  618. if(fseek(f, -1, SEEK_CUR)<0)
  619. {
  620. perror("Unable to set position in file");
  621. goto _ttail_file_next_line_err;
  622. }
  623. break;
  624. }
  625. }
  626. res = ftell(f);
  627. free(buff);
  628. return res;
  629. _ttail_file_next_line_err:
  630. free(buff);
  631. return -1;
  632. }
  633. inline long _ttail_file_start_line(ttail_t *ttail, size_t id)
  634. {
  635. #define _STARTLN_BUFFLEN 32
  636. FILE *f;
  637. long res; /* function result */
  638. long read_beg, cur, last, start;
  639. int read_sz;
  640. int c;
  641. f = ttail->logfile[id];
  642. if((start = ftell(f)) < 0)
  643. {
  644. perror("Unable to get position in file");
  645. return -1;
  646. }
  647. res = 0;
  648. read_beg = start;
  649. while(!res && start)
  650. {
  651. if(fseek(f, read_beg, SEEK_SET) < 0)
  652. {
  653. perror("Unable to set position in file");
  654. return -1;
  655. }
  656. start = read_beg;
  657. read_sz = start <= _STARTLN_BUFFLEN?start:_STARTLN_BUFFLEN;
  658. read_beg = start - read_sz;
  659. if(fseek(f, read_beg, SEEK_SET) < 0)
  660. {
  661. perror("Unable to set position in file");
  662. return -1;
  663. }
  664. last = -1; /* last pos we saw a '\n' */
  665. cur = read_beg;
  666. while(cur <= start)
  667. {
  668. c = getc(f);
  669. if(c == EOF)
  670. {
  671. if(!res)
  672. {
  673. return 0;
  674. }
  675. break;
  676. }
  677. else if (c =='\n')
  678. {
  679. last = cur;
  680. }
  681. else if(last >= 0)
  682. {
  683. res = cur;
  684. last = -1;
  685. }
  686. cur++;
  687. }
  688. if(!read_beg)
  689. {
  690. break;
  691. }
  692. }
  693. if(fseek(f, res, SEEK_SET) < 0)
  694. {
  695. perror("Unable to set position in file");
  696. return -1;
  697. }
  698. return res;
  699. }
  700. inline off_t _ttail_file_search_from_end(ttail_t* t , size_t id ,
  701. const struct tm* tm)
  702. {
  703. FILE *f;
  704. struct tm curtm;
  705. off_t last;
  706. int ret, cmpret;
  707. off_t result;
  708. result = -1;
  709. f = t->logfile[id];
  710. if(fseek(f, -1, SEEK_END) < 0)
  711. {
  712. return -1;
  713. }
  714. while(1)
  715. {
  716. last = _ttail_file_start_line(t, id);
  717. if(last < 0)
  718. {
  719. break;
  720. }
  721. if(ttail_file_getline(t, id) < 0)
  722. {
  723. break;
  724. }
  725. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &curtm);
  726. if(ret < 0)
  727. {
  728. break;
  729. }
  730. cmpret = ttail_tm_cmp(&curtm, tm);
  731. if(!ret)
  732. {
  733. if(cmpret >= 0)
  734. {
  735. /* found but continue to search the first one*/
  736. result = last;
  737. }
  738. else
  739. {
  740. if(cmpret < 0 && result < 0)
  741. {
  742. result = last;
  743. }
  744. break;
  745. }
  746. }
  747. if(last == 0)
  748. {
  749. /* considere the begining of the file as the answer */
  750. return 0;
  751. }
  752. if(fseek(f, last-1, SEEK_SET))
  753. {
  754. return -1;
  755. }
  756. }
  757. return result;
  758. }
  759. inline int _ttail_file_off_cmp(ttail_t* t, size_t id, off_t off,
  760. const struct tm* tm, int *res)
  761. {
  762. if(fseek(t->logfile[id], off, SEEK_SET))
  763. {
  764. perror("Unable to set position in file");
  765. return -1;
  766. }
  767. if(ttail_file_getline(t, id) < 0)
  768. {
  769. perror("Unable to read a line from file");
  770. return -1;
  771. }
  772. return _ttail_file_cur_cmp(t, id, tm, res);
  773. }
  774. inline int _ttail_file_cur_cmp(ttail_t* t, size_t id, const struct tm* tm ,
  775. int* res)
  776. {
  777. int ret;
  778. struct tm ctm;
  779. if(ttail_file_getline(t, id) < 0)
  780. {
  781. return -1;
  782. }
  783. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &ctm);
  784. if(ret)
  785. {
  786. return -1;
  787. }
  788. else if(ret == 1)
  789. {
  790. return 1;
  791. }
  792. *res = ttail_tm_cmp(&ctm, tm);
  793. return 0;
  794. }
  795. void _ttail_search_file_free(ttail_t* t)
  796. {
  797. if(!t->session)
  798. {
  799. return;
  800. }
  801. if(t->session->file.buf)
  802. {
  803. free(t->session->file.buf);
  804. }
  805. if(t->session->file.file_sz)
  806. {
  807. free(t->session->file.file_sz);
  808. }
  809. if(t->session->file.vfile)
  810. {
  811. free(t->session->file.vfile);
  812. }
  813. free(t->session);
  814. }