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