Fawkes API  Fawkes Development Version
filetype.cpp
1 
2 /***************************************************************************
3  * filetype.cpp - little utility to decide on filetype
4  *
5  * Generated: Sun Oct 26 10:52:59 2008 (split off cpp file)
6  * Copyright 2005-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/system/filetype.h>
25 #include <core/exception.h>
26 
27 #ifdef HAVE_LIBMAGIC
28 # include <magic.h>
29 #endif
30 
31 #include <cstdio>
32 #include <sys/types.h>
33 #include <unistd.h>
34 
35 namespace fawkes {
36 
37 /** Get filetype of file.
38  * Returns a long decriptive string of the filetype, similar to the file
39  * console utility.
40  * @param filename path to the file whose type should be determined
41  * @return descriptive string
42  */
43 std::string
44 filetype_file(const char *filename)
45 {
46  std::string rv;
47 
48 #ifdef HAVE_LIBMAGIC
49  magic_t m = magic_open( MAGIC_ERROR );
50  magic_load( m, NULL );
51 
52  const char * res = magic_file( m, filename );
53  if ( res == NULL ) {
54  fawkes::Exception e("Failed to determine file type of %s: %s", filename, magic_error(m));
55  magic_close(m);
56  throw e;
57  }
58 
59  rv = res;
60  magic_close( m );
61 #else
62  throw fawkes::Exception("Failed to determine file type of %s "
63  "(libmagic not available at compile time)",
64  filename);
65 #endif
66 
67  return rv;
68 }
69 
70 
71 /** Get filetype of file given by file descriptor.
72  * Returns a long decriptive string of the filetype, similar to the file
73  * console utility.
74  * @param fd file descriptor of open file, make sure the file descriptor is rewinded
75  * Warning, the file descriptor is closed by the underlying libmagic. Use dup() to
76  * duplicate it and pass this as file descriptor if you need the file afterwards.
77  * @return descriptive string
78  */
79 std::string
81 {
82  std::string rv;
83 
84 #ifdef HAVE_LIBMAGIC
85  magic_t m = magic_open( MAGIC_ERROR );
86  magic_load( m, NULL );
87 
88  const char * res = magic_descriptor( m, fd );
89  if ( res == NULL ) {
90  fawkes::Exception e("Failed to determine file type of descriptor: %s", magic_error(m));
91  magic_close(m);
92  throw e;
93  }
94 
95  rv = res;
96  magic_close( m );
97 #else
98  throw fawkes::Exception("Failed to determine file type "
99  "(libmagic not available at compile time)");
100 #endif
101 
102  return rv;
103 }
104 
105 
106 /** Get mime-type of file.
107  * This function gives a brief mime-type for the given file.
108  * @param filename path to the file whose type should be determined
109  * @return descriptive string
110  */
111 std::string
112 mimetype_file(const char *filename)
113 {
114  std::string rv;
115 
116 #ifdef HAVE_LIBMAGIC
117 # ifdef MAGIC_MIME_TYPE
118  magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME_TYPE );
119 # else
120  magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME );
121 # endif
122  magic_load( m, NULL );
123 
124  const char * res = magic_file( m, filename );
125  if ( res == NULL ) {
126  fawkes::Exception e("Failed to determine mime type of %s: %s", filename, magic_error(m));
127  magic_close(m);
128  throw e;
129  }
130 
131  rv = res;
132 # ifndef MAGIC_MIME_TYPE
133  rv = rv.substr(0, rv.find(","));
134 # endif
135  magic_close(m);
136 #else
137  throw fawkes::Exception("Failed to determine file type of %s "
138  "(libmagic not available at compile time)",
139  filename);
140 #endif
141  return rv;
142 }
143 
144 
145 /** Get mime-type of file given by file descriptor.
146  * This function gives a brief mime-type for the given file.
147  * @param fd file descriptor of open file, make sure the file descriptor is rewinded.
148  * Warning, the file descriptor is closed by the underlying libmagic. Use dup() to
149  * duplicate it and pass this as file descriptor if you need the file afterwards.
150  * @return descriptive string
151  */
152 std::string
154 {
155  std::string rv;
156 
157 #ifdef HAVE_LIBMAGIC
158 # ifdef MAGIC_MIME_TYPE
159  magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME_TYPE );
160 # else
161  magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME );
162 # endif
163  magic_load( m, NULL );
164 
165  const char * res = magic_descriptor( m, fd );
166  if ( res == NULL ) {
167  fawkes::Exception e("Failed to determine mime type of descriptor: %s", magic_error(m));
168  magic_close(m);
169  throw e;
170  }
171 
172  rv = res;
173 # ifndef MAGIC_MIME_TYPE
174  rv = rv.substr(0, rv.find(","));
175 # endif
176  magic_close(m);
177 #else
178  throw fawkes::Exception("Failed to determine file type "
179  "(libmagic not available at compile time)");
180 #endif
181  return rv;
182 }
183 
184 } // end namespace fawkes
185 
Fawkes library namespace.
std::string mimetype_file(const char *filename)
Get mime-type of file.
Definition: filetype.cpp:112
Base class for exceptions in Fawkes.
Definition: exception.h:36
std::string filetype_file(const char *filename)
Get filetype of file.
Definition: filetype.cpp:44