SMB2: dissect new signing capability negotiate context
[wireshark-sm.git] / fileset.c
blob14436322d593dbafd6696c0a32e5f3b81170579f
1 /* fileset.c
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
9 */
11 #include <config.h>
13 #ifdef HAVE_SYS_WAIT_H
14 # include <sys/wait.h>
15 #endif
17 #include <stdio.h>
18 #include <string.h>
20 #include <glib.h>
22 #include <wsutil/file_util.h>
23 #include <wsutil/filesystem.h>
25 #include <epan/strutil.h>
27 #include "fileset.h"
29 typedef struct _fileset {
30 GList *entries;
31 char *dirname;
32 } 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,
43 * or 0 if not.
45 #ifdef _WIN32
46 /* Microsoft's documentation says this is the creation time */
47 #define ST_CREATE_TIME(statb) ((statb).st_ctime)
48 #else /* _WIN32 */
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)
54 #else /* nothing */
55 #define ST_CREATE_TIME(statb) (0)
56 #endif /* creation time on UN*X */
57 #endif /* _WIN32 */
59 /* is this a probable file of a file set (does the naming pattern match)? */
60 gboolean
61 fileset_filename_match_pattern(const char *fname)
63 char *pfx;
64 size_t baselen;
65 size_t minlen = strlen("_00001_20050418010750");
66 char *filename;
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 */
78 *pfx = '\0';
80 /* filename long enough? */
81 baselen = strlen(filename);
82 if(baselen < minlen) {
83 g_free(filename);
84 return FALSE;
87 /* there must be two underscores at special places */
88 if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
89 g_free(filename);
90 return FALSE;
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 */
98 while(minlen--) {
99 baselen--;
101 if(!g_ascii_isdigit( filename[baselen])) {
102 g_free(filename);
103 return FALSE;
107 g_free(filename);
109 /* ok, seems to be good */
110 return TRUE;
114 /* test, if both files could be in the same file set */
115 /* (the filenames must already be in correct shape) */
116 static gboolean
117 fileset_is_file_in_set(const char *fname1, const char *fname2)
119 char *pfx1;
120 char *pfx2;
121 char *dup_f1;
122 char *dup_f2;
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) {
141 g_free(dup_f1);
142 g_free(dup_f2);
143 return FALSE;
146 *(pfx1-minlen) = '\0';
147 *(pfx2-minlen) = '\0';
149 if(strcmp(dup_f1, dup_f2) != 0) {
150 g_free(dup_f1);
151 g_free(dup_f2);
152 return FALSE;
155 g_free(dup_f1);
156 g_free(dup_f2);
157 return TRUE;
160 /* GCompareFunc helper for g_list_find_custom() */
161 static gint
162 fileset_find_by_path(gconstpointer a, gconstpointer b)
164 const fileset_entry *entry;
165 const char *path;
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 */
174 void
175 fileset_update_file(const char *path)
177 int fh, result;
178 ws_statb64 buf;
179 fileset_entry *entry = NULL;
180 GList *entry_list;
182 fh = ws_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
183 if(fh != -1) {
185 /* Get statistics */
186 result = ws_fstat64( fh, &buf );
188 /* Show statistics if they are valid */
189 if( result == 0 ) {
190 entry_list = g_list_find_custom(set.entries, path,
191 fileset_find_by_path);
193 if (entry_list) {
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;
201 ws_close(fh);
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)
209 int fh, result;
210 ws_statb64 buf;
211 char *path;
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 */);
218 if(fh != -1) {
220 /* Get statistics */
221 result = ws_fstat64( fh, &buf );
223 /* Show statistics if they are valid */
224 if( result == 0 ) {
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);
237 ws_close(fh);
240 g_free(path);
242 return entry;
246 /* compare two list entries by creation date/time (through filename) */
247 static gint
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)
260 GList *le;
262 /* Add all entries to the dialog. */
263 fileset_dlg_begin_add_file(window);
264 le = g_list_first(set.entries);
265 while(le) {
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 */
274 void
275 fileset_add_dir(const char *fname, void *window)
277 WS_DIR *dir; /* scanned directory */
278 WS_DIRENT *file; /* current file */
279 const char *name;
280 GString *dirname;
281 gchar *fname_dup;
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));
287 g_free(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 */);
302 } /* while */
304 ws_dir_close(dir);
305 } /* if */
306 } else {
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 */
322 const char *
323 fileset_get_dirname(void)
325 return set.dirname;
329 /* get the current list entry, or NULL */
330 static GList *
331 fileset_get_current(void)
333 GList *le;
334 fileset_entry *entry;
337 /* add all entries to the dialog */
338 le = g_list_first(set.entries);
339 while(le) {
340 entry = (fileset_entry *)le->data;
341 if(entry->current) {
342 return le;
344 le = g_list_next(le);
347 return NULL;
351 /* get the file set entry after the current one, or NULL */
352 fileset_entry *
353 fileset_get_next(void)
355 GList *le;
358 le = fileset_get_current();
359 if(le == NULL) {
360 return NULL;
363 le = g_list_next(le);
364 if(le == NULL) {
365 return NULL;
368 return (fileset_entry *)le->data;
372 /* get the file set entry before the current one, or NULL */
373 fileset_entry *
374 fileset_get_previous(void)
376 GList *le;
379 le = fileset_get_current();
380 if(le == NULL) {
381 return NULL;
384 le = g_list_previous(le);
385 if(le == NULL) {
386 return NULL;
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);
401 entry->name = NULL;
402 g_free(entry);
406 /* delete the whole file set */
407 void fileset_delete(void)
409 /* free the entry list */
410 if(set.entries) {
411 g_list_foreach(set.entries, fileset_entry_delete, NULL);
412 g_list_free(set.entries);
413 set.entries = NULL;
416 /* free the rest */
417 if(set.dirname) {
418 g_free( (gpointer) set.dirname);
419 set.dirname = NULL;
424 * Editor modelines - https://www.wireshark.org/tools/modelines.html
426 * Local variables:
427 * c-basic-offset: 4
428 * tab-width: 8
429 * indent-tabs-mode: nil
430 * End:
432 * vi: set shiftwidth=4 tabstop=8 expandtab:
433 * :indentSize=4:tabSize=8:noTabs=true: