fix a minor bug: don't show a single file twice in the fileset dialog
[metze/wireshark/wip.git] / fileset.c
1 /* fileset.c
2  * Routines for handling file sets
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36
37 #ifdef HAVE_DIRENT_H
38 #include <dirent.h>
39 #endif
40
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 #endif
44
45 #ifdef HAVE_SYS_WAIT_H
46 # include <sys/wait.h>
47 #endif
48
49 #include <string.h>
50 #include <ctype.h>
51
52 #include <glib.h>
53
54 #include <wiretap/file_util.h>
55 #include "globals.h"
56
57 #include <epan/filesystem.h>
58
59 #include "fileset.h"
60
61
62
63 typedef struct _fileset {
64   GList         *entries;
65   const char    *dirname;
66 } fileset;
67
68 /* this is the fileset's global data */
69 fileset set = { NULL, NULL};
70
71
72 /* is this a probable file of a file set (does the naming pattern match)? */
73 gboolean
74 fileset_filename_match_pattern(const char *fname)
75 {
76     char        *pfx;
77     int         baselen;
78     int         minlen = strlen("_00001_20050418010750");
79     char        *filename;
80   
81
82     /* d:\dir1\test_00001_20050418010750.cap */
83     filename = g_strdup(get_basename(fname));
84
85     /* test_00001_20050418010750.cap */
86     pfx = strrchr(filename, '.');
87     if(pfx == NULL) {
88         return FALSE;
89     }
90     /* test_00001_20050418010750 */
91     *pfx = '\0';
92
93     /* filename long enough? */
94     baselen = strlen(filename);
95     if(baselen < minlen) {
96         g_free(filename);
97         return FALSE;
98     }
99
100     /* there must be two underscores at special places */
101     if(filename[baselen-minlen] != '_' || filename[baselen-minlen+6] != '_') {
102         g_free(filename);
103         return FALSE;
104     }
105
106     /* replace the two underscores by digits */
107     filename[baselen-minlen] = '0';
108     filename[baselen-minlen+6] = '0';
109
110     /* we should have only digits now */
111     while(minlen--) {
112         baselen--;
113
114         if(!isdigit( (int) filename[baselen])) {
115             g_free(filename);
116             return FALSE;
117         }
118     }
119
120     g_free(filename);
121
122     /* ok, seems to be good */
123     return TRUE;
124 }
125
126
127 /* test, if both files could be in the same file set */
128 /* (the filenames must already be in correct shape) */
129 gboolean
130 fileset_is_file_in_set(const char *fname1, const char *fname2)
131 {
132     char        *pfx1;
133     char        *pfx2;
134     char        *dup_f1;
135     char        *dup_f2;
136     int         minlen = strlen("_00001_20050418010750");
137
138
139     /* just to be sure ... */
140     g_assert(fileset_filename_match_pattern(fname1));
141     g_assert(fileset_filename_match_pattern(fname2));
142
143     dup_f1 = g_strdup(fname1);
144     dup_f2 = g_strdup(fname2);
145
146     pfx1 = strrchr(dup_f1, '.');
147     pfx2 = strrchr(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
171 /* we know this file is part of the set, so add it */
172 static fileset_entry *
173 fileset_add_file(const char *dirname, const char *fname, gboolean current)
174 {
175     int fh, result;
176     struct stat buf;
177     char *path;
178     fileset_entry *entry = NULL;
179
180
181     path = g_strdup_printf("%s%s", dirname, fname);
182
183     fh = eth_open( path, O_RDONLY, 0000 /* no creation so don't matter */);
184     if(fh !=  -1) {
185
186         /* Get statistics */
187         result = fstat( fh, &buf );
188
189         /* Show statistics if they are valid */
190         if( result == 0 ) {
191             entry = g_malloc(sizeof(fileset_entry));
192
193             entry->fullname = g_strdup(path);
194             entry->name     = g_strdup(fname);
195             entry->ctime    = buf.st_ctime;
196             entry->mtime    = buf.st_mtime;
197             entry->size     = buf.st_size;
198             entry->current  = current;
199
200             set.entries = g_list_append(set.entries, entry);
201         }
202
203         eth_close(fh);
204     }
205
206     g_free(path);
207
208     return entry;
209 }
210
211
212 /* compare two list entries by creation date/time (through filename) */
213 static gint
214 fileset_sort_compare(gconstpointer a, gconstpointer b)
215 {
216     const fileset_entry *entry_a = a;
217     const fileset_entry *entry_b = b;
218
219         return strcmp(entry_a->name, entry_b->name);
220 }
221
222
223 /* add all file set entries to the dialog */
224 void fileset_update_dlg(void)
225 {
226     GList         *le;
227
228
229     /* add all entires to the dialog */
230     le = g_list_first(set.entries);
231     while(le) {
232         fileset_dlg_add_file(le->data);
233         le = g_list_next(le);
234     }
235 }
236
237
238 /* walk through the directory of the loaded file and add every file matching the current file */
239 void
240 fileset_add_dir(const char *fname)
241 {
242     ETH_DIR       *dir;             /* scanned directory */
243     ETH_DIRENT    *file;            /* current file */
244     const char    *name;
245     fileset_entry *entry;
246     GString       *dirname;
247     gchar         *fname_dup;
248
249
250     /* get (convert) directory name, but don't touch the given string */
251     fname_dup = get_dirname(g_strdup(fname));
252     dirname = g_string_new(fname_dup);
253     g_free(fname_dup);
254
255     set.dirname = g_strdup(dirname->str);    
256     
257     dirname = g_string_append_c(dirname, G_DIR_SEPARATOR);
258
259     /* is the current file probably a part of any fileset? */
260     if(fileset_filename_match_pattern(fname)) {
261         /* yes, go through the files in the directory and check if the file in question is part of the current file set */
262         if ((dir = eth_dir_open(dirname->str, 0, NULL)) != NULL) {
263                 while ((file = eth_dir_read_name(dir)) != NULL) {
264                     name = eth_dir_get_name(file);
265                 if(fileset_filename_match_pattern(name) && fileset_is_file_in_set(name, get_basename(fname))) {
266                     fileset_add_file(dirname->str, name, strcmp(name, get_basename(fname))== 0 /* current */);
267                 }
268             } /* while */
269
270             eth_dir_close(dir);
271         } /* if */
272     } else {
273         /* no, this is a "standalone file", just add this one */
274         entry = fileset_add_file(dirname->str, get_basename(fname), TRUE /* current */);
275         /* don't add the file to the dialog here, this will be done in fileset_update_dlg() below */
276     }
277
278     g_string_free(dirname, TRUE /* free_segment */);
279
280     /* sort entries by creation time */
281     set.entries = g_list_sort(set.entries, fileset_sort_compare);
282
283     fileset_update_dlg();
284 }
285
286
287 /* get current directory name */
288 const char *
289 fileset_get_dirname(void)
290 {
291     return set.dirname;
292 }
293
294
295 /* get the current list entry, or NULL */
296 static GList *
297 fileset_get_current(void)
298 {
299     GList         *le;
300     fileset_entry *entry;
301
302
303     /* add all entires to the dialog */
304     le = g_list_first(set.entries);
305     while(le) {
306         entry = le->data;
307         if(entry->current) {
308             return le;
309         }
310         le = g_list_next(le);
311     }
312
313     return NULL;
314 }
315
316
317 /* get the file set entry after the current one, or NULL */
318 fileset_entry *
319 fileset_get_next(void)
320 {
321     GList         *le;
322
323
324     le = fileset_get_current();
325     if(le == NULL) {
326         return NULL;
327     }
328
329     le = g_list_next(le);
330     if(le == NULL) {
331         return NULL;
332     }
333
334     return le->data;
335 }
336
337
338 /* get the file set entry before the current one, or NULL */
339 fileset_entry *
340 fileset_get_previous(void)
341 {
342     GList         *le;
343
344
345     le = fileset_get_current();
346     if(le == NULL) {
347         return NULL;
348     }
349
350     le = g_list_previous(le);
351     if(le == NULL) {
352         return NULL;
353     }
354
355     return le->data;
356 }
357
358
359 /* delete a single entry */
360 static void fileset_entry_delete(gpointer data, gpointer user_data _U_)
361 {
362     fileset_entry *entry = data;
363
364     g_free( (gpointer) entry->fullname);
365     entry->fullname = NULL;
366     g_free( (gpointer) entry->name);
367     entry->name = NULL;
368 }
369
370
371 /* delete the whole file set */
372 void fileset_delete(void)
373 {
374     /* free the entry list */
375     if(set.entries) {
376         g_list_foreach(set.entries, fileset_entry_delete, NULL);
377         g_list_free(set.entries);
378         set.entries = NULL;
379     }
380
381     /* free the rest */
382     if(set.dirname) {
383         g_free( (gpointer) set.dirname);
384         set.dirname = NULL;
385     }
386 }
387
388