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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. void _ttail_search_print_files(ttail_t* t, int out)
  142. {
  143. size_t i;
  144. int fd;
  145. char buf[8192];
  146. int r;
  147. off_t cur_off;
  148. /* if no date_min t->session->file.id == 0 */
  149. for(i=t->session->file.off_min.id; i<t->logfile_sz; i++)
  150. {
  151. if(!t->logfile[i])
  152. {
  153. continue;
  154. }
  155. fd = fileno(t->logfile[i]);
  156. if(t->flag & TTAIL_FLAG_DATE_MIN &&
  157. i==t->session->file.off_min.id)
  158. {
  159. if(t->flag & TTAIL_FLAG_DATE_MAX)
  160. {
  161. fseek(t->logfile[i],
  162. t->session->file.off_min.off,
  163. SEEK_SET);
  164. }
  165. else
  166. {
  167. lseek(fd, t->session->file.off_min.off,
  168. SEEK_SET);
  169. }
  170. }
  171. else
  172. {
  173. if(t->flag & TTAIL_FLAG_DATE_MAX)
  174. {
  175. fseek(t->logfile[i], 0, SEEK_SET);
  176. }
  177. else
  178. {
  179. lseek(fd, 0, SEEK_SET);
  180. }
  181. }
  182. cur_off = 0;
  183. while(1)
  184. {
  185. if(t->flag & TTAIL_FLAG_DATE_MAX)
  186. {
  187. if(i >= t->session->file.off_max.id &&
  188. cur_off >= t->session->file.off_max.off)
  189. {
  190. return;
  191. }
  192. if((r = ttail_file_getline(t, i)) < 0)
  193. {
  194. break;
  195. }
  196. cur_off += r;
  197. write(out, ttail_file_getline_buf(t),
  198. strlen(ttail_file_getline_buf(t)));
  199. }
  200. else
  201. {
  202. r = read(fd, buf, sizeof(buf));
  203. if(r == -1)
  204. {
  205. perror("unable to read file");
  206. return;
  207. }
  208. else if(r == 0)
  209. {
  210. break;
  211. }
  212. cur_off += r;
  213. r = write(out, buf, r);
  214. if(r == -1 || r == 0)
  215. {
  216. perror("Unable to write result");
  217. return;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
  224. const struct tm** ftm, short min)
  225. {
  226. int cmin, cmax, ret;
  227. size_t valid;
  228. int cmpres;
  229. off_t tmpoff;
  230. ttail_files_off_t *files_off;
  231. size_t *id;
  232. off_t *off;
  233. files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
  234. id = &(files_off->id),
  235. off = &(files_off->off);
  236. *id = *off = 0;
  237. valid = 0;
  238. while(*id < t->logfile_sz)
  239. {
  240. if(!ftm[*id])
  241. {
  242. (*id)++;
  243. continue;
  244. }
  245. valid++;
  246. cmin = ttail_tm_cmp(&(ftm[*id][0]), in);
  247. cmax = ttail_tm_cmp(&(ftm[*id][1]), in);
  248. if(!cmin)
  249. {
  250. /* found at the begining of the file */
  251. return 0;
  252. }
  253. else if (cmax == 0)
  254. {
  255. /* found at EOF */
  256. if(min)
  257. {
  258. *off = _ttail_file_search_from_end(t, *id, in);
  259. if(*off < 0)
  260. {
  261. *off = 0;
  262. return -1;
  263. }
  264. }
  265. else
  266. {
  267. *off = t->session->file.file_sz[*id];
  268. }
  269. return 0;
  270. }
  271. else if(cmin > 0)
  272. {
  273. /* found at start of file */
  274. off = 0;
  275. return 0;
  276. }
  277. else if(cmax > 0)
  278. {
  279. /* somewhere in current file */
  280. ret = _ttail_search_file_binary_search(t, in, ftm, min);
  281. if(ret)
  282. {
  283. if(t->verbose > 2)
  284. {
  285. fprintf(stderr, "Error while \
  286. running binary search algorithm in '%s'\n", t->logfile_name[*id]);
  287. }
  288. *id = 0;
  289. return ret;
  290. }
  291. /* searching for equivalent */
  292. tmpoff = *off;
  293. while(1)
  294. {
  295. if(fseek(t->logfile[*id], tmpoff, SEEK_SET) < 0)
  296. {
  297. return -1;
  298. }
  299. tmpoff = min?
  300. _ttail_file_start_line(t, *id):
  301. _ttail_file_next_line(t, *id);
  302. if(tmpoff < 0)
  303. {
  304. break;
  305. }
  306. cmpres=0;
  307. ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
  308. if((min && cmpres < 0) || (!min && cmpres > 0))
  309. {
  310. break;
  311. }
  312. *off = tmpoff;
  313. if(min)
  314. {
  315. tmpoff--;
  316. }
  317. }
  318. return 0;
  319. }
  320. else if(*id == t->logfile_sz - 1 && cmax < 0)
  321. {
  322. *off = t->session->file.file_sz[*id];
  323. return 0;
  324. }
  325. (*id)++;
  326. }
  327. if(!valid)
  328. {
  329. fprintf(stderr, "No files to scan\n");
  330. return 0;
  331. }
  332. /* the answer is somewhere in *id file */
  333. *off = _ttail_file_search_from_end(t, *id, in);
  334. return 0;
  335. }
  336. inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
  337. const struct tm** ftm, short min)
  338. {
  339. off_t cur, sz, d, prev, tmp;
  340. int ret, cmpres;
  341. ttail_files_off_t *files_off;
  342. size_t id;
  343. off_t *off;
  344. files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
  345. id = files_off->id,
  346. off = &(files_off->off);
  347. sz = t->session->file.file_sz[id];
  348. d = cur = sz / 2;
  349. prev = 0;
  350. cmpres = 0;
  351. while(1)
  352. {
  353. if(fseek(t->logfile[id], cur, SEEK_SET) < 0)
  354. {
  355. perror("Unable to move in the file");
  356. return -1;
  357. }
  358. if(cur > prev)
  359. {
  360. cur = _ttail_file_next_line(t, id);
  361. if(cur < 0)
  362. {
  363. /* not sure errno is really set */
  364. perror("Error searching previous line");
  365. return -1;
  366. }
  367. }
  368. else
  369. {
  370. cur = _ttail_file_start_line(t, id);
  371. if(cur < 0)
  372. {
  373. /* not sure errno is really set */
  374. perror("Error searching previous line");
  375. return -1;
  376. }
  377. }
  378. if(cur == prev)
  379. {
  380. *off = cur;
  381. return 0;
  382. }
  383. prev = cur;
  384. ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
  385. if(ret < 0)
  386. {
  387. if(t->verbose > 2)
  388. {
  389. fprintf(stderr, "Error comparing a logline \
  390. to a date directly from a file\n");
  391. }
  392. return -1;
  393. }
  394. else if(cmpres == 0)
  395. {
  396. *off = cur;
  397. break;
  398. }
  399. else if(cmpres < 0)
  400. {
  401. tmp = _ttail_file_next_line(t,id);
  402. ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
  403. if(cmpres >=0)
  404. {
  405. *off = tmp;
  406. break;
  407. }
  408. d/=2;
  409. cur += d;
  410. cur %= t->session->file.file_sz[id];
  411. }
  412. else
  413. {
  414. d/=2;
  415. cur -= d;
  416. if(cur < 0)
  417. {
  418. cur = 0;
  419. }
  420. }
  421. }
  422. return 0;
  423. }
  424. int ttail_search_files_init(ttail_t* t)
  425. {
  426. struct stat stat;
  427. FILE *fp;
  428. int fd;
  429. size_t i;
  430. off_t *file_sz;
  431. t->session = (ttail_search_t*)malloc(sizeof(ttail_search_file_t));
  432. if(!t->session)
  433. {
  434. perror("Unable to allocate memory for search session");
  435. goto _ttail_search_closest_files_alloc_session_err;
  436. }
  437. memset(t->session, 0, sizeof(ttail_search_file_t));
  438. file_sz = malloc(sizeof(off_t)*t->logfile_sz);
  439. if(!file_sz)
  440. {
  441. perror("Unable to allocate memory for file sizes");
  442. goto _ttail_search_closest_files_alloc_err;
  443. }
  444. t->session->file.file_sz = file_sz;
  445. #ifdef HUGEFILE
  446. t->session->file.sz_div = 0;
  447. #endif
  448. for(i=0;i<t->logfile_sz;i++)
  449. {
  450. fp = t->logfile[i];
  451. if(!fp)
  452. {
  453. file_sz[i] = 0;
  454. continue;
  455. }
  456. if((fd = fileno(fp)) < 0)
  457. {
  458. perror("Unable to get fp");
  459. goto _ttail_search_closest_files_err;
  460. }
  461. if(fstat(fileno(fp), &stat))
  462. {
  463. perror("Unable to get file size");
  464. goto _ttail_search_closest_files_err;
  465. }
  466. file_sz[i] = stat.st_size;
  467. }
  468. /* we got all real size, determining if we need a divisor */
  469. /*
  470. * not implemented
  471. */
  472. if(_ttail_search_closest_files_set_fsizes(t))
  473. {
  474. goto _ttail_search_closest_files_err;
  475. }
  476. t->session->file.buf_sz = 128;
  477. t->session->file.buf = malloc(t->session->file.buf_sz);
  478. if(!t->session->file.buf)
  479. {
  480. goto _ttail_search_closest_files_err;
  481. }
  482. if(_ttail_search_files_fmt_init(t) < 0)
  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_files_fmt_init(ttail_t* t)
  496. {
  497. int fmt_id;
  498. size_t i,j;
  499. const char *buff;
  500. const char *fmt[] = TTAIL_DEFAULT_FORMATS;
  501. if(t->flag & TTAIL_FLAG_FORMAT)
  502. {
  503. return 1;
  504. }
  505. fmt_id = -1;
  506. for(i=0; i<t->logfile_sz; i++)
  507. {
  508. if(!t->logfile[i]) {
  509. continue;
  510. }
  511. for(j=0; j<10; j++)
  512. {
  513. //try to guess fmt on the 10 first lines
  514. if(ttail_file_getline(t, i) < 0) {
  515. break;
  516. }
  517. buff = ttail_logline_subst(t, ttail_file_getline_buf(t));
  518. //buff contains the lines starting by the date
  519. fmt_id = ttail_format_guess(buff, NULL);
  520. if(fmt_id >= 0)
  521. {
  522. break;
  523. }
  524. }
  525. rewind(t->logfile[i]);
  526. if(fmt_id >= 0)
  527. {
  528. if(t->verbose > 0)
  529. {
  530. fprintf(stderr,
  531. "Detected format %s in file %s\n",
  532. fmt[fmt_id], t->logfile_name[i]);
  533. }
  534. break;
  535. }
  536. }
  537. if(fmt_id >= 0)
  538. {
  539. buff = fmt[fmt_id];
  540. t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
  541. if(!t->fmt)
  542. {
  543. perror("Unable to allocate memory for date format");
  544. return -1;
  545. }
  546. strcpy(t->fmt, buff);
  547. t->flag |= TTAIL_FLAG_FORMAT;
  548. return 0;
  549. }
  550. fprintf(stderr, "Unable to detect date format from logfiles\n");
  551. return -1;
  552. }
  553. int _ttail_search_closest_files_set_fsizes(ttail_t* t)
  554. {
  555. size_t i;
  556. off_t *vfile, *vsz;
  557. vfile = malloc(sizeof(off_t)*t->logfile_sz);
  558. if(!vfile)
  559. {
  560. perror("Unable to allocate memory for file size sum");
  561. return -1;
  562. }
  563. t->session->file.vfile = vfile;
  564. vsz = &(t->session->file.vsz);
  565. *vsz = 0;
  566. for(i=0; i<t->logfile_sz;i++)
  567. {
  568. vfile[i] = *vsz;
  569. #ifdef HUGEFILE
  570. *vsz += t->session->file.file_sz[i] >> t->session->file.sz_div;
  571. #else
  572. *vsz += t->session->file.file_sz[i];
  573. #endif
  574. }
  575. t->session->file.vpos = 0;
  576. return 0;
  577. }
  578. inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
  579. {
  580. FILE *fp;
  581. long cur;
  582. memset(tm, 0, sizeof(struct tm)*2);
  583. fp = t->logfile[id];
  584. if(!fp)
  585. {
  586. return 1;
  587. }
  588. if(fseek(fp, 0, SEEK_SET) < 0)
  589. {
  590. perror("Unable to manipulate fp");
  591. return -1;
  592. }
  593. while(1)
  594. {
  595. if(ttail_file_getline(t, id) < 0)
  596. {
  597. return 1;
  598. }
  599. if(!ttail_logline2date(t, ttail_file_getline_buf(t), tm))
  600. {
  601. break;
  602. }
  603. }
  604. if(fseek(fp, -1, SEEK_END) < 0)
  605. {
  606. perror("Unable to manipulate fp");
  607. return -1;
  608. }
  609. while(1)
  610. {
  611. if((cur = _ttail_file_start_line(t, id)) < 0)
  612. {
  613. return -1;
  614. }
  615. if(ttail_file_getline(t, id) < 0)
  616. {
  617. return 1;
  618. }
  619. if(!ttail_logline2date(t, ttail_file_getline_buf(t), tm+1))
  620. {
  621. break;
  622. }
  623. if(!cur)
  624. {
  625. return 1;
  626. }
  627. if(fseek(fp, cur-1, SEEK_SET) < 0)
  628. {
  629. perror("Unable to manipulate fp");
  630. return -1;
  631. }
  632. }
  633. return 0;
  634. }
  635. int _ttail_file_reopen(ttail_t* t, size_t id)
  636. {
  637. if(t->logfile[id])
  638. {
  639. fclose(t->logfile[id]);
  640. }
  641. t->logfile[id] = fopen(t->logfile_name[id], "r");
  642. if(!t->logfile[id] && t->verbose > 2)
  643. {
  644. fprintf(stderr, "Unable to reopen '%s'\n",
  645. t->logfile_name[id]);
  646. }
  647. return t->logfile[id]?0:-1;
  648. }
  649. inline long _ttail_file_next_line(ttail_t *t, size_t id)
  650. {
  651. FILE *f;
  652. ssize_t s;
  653. size_t r;
  654. char *buff;
  655. long res;
  656. int c;
  657. f = t->logfile[id];
  658. r=0;
  659. buff = NULL;
  660. s = getline(&buff, &r, f);
  661. if(s < 0)
  662. {
  663. goto _ttail_file_next_line_err;
  664. }
  665. while(1)
  666. {
  667. c = getc(f);
  668. if(c == EOF)
  669. {
  670. return 0;
  671. }
  672. else if(c!='\n')
  673. {
  674. if(fseek(f, -1, SEEK_CUR)<0)
  675. {
  676. perror("Unable to set position in file");
  677. goto _ttail_file_next_line_err;
  678. }
  679. break;
  680. }
  681. }
  682. res = ftell(f);
  683. free(buff);
  684. return res;
  685. _ttail_file_next_line_err:
  686. free(buff);
  687. return -1;
  688. }
  689. inline long _ttail_file_start_line(ttail_t *ttail, size_t id)
  690. {
  691. #define _STARTLN_BUFFLEN 32
  692. FILE *f;
  693. long res; /* function result */
  694. long read_beg, cur, last, start;
  695. int read_sz;
  696. int c;
  697. f = ttail->logfile[id];
  698. if((start = ftell(f)) < 0)
  699. {
  700. perror("Unable to get position in file");
  701. return -1;
  702. }
  703. res = 0;
  704. read_beg = start;
  705. while(!res && start)
  706. {
  707. if(fseek(f, read_beg, SEEK_SET) < 0)
  708. {
  709. perror("Unable to set position in file");
  710. return -1;
  711. }
  712. start = read_beg;
  713. read_sz = start <= _STARTLN_BUFFLEN?start:_STARTLN_BUFFLEN;
  714. read_beg = start - read_sz;
  715. if(fseek(f, read_beg, SEEK_SET) < 0)
  716. {
  717. perror("Unable to set position in file");
  718. return -1;
  719. }
  720. last = -1; /* last pos we saw a '\n' */
  721. cur = read_beg;
  722. while(cur <= start)
  723. {
  724. c = getc(f);
  725. if(c == EOF)
  726. {
  727. if(!res)
  728. {
  729. return 0;
  730. }
  731. break;
  732. }
  733. else if (c =='\n')
  734. {
  735. last = cur;
  736. }
  737. else if(last >= 0)
  738. {
  739. res = cur;
  740. last = -1;
  741. }
  742. cur++;
  743. }
  744. if(!read_beg)
  745. {
  746. break;
  747. }
  748. }
  749. if(fseek(f, res, SEEK_SET) < 0)
  750. {
  751. perror("Unable to set position in file");
  752. return -1;
  753. }
  754. return res;
  755. }
  756. inline off_t _ttail_file_search_from_end(ttail_t* t , size_t id ,
  757. const struct tm* tm)
  758. {
  759. FILE *f;
  760. struct tm curtm;
  761. off_t last;
  762. int ret, cmpret;
  763. off_t result;
  764. result = -1;
  765. f = t->logfile[id];
  766. if(fseek(f, -1, SEEK_END) < 0)
  767. {
  768. return -1;
  769. }
  770. while(1)
  771. {
  772. last = _ttail_file_start_line(t, id);
  773. if(last < 0)
  774. {
  775. break;
  776. }
  777. if(ttail_file_getline(t, id) < 0)
  778. {
  779. break;
  780. }
  781. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &curtm);
  782. if(ret < 0)
  783. {
  784. break;
  785. }
  786. cmpret = ttail_tm_cmp(&curtm, tm);
  787. if(!ret)
  788. {
  789. if(cmpret >= 0)
  790. {
  791. /* found but continue to search the first one*/
  792. result = last;
  793. }
  794. else
  795. {
  796. if(cmpret < 0 && result < 0)
  797. {
  798. result = last;
  799. }
  800. break;
  801. }
  802. }
  803. if(last == 0)
  804. {
  805. /* considere the begining of the file as the answer */
  806. return 0;
  807. }
  808. if(fseek(f, last-1, SEEK_SET))
  809. {
  810. return -1;
  811. }
  812. }
  813. return result;
  814. }
  815. inline int _ttail_file_off_cmp(ttail_t* t, size_t id, off_t off,
  816. const struct tm* tm, int *res)
  817. {
  818. if(fseek(t->logfile[id], off, SEEK_SET))
  819. {
  820. perror("Unable to set position in file");
  821. return -1;
  822. }
  823. if(ttail_file_getline(t, id) < 0)
  824. {
  825. perror("Unable to read a line from file");
  826. return -1;
  827. }
  828. return _ttail_file_cur_cmp(t, id, tm, res);
  829. }
  830. inline int _ttail_file_cur_cmp(ttail_t* t, size_t id, const struct tm* tm ,
  831. int* res)
  832. {
  833. int ret;
  834. struct tm ctm;
  835. if(ttail_file_getline(t, id) < 0)
  836. {
  837. return -1;
  838. }
  839. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &ctm);
  840. if(ret)
  841. {
  842. return -1;
  843. }
  844. else if(ret == 1)
  845. {
  846. return 1;
  847. }
  848. *res = ttail_tm_cmp(&ctm, tm);
  849. return 0;
  850. }
  851. void _ttail_search_file_free(ttail_t* t)
  852. {
  853. if(!t->session)
  854. {
  855. return;
  856. }
  857. if(t->session->file.buf)
  858. {
  859. free(t->session->file.buf);
  860. }
  861. if(t->session->file.file_sz)
  862. {
  863. free(t->session->file.file_sz);
  864. }
  865. if(t->session->file.vfile)
  866. {
  867. free(t->session->file.vfile);
  868. }
  869. free(t->session);
  870. }