Browse Source

File spliting

Yann Weber 7 years ago
parent
commit
2ff86aadef
6 changed files with 218 additions and 166 deletions
  1. 5
    4
      src/Makefile.am
  2. 11
    46
      src/include/ttail_search.h
  3. 56
    0
      src/include/ttail_search_files.h
  4. 1
    112
      src/ttail_search.c
  5. 135
    0
      src/ttail_search_files.c
  6. 10
    4
      tests/ttail_search_check.c

+ 5
- 4
src/Makefile.am View File

@@ -1,8 +1,9 @@
1 1
 bin_PROGRAMS = ttail
2 2
 noinst_LIBRARIES= libttail.a
3 3
 
4
-ttail_SOURCES = main.c ttail_init.c ttail_search.c
5
-libttail_a_SOURCES = ttail_init.c ttail_search.c
4
+libttail_a_SOURCES = ttail_init.c ttail_search.c ttail_search_files.c
5
+ttail_SOURCES = main.c $(libttail_a_SOURCES)
6
+#ttail_SOURCES = main.c ttail_init.c ttail_search.c ttail_search_files.c
6 7
 if HUGEFILE
7 8
 ttail_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
8 9
 libttail_a_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
@@ -11,10 +12,10 @@ endif
11 12
 if DEBUG
12 13
 if HUGEFILE
13 14
 bin_PROGRAMS += ttail_no_huge
14
-ttail_no_huge_SOURCES = main.c ttail_init.c ttail_search.c
15
+ttail_no_huge_SOURCES = $(ttail_SOURCES)
15 16
 else
16 17
 bin_PROGRAMS += ttail_huge
17
-ttail_huge_SOURCES = main.c ttail_init.c ttail_search.c
18
+ttail_huge_SOURCES = $(ttail_SOURCES)
18 19
 ttail_huge_CFLAGS = @CFLAGS@ -DTTAIL_HUGE_FILE
19 20
 endif
20 21
 endif

+ 11
- 46
src/include/ttail_search.h View File

@@ -10,31 +10,21 @@
10 10
 #include <unistd.h>
11 11
 
12 12
 typedef union _ttail_search_u ttail_search_t;
13
-
14
-#include "config.h"
13
+typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
15 14
 #include "ttail.h"
16
-
17 15
 typedef char* (*ttail_search_subst_f)(ttail_t*, const char*);
16
+/**@brief Comparison results
17
+ *
18
+ * -1 if a < b, 1 if a > b, 0 if a == b, -2 if exception
19
+ *
20
+ * If an exception occur, exception flags are set with | on the result.
21
+ * Exceptions flags are : TTAIL_NODATE_A and TTAIL_NODATE_B
22
+ */
23
+typedef int ttail_cmp_res;
18 24
 
19
-typedef struct _ttail_search_file_s ttail_search_file_t;
20
-typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
21 25
 
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
-};
26
+#include "config.h"
27
+#include "ttail_search_files.h"
38 28
 
39 29
 /*<! Private search session for stdin */
40 30
 struct _ttail_search_stdin_s
@@ -52,43 +42,18 @@ union _ttail_search_u
52 42
 	ttail_search_stdin_t std;
53 43
 };
54 44
 
55
-/**@brief Comparison results
56
- *
57
- * -1 if a < b, 1 if a > b, 0 if a == b, -2 if exception
58
- *
59
- * If an exception occur, exception flags are set with | on the result.
60
- * Exceptions flags are : TTAIL_NODATE_A and TTAIL_NODATE_B
61
- */
62
-typedef int ttail_cmp_res;
63
-
64 45
 /**@brief Begin a search session placing it in a ready to print situation
65 46
  *@param ttail_t*
66 47
  *@return 0 if ok -1 if fatal error 1 if not found
67 48
  */
68 49
 int ttail_search_closest(ttail_t*);
69 50
 
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 51
 /**@brief @ref ttail_search_closest() implementation for stdin
79 52
  *@param ttail_t*
80 53
  *@return 0 if ok -1 if fatal error 1 if not found
81 54
  */
82 55
 int _ttail_search_closest_stdin(ttail_t*);
