2 * Routines for handling file sets
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
41 #ifdef HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
51 #include <wsutil/file_util.h>
52 #include <wsutil/filesystem.h>
55 #include <epan/strutil.h>
61 typedef struct _fileset {
66 /* this is the fileset's global data */
67 static fileset set = { NULL, NULL};
70 /* is this a probable file of a file set (does the naming pattern match)? */
72 fileset_filename_match_pattern(const char *fname)
76 size_t minlen = strlen("_00001_20050418010750");
80 /* d:\dir1\test_00001_20050418010750.cap */
81 filename = g_strdup(get_basename(fname));
83 /* test_00001_20050418010750.cap */
84 pfx = strrchr(filename, '.');
85 if(pfx == NULL) { /* suffix is optional */
86 pfx = filename + strlen(filename);
88 /* test_00001_20050418010750 */
91 /* filename long enough? */
92 baselen = strlen(filename);
93 if(baselen < minlen) {
98 /* there must be two underscores at special places */
99 if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
104 /* replace the two underscores by digits */
105 filename[baselen-minlen] = '0';
106 filename[baselen-minlen+6] = '0';
108 /* we should have only digits now */
112 if(!isdigit( (guchar) filename[baselen])) {
120 /* ok, seems to be good */
125 /* test, if both files could be in the same file set */
126 /* (the filenames must already be in correct shape) */
128 fileset_is_file_in_set(const char *fname1, const char *fname2)
134 size_t minlen = strlen("_00001_20050418010750");
137 /* just to be sure ... */
138 g_assert(fileset_filename_match_pattern(fname1));
139 g_assert(fileset_filename_match_pattern(fname2));
141 dup_f1 = g_strdup(fname1);
142 dup_f2 = g_strdup(fname2);
144 pfx1 = strrchr(dup_f1, '.');
145 pfx2 = strrchr(dup_f2, '.');
146 /* suffix is optional */
147 if (!pfx1) pfx1 = dup_f1 + strlen(dup_f1);
148 if (!pfx2) pfx2 = dup_f2 + strlen(dup_f2);
150 /* the optional suffix (file extension) must be equal */
151 if(strcmp(pfx1, pfx2) != 0) {
157 *(pfx1-minlen) = '\0';
158 *(pfx2-minlen) = '\0';
160 if(strcmp(dup_f1, dup_f2) != 0) {
171 /* GCompareFunc helper for g_list_find_custom() */
173 fileset_find_by_path(gconstpointer a, gconstpointer b)
175 const fileset_entry *entry;
178 entry = (const fileset_entry *) a;
179 path = (const char *) b;
181 return g_strcmp0(entry->fullname, path);
184 /* update the time and size of this file in the list */
186 fileset_update_file(const char *path)
190 fileset_entry *entry = NULL;
193 fh = ws_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
197 result = ws_fstat64( fh, &buf );
199 /* Show statistics if they are valid */
201 entry_list = g_list_find_custom(set.entries, path,
202 fileset_find_by_path);
205 entry = (fileset_entry *) entry_list->data;
206 entry->ctime = buf.st_ctime;
207 entry->mtime = buf.st_mtime;
208 entry->size = buf.st_size;
216 /* we know this file is part of the set, so add it */
217 static fileset_entry *
218 fileset_add_file(const char *dirname, const char *fname, gboolean current)
223 fileset_entry *entry = NULL;
226 path = g_strdup_printf("%s%s", dirname, fname);
228 fh = ws_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
232 result = ws_fstat64( fh, &buf );
234 /* Show statistics if they are valid */
236 entry = (fileset_entry *)g_malloc(sizeof(fileset_entry));
238 entry->fullname = g_strdup(path);
239 entry->name = g_strdup(fname);
240 entry->ctime = buf.st_ctime;
241 entry->mtime = buf.st_mtime;
242 entry->size = buf.st_size;
243 entry->current = current;
245 set.entries = g_list_append(set.entries, entry);
257 /* compare two list entries by creation date/time (through filename) */
259 fileset_sort_compare(gconstpointer a, gconstpointer b)
261 const fileset_entry *entry_a = (const fileset_entry *)a;
262 const fileset_entry *entry_b = (const fileset_entry *)b;
264 return strcmp(entry_a->name, entry_b->name);
268 /* add all file set entries to the dialog */
269 void fileset_update_dlg(void *window)
274 /* add all entires to the dialog */
275 le = g_list_first(set.entries);
277 fileset_dlg_add_file((fileset_entry *)le->data, window);
278 le = g_list_next(le);
283 /* walk through the directory of the loaded file and add every file matching the current file */
285 fileset_add_dir(const char *fname, void *window)
287 WS_DIR *dir; /* scanned directory */
288 WS_DIRENT *file; /* current file */
294 /* get (convert) directory name, but don't touch the given string */
295 fname_dup = get_dirname(g_strdup(fname));
296 dirname = g_string_new(fname_dup);
299 set.dirname = g_strdup(dirname->str);
301 dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
303 /* is the current file probably a part of any fileset? */
304 if(fileset_filename_match_pattern(fname)) {
305 /* yes, go through the files in the directory and check if the file in question is part of the current file set */
306 if ((dir = ws_dir_open(dirname->str, 0, NULL)) != NULL) {
307 while ((file = ws_dir_read_name(dir)) != NULL) {
308 name = ws_dir_get_name(file);
309 if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
310 fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
317 /* no, this is a "standalone file", just add this one */
318 fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
319 /* don't add the file to the dialog here, this will be done in fileset_update_dlg() below */
322 g_string_free(dirname, TRUE /* free_segment */);
324 /* sort entries by creation time */
325 set.entries = g_list_sort(set.entries, fileset_sort_compare);
327 fileset_update_dlg(window);
331 /* get current directory name */
333 fileset_get_dirname(void)
339 /* get the current list entry, or NULL */
341 fileset_get_current(void)
344 fileset_entry *entry;
347 /* add all entires to the dialog */
348 le = g_list_first(set.entries);
350 entry = (fileset_entry *)le->data;
354 le = g_list_next(le);
361 /* get the file set entry after the current one, or NULL */
363 fileset_get_next(void)
368 le = fileset_get_current();
373 le = g_list_next(le);
378 return (fileset_entry *)le->data;
382 /* get the file set entry before the current one, or NULL */
384 fileset_get_previous(void)
389 le = fileset_get_current();
394 le = g_list_previous(le);
399 return (fileset_entry *)le->data;
403 /* delete a single entry */
404 static void fileset_entry_delete(gpointer data, gpointer user_data _U_)
406 fileset_entry *entry = (fileset_entry *)data;
408 g_free( (gpointer) entry->fullname);
409 entry->fullname = NULL;
410 g_free( (gpointer) entry->name);
416 /* delete the whole file set */
417 void fileset_delete(void)
419 /* free the entry list */
421 g_list_foreach(set.entries, fileset_entry_delete, NULL);
422 g_list_free(set.entries);
428 g_free( (gpointer) set.dirname);