2 * Routines for handling file sets
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
13 #ifdef HAVE_SYS_WAIT_H
14 # include <sys/wait.h>
22 #include <wsutil/file_util.h>
23 #include <wsutil/filesystem.h>
25 #include <epan/strutil.h>
29 typedef struct _fileset {
35 * This is the fileset's global data.
37 * XXX This should probably be per-main-window instead of global.
39 static fileset set = { NULL, NULL};
42 * Given a stat structure, get the creation time of the file if available,
46 /* Microsoft's documentation says this is the creation time */
47 #define ST_CREATE_TIME(statb) ((statb).st_ctime)
49 /* UN*X - do we have a creation time? */
50 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
51 #define ST_CREATE_TIME(statb) ((statb).st_birthtime)
52 #elif defined(HAVE_STRUCT_STAT___ST_BIRTHTIME)
53 #define ST_CREATE_TIME(statb) ((statb).__st_birthtime)
55 #define ST_CREATE_TIME(statb) (0)
56 #endif /* creation time on UN*X */
59 /* is this a probable file of a file set (does the naming pattern match)? */
61 fileset_filename_match_pattern(const char *fname)
65 size_t minlen = strlen("_00001_20050418010750");
69 /* d:\dir1\test_00001_20050418010750.cap */
70 filename = g_strdup(get_basename(fname));
72 /* test_00001_20050418010750.cap */
73 pfx = strrchr(filename, '.');
74 if(pfx == NULL) { /* suffix is optional */
75 pfx = filename + strlen(filename);
77 /* test_00001_20050418010750 */
80 /* filename long enough? */
81 baselen = strlen(filename);
82 if(baselen < minlen) {
87 /* there must be two underscores at special places */
88 if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
93 /* replace the two underscores by digits */
94 filename[baselen-minlen] = '0';
95 filename[baselen-minlen+6] = '0';
97 /* we should have only digits now */
101 if(!g_ascii_isdigit( filename[baselen])) {
109 /* ok, seems to be good */
114 /* test, if both files could be in the same file set */
115 /* (the filenames must already be in correct shape) */
117 fileset_is_file_in_set(const char *fname1, const char *fname2)
123 size_t minlen = strlen("_00001_20050418010750");
126 /* just to be sure ... */
127 g_assert(fileset_filename_match_pattern(fname1));
128 g_assert(fileset_filename_match_pattern(fname2));
130 dup_f1 = g_strdup(fname1);
131 dup_f2 = g_strdup(fname2);
133 pfx1 = strrchr(dup_f1, '.');
134 pfx2 = strrchr(dup_f2, '.');
135 /* suffix is optional */
136 if (!pfx1) pfx1 = dup_f1 + strlen(dup_f1);
137 if (!pfx2) pfx2 = dup_f2 + strlen(dup_f2);
139 /* the optional suffix (file extension) must be equal */
140 if(strcmp(pfx1, pfx2) != 0) {
146 *(pfx1-minlen) = '\0';
147 *(pfx2-minlen) = '\0';
149 if(strcmp(dup_f1, dup_f2) != 0) {
160 /* GCompareFunc helper for g_list_find_custom() */
162 fileset_find_by_path(gconstpointer a, gconstpointer b)
164 const fileset_entry *entry;
167 entry = (const fileset_entry *) a;
168 path = (const char *) b;
170 return g_strcmp0(entry->fullname, path);
173 /* update the time and size of this file in the list */
175 fileset_update_file(const char *path)
179 fileset_entry *entry = NULL;
182 fh = ws_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
186 result = ws_fstat64( fh, &buf );
188 /* Show statistics if they are valid */
190 entry_list = g_list_find_custom(set.entries, path,
191 fileset_find_by_path);
194 entry = (fileset_entry *) entry_list->data;
195 entry->ctime = ST_CREATE_TIME(buf);
196 entry->mtime = buf.st_mtime;
197 entry->size = buf.st_size;
205 /* we know this file is part of the set, so add it */
206 static fileset_entry *
207 fileset_add_file(const char *dirname, const char *fname, gboolean current)
212 fileset_entry *entry = NULL;
215 path = g_strdup_printf("%s%s", dirname, fname);
217 fh = ws_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
221 result = ws_fstat64( fh, &buf );
223 /* Show statistics if they are valid */
225 entry = (fileset_entry *)g_malloc(sizeof(fileset_entry));
227 entry->fullname = g_strdup(path);
228 entry->name = g_strdup(fname);
229 entry->ctime = ST_CREATE_TIME(buf);
230 entry->mtime = buf.st_mtime;
231 entry->size = buf.st_size;
232 entry->current = current;
234 set.entries = g_list_append(set.entries, entry);
246 /* compare two list entries by creation date/time (through filename) */
248 fileset_sort_compare(gconstpointer a, gconstpointer b)
250 const fileset_entry *entry_a = (const fileset_entry *)a;
251 const fileset_entry *entry_b = (const fileset_entry *)b;
253 return strcmp(entry_a->name, entry_b->name);
257 /* add all file set entries to the dialog */
258 void fileset_update_dlg(void *window)
262 /* Add all entries to the dialog. */
263 fileset_dlg_begin_add_file(window);
264 le = g_list_first(set.entries);
266 fileset_dlg_add_file((fileset_entry *)le->data, window);
267 le = g_list_next(le);
269 fileset_dlg_end_add_file(window);
273 /* walk through the directory of the loaded file and add every file matching the current file */
275 fileset_add_dir(const char *fname, void *window)
277 WS_DIR *dir; /* scanned directory */
278 WS_DIRENT *file; /* current file */
284 /* get (convert) directory name, but don't touch the given string */
285 fname_dup = g_strdup(fname);
286 dirname = g_string_new(get_dirname(fname_dup));
289 set.dirname = g_strdup(dirname->str);
291 dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
293 /* is the current file probably a part of any fileset? */
294 if(fileset_filename_match_pattern(fname)) {
295 /* yes, go through the files in the directory and check if the file in question is part of the current file set */
296 if ((dir = ws_dir_open(dirname->str, 0, NULL)) != NULL) {
297 while ((file = ws_dir_read_name(dir)) != NULL) {
298 name = ws_dir_get_name(file);
299 if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
300 fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
307 /* no, this is a "standalone file", just add this one */
308 fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
309 /* don't add the file to the dialog here, this will be done in fileset_update_dlg() below */
312 g_string_free(dirname, TRUE /* free_segment */);
314 /* sort entries by creation time */
315 set.entries = g_list_sort(set.entries, fileset_sort_compare);
317 fileset_update_dlg(window);
321 /* get current directory name */
323 fileset_get_dirname(void)
329 /* get the current list entry, or NULL */
331 fileset_get_current(void)
334 fileset_entry *entry;
337 /* add all entries to the dialog */
338 le = g_list_first(set.entries);
340 entry = (fileset_entry *)le->data;
344 le = g_list_next(le);
351 /* get the file set entry after the current one, or NULL */
353 fileset_get_next(void)
358 le = fileset_get_current();
363 le = g_list_next(le);
368 return (fileset_entry *)le->data;
372 /* get the file set entry before the current one, or NULL */
374 fileset_get_previous(void)
379 le = fileset_get_current();
384 le = g_list_previous(le);
389 return (fileset_entry *)le->data;
393 /* delete a single entry */
394 static void fileset_entry_delete(gpointer data, gpointer user_data _U_)
396 fileset_entry *entry = (fileset_entry *)data;
398 g_free( (gpointer) entry->fullname);
399 entry->fullname = NULL;
400 g_free( (gpointer) entry->name);
406 /* delete the whole file set */
407 void fileset_delete(void)
409 /* free the entry list */
411 g_list_foreach(set.entries, fileset_entry_delete, NULL);
412 g_list_free(set.entries);
418 g_free( (gpointer) set.dirname);
424 * Editor modelines - http://www.wireshark.org/tools/modelines.html
429 * indent-tabs-mode: nil
432 * vi: set shiftwidth=4 tabstop=8 expandtab:
433 * :indentSize=4:tabSize=8:noTabs=true: