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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/* fileset.c
* Routines for handling file sets
*
* $Id$
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#include <string.h>
#include <glib.h>
#include "globals.h"
#include <epan/filesystem.h>
#include "fileset.h"
typedef struct _fileset {
GList *entries;
const char *dirname;
} fileset;
/* this is the fileset's global data */
fileset set = { NULL, NULL};
/* is this a probable file of a file set (does the naming pattern match)? */
gboolean
fileset_filename_match_pattern(const char *fname)
{
char *pfx;
int baselen;
int minlen = strlen("_00001_20050418010750");
char *filename;
/* d:\dir1\test_00001_20050418010750.cap */
filename = g_strdup(get_basename(fname));
/* test_00001_20050418010750.cap */
pfx = strrchr(filename, '.');
if(pfx == NULL) {
return FALSE;
}
/* test_00001_20050418010750 */
*pfx = '\0';
/* filename long enough? */
baselen = strlen(filename);
if(baselen < minlen) {
g_free(filename);
return FALSE;
}
/* there must be two underscores at special places */
if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
g_free(filename);
return FALSE;
}
/* replace the two underscores by digits */
filename[baselen-minlen] = '0';
filename[baselen-minlen+6] = '0';
/* we should have only digits now */
while(minlen--) {
baselen--;
if(!isdigit(filename[baselen])) {
g_free(filename);
return FALSE;
}
}
g_free(filename);
/* ok, seems to be good */
return TRUE;
}
/* test, if both files could be in the same file set */
gboolean
fileset_is_file_in_set(const char *fname1, const char *fname2)
{
char *pfx1;
char *pfx2;
char *dup_f1;
char *dup_f2;
int minlen = strlen("_00001_20050418010750");
dup_f1 = g_strdup(fname1);
dup_f2 = g_strdup(fname2);
pfx1 = strrchr(dup_f1, '.');
pfx2 = strrchr(dup_f2, '.');
if(strcmp(pfx1, pfx2) != 0) {
g_free(dup_f1);
g_free(dup_f2);
return FALSE;
}
*(pfx1-minlen) = '\0';
*(pfx2-minlen) = '\0';
if(strcmp(dup_f1, dup_f2) != 0) {
g_free(dup_f1);
g_free(dup_f2);
return FALSE;
}
g_free(dup_f1);
g_free(dup_f2);
return TRUE;
}
/* we know this file is part of the set, so add it */
fileset_entry *
fileset_add_file(const char *dirname, const char *fname, gboolean current)
{
int fh, result;
struct stat buf;
char *path;
fileset_entry *entry = NULL;
path = g_strdup_printf("%s%s", dirname, fname);
fh = open( path, O_RDONLY );
if(fh != -1) {
/* Get statistics */
result = fstat( fh, &buf );
/* Show statistics if they are valid */
if( result == 0 ) {
entry = g_malloc(sizeof(fileset_entry));
entry->fullname = g_strdup(path);
entry->name = g_strdup(fname);
entry->ctime = buf.st_ctime;
entry->mtime = buf.st_mtime;
entry->size = buf.st_size;
entry->current = current;
set.entries = g_list_append(set.entries, entry);
}
close(fh);
}
g_free(path);
return entry;
}
/* compare two list entries by creation date/time */
gint
fileset_compare_creation(gconstpointer a, gconstpointer b)
{
const fileset_entry *entry_a = a;
const fileset_entry *entry_b = b;
return entry_a->ctime - entry_b->ctime;
}
/* add all file set entries to the dialog */
void fileset_update_dlg(void)
{
GList *le;
/* add all entires to the dialog */
le = g_list_first(set.entries);
while(le) {
fileset_dlg_add_file(le->data);
le = g_list_next(le);
}
}
/* walk through the directory of the loaded file and add every file matching the current file */
void
fileset_add_dir(const char *fname)
{
#if GLIB_MAJOR_VERSION < 2
DIR *dir; /* scanned directory */
struct dirent *file; /* current file */
gchar *name;
#else /* GLIB 2 */
GDir *dir; /* scanned directory */
GError **dummy;
const char *name;
#endif
fileset_entry *entry;
GString *dirname;
gchar *fname_dup;
/* get (convert) directory name, but don't touch the given string */
fname_dup = get_dirname(g_strdup(fname));
dirname = g_string_new(fname_dup);
g_free(fname_dup);
set.dirname = g_strdup(dirname->str);
dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
dummy = g_malloc(sizeof(GError *));
*dummy = NULL;
/* if the current file can't be part of any fileset, do nothing */
if(!fileset_filename_match_pattern(fname)) {
entry = fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
if(entry) {
fileset_dlg_add_file(entry);
}
}
/* go through the files in the directory and check if it's part of the current file set */
#if GLIB_MAJOR_VERSION < 2
if ((dir = opendir(dirname)) != NULL) {
while ((file = readdir(dir)) != NULL) {
name = (gchar *)file->d_name;
#else
if ((dir = g_dir_open(dirname->str, 0, dummy)) != NULL) {
while ((name = g_dir_read_name(dir)) != NULL) {
#endif
if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
}
}
}
g_free(dummy);
g_string_free(dirname, TRUE /* free_segment */);
/* sort entries by creation time */
set.entries = g_list_sort(set.entries, fileset_compare_creation);
fileset_update_dlg();
}
/* get current directory name */
const char *
fileset_get_dirname(void)
{
return set.dirname;
}
/* get the current list entry, or NULL */
GList *
fileset_get_current(void)
{
GList *le;
fileset_entry *entry;
/* add all entires to the dialog */
le = g_list_first(set.entries);
while(le) {
entry = le->data;
if(entry->current) {
return le;
}
le = g_list_next(le);
}
return NULL;
}
/* get the file set entry after the current one, or NULL */
fileset_entry *
fileset_get_next(void)
{
GList *le;
le = fileset_get_current();
if(le == NULL) {
return NULL;
}
le = g_list_next(le);
if(le == NULL) {
return NULL;
}
return le->data;
}
/* get the file set entry before the current one, or NULL */
fileset_entry *
fileset_get_previous(void)
{
GList *le;
le = fileset_get_current();
if(le == NULL) {
return NULL;
}
le = g_list_previous(le);
if(le == NULL) {
return NULL;
}
return le->data;
}
/* delete a single entry */
void fileset_entry_delete(gpointer data, gpointer user_data)
{
fileset_entry *entry = data;
g_free( (gpointer) entry->fullname);
entry->fullname = NULL;
g_free( (gpointer) entry->name);
entry->name = NULL;
}
/* delete the whole file set */
void fileset_delete(void)
{
/* free the entry list */
if(set.entries) {
g_list_foreach(set.entries, fileset_entry_delete, NULL);
g_list_free(set.entries);
set.entries = NULL;
}
/* free the rest */
if(set.dirname) {
g_free( (gpointer) set.dirname);
set.dirname = NULL;
}
}
|