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.
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
45 #ifdef HAVE_SYS_WAIT_H
46 # include <sys/wait.h>
56 #include <epan/filesystem.h>
62 typedef struct _fileset {
67 /* this is the fileset's global data */
68 fileset set = { NULL, NULL};
71 /* is this a probable file of a file set (does the naming pattern match)? */
73 fileset_filename_match_pattern(const char *fname)
77 int minlen = strlen("_00001_20050418010750");
81 /* d:\dir1\test_00001_20050418010750.cap */
82 filename = g_strdup(get_basename(fname));
84 /* test_00001_20050418010750.cap */
85 pfx = strrchr(filename, '.');
89 /* test_00001_20050418010750 */
92 /* filename long enough? */
93 baselen = strlen(filename);
94 if(baselen < minlen) {
99 /* there must be two underscores at special places */
100 if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
105 /* replace the two underscores by digits */
106 filename[baselen-minlen] = '0';
107 filename[baselen-minlen+6] = '0';
109 /* we should have only digits now */
113 if(!isdigit( (int) filename[baselen])) {
121 /* ok, seems to be good */
126 /* test, if both files could be in the same file set */
128 fileset_is_file_in_set(const char *fname1, const char *fname2)
134 int minlen = strlen("_00001_20050418010750");
137 dup_f1 = g_strdup(fname1);
138 dup_f2 = g_strdup(fname2);
140 pfx1 = strrchr(dup_f1, '.');
141 pfx2 = strrchr(dup_f2, '.');
143 if(strcmp(pfx1, pfx2) != 0) {
149 *(pfx1-minlen) = '\0';
150 *(pfx2-minlen) = '\0';
152 if(strcmp(dup_f1, dup_f2) != 0) {
164 /* we know this file is part of the set, so add it */
166 fileset_add_file(const char *dirname, const char *fname, gboolean current)
171 fileset_entry *entry = NULL;
174 path = g_strdup_printf("%s%s", dirname, fname);
176 fh = open( path, O_RDONLY );
180 result = fstat( fh, &buf );
182 /* Show statistics if they are valid */
184 entry = g_malloc(sizeof(fileset_entry));
186 entry->fullname = g_strdup(path);
187 entry->name = g_strdup(fname);
188 entry->ctime = buf.st_ctime;
189 entry->mtime = buf.st_mtime;
190 entry->size = buf.st_size;
191 entry->current = current;
193 set.entries = g_list_append(set.entries, entry);
205 /* compare two list entries by creation date/time */
207 fileset_compare_creation(gconstpointer a, gconstpointer b)
209 const fileset_entry *entry_a = a;
210 const fileset_entry *entry_b = b;
212 return entry_a->ctime - entry_b->ctime;
216 /* add all file set entries to the dialog */
217 void fileset_update_dlg(void)
222 /* add all entires to the dialog */
223 le = g_list_first(set.entries);
225 fileset_dlg_add_file(le->data);
226 le = g_list_next(le);
231 /* walk through the directory of the loaded file and add every file matching the current file */
233 fileset_add_dir(const char *fname)
235 #if GLIB_MAJOR_VERSION < 2
236 DIR *dir; /* scanned directory */
237 struct dirent *file; /* current file */
240 GDir *dir; /* scanned directory */
244 fileset_entry *entry;
249 /* get (convert) directory name, but don't touch the given string */
250 fname_dup = get_dirname(g_strdup(fname));
251 dirname = g_string_new(fname_dup);
254 set.dirname = g_strdup(dirname->str);
256 dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
258 /* if the current file can't be part of any fileset, do nothing */
259 if(!fileset_filename_match_pattern(fname)) {
260 entry = fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
262 fileset_dlg_add_file(entry);
266 /* go through the files in the directory and check if it's part of the current file set */
267 #if GLIB_MAJOR_VERSION < 2
268 if ((dir = opendir(dirname)) != NULL) {
269 while ((file = readdir(dir)) != NULL) {
270 name = (gchar *)file->d_name;
272 dummy = g_malloc(sizeof(GError *));
275 if ((dir = g_dir_open(dirname->str, 0, dummy)) != NULL) {
276 while ((name = g_dir_read_name(dir)) != NULL) {
278 if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
279 fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
285 g_string_free(dirname, TRUE /* free_segment */);
287 /* sort entries by creation time */
288 set.entries = g_list_sort(set.entries, fileset_compare_creation);
290 fileset_update_dlg();
294 /* get current directory name */
296 fileset_get_dirname(void)
302 /* get the current list entry, or NULL */
304 fileset_get_current(void)
307 fileset_entry *entry;
310 /* add all entires to the dialog */
311 le = g_list_first(set.entries);
317 le = g_list_next(le);
324 /* get the file set entry after the current one, or NULL */
326 fileset_get_next(void)
331 le = fileset_get_current();
336 le = g_list_next(le);
345 /* get the file set entry before the current one, or NULL */
347 fileset_get_previous(void)
352 le = fileset_get_current();
357 le = g_list_previous(le);
366 /* delete a single entry */
367 void fileset_entry_delete(gpointer data, gpointer user_data)
369 fileset_entry *entry = data;
371 g_free( (gpointer) entry->fullname);
372 entry->fullname = NULL;
373 g_free( (gpointer) entry->name);
378 /* delete the whole file set */
379 void fileset_delete(void)
381 /* free the entry list */
383 g_list_foreach(set.entries, fileset_entry_delete, NULL);
384 g_list_free(set.entries);
390 g_free( (gpointer) set.dirname);