timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ttail_search_files.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. if(!buff)
  532. {
  533. if(ttail_permissive(t))
  534. {
  535. continue;
  536. }
  537. fprintf(stderr,
  538. "Unable to find prefix in '%s' logline",
  539. t->logfile_name[i]);
  540. if(t->verbose > 0)
  541. {
  542. fprintf(stderr, " : '%s'",
  543. ttail_file_getline_buf(t));
  544. }
  545. fprintf(stderr, "\n");
  546. return -1;
  547. }
  548. //buff contains the lines starting by the date
  549. fmt_id = ttail_format_guess(buff, NULL);
  550. if(fmt_id >= 0)
  551. {
  552. break;
  553. }
  554. if(!ttail_permissive(t))
  555. {
  556. ttail_strict_msg();
  557. break;
  558. }
  559. }
  560. rewind(t->logfile[i]);
  561. if(fmt_id >= 0)
  562. {
  563. if(t->verbose > 0)
  564. {
  565. fprintf(stderr,
  566. "Detected format %s in file %s\n",
  567. fmt[fmt_id], t->logfile_name[i]);
  568. }
  569. break;
  570. }
  571. }
  572. if(fmt_id >= 0)
  573. {
  574. buff = fmt[fmt_id];
  575. t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
  576. if(!t->fmt)
  577. {
  578. perror("Unable to allocate memory for date format");
  579. return -1;
  580. }
  581. strcpy(t->fmt, buff);
  582. t->flag |= TTAIL_FLAG_FORMAT;
  583. return 0;
  584. }
  585. fprintf(stderr, "Unable to detect date format from logfiles\n");
  586. return -1;
  587. }
  588. int _ttail_search_closest_files_set_fsizes(ttail_t* t)
  589. {
  590. size_t i;
  591. off_t *vfile, *vsz;
  592. vfile = malloc(sizeof(off_t)*t->logfile_sz);
  593. if(!vfile)
  594. {
  595. perror("Unable to allocate memory for file size sum");
  596. return -1;
  597. }
  598. t->session->file.vfile = vfile;
  599. vsz = &(t->session->file.vsz);
  600. *vsz = 0;
  601. for(i=0; i<t->logfile_sz;i++)
  602. {
  603. vfile[i] = *vsz;
  604. #ifdef HUGEFILE
  605. *vsz += t->session->file.file_sz[i] >> t->session->file.sz_div;
  606. #else
  607. *vsz += t->session->file.file_sz[i];
  608. #endif
  609. }
  610. t->session->file.vpos = 0;
  611. return 0;
  612. }
  613. inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
  614. {
  615. FILE *fp;
  616. long cur;
  617. int ret;
  618. memset(tm, 0, sizeof(struct tm)*2);
  619. fp = t->logfile[id];
  620. if(!fp)
  621. {
  622. fprintf(stderr, "File pointer is null !\n");
  623. return 1;
  624. }
  625. if(fseek(fp, 0, SEEK_SET) < 0)
  626. {
  627. perror("Unable to manipulate fp");
  628. return -1;
  629. }
  630. while(1)
  631. {
  632. if(ttail_file_getline(t, id) < 0)
  633. {
  634. return 1;
  635. }
  636. if(!(ret = ttail_logline2date(t, ttail_file_getline_buf(t), tm)))
  637. {
  638. break;
  639. }
  640. if(!ttail_permissive(t))
  641. {
  642. fprintf(stderr,
  643. "Unable to find %s in logline",
  644. ret == 1?"prefix":"date");
  645. if(t->verbose > 0)
  646. {
  647. fprintf(stderr, " : '%s'\n",
  648. ttail_file_getline_buf(t));
  649. }
  650. else
  651. {
  652. fprintf(stderr,"\n");
  653. }
  654. ttail_strict_msg();
  655. return -1;
  656. }
  657. }
  658. if(fseek(fp, -1, SEEK_END) < 0)
  659. {
  660. perror("Unable to manipulate fp");
  661. return -1;
  662. }
  663. while(1)
  664. {
  665. if((cur = _ttail_file_start_line(t, id)) < 0)
  666. {
  667. fprintf(stderr, "Error will searching line starts in\
  668. %s\n", t->logfile_name[id]);
  669. return -1;
  670. }
  671. if(ttail_file_getline(t, id) < 0)
  672. {
  673. return 1;
  674. }
  675. if(!ttail_logline2date(t, ttail_file_getline_buf(t), tm+1))
  676. {
  677. break;
  678. }
  679. if(!cur)
  680. {
  681. return 1;
  682. }
  683. else if(!ttail_permissive(t))
  684. {
  685. if(t->verbose <= 0)
  686. {
  687. fprintf(stderr,
  688. "Unable to find a date in logline\n");
  689. }
  690. else
  691. {
  692. fprintf(stderr,
  693. "Unable to find a date in '%s'\n",
  694. ttail_file_getline_buf(t));
  695. }
  696. ttail_strict_msg();
  697. return -1;
  698. }
  699. if(fseek(fp, cur-1, SEEK_SET) < 0)
  700. {
  701. perror("Unable to manipulate fp");
  702. return -1;
  703. }
  704. }
  705. return 0;
  706. }
  707. int _ttail_file_reopen(ttail_t* t, size_t id)
  708. {
  709. if(t->logfile[id])
  710. {
  711. fclose(t->logfile[id]);
  712. }
  713. t->logfile[id] = fopen(t->logfile_name[id], "r");
  714. if(!t->logfile[id] && t->verbose > 2)
  715. {
  716. fprintf(stderr, "Unable to reopen '%s'\n",
  717. t->logfile_name[id]);
  718. }
  719. return t->logfile[id]?0:-1;
  720. }
  721. inline long _ttail_file_next_line(ttail_t *t, size_t id)
  722. {
  723. FILE *f;
  724. ssize_t s;
  725. size_t r;
  726. char *buff;
  727. long res;
  728. int c;
  729. f = t->logfile[id];
  730. r=0;
  731. buff = NULL;
  732. s = getline(&buff, &r, f);
  733. if(s < 0)
  734. {
  735. goto _ttail_file_next_line_err;
  736. }
  737. while(1)
  738. {
  739. c = getc(f);
  740. if(c == EOF)
  741. {
  742. return 0;
  743. }
  744. else if(c!='\n')
  745. {
  746. if(fseek(f, -1, SEEK_CUR)<0)
  747. {
  748. perror("Unable to set position in file");
  749. goto _ttail_file_next_line_err;
  750. }
  751. break;
  752. }
  753. }
  754. res = ftell(f);
  755. free(buff);
  756. return res;
  757. _ttail_file_next_line_err:
  758. free(buff);
  759. return -1;
  760. }
  761. inline long _ttail_file_start_line(ttail_t *ttail, size_t id)
  762. {
  763. #define _STARTLN_BUFFLEN 32
  764. FILE *f;
  765. long res; /* function result */
  766. long read_beg, cur, last, start;
  767. int read_sz;
  768. int c;
  769. f = ttail->logfile[id];
  770. if((start = ftell(f)) < 0)
  771. {
  772. perror("Unable to get position in file");
  773. return -1;
  774. }
  775. res = 0;
  776. read_beg = start;
  777. while(!res && start)
  778. {
  779. if(fseek(f, read_beg, SEEK_SET) < 0)
  780. {
  781. perror("Unable to set position in file");
  782. return -1;
  783. }
  784. start = read_beg;
  785. read_sz = start <= _STARTLN_BUFFLEN?start:_STARTLN_BUFFLEN;
  786. read_beg = start - read_sz;
  787. if(fseek(f, read_beg, SEEK_SET) < 0)
  788. {
  789. perror("Unable to set position in file");
  790. return -1;
  791. }
  792. last = -1; /* last pos we saw a '\n' */
  793. cur = read_beg;
  794. while(cur <= start)
  795. {
  796. c = getc(f);
  797. if(c == EOF)
  798. {
  799. if(!res)
  800. {
  801. return 0;
  802. }
  803. break;
  804. }
  805. else if (c =='\n')
  806. {
  807. last = cur;
  808. }
  809. else if(last >= 0)
  810. {
  811. res = cur;
  812. last = -1;
  813. }
  814. cur++;
  815. }
  816. if(!read_beg)
  817. {
  818. break;
  819. }
  820. }
  821. if(fseek(f, res, SEEK_SET) < 0)
  822. {
  823. perror("Unable to set position in file");
  824. return -1;
  825. }
  826. return res;
  827. }
  828. inline off_t _ttail_file_search_from_end(ttail_t* t , size_t id ,
  829. const struct tm* tm)
  830. {
  831. FILE *f;
  832. struct tm curtm;
  833. off_t last;
  834. int ret, cmpret;
  835. off_t result;
  836. result = -1;
  837. f = t->logfile[id];
  838. if(fseek(f, -1, SEEK_END) < 0)
  839. {
  840. return -1;
  841. }
  842. while(1)
  843. {
  844. last = _ttail_file_start_line(t, id);
  845. if(last < 0)
  846. {
  847. break;
  848. }
  849. if(ttail_file_getline(t, id) < 0)
  850. {
  851. break;
  852. }
  853. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &curtm);
  854. if(ret < 0)
  855. {
  856. break;
  857. }
  858. else if(ret > 0 && !ttail_permissive(t))
  859. {
  860. fprintf(stderr, "Unable to find the %s in logline",
  861. ret==1?"prefix":"date");
  862. if(t->verbose > 0)
  863. {
  864. fprintf(stderr, " : '%s'\n",
  865. ttail_file_getline_buf(t));
  866. }
  867. else
  868. {
  869. fprintf(stderr, "\n");
  870. }
  871. ttail_strict_msg();
  872. break;
  873. }
  874. cmpret = ttail_tm_cmp(&curtm, tm);
  875. if(!ret)
  876. {
  877. if(cmpret >= 0)
  878. {
  879. /* found but continue to search the first one*/
  880. result = last;
  881. }
  882. else
  883. {
  884. if(cmpret < 0 && result < 0)
  885. {
  886. result = last;
  887. }
  888. break;
  889. }
  890. }
  891. if(last == 0)
  892. {
  893. /* considere the begining of the file as the answer */
  894. return 0;
  895. }
  896. if(fseek(f, last-1, SEEK_SET))
  897. {
  898. return -1;
  899. }
  900. }
  901. return result;
  902. }
  903. inline int _ttail_file_off_cmp(ttail_t* t, size_t id, off_t off,
  904. const struct tm* tm, int *res)
  905. {
  906. if(fseek(t->logfile[id], off, SEEK_SET))
  907. {
  908. perror("Unable to set position in file");
  909. return -1;
  910. }
  911. if(ttail_file_getline(t, id) < 0)
  912. {
  913. perror("Unable to read a line from file");
  914. return -1;
  915. }
  916. return _ttail_file_cur_cmp(t, id, tm, res);
  917. }
  918. inline int _ttail_file_cur_cmp(ttail_t* t, size_t id, const struct tm* tm ,
  919. int* res)
  920. {
  921. int ret;
  922. struct tm ctm;
  923. if(ttail_file_getline(t, id) < 0)
  924. {
  925. return -1;
  926. }
  927. ret = ttail_logline2date(t, ttail_file_getline_buf(t), &ctm);
  928. if(ret < 0)
  929. {
  930. return -1;
  931. }
  932. else if(ret > 1)
  933. {
  934. return 1;
  935. }
  936. *res = ttail_tm_cmp(&ctm, tm);
  937. return 0;
  938. }
  939. void _ttail_search_file_free(ttail_t* t)
  940. {
  941. if(!t->session)
  942. {
  943. return;
  944. }
  945. if(t->session->file.buf)
  946. {
  947. free(t->session->file.buf);
  948. }
  949. if(t->session->file.file_sz)
  950. {
  951. free(t->session->file.file_sz);
  952. }
  953. if(t->session->file.vfile)
  954. {
  955. free(t->session->file.vfile);
  956. }
  957. free(t->session);
  958. }