83 56
 
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
-
92 57
 /**@brief Loglines comparison function
93 58
  *@param ttail_t* ttail
94 59
  *@param const char* logline a

+ 56
- 0
src/include/ttail_search_files.h View File

@@ -0,0 +1,56 @@
1
+#ifndef _ttail_search_files_h__
2
+#define _ttail_search_files_h__
3
+
4
+#include <errno.h>
5
+#include <string.h>
6
+#include <time.h>
7
+#include <regex.h>
8
+#include <sys/types.h>
9
+#include <sys/stat.h>
10
+#include <unistd.h>
11
+
12
+typedef struct _ttail_search_file_s ttail_search_file_t;
13
+
14
+
15
+/*<! Private search session for logfiles */
16
+struct _ttail_search_file_s
17
+{
18
+	/*<! logfile sizes */
19
+	off_t *file_sz;
20
+	#ifdef TTAIL_HUGE_FILE
21
+	/*<! Shift width to apply on size to compute stuff */
22
+	short sz_div;
23
+	#endif
24
+	/*<! Computed files start size */
25
+	off_t *vfile;
26
+	/*<! Computed file sizes sum */
27
+	off_t vsz;
28
+	/*<! Computed position */
29
+	off_t vpos;
30
+};
31
+
32
+#include "config.h"
33
+#include "ttail.h"
34
+#include "ttail_search.h"
35
+
36
+/**@brief @ref ttail_search_closest() implementation for logfiles
37
+ *@param ttail_t*
38
+ *@return 0 if ok -1 if fatal error 1 if not found
39
+ */
40
+int _ttail_search_closest_files(ttail_t*);
41
+int _ttail_search_closest_files_init(ttail_t*);
42
+int _ttail_search_closest_files_set_fsizes(ttail_t*);
43
+
44
+/**@brief Attempt to reopen a file
45
+ *@param ttail_t* ttail
46
+ *@param size_t id file id in ttail
47
+ *@return 0 on success, -1 on failure and errno is set
48
+ *@throw EINVAL if id is too big
49
+ */
50
+int _ttail_file_reopen(ttail_t*, size_t);
51
+
52
+/**@brief Free the ttail_search_file_t session
53
+ *@param ttail_t* ttail
54
+ */
55
+void _ttail_search_file_free(ttail_t*);
56
+#endif

+ 1
- 112
src/ttail_search.c View File

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

+ 135
- 0
src/ttail_search_files.c View File

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

+ 10
- 4
tests/ttail_search_check.c View File

@@ -32,11 +32,17 @@ void setup_closest_fileinit(void)
32 32
 	}
33 33
 }
34 34
 
35
+void teardown_closest_fileinit(void)
36
+{
37
+	teardown_closest();
38
+	_ttail_search_file_free(ttail);
39
+}
40
+
35 41
 START_TEST (test_search_closest_init)
36 42
 {
37 43
 	int ret;
38 44
 
39
-	ret = ttail_search_closest(ttail);
45
+	ret = _ttail_search_closest_files_init(ttail);
40 46
 	ck_assert_int_eq(ret,0);
41 47
 	ck_assert(ttail->session->file.vpos == 0);
42 48
 	#ifdef HUGEFILE
@@ -48,7 +54,7 @@ END_TEST
48 54
 START_TEST (test_search_closest_init_filesz)
49 55
 {
50 56
 	size_t i;
51
-	ttail_search_closest(ttail);
57
+	_ttail_search_closest_files_init(ttail);
52 58
 	for(i=0;i<5;i++)
53 59
 	{
54 60
 		ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
@@ -62,7 +68,7 @@ START_TEST (test_search_closest_init_vfile)
62 68
 	size_t i;
63 69
 	off_t full_sz;
64 70
 
65
-	ttail_search_closest(ttail);
71
+	_ttail_search_closest_files_init(ttail);
66 72
 
67 73
 	full_sz = 0;
68 74
 	for(i=0;i<5;i++)
@@ -83,7 +89,7 @@ Suite * ttail_search_suite(void)
83 89
 	tc_search_closest_fileinit = tcase_create("\
84 90
 ttail_logline_closest_files_init() checks");
85 91
 	tcase_add_checked_fixture(tc_search_closest_fileinit,
86
-		setup_closest_fileinit, teardown_closest);
92
+		setup_closest_fileinit, teardown_closest_fileinit);
87 93
 	tcase_add_test(tc_search_closest_fileinit, test_search_closest_init);
88 94
 	tcase_add_test(tc_search_closest_fileinit,
89 95
 		test_search_closest_init_filesz);

Loading…
Cancel
Save