From Nils Ljungberg:
[obnox/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         if(entry) {
276             fileset_dlg_add_file(entry);
277         }
278     }
279
280     g_string_free(dirname, TRUE /* free_segment */);
281
282     /* sort entries by creation time */
283     set.entries = g_list_sort(set.entries, fileset_sort_compare);
284
285     fileset_update_dlg();
286 }
287
288
289 /* get current directory name */
290 const char *
291 fileset_get_dirname(void)
292 {
293     return set.dirname;
294 }
295
296
297 /* get the current list entry, or NULL */
298 static GList *
299 fileset_get_current(void)
300 {
301     GList         *le;
302     fileset_entry *entry;
303
304
305     /* add all entires to the dialog */
306     le = g_list_first(set.entries);
307     while(le) {
308         entry = le->data;
309         if(entry->current) {
310             return le;
311         }
312         le = g_list_next(le);
313     }
314
315     return NULL;
316 }
317
318
319 /* get the file set entry after the current one, or NULL */
320 fileset_entry *
321 fileset_get_next(void)
322 {
323     GList         *le;
324
325
326     le = fileset_get_current();
327     if(le == NULL) {
328         return NULL;
329     }
330
331     le = g_list_next(le);
332     if(le == NULL) {
333         return NULL;
334     }
335
336     return le->data;
337 }
338
339
340 /* get the file set entry before the current one, or NULL */
341 fileset_entry *
342 fileset_get_previous(void)
343 {
344     GList         *le;
345
346
347     le = fileset_get_current();
348     if(le == NULL) {
349         return NULL;
350     }
351
352     le = g_list_previous(le);
353     if(le == NULL) {
354         return NULL;
355     }
356
357     return le->data;
358 }
359
360
361 /* delete a single entry */
362 static void fileset_entry_delete(gpointer data, gpointer user_data _U_)
363 {
364     fileset_entry *entry = data;
365
366     g_free( (gpointer) entry->fullname);
367     entry->fullname = NULL;
368     g_free( (gpointer) entry->name);
369     entry->name = NULL;
370 }
371
372
373 /* delete the whole file set */
374 void fileset_delete(void)
375 {
376     /* free the entry list */
377     if(set.entries) {
378         g_list_foreach(set.entries, fileset_entry_delete, NULL);
379         g_list_free(set.entries);
380         set.entries = NULL;
381     }
382
383     /* free the rest */
384     if(set.dirname) {
385         g_free( (gpointer) set.dirname);
386         set.dirname = NULL;
387     }
388 }
389
390