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

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