Browse Source

Started to implement file search op

Yann Weber 7 years ago
parent
commit
26f51ae8a6
7 changed files with 277 additions and 28 deletions
  1. 11
    1
      configure.ac
  2. 16
    1
      src/Makefile.am
  3. 0
    5
      src/include/config.h.in
  4. 13
    2
      src/include/ttail.h
  5. 90
    4
      src/include/ttail_search.h
  6. 120
    1
      src/ttail_search.c
  7. 27
    14
      tests/ttail_search_check.c

+ 11
- 1
configure.ac View File

6
 AM_INIT_AUTOMAKE
6
 AM_INIT_AUTOMAKE
7
 
7
 
8
 #configure options
8
 #configure options
9
+AC_ARG_ENABLE([huge-file],[  --enable-huge-file to enable huge file support],
10
+[case "${enableval}" in
11
+	yes) huge_file=true ;;
12
+	no)  huge_file=false ;;
13
+	*) AC_MSG_ERROR([bad value ${enableval} for --enable-huge-file]) ;;
14
+esac],[huge_file=false])
15
+AM_CONDITIONAL([HUGEFILE], [test x$huge_file = xtrue])
9
 AC_ARG_ENABLE([debug],[  --enable-debug turn on debugging],
16
 AC_ARG_ENABLE([debug],[  --enable-debug turn on debugging],
10
 [case "${enableval}" in
17
 [case "${enableval}" in
11
 	yes) debug=true ;;
18
 	yes) debug=true ;;
51
 	AX_CHECK_CFLAGS([-fgnu89-inline])
58
 	AX_CHECK_CFLAGS([-fgnu89-inline])
52
 ])
59
 ])
53
 AX_CHECK_CFLAGS([-D_XOPEN_SOURCE -D_GNU_SOURCE])
60
 AX_CHECK_CFLAGS([-D_XOPEN_SOURCE -D_GNU_SOURCE])
54
-
61
+#Condition done in Makefile.am
62
+#AS_IF([test x$huge_file = xtrue], [
63
+#AX_CHECK_CFLAGS([-DTTAIL_HUGE_FILE])
64
+#],[])
55
 
65
 
56
 # Checks for library functions.
66
 # Checks for library functions.
57
 AC_FUNC_MALLOC
67
 AC_FUNC_MALLOC

+ 16
- 1
src/Makefile.am View File

3
 
3
 
4
 libttail_init_a_SOURCES = ttail_init.c
4
 libttail_init_a_SOURCES = ttail_init.c
5
 libttail_search_a_SOURCES = ttail_search.c
5
 libttail_search_a_SOURCES = ttail_search.c
6
-libttail_search_a_CFLAGS = @CFLAGS@ -D_NOSTATIC
7
 ttail_SOURCES = main.c ttail_init.c ttail_search.c
6
 ttail_SOURCES = main.c ttail_init.c ttail_search.c
7
+if HUGEFILE
8
+ttail_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
9
+libttail_search_a_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
10
+libttail_init_a_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
11
+endif
12
+
13
+if DEBUG
14
+if HUGEFILE
15
+bin_PROGRAMS += ttail_no_huge
16
+ttail_no_huge_SOURCES = main.c ttail_init.c ttail_search.c
17
+else
18
+bin_PROGRAMS += ttail_huge
19
+ttail_huge_SOURCES = main.c ttail_init.c ttail_search.c
20
+ttail_huge_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
21
+endif
22
+endif

+ 0
- 5
src/include/config.h.in View File

1
 /* src/include/config.h.in.  Generated from configure.ac by autoheader.  */
1
 /* src/include/config.h.in.  Generated from configure.ac by autoheader.  */
2
 
2
 
3
 /* used by libcheck to disable static functions */
3
 /* used by libcheck to disable static functions */
4
-#ifdef _NOSTATIC
5
-#define _static
6
-#else
7
-#define _static static
8
-#endif
9
 
4
 
10
 #undef TTAIL_FMT_REGEX
5
 #undef TTAIL_FMT_REGEX
11
 
6
 

+ 13
- 2
src/include/ttail.h View File

12
 #include <time.h>
12
 #include <time.h>
13
 #include <unistd.h>
13
 #include <unistd.h>
14
 
14
 
15
+typedef struct _ttail_s ttail_t;
16
+
17
+#include "config.h"
18
+#include "ttail_search.h"
19
+
15
 #define TTAIL_FLAG_PREFIX 1
20
 #define TTAIL_FLAG_PREFIX 1
16
 #define TTAIL_FLAG_DATE_MIN 2
21
 #define TTAIL_FLAG_DATE_MIN 2
