/*
* Copyright 2017 Yann Weber
*
* This file is part of Ttail.
*
* Ttail is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Ttail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Ttail. If not, see .
*/
#ifndef _ttail_search_files_h__
#define _ttail_search_files_h__
#include
#include
#include
#include
#include
#include
#include
typedef struct _ttail_search_file_s ttail_search_file_t;
struct _ttail_files_off_s
{
size_t id;
off_t off;
};
typedef struct _ttail_files_off_s ttail_files_off_t;
/*session->file.buf), &(TTAIL->session->file.buf_sz),\
TTAIL->logfile[ID]))
/*session->file.buf)
/**@brief Move an element to end
*@param void* v : array
*@param size_t i : elt to move
*@param size_t count : v elements count - 1
*@param type t : v type
*/
#define TTAIL_MVF(v, i, count, t) {\
t tmp;\
tmp = v[i];\
memmove(v+i, v+i+1, (count - i) * sizeof(t));\
v[count] = tmp;\
}
/**@brief Move back an element to j position
*@warning j < i
*@param void* v
*@param size_t i : src index
*@param size_t j : dst index
*@param type t : v type
*/
#define TTAIL_MVBF(v, i, j, t) {\
t tmp;\
tmp = v[i];\
memmove(v+j+1, v+j, (i - j)*sizeof(t));\
v[j] = tmp;\
}
/**@brief Init the ttail->session
*@note Needed for format autodetection
*@return 0 on success -1 on failure
*/
int ttail_search_files_init(ttail_t*);
/**@brief Date format initialisation using logfiles
*
*If no format set yet attempt to guess it from logfiles
*@param ttail_t*
*@return 0 on success -1 on failure 1 if format was allready set
*@see _ttail_search_files_fmt_guess()
*/
int _ttail_search_files_fmt_init(ttail_t*);
/**@brief @ref ttail_search_closest() implementation for logfiles
*
*@warning Expect that ttail_search_closest_files_init() has been called
*Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
*@param ttail_t*
*@return 0 if ok -1 if fatal error 1 if not found
*/
int _ttail_search_closest_files(ttail_t*);
/**@brief Output result loglines to stdout
*@param ttail_t*
*@param int fd
*/
void _ttail_search_print_files(ttail_t*, int);
int _ttail_search_closest_files_set_fsizes(ttail_t*);
/**@brief Binary search of the last logline with a date < tm
*@param ttail ttail_t*
*@param tm struct tm*
*param min short if 1 process min else process max
*@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
*@return 0 if ok -1 if error 1 if empty result
*/
int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
const struct tm**, short);
/**@brief Binary search of the last logline with a date < tm in a file
*
*@note uses ttail_t session data to know where to search
*@param ttail ttail_t*
*@param tm struct tm*
*@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
*param min short if 1 process min else process max
*@return 0 if ok -1 if error 1 if empty result
*/
int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
const struct tm**, short);
/**@brief Attempt to reopen a file
*@param ttail_t* ttail
*@param size_t id file id in ttail
*@return 0 on success, -1 on failure and errno is set
*@throw EINVAL if id is too big
*/
int _ttail_file_reopen(ttail_t*, size_t);
/**@brief Set min & max date of file
*@param ttail_t* ttail
*@param size_t id file id in ttail
*@param struct tm[2] will be set to min & max
*@return 0 on success 1 if no date found or no fp -1 if error
*/
inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
/**@brief Sort logfiles list
*@param ttail_t* ttail
*@param struct tm** ftm : files min and max date
*@return 0 on success -1 if error
*/
inline int _ttail_file_sort(ttail_t*, struct tm**);
/**@brief Search next line
*
*Set f pos to next line begining and return the position
*@param ttail_t* t
*@param size_t id logfile id
*@return -1 on error 0 on EOF else return the next line position
*/
inline long _ttail_file_next_line(ttail_t*, size_t);
/**@brief Search line start
*
*Set f pos to line begining and return the position
*@param ttail_t* t
*@param size_t id logfile id
*@return -1 on error else return the next line position
*/
inline long _ttail_file_start_line(ttail_t*, size_t);
/**@brief Search last line with a date < tm from EOF
*@param ttail ttail_t*
*@param id size_t
*@param tm const struct tm*
*@return -1 on error else the offset of the line first chr
*/
inline off_t _ttail_file_search_from_end(ttail_t*, size_t, const struct tm*);
/**@brief Read a line from off and compare its date to tm
*@param t ttail_t*
*@param id site_t
*@param off off_t
*@param tm const struct tm*
*@param res int* see return value of @ref ttail_tm_cmp()
*@return 0 on success -1 on error 1 if no date found
*/
inline int _ttail_file_off_cmp(ttail_t*, size_t, off_t, const struct tm*, int*);
/**@brief Same than _ttail_file_off_cmp() but from current pos
*@return 0 on success -1 on error 1 if no date found*/
inline int _ttail_file_cur_cmp(ttail_t*, size_t, const struct tm*, int*);
/**@brief Free the ttail_search_file_t session
*@param ttail_t* ttail
*/
void _ttail_search_file_free(ttail_t*);
#endif