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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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. goto _ttail_search_closest_files_loop_err;
  54. }
  55. else if (ret == 1)
  56. {
  57. fprintf(stderr, "Error : unable to find a valid date \
  58. in '%s'\n", t->logfile_name[i]);
  59. free(ftm[i]);
  60. ftm[i] = NULL;
  61. fclose(t->logfile[i]);
  62. t->logfile[i] = NULL;
  63. if(!ttail_permissive(t))
  64. {
  65. ttail_strict_msg();
  66. goto _ttail_search_closest_files_loop_err;
  67. }
  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_file_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, *id):
  305. _ttail_file_next_line(t, *id);
  306. if(tmpoff < 0)
  307. {
  308. break;
  309. }
  310. cmpres=0;
  311. ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
  312. if(ret > 0 && !ttail_permissive(t))
  313. {
  314. ttail_strict_msg();
  315. return -1;
  316. }
  317. if((min && cmpres < 0) || (!min && cmpres > 0))
  318. {
  319. break;
  320. }
  321. *off = tmpoff;
  322. if(min)
  323. {
  324. tmpoff--;
  325. }
  326. }
  327. return 0;
  328. }
  329. else if(*id == t->logfile_sz - 1 && cmax < 0)
  330. {
  331. *off = t->session->file.file_sz[*id];
  332. return 0;
  333. }
  334. (*id)++;
  335. }
  336. if(!valid)
  337. {
  338. fprintf(stderr, "No files to scan\n");
  339. return 0;
  340. }
  341. if(*id == t->logfile_sz)
  342. {
  343. return 0;
  344. }
  345. /* the answer is somewhere in *id file */
  346. *off = _ttail_file_search_from_end(t, *id, in);
  347. return 0;
  348. }
  349. inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
  350. const struct tm** ftm, short min)
  351. {
  352. off_t cur, sz, d, prev, tmp;
  353. int ret, cmpres;
  354. ttail_files_off_t *files_off;
  355. size_t id;
  356. off_t *off;
  357. files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
  358. id = files_off->id,
  359. off = &(files_off->off);
  360. sz = t->session->file.file_sz[id];
  361. d = cur = sz / 2;
  362. prev = 0;
  363. cmpres = 0;
  364. while(1)
  365. {
  366. if(fseek(t->logfile[id], cur, SEEK_SET) < 0)
  367. {
  368. perror("Unable to move in the file");
  369. return -1;
  370. }
  371. if(cur > prev)
  372. {
  373. cur = _ttail_file_next_line(t, id);
  374. if(cur < 0)
  375. {
  376. /* not sure errno is really set */
  377. perror("Error searching previous line");
  378. return -1;
  379. }
  380. }
  381. else
  382. {
  383. cur = _ttail_file_start_line(t, id);
  384. if(cur < 0)
  385. {
  386. /* not sure errno is really set */
  387. perror("Error searching previous line");
  388. return -1;
  389. }
  390. }
  391. if(cur == prev)
  392. {
  393. *off = cur;
  394. return 0;
  395. }
  396. prev = cur;
  397. ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
  398. if(ret < 0)
  399. {
  400. if(t->verbose > 2)
  401. {
  402. fprintf(stderr, "Error comparing a logline \
  403. to a date directly from a file\n");
  404. }
  405. return -1;
  406. }
  407. else if(cmpres == 0)
  408. {
  409. *off = cur;
  410. break;
  411. }
  412. else if(cmpres < 0)
  413. {
  414. tmp = _ttail_file_next_line(t,id);
  415. ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
  416. if(cmpres >=0)
  417. {
  418. *off = tmp;
  419. break;
  420. }
  421. d/=2;
  422. cur += d;
  423. cur %= t->session->file.file_sz[id];
  424. }
  425. else
  426. {
  427. d/=2;
  428. cur -= d;
  429. if(cur < 0)
  430. {
  431. cur = 0;
  432. }
  433. }
  434. }
  435. return 0;
  436. }
  437. int ttail_search_files_init(ttail_t* t)
  438. {
  439. struct stat stat;
  440. FILE *fp;
  441. int fd;
  442. size_t i;
  443. off_t *file_sz;
  444. t->session = (ttail_search_t*)malloc(sizeof(ttail_search_file_t));
  445. if(!t->session)
  446. {
  447. perror("Unable to allocate memory for search session");
  448. goto _ttail_search_closest_files_alloc_session_err;
  449. }
  450. memset(t->session, 0, sizeof(ttail_search_file_t));
  451. file_sz = malloc(sizeof(off_t)*t->logfile_sz);
  452. if(!file_sz)
  453. {
  454. perror("Unable to allocate memory for file sizes");
  455. goto _ttail_search_closest_files_alloc_err;
  456. }
  457. t->session->file.file_sz = file_sz;
  458. #ifdef HUGEFILE
  459. t->session->file.sz_div = 0;
  460. #endif
  461. for(i=0;i<t->logfile_sz;i++)
  462. {
  463. fp = t->logfile[i];
  464. if(!fp)
  465. {
  466. file_sz[i] = 0;
  467. continue;
  468. }
  469. if((fd = fileno(fp)) < 0)
  470. {
  471. perror("Unable to get fp");
  472. goto _ttail_search_closest_files_err;
  473. }
  474. if(fstat(fileno(fp), &stat))
  475. {
  476. perror("Unable to get file size");
  477. goto _ttail_search_closest_files_err;
  478. }
  479. file_sz[i] = stat.st_size;
  480. }
  481. /* we got all real size, determining if we need a divisor */
  482. /*
  483. * not implemented
  484. */
  485. if(_ttail_search_closest_files_set_fsizes(t))
  486. {
  487. goto _ttail_search_closest_files_err;
  488. }
  489. t->session->file.buf_sz = 128;
  490. t->session->file.buf = malloc(t->session->file.buf_sz);
  491. if(!t->session->file.buf)
  492. {
  493. goto _ttail_search_closest_files_err;
  494. }
  495. if(_ttail_search_files_fmt_init(t) < 0)
  496. {
  497. goto _ttail_search_closest_files_err;
  498. }
  499. return 0;
  500. _ttail_search_closest_files_err:
  501. free(file_sz);
  502. t->session->file.file_sz = NULL;
  503. _ttail_search_closest_files_alloc_err:
  504. free(t->session);
  505. _ttail_search_closest_files_alloc_session_err:
  506. return -1;
  507. }
  508. int _ttail_search_files_fmt_init(ttail_t* t)
  509. {
  510. int fmt_id;
  511. size_t i,j;
  512. const char *buff;
  513. const char *fmt[] = TTAIL_DEFAULT_FORMATS;
  514. if(t->flag & TTAIL_FLAG_FORMAT)
  515. {
  516. return 1;
  517. }
  518. fmt_id = -1;
  519. for(i=0; i<t->logfile_sz; i++)
  520. {
  521. if(!t->logfile[i]) {
  522. continue;
  523. }
  524. for(j=0; j<10; j++)
  525. {
  526. //try to guess fmt on the 10 first lines
  527. if(ttail_file_getline(t, i) < 0) {
  528. break;
  529. }
  530. buff = ttail_logline_subst(t, ttail_file_getline_buf(t));
  531. //buff contains the lines starting by the date
  532. fmt_id = ttail_format_guess(buff, NULL);
  533. if(fmt_id >= 0)
  534. {
  535. break;
  536. }
  537. if(!ttail_permissive(t))
  538. {
  539. ttail_strict_msg();
  540. break;
  541. }
  542. }
  543. rewind(t->logfile[i]);
  544. if(fmt_id >= 0)
  545. {
  546. if(t->verbose > 0)
  547. {
  548. fprintf(stderr,
  549. "Detected format %s in file %s\n",
  550. fmt[fmt_id], t->logfile_name[i]);
  551. }
  552. break;
  553. }
  554. }
  555. if(fmt_id >= 0)
  556. {
  557. buff = fmt[fmt_id];
  558. t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
  559. if(!t->fmt)
  560. {
  561. perror("Unable to allocate memory for date format");
  562. return -1;
  563. }
  564. strcpy(t->fmt, buff);
  565. t->flag |= TTAIL_FLAG_FORMAT;
  566. return 0;
  567. }
  568. fprintf(stderr, "Unable to detect date format from logfiles\n");
  569. return -1;
  570. }
  571. int _ttail_search_closest_files_set_fsizes(ttail_t* t)
  572. {
  573. size_t i;
  574. off_t *vfile, *vsz;
  575. vfile = malloc(sizeof(off_t)*t->logfile_sz);
  576. if(!vfile)
  577. {
  578. perror("Unable to allocate memory for file size sum");
  579. return -1;
  580. }
  581. t->session->file.vfile = vfile;
  582. vsz = &(t->session->file.vsz);
  583. *vsz = 0;
  584. for(i=0; i<t->logfile_sz;i++)
  585. {
  586. vfile[i] = *vsz;
  587. #ifdef HUGEFILE
  588. *vsz += t->session->file.file_sz[i] >> t->session->file.sz_div;
  589. #else
  590. *vsz += t->session->file.file_sz[i];
  591. #endif
  592. }
  593. t->session->file.vpos = 0;
  594. return 0;
  595. }
  596. inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
  597. {
  598. FILE *fp;
  599. long cur;
  600. int ret;
  601. memset(tm, 0, sizeof(struct tm)*2);
  602. fp = t->logfile[id];
  603. if(!fp)
  604. {
  605. fprintf(stderr, "File pointer is null !\n");
  606. return 1;
  607. }
  608. if(fseek(fp, 0, SEEK_SET) < 0)
  609. {
  610. perror("Unable to manipulate fp");
  611. return -1;
  612. }
  613. while(1)
  614. {
  615. if(ttail_file_getline(t, id) < 0)
  616. {
  617. return 1;
  618. }
  619. if(!(ret = ttail_logline2date(t, ttail_file_getline_buf(t), tm)))
  620. {
  621. break;
  622. }
  623. if(!ttail_permissive(t))
  624. {
  625. fprintf(stderr,
  626. "Unable to find %s in logline",
  627. ret == 1?"prefix":"date");
  628. if(t->verbose > 0)
  629. {
  630. fprintf(stderr, " : '%s'\n",
  631. ttail_file_getline_buf(t));
  632. }
  633. else
  634. {
  635. fprintf(stderr,"\n");
  636. }
  637. ttail_strict_msg();
  638. return -1;
  639. }
  640. }
  641. if(fseek(fp, -1, SEEK_END) < 0)
  642. {
  643. perror("Unable to manipulate fp");
  644. return -1;
  645. }
  646. while(1)
  647. {
  648. if((cur = _ttail_file_start_line(t, id)) < 0)
  649. {
  650. fprintf(stderr, "Error will searching line starts in\
  651. %s\n", t->logfile_name[id]);
  652. return -1;
  653. }
  654. if(ttail_file_getline(t, id) < 0)
  655. {
  656. return 1;
  657. }
  658. if(!ttail_logline2date(t, ttail_file_getline_buf(t), tm+1))
  659. {
  660. break;
  661. }
  662. if(!cur)
  663. {
  664. return 1;
  665. }
  666. else if(!ttail_permissive(t))
  667. {
  668. if(t->verbose <= 0)
  669. {
  670. fprintf(stderr,
  671. "Unable to find a date in logline\n");
  672. }
  673. else
  674. {
  675. fprintf(stderr,
  676. "Unable to find a date in '%s'\n",
  677. ttail_file_getline_buf(t));
  678. }
  679. ttail_strict_msg();
  680. return -1;
  681. }
  682. if(fseek(fp, cur-1, SEEK_SET) < 0)
  683. {
  684. perror("Unable to manipulate fp");
  685. return -1;
  686. }
  687. }
  688. return 0;
  689. }
  690. int _ttail_file_reopen(ttail_t* t, size_t id)
  691. {
  692. if(t->logfile[id])
  693. {
  694. fclose(t->logfile[id]);
  695. }
  696. t->logfile[id] = fopen(t->logfile_name[id], "r");
  697. if(!t->logfile[id] && t->verbose > 2)
  698. {
  699. fprintf(stderr, "Unable to reopen '%s'\n",
  700. t->logfile_name[id]);
  701. }
  702. return t->logfile[id]?0:-1;
  703. }
  704. inline long _ttail_file_next_line(ttail_t *t, size_t id)
  705. {
  706. FILE *f;
  707. ssize_t s;
  708. size_t r;
  709. char *buff;
  710. long res;
  711. int c;
  712. f = t->logfile[id];
  713. r=0;
  714. buff = NULL;
  715. s = getline(&buff, &r, f);
  716. if(s < 0)
  717. {
  718. goto _ttail_file_next_line_err;
  719. }
  720. while(1)
  721. {
  722. c = getc(f);
  723. if(c == EOF)
  724. {
  725. return 0;
  726. }
  727. else if(c!='\n')
  728. {
  729. if(fseek(f, -1, SEEK_CUR)<0)
  730. {
  731. perror("Unable to set position in file");
  732. goto _ttail_file_next_line_err;
  733. }
  734. break;
  735. }
  736. }
  737. res = ftell(f);
  738. free(buff);
  739. return res;
  740. _ttail_file_next_line_err:
  741. free(buff);
  742. return -1;
  743. }
  744. inline long _ttail_file_start_line(ttail_t *ttail, size_t id)
  745. {
  746. #define _STARTLN_BUFFLEN 32
  747. FILE *f;
  748. long res; /* function result */
  749. long read_beg, cur, last, start;
  750. int read_sz;
  751. int c;
  752. f = ttail->logfile[id];
  753. if((start = ftell(f)) < 0)
  754. {
  755. perror("Unable to get position in file");
  756. return -1;
  757. }
  758. res = 0;
  759. read_beg = start;
  760. while(!res && start)
  761. {
  762. if(fseek(f, read_beg, SEEK_SET) < 0)
  763. {
  764. perror("Unable to set position in file");
  765. return -1;
  766. }
  767. start = read_beg;
  768. read_sz = start <= _STARTLN_BUFFLEN?start:_STARTLN_BUFFLEN;
  769. read_beg = start - read_sz;
  770. if(fseek(f, read_beg, SEEK_SET) < 0)
  771. {
  772. perror("Unable to set position in file");
  773. return -1;
  774. }
  775. last = -1; /* last pos we saw a '\n' */
  776. cur = read_beg;
  777. while(cur <= start)
  778. {
  779. c = getc(f);
  780. if(c == EOF)
  781. {
  782. if(!res)
  783. {
  784. return 0;
  785. }
  786. break;
  787. }
  788. else if (c =='\n')
  789. {
  790. last = cur;
  791. }
  792. else if(last >= 0)
  793. {
  794. res = cur;
  795. last = -1;
  796. }
  797. cur++;
  798. }
  799. if(!read_beg)
  800. {
  801. break;
  802. }
  803. }
  804. if(fseek(f, res, SEEK_SET) < 0)
  805. {
  806. perror("Unable to set position in file");
  807. return -1;
  808. }
  809. return res;
  810. }
  811. inline off_t _ttail_file_search_from_end(ttail_t* t , size_t id ,
  812. const struct tm* tm)
  813. {
  814. FILE *f;
  815. struct tm curtm;
  816. off_t last;
  817. int ret, cmpret;
  818. off_t result;
  819. result = -1;
  820. f = t->logfile[id];
  821. if(fseek(f, -1, SEEK_END) < 0)
  822. {
  823. return -1;
  824. }
  825. while(1)
  826. {
  827. last = _ttail_file_start_line(t, id);
  828. if(last < 0)
  829. {
  830. break;
  831. }
  832. if(ttail_file_getline(t, id) < 0)
  833. {
  834. break;
  835. }
  836. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &curtm);
  837. if(ret < 0)
  838. {
  839. break;
  840. }
  841. else if(ret > 0 && !ttail_permissive(t))
  842. {
  843. fprintf(stderr, "Unable to find the %s in logline",
  844. ret==1?"prefix":"date");
  845. if(t->verbose > 0)
  846. {
  847. fprintf(stderr, " : '%s'\n",
  848. ttail_file_getline_buf(t));
  849. }
  850. else
  851. {
  852. fprintf(stderr, "\n");
  853. }
  854. ttail_strict_msg();
  855. break;
  856. }
  857. cmpret = ttail_tm_cmp(&curtm, tm);
  858. if(!ret)
  859. {
  860. if(cmpret >= 0)
  861. {
  862. /* found but continue to search the first one*/
  863. result = last;
  864. }
  865. else
  866. {
  867. if(cmpret < 0 && result < 0)
  868. {
  869. result = last;
  870. }
  871. break;
  872. }
  873. }
  874. if(last == 0)
  875. {
  876. /* considere the begining of the file as the answer */
  877. return 0;
  878. }
  879. if(fseek(f, last-1, SEEK_SET))
  880. {
  881. return -1;
  882. }
  883. }
  884. return result;
  885. }
  886. inline int _ttail_file_off_cmp(ttail_t* t, size_t id, off_t off,
  887. const struct tm* tm, int *res)
  888. {
  889. if(fseek(t->logfile[id], off, SEEK_SET))
  890. {
  891. perror("Unable to set position in file");
  892. return -1;
  893. }
  894. if(ttail_file_getline(t, id) < 0)
  895. {
  896. perror("Unable to read a line from file");
  897. return -1;
  898. }
  899. return _ttail_file_cur_cmp(t, id, tm, res);
  900. }
  901. inline int _ttail_file_cur_cmp(ttail_t* t, size_t id, const struct tm* tm ,
  902. int* res)
  903. {
  904. int ret;
  905. struct tm ctm;
  906. if(ttail_file_getline(t, id) < 0)
  907. {
  908. return -1;
  909. }
  910. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &ctm);
  911. if(ret < 0)
  912. {
  913. return -1;
  914. }
  915. else if(ret > 1)
  916. {
  917. return 1;
  918. }
  919. *res = ttail_tm_cmp(&ctm, tm);
  920. return 0;
  921. }
  922. void _ttail_search_file_free(ttail_t* t)
  923. {
  924. if(!t->session)
  925. {
  926. return;
  927. }
  928. if(t->session->file.buf)
  929. {
  930. free(t->session->file.buf);
  931. }
  932. if(t->session->file.file_sz)
  933. {
  934. free(t->session->file.file_sz);
  935. }
  936. if(t->session->file.vfile)
  937. {
  938. free(t->session->file.vfile);
  939. }
  940. free(t->session);
  941. }