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