]> git.street.me.uk Git - andy/viking.git/blob - src/file_magic.c
Tidy up type usage
[andy/viking.git] / src / file_magic.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4  *
5  * Copyright (C) 2016, Rob Norris <rw_norris@hotmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "file_magic.h"
27 #include "file.h"
28 #ifdef HAVE_MAGIC_H
29 #include <magic.h>
30 #endif
31 #include <string.h>
32
33 /**
34  * file_magic_check:
35  * @filename:     The file
36  * @magic_string: Type of file (this part matched against the magic library response)
37  * @extension:    Optional extension to try when Magic Library is not available
38  *
39  * Returns: Whether the file is of the specified type.
40  *  Uses the Magic library if available to determine the file against the supplied type.
41  *  Otherwise uses a rudimentary file extension check.
42  */
43 gboolean file_magic_check ( const gchar *filename, const gchar *magic_string, const gchar *extension )
44 {
45         gboolean is_requested_file_type = FALSE;
46 #ifdef HAVE_MAGIC_H
47 #ifdef MAGIC_VERSION
48         // Or magic_version() if available - probably need libmagic 5.18 or so
49         //  (can't determine exactly which version the versioning became available)
50         g_debug ("%s: magic version: %d", __FUNCTION__, MAGIC_VERSION );
51 #endif
52         magic_t myt = magic_open ( MAGIC_CONTINUE|MAGIC_ERROR|MAGIC_MIME );
53         if ( myt ) {
54 #ifdef WINDOWS
55                 // We have to 'package' the magic database ourselves :(
56                 //  --> %PROGRAM FILES%\Viking\magic.mgc
57                 int ml = magic_load ( myt, ".\\magic.mgc" );
58 #else
59                 // Use system default
60                 int ml = magic_load ( myt, NULL );
61 #endif
62                 if ( ml == 0 ) {
63                         const char* magic = magic_file ( myt, filename );
64                         g_debug ("%s: magic output: %s", __FUNCTION__, magic );
65
66                         if ( g_ascii_strncasecmp ( magic, magic_string, strlen(magic_string) ) == 0 )
67                                 is_requested_file_type = TRUE;
68                 }
69                 else {
70                         g_critical ("%s: magic load database failure", __FUNCTION__ );
71                 }
72
73                 magic_close ( myt );
74         }
75         else
76 #endif
77                 is_requested_file_type = a_file_check_ext ( filename, extension );
78
79         return is_requested_file_type;
80 }