17
 #define TTAIL_FLAG_DATE_MAX 4
22
 #define TTAIL_FLAG_DATE_MAX 4
32
 
37
 
33
 	/*! A strptime format matching datetime in logfile */
38
 	/*! A strptime format matching datetime in logfile */
34
 	char *fmt;
39
 	char *fmt;
35
-
40
+	
41
+	/*! Date min set from cli */
36
 	struct tm date_min;
42
 	struct tm date_min;
37
 	struct tm date_max;
43
 	struct tm date_max;
38
 
44
 
39
 	int verbose;
45
 	int verbose;
46
+	/**@brief Status flag
47
+	 *
48
+	 *see TTAIL_FLAG_*
49
+	 */
40
 	int flag;
50
 	int flag;
51
+
52
+	ttail_search_t *session;
41
 };
53
 };
42
-typedef struct _ttail_s ttail_t;
43
 
54
 
44
 #endif
55
 #endif

+ 90
- 4
src/include/ttail_search.h View File

1
-#ifndef _ttail_init_h__
2
-#define _ttail_init_h__
1
+#ifndef _ttail_search_h__
2
+#define _ttail_search_h__
3
 
3
 
4
+#include <errno.h>
4
 #include <string.h>
5
 #include <string.h>
5
 #include <time.h>
6
 #include <time.h>
6
 #include <regex.h>
7
 #include <regex.h>
8
+#include <sys/types.h>
9
+#include <sys/stat.h>
10
+#include <unistd.h>
11
+
12
+typedef union _ttail_search_u ttail_search_t;
7
 
13
 
8
 #include "config.h"
14
 #include "config.h"
9
 #include "ttail.h"
15
 #include "ttail.h"
10
 
16
 
17
+typedef char* (*ttail_search_subst_f)(ttail_t*, const char*);
18
+
19
+typedef struct _ttail_search_file_s ttail_search_file_t;
20
+typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
21
+
22
+/*<! Private search session for logfiles */
23
+struct _ttail_search_file_s
24
+{
25
+	/*<! logfile sizes */
26
+	off_t *file_sz;
27
+	#ifdef TTAIL_HUGE_FILE
28
+	/*<! Shift width to apply on size to compute stuff */
29
+	short sz_div;
30
+	#endif
31
+	/*<! Computed files start size */
32
+	off_t *vfile;
33
+	/*<! Computed file sizes sum */
34
+	off_t vsz;
35
+	/*<! Computed position */
36
+	off_t vpos;
37
+};
38
+
39
+/*<! Private search session for stdin */
40
+struct _ttail_search_stdin_s
41
+{
42
+	char *buff;
43
+	size_t buff_sz;
44
+};
45
+
46
+/**@brief Search session is an union depending in the search method
47
+ * employed
48
+ */
49
+union _ttail_search_u
50
+{
51
+	ttail_search_file_t file;
52
+	ttail_search_stdin_t std;
53
+};
54
+
11
 /**@brief Comparison results
55
 /**@brief Comparison results
12
  *
56
  *
13
  * -1 if a < b, 1 if a > b, 0 if a == b, -2 if exception
57
  * -1 if a < b, 1 if a > b, 0 if a == b, -2 if exception
17
  */
61
  */
18
 typedef int ttail_cmp_res;
62
 typedef int ttail_cmp_res;
19
 
63
 
64
+/**@brief Begin a search session placing it in a ready to print situation
65
+ *@param ttail_t*
66
+ *@return 0 if ok -1 if fatal error 1 if not found
67
+ */
68
+int ttail_search_closest(ttail_t*);
69
+
70
+/**@brief @ref ttail_search_closest() implementation for logfiles
71
+ *@param ttail_t*
72
+ *@return 0 if ok -1 if fatal error 1 if not found
73
+ */
74
+int _ttail_search_closest_files(ttail_t*);
75
+int _ttail_search_closest_files_init(ttail_t*);
76
+int _ttail_search_closest_files_set_fsizes(ttail_t*);
77
+
78
+/**@brief @ref ttail_search_closest() implementation for stdin
79
+ *@param ttail_t*
80
+ *@return 0 if ok -1 if fatal error 1 if not found
81
+ */
82
+int _ttail_search_closest_stdin(ttail_t*);
83
+
84
+/**@brief Attempt to reopen a file
85
+ *@param ttail_t* ttail
86
+ *@param size_t id file id in ttail
87
+ *@return 0 on success, -1 on failure and errno is set
88
+ *@throw EINVAL if id is too big
89
+ */
90
+int _ttail_file_reopen(ttail_t*, size_t);
91
+
20
 /**@brief Loglines comparison function
92
 /**@brief Loglines comparison function
21
  *@param ttail_t* ttail
93
  *@param ttail_t* ttail
22
  *@param const char* logline a
94
  *@param const char* logline a
44
 /**@brief Apply the choosen substitution to the logline
116
 /**@brief Apply the choosen substitution to the logline
45
  *@param ttail_t* ttail
117
  *@param ttail_t* ttail
46
  *@param const char* logline
118
  *@param const char* logline
47
- *@return a pointer on the begining of the subst string
119
+ *@return a pointer on the end of the subst string
120
+ */
121
+const char* ttail_logline_subst(ttail_t*, const char*);
122
+
123
+/**@brief Regex logline prefix substitution
124
+ *@param ttail_t* ttail
125
+ *@param const char* logline
126
+ *@return a pointer on the end of the subst string
127
+ */
128
+const char* _ttail_logline_subst_re(ttail_t*, const char*);
129
+
130
+/**@brief Static len logline prefix substitution
131
+ *@param ttail_t* ttail
132
+ *@param const char* logline
133
+ *@return a pointer on the end of the subst string
48
  */
