2 * Routines for handling file sets
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
45 #ifdef HAVE_SYS_WAIT_H
46 # include <sys/wait.h>
54 #include <wiretap/file_util.h>
57 #include <epan/filesystem.h>
63 typedef struct _fileset {
68 /* this is the fileset's global data */
69 fileset set = { NULL, NULL};
72 /* is this a probable file of a file set (does the naming pattern match)? */
74 fileset_filename_match_pattern(const char *fname)
78 int minlen = strlen("_00001_20050418010750");
82 /* d:\dir1\test_00001_20050418010750.cap */
83 filename = g_strdup(get_basename(fname));
85 /* test_00001_20050418010750.cap */
86 pfx = strrchr(filename, '.');
90 /* test_00001_20050418010750 */
93 /* filename long enough? */
94 baselen = strlen(filename);
95 if(baselen < minlen) {
100 /* there must be two underscores at special places */
101 if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
106 /* replace the two underscores by digits */
107 filename[baselen-minlen] = '0';
108 filename[baselen-minlen+6] = '0';
110 /* we should have only digits now */
114 if(!isdigit( (int) filename[baselen])) {
122 /* ok, seems to be good */
127 /* test, if both files could be in the same file set */
128 /* (the filenames must already be in correct shape) */
130 fileset_is_file_in_set(const char *fname1, const char *fname2)
136 int minlen = strlen("_00001_20050418010750");
139 /* just to be sure ... */
140 g_assert(fileset_filename_match_pattern(fname1));
141 g_assert(fileset_filename_match_pattern(fname2));
143 dup_f1 = g_strdup(fname1);
144 dup_f2 = g_strdup(fname2);
146 pfx1 = strrchr(dup_f1, '.');
147 pfx2 = strrchr(dup_f2, '.');
149 /* the optional suffix (file extension) must be equal */
150 if(strcmp(pfx1, pfx2) != 0) {
156 *(pfx1-minlen) = '\0';
157 *(pfx2-minlen) = '\0';
159 if(strcmp(dup_f1, dup_f2) != 0) {
171 /* we know this file is part of the set, so add it */
172 static fileset_entry *
173 fileset_add_file(const char *dirname, const char *fname, gboolean current)
178 fileset_entry *entry = NULL;
181 path = g_strdup_printf("%s%s", dirname, fname);
183 fh = eth_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
187 result = fstat( fh, &buf );
189 /* Show statistics if they are valid */
191 entry = g_malloc(sizeof(fileset_entry));
193 entry->fullname = g_strdup(path);
194 entry->name = g_strdup(fname);
195 entry->ctime = buf.st_ctime;
196 entry->mtime = buf.st_mtime;
197 entry->size = buf.st_size;
198 entry->current = current;
200 set.entries = g_list_append(set.entries, entry);
212 /* compare two list entries by creation date/time (through filename) */
214 fileset_sort_compare(gconstpointer a, gconstpointer b)
216 const fileset_entry *entry_a = a;
217 const fileset_entry *entry_b = b;
219 return strcmp(entry_a->name, entry_b->name);
223 /* add all file set entries to the dialog */
224 void fileset_update_dlg(void)
229 /* add all entires to the dialog */
230 le = g_list_first(set.entries);
232 fileset_dlg_add_file(le->data);
233 le = g_list_next(le);
238 /* walk through the directory of the loaded file and add every file matching the current file */
240 fileset_add_dir(const char *fname)
242 ETH_DIR *dir; /* scanned directory */
243 ETH_DIRENT *file; /* current file */
245 fileset_entry *entry;
250 /* get (convert) directory name, but don't touch the given string */
251 fname_dup = get_dirname(g_strdup(fname));
252 dirname = g_string_new(fname_dup);
255 set.dirname = g_strdup(dirname->str);
257 dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
259 /* is the current file probably a part of any fileset? */
260 if(fileset_filename_match_pattern(fname)) {
261 /* yes, go through the files in the directory and check if the file in question is part of the current file set */
262 if ((dir = eth_dir_open(dirname->str, 0, NULL)) != NULL) {
263 while ((file = eth_dir_read_name(dir)) != NULL) {
264 name = eth_dir_get_name(file);
265 if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
266 fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
273 /* no, this is a "standalone file", just add this one */
274 entry = fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
275 /* don't add the file to the dialog here, this will be done in fileset_update_dlg() below */
278 g_string_free(dirname, TRUE /* free_segment */);
280 /* sort entries by creation time */
281 set.entries = g_list_sort(set.entries, fileset_sort_compare);
283 fileset_update_dlg();
287 /* get current directory name */
289 fileset_get_dirname(void)
295 /* get the current list entry, or NULL */
297 fileset_get_current(void)
300 fileset_entry *entry;
303 /* add all entires to the dialog */
304 le = g_list_first(set.entries);
310 le = g_list_next(le);
317 /* get the file set entry after the current one, or NULL */
319 fileset_get_next(void)
324 le = fileset_get_current();
329 le = g_list_next(le);
338 /* get the file set entry before the current one, or NULL */
340 fileset_get_previous(void)
345 le = fileset_get_current();
350 le = g_list_previous(le);
359 /* delete a single entry */
360 static void fileset_entry_delete(gpointer data, gpointer user_data _U_)
362 fileset_entry *entry = data;
364 g_free( (gpointer) entry->fullname);
365 entry->fullname = NULL;
366 g_free( (gpointer) entry->name);
371 /* delete the whole file set */
372 void fileset_delete(void)
374 /* free the entry list */
376 g_list_foreach(set.entries, fileset_entry_delete, NULL);
377 g_list_free(set.entries);
383 g_free( (gpointer) set.dirname);