Add ability to follow UDP stream by index
[metze/wireshark/wip.git] / fileset.c
1 /* fileset.c
2  * Routines for handling file sets
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <config.h>
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
32
33 #ifdef HAVE_DIRENT_H
34 #include <dirent.h>
35 #endif
36
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
39 #endif
40
41 #ifdef HAVE_SYS_WAIT_H
42 # include <sys/wait.h>
43 #endif
44
45 #include <stdio.h>
46 #include <string.h>
47
48 #include <glib.h>
49
50 #include <wsutil/file_util.h>
51 #include <wsutil/filesystem.h>
52 #include "globals.h"
53
54 #include <epan/strutil.h>
55
56 #include "fileset.h"
57
58
59
60 typedef struct _fileset {
61     GList   *entries;
62     char    *dirname;
63 } fileset;
64
65 /* this is the fileset's global data */
66 static fileset set = { NULL, NULL};
67
68
69 /* is this a probable file of a file set (does the naming pattern match)? */
70 gboolean
71 fileset_filename_match_pattern(const char *fname)
72 {
73     char        *pfx;
74     size_t       baselen;
75     size_t      minlen = strlen("_00001_20050418010750");
76     char        *filename;
77
78
79     /* d:\dir1\test_00001_20050418010750.cap */
80     filename = g_strdup(get_basename(fname));
81
82     /* test_00001_20050418010750.cap */
83     pfx = strrchr(filename, '.');
84     if(pfx == NULL) {  /* suffix is optional */
85         pfx = filename + strlen(filename);
86     }
87     /* test_00001_20050418010750 */
88     *pfx = '\0';
89
90     /* filename long enough? */
91     baselen = strlen(filename);
92     if(baselen < minlen) {
93         g_free(filename);
94         return FALSE;
95     }
96
97     /* there must be two underscores at special places */
98     if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
99         g_free(filename);
100         return FALSE;
101     }
102
103     /* replace the two underscores by digits */
104     filename[baselen-minlen] = '0';
105     filename[baselen-minlen+6] = '0';
106
107     /* we should have only digits now */
108     while(minlen--) {
109         baselen--;
110
111         if(!g_ascii_isdigit( filename[baselen])) {
112             g_free(filename);
113             return FALSE;
114         }
115     }
116
117     g_free(filename);
118
119     /* ok, seems to be good */
120     return TRUE;
121 }
122
123
124 /* test, if both files could be in the same file set */
125 /* (the filenames must already be in correct shape) */
126 static gboolean
127 fileset_is_file_in_set(const char *fname1, const char *fname2)
128 {
129     char        *pfx1;
130     char        *pfx2;
131     char        *dup_f1;
132     char        *dup_f2;
133     size_t       minlen = strlen("_00001_20050418010750");
134
135
136     /* just to be sure ... */
137     g_assert(fileset_filename_match_pattern(fname1));
138     g_assert(fileset_filename_match_pattern(fname2));
139
140     dup_f1 = g_strdup(fname1);
141     dup_f2 = g_strdup(fname2);
142
143     pfx1 = strrchr(dup_f1, '.');
144     pfx2 = strrchr(dup_f2, '.');
145     /* suffix is optional */
146     if (!pfx1) pfx1 = dup_f1 + strlen(dup_f1);
147     if (!pfx2) pfx2 = dup_f2 + strlen(dup_f2);
148
149     /* the optional suffix (file extension) must be equal */
150     if(strcmp(pfx1, pfx2) != 0) {
151         g_free(dup_f1);
152         g_free(dup_f2);
153         return FALSE;
154     }
155
156     *(pfx1-minlen) = '\0';
157     *(pfx2-minlen) = '\0';
158
159     if(strcmp(dup_f1, dup_f2) != 0) {
160         g_free(dup_f1);
161         g_free(dup_f2);
162         return FALSE;
163     }
164
165     g_free(dup_f1);
166     g_free(dup_f2);
167     return TRUE;
168 }
169
170 /* GCompareFunc helper for g_list_find_custom() */
171 static gint
172 fileset_find_by_path(gconstpointer a, gconstpointer b)
173 {
174     const fileset_entry *entry;
175     const char *path;
176
177     entry = (const fileset_entry *) a;
178     path  = (const char *) b;
179
180     return g_strcmp0(entry->fullname, path);
181 }
182
183 /* update the time and size of this file in the list */
184 void
185 fileset_update_file(const char *path)
186 {
187     int fh, result;
188     ws_statb64 buf;
189     fileset_entry *entry = NULL;
190     GList *entry_list;
191
192     fh = ws_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
193     if(fh !=  -1) {
194
195         /* Get statistics */
196         result = ws_fstat64( fh, &buf );
197
198         /* Show statistics if they are valid */
199         if( result == 0 ) {
200             entry_list = g_list_find_custom(set.entries, path,
201                                             fileset_find_by_path);
202
203             if (entry_list) {
204                 entry = (fileset_entry *) entry_list->data;
205                 entry->ctime    = buf.st_ctime;
206                 entry->mtime    = buf.st_mtime;
207                 entry->size     = buf.st_size;
208             }
209         }
210
211         ws_close(fh);
212     }
213 }
214
215 /* we know this file is part of the set, so add it */
216 static fileset_entry *
217 fileset_add_file(const char *dirname, const char *fname, gboolean current)
218 {
219     int fh, result;
220     ws_statb64 buf;
221     char *path;
222     fileset_entry *entry = NULL;
223
224
225     path = g_strdup_printf("%s%s", dirname, fname);
226
227     fh = ws_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
228     if(fh !=  -1) {
229
230         /* Get statistics */
231         result = ws_fstat64( fh, &buf );
232
233         /* Show statistics if they are valid */
234         if( result == 0 ) {
235             entry = (fileset_entry *)g_malloc(sizeof(fileset_entry));
236
237             entry->fullname = g_strdup(path);
238             entry->name     = g_strdup(fname);
239             entry->ctime    = buf.st_ctime;
240             entry->mtime    = buf.st_mtime;
241             entry->size     = buf.st_size;
242             entry->current  = current;
243
244             set.entries = g_list_append(set.entries, entry);
245         }
246
247         ws_close(fh);
248     }
249
250     g_free(path);
251
252     return entry;
253 }
254
255
256 /* compare two list entries by creation date/time (through filename) */
257 static gint
258 fileset_sort_compare(gconstpointer a, gconstpointer b)
259 {
260     const fileset_entry *entry_a = (const fileset_entry *)a;
261     const fileset_entry *entry_b = (const fileset_entry *)b;
262
263     return strcmp(entry_a->name, entry_b->name);
264 }
265
266
267 /* add all file set entries to the dialog */
268 void fileset_update_dlg(void *window)
269 {
270     GList         *le;
271
272
273     /* add all entires to the dialog */
274     le = g_list_first(set.entries);
275     while(le) {
276         fileset_dlg_add_file((fileset_entry *)le->data, window);
277         le = g_list_next(le);
278     }
279 }
280
281
282 /* walk through the directory of the loaded file and add every file matching the current file */
283 void
284 fileset_add_dir(const char *fname, void *window)
285 {
286     WS_DIR        *dir;             /* scanned directory */
287     WS_DIRENT     *file;            /* current file */
288     const char    *name;
289     GString       *dirname;
290     gchar         *fname_dup;
291
292
293     /* get (convert) directory name, but don't touch the given string */
294     fname_dup = get_dirname(g_strdup(fname));
295     dirname = g_string_new(fname_dup);
296     g_free(fname_dup);
297
298     set.dirname = g_strdup(dirname->str);
299
300     dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
301
302     /* is the current file probably a part of any fileset? */
303     if(fileset_filename_match_pattern(fname)) {
304         /* yes, go through the files in the directory and check if the file in question is part of the current file set */
305         if ((dir = ws_dir_open(dirname->str, 0, NULL)) != NULL) {
306             while ((file = ws_dir_read_name(dir)) != NULL) {
307                 name = ws_dir_get_name(file);
308                 if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
309                     fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
310                 }
311             } /* while */
312
313             ws_dir_close(dir);
314         } /* if */
315     } else {
316         /* no, this is a "standalone file", just add this one */
317         fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
318         /* don't add the file to the dialog here, this will be done in fileset_update_dlg() below */
319     }
320
321     g_string_free(dirname, TRUE /* free_segment */);
322
323     /* sort entries by creation time */
324     set.entries = g_list_sort(set.entries, fileset_sort_compare);
325
326     fileset_update_dlg(window);
327 }
328
329
330 /* get current directory name */
331 const char *
332 fileset_get_dirname(void)
333 {
334     return set.dirname;
335 }
336
337
338 /* get the current list entry, or NULL */
339 static GList *
340 fileset_get_current(void)
341 {
342     GList         *le;
343     fileset_entry *entry;
344
345
346     /* add all entires to the dialog */
347     le = g_list_first(set.entries);
348     while(le) {
349         entry = (fileset_entry *)le->data;
350         if(entry->current) {
351             return le;
352         }
353         le = g_list_next(le);
354     }
355
356     return NULL;
357 }
358
359
360 /* get the file set entry after the current one, or NULL */
361 fileset_entry *
362 fileset_get_next(void)
363 {
364     GList         *le;
365
366
367     le = fileset_get_current();
368     if(le == NULL) {
369         return NULL;
370     }
371
372     le = g_list_next(le);
373     if(le == NULL) {
374         return NULL;
375     }
376
377     return (fileset_entry *)le->data;
378 }
379
380
381 /* get the file set entry before the current one, or NULL */
382 fileset_entry *
383 fileset_get_previous(void)
384 {
385     GList         *le;
386
387
388     le = fileset_get_current();
389     if(le == NULL) {
390         return NULL;
391     }
392
393     le = g_list_previous(le);
394     if(le == NULL) {
395         return NULL;
396     }
397
398     return (fileset_entry *)le->data;
399 }
400
401
402 /* delete a single entry */
403 static void fileset_entry_delete(gpointer data, gpointer user_data _U_)
404 {
405     fileset_entry *entry = (fileset_entry *)data;
406
407     g_free( (gpointer) entry->fullname);
408     entry->fullname = NULL;
409     g_free( (gpointer) entry->name);
410     entry->name = NULL;
411     g_free(entry);
412 }
413
414
415 /* delete the whole file set */
416 void fileset_delete(void)
417 {
418     /* free the entry list */
419     if(set.entries) {
420         g_list_foreach(set.entries, fileset_entry_delete, NULL);
421         g_list_free(set.entries);
422         set.entries = NULL;
423     }
424
425     /* free the rest */
426     if(set.dirname) {
427         g_free( (gpointer) set.dirname);
428         set.dirname = NULL;
429     }
430 }
431
432 /*
433  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
434  *
435  * Local variables:
436  * c-basic-offset: 4
437  * tab-width: 8
438  * indent-tabs-mode: nil
439  * End:
440  *
441  * vi: set shiftwidth=4 tabstop=8 expandtab:
442  * :indentSize=4:tabSize=8:noTabs=true:
443  */