134
  */
49
-_static const char* ttail_logline_subst(ttail_t*, const char*);
135
+const char* _ttail_logline_subst_len(ttail_t*, const char*);
50
 
136
 
51
 #endif
137
 #endif

+ 120
- 1
src/ttail_search.c View File

1
 #include "ttail_search.h"
1
 #include "ttail_search.h"
2
 
2
 
3
+int ttail_search_closest(ttail_t* ttail)
4
+{
5
+	if(ttail->session != NULL)
6
+	{
7
+		fprintf(stderr, "A session is allready started\n");
8
+		return -1;
9
+	}
10
+	ttail->session = (ttail_search_t*)malloc(ttail->logfile_sz?
11
+		sizeof(ttail_search_file_t):sizeof(ttail_search_stdin_t));
12
+	if(!ttail->session)
13
+	{
14
+		perror("Unable to allocate memory for search session");
15
+		return -1;
16
+	}
17
+	return ttail->logfile_sz?\
18
+		_ttail_search_closest_files(ttail):\
19
+		_ttail_search_closest_stdin(ttail);
20
+}
21
+
22
+int _ttail_search_closest_files(ttail_t* t)
23
+{
24
+	return _ttail_search_closest_files_init(t);
25
+}
26
+
27
+int _ttail_search_closest_files_init(ttail_t* t)
28
+{
29
+	struct stat stat;
30
+	FILE *fp;
31
+	size_t i;
32
+	off_t *file_sz;
33
+
34
+	file_sz = malloc(sizeof(off_t)*t->logfile_sz);
35
+	if(!file_sz)
36
+	{
37
+		perror("Unable to allocate memory for file sizes");
38
+		goto _ttail_search_closest_files_alloc_err;
39
+	}
40
+	t->session->file.file_sz = file_sz;
41
+	#ifdef HUGEFILE
42
+	t->session->file.sz_div = 0;
43
+	#endif
44
+
45
+	for(i=0;i<t->logfile_sz;i++)
46
+	{
47
+		fp = t->logfile[i];
48
+		if(!fp)
49
+		{
50
+			if(_ttail_file_reopen(t,i))
51
+			{
52
+				file_sz[i] = 0;
53
+				continue;
54
+			}
55
+		}
56
+		if(fstat(fileno(fp), &stat))
57
+		{
58
+			perror("Unable to get file size");
59
+			goto _ttail_search_closest_files_err;
60
+		}
61
+		file_sz[i] = stat.st_size;
62
+	}
63
+	/* we got all real size, determining if we need a divisor */
64
+	/*
65
+	 * not implemented
66
+	 */
67
+
68
+	_ttail_search_closest_files_err:
69
+	free(file_sz);
70
+	t->session->file.file_sz = NULL;
71
+	_ttail_search_closest_files_alloc_err:
72
+	return -1;
73
+}
74
+
75
+int _ttail_search_closest_files_set_fsizes(ttail_t* t)
76
+{
77
+	size_t i;
78
+	off_t *vfile, *vsz;
79
+	vfile = malloc(sizeof(off_t)*t->logfile_sz);
80
+	if(!vfile)
81
+	{
82
+		perror("Unable to allocate memory for file size sum");
83
+		return -1;
84
+	}
85
+	t->session->file.vfile = vfile;
86
+	vsz = &(t->session->file.vsz);
87
+	*vsz = 0;
88
+	for(i=0; i<t->logfile_sz;i++)
89
+	{
90
+		vfile[i] = *vsz;
91
+		#ifdef HUGEFILE
92
+		vsz += t->session->file.file_sz[i] >> t->session->file.sz_div;
93
+		#else
94
+		vsz += t->session->file.file_sz[i];
95
+		#endif
96
+	}
97
+	t->session->file.vpos = 0;
98
+	return 0;
99
+}
100
+
101
+int _ttail_file_reopen(ttail_t* t, size_t id)
102
+{
103
+	if(t->logfile[id])
104
+	{
105
+		fclose(t->logfile[id]);
106
+	}
107
+	t->logfile[id] = fopen(t->logfile_name[id], "r");
108
+	if(!t->logfile[id] && t->verbose > 2)
109
+	{
110
+		fprintf(stderr, "Unable to reopen '%s'\n",
111
+			t->logfile_name[id]);
112
+	}
113
+	return t->logfile[id]?0:-1;
114
+}
115
+
116
+int _ttail_search_closest_stdin(ttail_t* t)
117
+{
118
+	fprintf(stderr, "Search on stdin not implemented yet");
119
+	return -1;
120
+}
121
+
3
 int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
122
 int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
4
 {
123
 {
5
 	ttail_logline_subst(ttail, logline);
124
 	ttail_logline_subst(ttail, logline);
6
 	return 0;
125
 	return 0;
7
 }
126
 }
8
 
127
 
9
-_static const char* ttail_logline_subst(ttail_t* ttail, const char* logline)
128
+const char* ttail_logline_subst(ttail_t* ttail, const char* logline)
10
 {
129
 {
11
 	return logline;
130
 	return logline;
12
 }
131
 }

+ 27
- 14
tests/ttail_search_check.c View File

1
 #include <check.h>
1
 #include <check.h>
2
+#include <libgen.h>
2
 
3
 
3
 #include "ttail.h"
4
 #include "ttail.h"
4
 #include "ttail_search.h"
5
 #include "ttail_search.h"
5
 
6
 
6
 ttail_t *ttail;
7
 ttail_t *ttail;
7
-#define __Fname_sz 5
8
-#define FNAME_NO_EXIST 0
9
-#define FNAME_EXIST 1
10
 
8
 
11
-START_TEST (test_search_subst)
9
+char *samples[5] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
10
+	"./samples/4.log", "./samples/5.log"};
11
+off_t samples_sz[5] = { 442, 141, 893, 2587, 2310 };
12
+
13
+
14
+void setup_closest(void)
15
+{
16
+
17
+}
18
+
19
+void teardown_closest(void)
20
+{
21
+
22
+}
23
+
24
+START_TEST (test_search_closest)
12
 {
25
 {
13
 	ttail_logline_subst(ttail, "Hello world !");
26
 	ttail_logline_subst(ttail, "Hello world !");
14
 }
27
 }
17
 Suite * ttail_search_suite(void)
30
 Suite * ttail_search_suite(void)
18
 {
31
 {
19
 	Suite *s;
32
 	Suite *s;
20
-	TCase *tc_search_subst;
33
+	TCase *tc_search_closest;
21
 
34
 
22
 	s = suite_create("ttail search checks");
35
 	s = suite_create("ttail search checks");
23
-	tc_search_subst = tcase_create("ttail_logline_subst checks");
24
-	/*
25
-	tcase_add_checked_fixture(tc_serch_,
26
-		setup_ttail_empty, teardown_ttail);
27
-	*/
28
-	tcase_add_test(tc_search_subst, test_search_subst);
29
-
30
-	suite_add_tcase(s, tc_search_subst);
36
+	tc_search_closest = tcase_create("ttail_logline_closest checks");
37
+	tcase_add_checked_fixture(tc_search_closest,
38
+		setup_closest, teardown_closest);
39
+	tcase_add_test(tc_search_closest, test_search_closest);
40
+
41
+	suite_add_tcase(s, tc_search_closest);
31
 	return s;
42
 	return s;
32
 }
43
 }
33
 
44
 
34
-int main(void)
45
+int main(int argc, char **argv)
35
 {
46
 {
36
 	int number_failed = 0;
47
 	int number_failed = 0;
37
 	SRunner *sr;
48
 	SRunner *sr;
38
 
49
 
50
+	chdir(dirname(argv[0])); /* move in ./tests dir */
51
+
39
 	sr = srunner_create(ttail_search_suite());
52
 	sr = srunner_create(ttail_search_suite());
40
 	srunner_set_fork_status(sr, CK_FORK);
53
 	srunner_set_fork_status(sr, CK_FORK);
41
 
54
 

Loading…
Cancel
Save