-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmti_test.c
223 lines (188 loc) · 5.72 KB
/
fmti_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdlib.h>
#include <dirent.h>
#include <assert.h>
#include <getopt.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/stat.h>
#include "fmti.h"
#define MAX_PATH_LEN 512
#define FMTI_DEF_LIB_SRC "/etc/fmti/full"
#define FMTI_TEST_HELP \
"Usage: %s [OPTION...] [STRING...]\n\n" \
" -h, --help display this help and exit\n"\
"\n"\
" -s, --sig-path set the signature path (def is /etc/fmti/full)\n"\
" -c, --check-file check the filetype of file or file in dir\n"\
"\n"
uint8_t *g_matchmode_tbl[] =
{
"FMTI_MATCH_MODE_NO",
"FMTI_MATCH_MODE_EXT",
"FMTI_MATCH_MODE_REG_PRIO",
"FMTI_MATCH_MODE_REG_EXT",
"FMTI_MATCH_MODE_REG_ID",
};
typedef enum
{
FMTI_TEST_CMD_NO = 0,
FMTI_TEST_CMD_CHECK_FILE,
} fmti_test_cmd;
static void checkFiles(FMTI_S *fmti, char *path);
static void fmti_test_help(void);
static char *progname; /* used throughout */
static int opt;
static struct option longopts[] =
{
{"help", 0, NULL, 'h'},
{"sig-path", 1, NULL, 's'},
{"check-file", 1, NULL, 'c'},
{0,0,0,0}
};
int main(int argc, char **argv)
{
int ret = FMTI_OK;
FMTI_S *fmti = NULL;
char *sig_path = FMTI_DEF_LIB_SRC;
int cmd = FMTI_TEST_CMD_NO;
int helpinfo = 1;
char *file_path = NULL;
if((progname = strrchr(argv[0], '/')) != NULL)
progname++;
else
progname = argv[0];
while((opt = getopt_long(argc, argv, "hs:c:", longopts, NULL)) != -1)
{
switch(opt)
{
case 's':
sig_path = strdup(optarg);
break;
case 'c':
cmd = FMTI_TEST_CMD_CHECK_FILE;
file_path = strdup(optarg);
helpinfo = 0;
break;
case 'h':
helpinfo++;
break;
case '?':
helpinfo++;
break;
default:
helpinfo++;
break;
}
}
if(helpinfo)
{
fmti_test_help();
}
if(cmd == FMTI_TEST_CMD_CHECK_FILE)
{
fmti = fmti_init();
if(NULL == fmti)
{
printf("fmti_init ERR\n");
goto exit;
}
ret = fmti_load_lib(fmti, sig_path);
if(ret != FMTI_OK)
{
printf("fmti_load_lib %s ERR\n", sig_path);
goto exit;
}
printf("load %d lib OK\n", fmti->lib_num);
//fmti_print_all_lib(fmti->lib);
checkFiles(fmti, file_path);
}
exit:
fmti_free(fmti);
return 0;
}
static void checkFiles(FMTI_S *fmti, char *path)
{
char tmp_path[MAX_PATH_LEN];
struct dirent *filename;
DIR *dir;
struct stat s;
FMTI_MATCH_RESULT match_res;
lstat(path, &s);
if(!S_ISDIR(s.st_mode))
{
fmti_file(fmti, path, &match_res);
printf("[file]: %s\n", path);
printf("[file ext]: %s\n", match_res.ext);
printf("[ext match]: %d\n", match_res.ext_match);
printf("[matchmode]: %s\n", g_matchmode_tbl[match_res.match_mode]);
printf("[priority]: %d\n", match_res.priority);
printf("[id]: %d\n", match_res.id);
if(match_res.match_lib)
{
FMTI_LIB *match_lib = match_res.match_lib;
if(match_lib->puid)
printf("[puid]: %s\n", match_lib->puid);
printf("[name]: %s\n", match_lib->name);
if(match_lib->mime)
printf("[mime]: %s\n", match_lib->mime);
if(match_lib->ext)
printf("[lib_ext]: %s\n", match_lib->ext);
//printf("[priority]: %d\n", match_lib->priority);
}
printf("\n");
}
else
{
dir = opendir(path);
if(dir == NULL)
{
printf("open dir %s error!\n", path);
exit(1);
}
while((filename = readdir(dir)) != NULL)
{
if(!strcmp(filename->d_name, ".")||!strcmp(filename->d_name, ".."))
continue;
sprintf(tmp_path, "%s/%s", path, filename->d_name);
lstat(tmp_path, &s);
if(S_ISDIR(s.st_mode))
{
checkFiles(fmti, tmp_path);
}
else
{
fmti_file(fmti, path, &match_res);
printf("[file]: %s\n", tmp_path);
printf("[file ext]: %s\n", match_res.ext);
printf("[ext match]: %d\n", match_res.ext_match);
printf("[matchmode]: %s\n", g_matchmode_tbl[match_res.match_mode]);
printf("[priority]: %d\n", match_res.priority);
printf("[id]: %d\n", match_res.id);
if(match_res.match_lib)
{
FMTI_LIB *match_lib = match_res.match_lib;
if(match_lib->puid)
printf("[puid]: %s\n", match_lib->puid);
printf("[name]: %s\n", match_lib->name);
if(match_lib->mime)
printf("[mime]: %s\n", match_lib->mime);
if(match_lib->ext)
printf("[lib_ext]: %s\n", match_lib->ext);
//printf("[priority]: %d\n", match_lib->priority);
}
printf("\n");
}
}
closedir(dir);
}
}
static void fmti_test_help(void)
{
(void)fprintf(stderr, FMTI_TEST_HELP, progname);
exit(1);
}