2 * Routines for handling file sets
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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.
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
41 #ifdef HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
50 #include <epan/filesystem.h>
56 typedef struct _fileset {
61 /* this is the fileset's global data */
62 fileset set = { NULL, NULL};
65 /* is this a probable file of a file set (does the naming pattern match)? */
67 fileset_filename_match_pattern(const char *fname)
71 int minlen = strlen("_00001_20050418010750");
75 /* d:\dir1\test_00001_20050418010750.cap */
76 filename = g_strdup(get_basename(fname));
78 /* test_00001_20050418010750.cap */
79 pfx = strrchr(filename, '.');
83 /* test_00001_20050418010750 */
86 /* filename long enough? */
87 baselen = strlen(filename);
88 if(baselen < minlen) {
93 /* there must be two underscores at special places */
94 if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
99 /* replace the two underscores by digits */
100 filename[baselen-minlen] = '0';
101 filename[baselen-minlen+6] = '0';
103 /* we should have only digits now */
107 if(!isdigit(filename[baselen])) {
115 /* ok, seems to be good */
120 /* test, if both files could be in the same file set */
122 fileset_is_file_in_set(const char *fname1, const char *fname2)
128 int minlen = strlen("_00001_20050418010750");
131 dup_f1 = g_strdup(fname1);
132 dup_f2 = g_strdup(fname2);
134 pfx1 = strrchr(dup_f1, '.');
135 pfx2 = strrchr(dup_f2, '.');
137 if(strcmp(pfx1, pfx2) != 0) {
143 *(pfx1-minlen) = '\0';
144 *(pfx2-minlen) = '\0';
146 if(strcmp(dup_f1, dup_f2) != 0) {
158 /* we know this file is part of the set, so add it */
160 fileset_add_file(const char *dirname, const char *fname, gboolean current)
165 fileset_entry *entry = NULL;
168 path = g_strdup_printf("%s%s", dirname, fname);
170 fh = open( path, _O_RDONLY );
174 result = _fstat( fh, &buf );
176 /* Show statistics if they are valid */
178 entry = g_malloc(sizeof(fileset_entry));
180 entry->fullname = g_strdup(path);
181 entry->name = g_strdup(fname);
182 entry->ctime = buf.st_ctime;
183 entry->mtime = buf.st_mtime;
184 entry->size = buf.st_size;
185 entry->current = current;
187 set.entries = g_list_append(set.entries, entry);
199 /* compare two list entries by creation date/time */
201 fileset_compare_creation(gconstpointer a, gconstpointer b)
203 const fileset_entry *entry_a = a;
204 const fileset_entry *entry_b = b;
206 return entry_a->ctime - entry_b->ctime;
210 /* add all file set entries to the dialog */
211 void fileset_update_dlg(void)
216 /* add all entires to the dialog */
217 le = g_list_first(set.entries);
219 fileset_dlg_add_file(le->data);
220 le = g_list_next(le);
225 /* walk through the directory of the loaded file and add every file matching the current file */
227 fileset_add_dir(const char *fname)
229 #if GLIB_MAJOR_VERSION < 2
230 DIR *dir; /* scanned directory */
231 struct dirent *file; /* current file */
234 GDir *dir; /* scanned directory */
238 fileset_entry *entry;
243 /* get (convert) directory name, but don't touch the given string */
244 fname_dup = get_dirname(g_strdup(fname));
245 dirname = g_string_new(fname_dup);
248 set.dirname = g_strdup(dirname->str);
250 dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
252 dummy = g_malloc(sizeof(GError *));
255 /* if the current file can't be part of any fileset, do nothing */
256 if(!fileset_filename_match_pattern(fname)) {
257 entry = fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
259 fileset_dlg_add_file(entry);
263 /* go through the files in the directory and check if it's part of the current file set */
264 #if GLIB_MAJOR_VERSION < 2
265 if ((dir = opendir(dirname)) != NULL) {
266 while ((file = readdir(dir)) != NULL) {
267 name = (gchar *)file->d_name;
269 if ((dir = g_dir_open(dirname->str, 0, dummy)) != NULL) {
270 while ((name = g_dir_read_name(dir)) != NULL) {
272 if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
273 fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
279 g_string_free(dirname, TRUE /* free_segment */);
281 /* sort entries by creation time */
282 set.entries = g_list_sort(set.entries, fileset_compare_creation);
284 fileset_update_dlg();
288 /* get current directory name */
290 fileset_get_dirname(void)
296 /* get the current list entry, or NULL */
298 fileset_get_current(void)
301 fileset_entry *entry;
304 /* add all entires to the dialog */
305 le = g_list_first(set.entries);
311 le = g_list_next(le);
318 /* get the file set entry after the current one, or NULL */
320 fileset_get_next(void)
325 le = fileset_get_current();
330 le = g_list_next(le);
339 /* get the file set entry before the current one, or NULL */
341 fileset_get_previous(void)
346 le = fileset_get_current();
351 le = g_list_previous(le);
360 /* delete a single entry */
361 void fileset_entry_delete(gpointer data, gpointer user_data)
363 fileset_entry *entry = data;
365 g_free( (gpointer) entry->fullname);
366 entry->fullname = NULL;
367 g_free( (gpointer) entry->name);
372 /* delete the whole file set */
373 void fileset_delete(void)
375 /* free the entry list */
377 g_list_foreach(set.entries, fileset_entry_delete, NULL);
378 g_list_free(set.entries);
384 g_free( (gpointer) set.dirname);