• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kio/kio
 

kio/kio

  • kio
  • kio
kmimemagic.cpp
1 /* This file is part of the TDE libraries
2  Copyright (C) 2014 Timothy Pearson <kb9vqf@pearsoncomputing.net>
3 
4  Small portions (the original KDE interface and utime code) are:
5  Copyright (C) 2000 Fritz Elfert <fritz@kde.org>
6  Copyright (C) 2004 Allan Sandfeld Jensen <kde@carewolf.com>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License version 2 as published by the Free Software Foundation.
11 
12  This library 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 GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "config.h"
24 #include "kmimemagic.h"
25 #include <kdebug.h>
26 #include <kapplication.h>
27 #include <tqfile.h>
28 #include <ksimpleconfig.h>
29 #include <kstandarddirs.h>
30 #include <kstaticdeleter.h>
31 #include <klargefile.h>
32 #include <assert.h>
33 
34 #include <magic.h>
35 
36 #ifndef MAGIC_MIME_TYPE
37 #define MAGIC_MIME_TYPE MAGIC_MIME
38 #endif
39 
40 // Taken from file/file.h
41 // Keep in sync with that header!
42 #define FILE_LOAD 0
43 
44 static void process(struct config_rec* conf, const TQString &);
45 
46 KMimeMagic* KMimeMagic::s_pSelf;
47 static KStaticDeleter<KMimeMagic> kmimemagicsd;
48 
49 KMimeMagic* KMimeMagic::self() {
50  if( !s_pSelf ) {
51  initStatic();
52  }
53  return s_pSelf;
54 }
55 
56 void KMimeMagic::initStatic() {
57  s_pSelf = kmimemagicsd.setObject( s_pSelf, new KMimeMagic() );
58  s_pSelf->setFollowLinks( true );
59 }
60 
61 #include <stdio.h>
62 #include <unistd.h>
63 #include <stdlib.h>
64 #include <sys/wait.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <fcntl.h>
68 #include <errno.h>
69 #include <ctype.h>
70 #include <time.h>
71 #include <utime.h>
72 #include <stdarg.h>
73 #include <tqregexp.h>
74 #include <tqstring.h>
75 
76 #define MIME_INODE_DIR "inode/directory"
77 #define MIME_INODE_CDEV "inode/chardevice"
78 #define MIME_INODE_BDEV "inode/blockdevice"
79 #define MIME_INODE_FIFO "inode/fifo"
80 #define MIME_INODE_LINK "inode/link"
81 #define MIME_INODE_SOCK "inode/socket"
82 #define MIME_BINARY_UNREADABLE "application/x-unreadable"
83 #define MIME_BINARY_ZEROSIZE "application/x-zerosize"
84 
95 class KMimeMagicUtimeConf {
96  public:
97  KMimeMagicUtimeConf() {
98  tmpDirs << TQString::fromLatin1("/tmp"); // default value
99 
100  // The trick is that we also don't want the user to override globally set
101  // directories. So we have to misuse KStandardDirs :}
102  TQStringList confDirs = KGlobal::dirs()->resourceDirs( "config" );
103  if ( !confDirs.isEmpty() ) {
104  TQString globalConf = confDirs.last() + "kmimemagicrc";
105  if ( TQFile::exists( globalConf ) ) {
106  KSimpleConfig cfg( globalConf );
107  cfg.setGroup( "Settings" );
108  tmpDirs = cfg.readListEntry( "atimeDirs" );
109  }
110  if ( confDirs.count() > 1 ) {
111  TQString localConf = confDirs.first() + "kmimemagicrc";
112  if ( TQFile::exists( localConf ) ) {
113  KSimpleConfig cfg( localConf );
114  cfg.setGroup( "Settings" );
115  tmpDirs += cfg.readListEntry( "atimeDirs" );
116  }
117  }
118  for ( TQStringList::Iterator it = tmpDirs.begin() ; it != tmpDirs.end() ; ++it ) {
119  TQString dir = *it;
120  if ( !dir.isEmpty() && dir[ dir.length()-1 ] != '/' ) {
121  (*it) += '/';
122  }
123  }
124  }
125  #if 0
126  // debug code
127  for ( TQStringList::Iterator it = tmpDirs.begin() ; it != tmpDirs.end() ; ++it ) {
128  kdDebug(7018) << " atimeDir: " << *it << endl;
129  }
130  #endif
131  }
132 
133  bool restoreAccessTime( const TQString & file ) const {
134  TQString dir = file.left( file.findRev( '/' ) );
135  bool res = tmpDirs.contains( dir );
136  //kdDebug(7018) << "restoreAccessTime " << file << " dir=" << dir << " result=" << res << endl;
137  return res;
138  }
139  TQStringList tmpDirs;
140 };
141 
142 TQString fixupMagicOutput(TQString &mime) {
143  if (mime == "inode/x-empty") {
144  return MIME_BINARY_ZEROSIZE;
145  }
146  else if (mime.contains("no read permission")) {
147  return MIME_BINARY_UNREADABLE;
148  }
149  else {
150  return mime;
151  }
152 }
153 
154 /* current config */
155 struct config_rec {
156  bool followLinks;
157  TQString resultBuf;
158  int accuracy;
159 
160  magic_t magic;
161  TQStringList databases;
162 
163  KMimeMagicUtimeConf * utimeConf;
164 };
165 
166 /*
167  * apprentice - load configuration from the magic file.
168  */
169 int KMimeMagic::apprentice( const TQString& magicfile ) {
170  TQString maindatabase = magicfile;
171  if (maindatabase == "") {
172 #ifdef HAVE_LIBMAGIC_GETPATH
173  maindatabase = magic_getpath(0, FILE_LOAD);
174 #else
175  maindatabase = TQString(LIBMAGIC_PATH);
176 #endif
177  }
178  conf->databases.clear();
179  conf->databases.append(maindatabase);
180  return magic_load(conf->magic, conf->databases[0].latin1());
181 }
182 
183 /*
184  * magic_process - process input file fn. Opens the file and reads a
185  * fixed-size buffer to begin processing the contents.
186  */
187 
188 void process(struct config_rec* conf, const TQString & fn) {
189  KDE_struct_stat sb;
190  TQCString fileName = TQFile::encodeName( fn );
191 
192  int magic_flags = MAGIC_ERROR|MAGIC_MIME_TYPE/*|MAGIC_DEBUG*/;
193  if (conf->followLinks) {
194  magic_flags |= MAGIC_SYMLINK;
195  }
196  magic_setflags(conf->magic, magic_flags);
197  conf->resultBuf = TQString(magic_file(conf->magic, fileName));
198  conf->resultBuf = fixupMagicOutput(conf->resultBuf);
199 
200  if ( conf->utimeConf && conf->utimeConf->restoreAccessTime( fn ) ) {
201  /*
202  * Try to restore access, modification times if read it.
203  * This changes the "change" time (ctime), but we can't do anything
204  * about that.
205  */
206  struct utimbuf utbuf;
207  utbuf.actime = sb.st_atime;
208  utbuf.modtime = sb.st_mtime;
209  (void) utime(fileName, &utbuf);
210  }
211 }
212 
213 KMimeMagic::KMimeMagic() {
214  // Magic file detection init
215  TQString mimefile = locate( "mime", "magic" );
216  init( mimefile );
217  // Add snippets from share/config/magic/*
218  TQStringList snippets = KGlobal::dirs()->findAllResources( "config", "magic/*.magic", true );
219  for ( TQStringList::Iterator it = snippets.begin() ; it != snippets.end() ; ++it ) {
220  if ( !mergeConfig( *it ) ) {
221  kdWarning() << k_funcinfo << "Failed to parse " << *it << endl;
222  }
223  }
224 }
225 
226 KMimeMagic::KMimeMagic(const TQString & _configfile) {
227  init( _configfile );
228 }
229 
230 void KMimeMagic::init( const TQString& _configfile ) {
231  int result;
232  conf = new config_rec;
233 
234  /* initialize libmagic */
235  conf->magic = magic_open(MAGIC_MIME_TYPE);
236  magicResult = NULL;
237  conf->followLinks = false;
238 
239  conf->utimeConf = 0L; // created on demand
240  /* on the first time through we read the magic file */
241  result = apprentice(_configfile);
242  if (result == -1) {
243  return;
244  }
245 }
246 
247 /*
248  * The destructor.
249  * Free the magic-table and other resources.
250  */
251 KMimeMagic::~KMimeMagic() {
252  if (conf) {
253  magic_close(conf->magic);
254  delete conf->utimeConf;
255  delete conf;
256  }
257  delete magicResult;
258 }
259 
260 bool KMimeMagic::mergeConfig(const TQString & _configfile) {
261  conf->databases.append(_configfile);
262  TQString merged_databases = conf->databases.join(":");
263 #ifdef MAGIC_VERSION
264  int magicvers = magic_version();
265 #else // MAGIC_VERSION
266  int magicvers = 0;
267 #endif // MAGIC_VERSION
268  if ((magicvers < 512) || (magicvers >= 518)) {
269  if (magic_load(conf->magic, merged_databases.latin1()) == 0) {
270  return true;
271  }
272  else {
273  return false;
274  }
275  }
276  else {
277  kdWarning(7018) << "KMimeMagic::mergeConfig disabled due to buggy libmagic version " << magicvers << endl;
278  return false;
279  }
280 }
281 
282 void KMimeMagic::setFollowLinks( bool _enable ) {
283  conf->followLinks = _enable;
284 }
285 
286 KMimeMagicResult *KMimeMagic::findBufferType(const TQByteArray &array) {
287  conf->resultBuf = TQString::null;
288  if ( !magicResult ) {
289  magicResult = new KMimeMagicResult();
290  }
291  magicResult->setInvalid();
292  conf->accuracy = 100;
293 
294  int nbytes = array.size();
295  if (nbytes == 0) {
296  conf->resultBuf = MIME_BINARY_ZEROSIZE;
297  }
298  else {
299  int magic_flags = MAGIC_ERROR|MAGIC_MIME_TYPE/*|MAGIC_DEBUG*/;
300  if (conf->followLinks) {
301  magic_flags |= MAGIC_SYMLINK;
302  }
303  magic_setflags(conf->magic, magic_flags);
304  conf->resultBuf = TQString(magic_buffer(conf->magic, array.data(), nbytes));
305  conf->resultBuf = fixupMagicOutput(conf->resultBuf);
306  }
307  /* if we have any results, put them in the request structure */
308  magicResult->setMimeType(conf->resultBuf.stripWhiteSpace());
309  magicResult->setAccuracy(conf->accuracy);
310  return magicResult;
311 }
312 
313 static void refineResult(KMimeMagicResult *r, const TQString & _filename) {
314  TQString tmp = r->mimeType();
315  if (tmp.isEmpty())
316  return;
317  if ( tmp == "text/x-c" || tmp == "text/x-objc" )
318  {
319  if ( _filename.right(2) == ".h" )
320  tmp += "hdr";
321  else
322  tmp += "src";
323  r->setMimeType(tmp);
324  }
325  else
326  if ( tmp == "text/x-c++" )
327  {
328  if ( _filename.endsWith(".h")
329  || _filename.endsWith(".hh")
330  || _filename.endsWith(".H")
331  || !_filename.right(4).contains('.'))
332  tmp += "hdr";
333  else
334  tmp += "src";
335  r->setMimeType(tmp);
336  }
337  else
338  if ( tmp == "application/x-sharedlib" )
339  {
340  if ( _filename.find( ".so" ) == -1 )
341  {
342  tmp = "application/x-executable";
343  r->setMimeType( tmp );
344  }
345  }
346 }
347 
348 KMimeMagicResult *KMimeMagic::findBufferFileType( const TQByteArray &data, const TQString &fn) {
349  KMimeMagicResult * r = findBufferType( data );
350  refineResult(r, fn);
351  return r;
352 }
353 
354 /*
355  * Find the content-type of the given file.
356  */
357 KMimeMagicResult* KMimeMagic::findFileType(const TQString & fn) {
358 #ifdef DEBUG_MIMEMAGIC
359  kdDebug(7018) << "KMimeMagic::findFileType " << fn << endl;
360 #endif
361  conf->resultBuf = TQString::null;
362 
363  if ( !magicResult ) {
364  magicResult = new KMimeMagicResult();
365  }
366  magicResult->setInvalid();
367  conf->accuracy = 100;
368 
369  if ( !conf->utimeConf ) {
370  conf->utimeConf = new KMimeMagicUtimeConf();
371  }
372 
373  /* process it based on the file contents */
374  process(conf, fn );
375 
376  /* if we have any results, put them in the request structure */
377  magicResult->setMimeType(conf->resultBuf.stripWhiteSpace());
378  magicResult->setAccuracy(conf->accuracy);
379  refineResult(magicResult, fn);
380  return magicResult;
381 }

kio/kio

Skip menu "kio/kio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kio/kio

Skip menu "kio/kio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kio/kio by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |