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 <core/exception.h>
25 #include <utils/system/filetype.h>
26 
27 #ifdef HAVE_LIBMAGIC
28 # include <magic.h>
29 #endif
30 
31 #include <sys/types.h>
32 
33 #include <cstdio>
34 #include <unistd.h>
35 
36 namespace fawkes {
37 
38 /** Get filetype of file.
39  * Returns a long decriptive string of the filetype, similar to the file
40  * console utility.
41  * @param filename path to the file whose type should be determined
42  * @return descriptive string
43  */
44 std::string
45 filetype_file(const char *filename)
46 {
47  std::string rv;
48 
49 #ifdef HAVE_LIBMAGIC
50  magic_t m = magic_open(MAGIC_ERROR);
51  magic_load(m, NULL);
52 
53  const char *res = magic_file(m, filename);
54  if (res == NULL) {
55  fawkes::Exception e("Failed to determine file type of %s: %s", filename, magic_error(m));
56  magic_close(m);
57  throw e;
58  }
59 
60  rv = res;
61  magic_close(m);
62 #else
63  throw fawkes::Exception("Failed to determine file type of %s "
64  "(libmagic not available at compile time)",
65  filename);
66 #endif
67 
68  return rv;
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 /** Get mime-type of file.
106  * This function gives a brief mime-type for the given file.
107  * @param filename path to the file whose type should be determined
108  * @return descriptive string
109  */
110 std::string
111 mimetype_file(const char *filename)
112 {
113  std::string rv;
114 
115 #ifdef HAVE_LIBMAGIC
116 # ifdef MAGIC_MIME_TYPE
117  magic_t m = magic_open(MAGIC_ERROR | MAGIC_MIME_TYPE);
118 # else
119  magic_t m = magic_open(MAGIC_ERROR | MAGIC_MIME);
120 # endif
121  magic_load(m, NULL);
122 
123  const char *res = magic_file(m, filename);
124  if (res == NULL) {
125  fawkes::Exception e("Failed to determine mime type of %s: %s", filename, magic_error(m));
126  magic_close(m);
127  throw e;
128  }
129 
130  rv = res;
131 # ifndef MAGIC_MIME_TYPE
132  rv = rv.substr(0, rv.find(","));
133 # endif
134  magic_close(m);
135 #else
136  throw fawkes::Exception("Failed to determine file type of %s "
137  "(libmagic not available at compile time)",
138  filename);
139 #endif
140  return rv;
141 }
142 
143 /** Get mime-type of file given by file descriptor.
144  * This function gives a brief mime-type for the given file.
145  * @param fd file descriptor of open file, make sure the file descriptor is rewinded.
146  * Warning, the file descriptor is closed by the underlying libmagic. Use dup() to
147  * duplicate it and pass this as file descriptor if you need the file afterwards.
148  * @return descriptive string
149  */
150 std::string
152 {
153  std::string rv;
154 
155 #ifdef HAVE_LIBMAGIC
156 # ifdef MAGIC_MIME_TYPE
157  magic_t m = magic_open(MAGIC_ERROR | MAGIC_MIME_TYPE);
158 # else
159  magic_t m = magic_open(MAGIC_ERROR | MAGIC_MIME);
160 # endif
161  magic_load(m, NULL);
162 
163  const char *res = magic_descriptor(m, fd);
164  if (res == NULL) {
165  fawkes::Exception e("Failed to determine mime type of descriptor: %s", magic_error(m));
166  magic_close(m);
167  throw e;
168  }
169 
170  rv = res;
171 # ifndef MAGIC_MIME_TYPE
172  rv = rv.substr(0, rv.find(","));
173 # endif
174  magic_close(m);
175 #else
176  throw fawkes::Exception("Failed to determine file type "
177  "(libmagic not available at compile time)");
178 #endif
179  return rv;
180 }
181 
182 } // end namespace fawkes
fawkes::filetype_file
std::string filetype_file(const char *filename)
Get filetype of file.
Definition: filetype.cpp:45
fawkes
Fawkes library namespace.
fawkes::mimetype_file
std::string mimetype_file(const char *filename)
Get mime-type of file.
Definition: filetype.cpp:111
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36