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