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