summaryrefslogtreecommitdiff
path: root/gtk/xdgmime/xdgmime.c
blob: e8734fb6cd5e30786819aaa017a9119cccfc59a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*- mode: C; c-file-style: "gnu" -*- */
/* xdgmime.c: XDG Mime Spec mime resolver.  Based on version 0.11 of the spec.
 *
 * More info can be found at http://www.freedesktop.org/standards/
 * 
 * Copyright (C) 2003  Red Hat, Inc.
 * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
 *
 * Licensed under the Academic Free License version 2.0
 * Or under the following terms:
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include "xdgmime.h"
#include "xdgmimeint.h"
#include "xdgmimeglob.h"
#include "xdgmimemagic.h"
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

static int initted = 0;
static XdgGlobHash *global_hash = NULL;
static XdgMimeMagic *global_magic = NULL;
const char *xdg_mime_type_unknown = "application/octet-stream";

static void
_xdg_mime_init_from_directory (const char *directory)
{
  char *file_name;

  file_name = malloc (strlen (directory) + strlen ("/mime/globs") + 1);
  strcpy (file_name, directory);
  strcat (file_name, "/mime/globs");
  _xdg_mime_glob_read_from_file (global_hash, file_name);
  free (file_name);

  file_name = malloc (strlen (directory) + strlen ("/mime/magic") + 1);
  strcpy (file_name, directory);
  strcat (file_name, "/mime/magic");
  _xdg_mime_magic_read_from_file (global_magic, file_name);
  free (file_name);
}

static void
xdg_mime_init (void)
{
  if (initted == 0)
    {
      const char *xdg_data_home;
      const char *xdg_data_dirs;
      const char *ptr;

      global_hash = _xdg_glob_hash_new ();
      global_magic = _xdg_mime_magic_new ();

      /* We look for globs and magic files based upon the XDG Base Directory
       * Specification
       */
      xdg_data_home = getenv ("XDG_DATA_HOME");
      if (xdg_data_home)
	{
	  _xdg_mime_init_from_directory (xdg_data_home);
	}
      else
	{
	  const char *home;

	  home = getenv ("HOME");
	  if (home != NULL)
	    {
	      char *guessed_xdg_home;

	      guessed_xdg_home = malloc (strlen (home) + strlen ("/.local/share/") + 1);
	      strcpy (guessed_xdg_home, home);
	      strcat (guessed_xdg_home, "/.local/share/");
	      _xdg_mime_init_from_directory (guessed_xdg_home);
	      free (guessed_xdg_home);
	    }
	}

      xdg_data_dirs = getenv ("XDG_DATA_DIRS");
      if (xdg_data_dirs == NULL)
	xdg_data_dirs = "/usr/local/share/:/usr/share/";

      ptr = xdg_data_dirs;

      while (*ptr != '\000')
	{
	  const char *end_ptr;
	  char *dir;
	  int len;

	  end_ptr = ptr;
	  while (*end_ptr != ':' && *end_ptr != '\000')
	    end_ptr ++;

	  if (end_ptr == ptr)
	    {
	      ptr++;
	      continue;
	    }

	  if (*end_ptr == ':')
	    len = end_ptr - ptr;
	  else
	    len = end_ptr - ptr + 1;
	  dir = malloc (len + 1);
	  strncpy (dir, ptr, len);
	  dir[len] = '\0';
	  _xdg_mime_init_from_directory (dir);
	  free (dir);

	  ptr = end_ptr;
	}
      initted = 1;
    }
}

const char *
xdg_mime_get_mime_type_for_data (const void *data,
				 size_t      len)
{
  const char *mime_type;

  xdg_mime_init ();

  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, len);

  if (mime_type)
    return mime_type;

  return XDG_MIME_TYPE_UNKNOWN;
}

const char *
xdg_mime_get_mime_type_for_file (const char *file_name)
{
  const char *mime_type;
  FILE *file;
  unsigned char *data;
  int max_extent;
  int bytes_read;
  struct stat statbuf;
  const char *base_name;

  if (file_name == NULL)
    return NULL;
  if (! _xdg_utf8_validate (file_name))
    return NULL;

  xdg_mime_init ();

  base_name = _xdg_get_base_name (file_name);
  mime_type = xdg_mime_get_mime_type_from_file_name (base_name);

  if (mime_type != XDG_MIME_TYPE_UNKNOWN)
    return mime_type;

  if (stat (file_name, &statbuf) != 0)
    return XDG_MIME_TYPE_UNKNOWN;

  if (!S_ISREG (statbuf.st_mode))
    return XDG_MIME_TYPE_UNKNOWN;

  /* FIXME: Need to make sure that max_extent isn't totally broken.  This could
   * be large and need getting from a stream instead of just reading it all
   * in. */
  max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
  data = malloc (max_extent);
  if (data == NULL)
    return XDG_MIME_TYPE_UNKNOWN;
        
      file = fopen (file_name, "r");
  if (file == NULL)
    {
      free (data);
      return XDG_MIME_TYPE_UNKNOWN;
    }

  bytes_read = fread (data, 1, max_extent, file);
  if (ferror (file))
    {
      free (data);
      fclose (file);
      return XDG_MIME_TYPE_UNKNOWN;
    }

  mime_type = _xdg_mime_magic_lookup_data (global_magic, data, bytes_read);

  free (data);
  fclose (file);

  if (mime_type)
    return mime_type;

  return XDG_MIME_TYPE_UNKNOWN;
}

const char *
xdg_mime_get_mime_type_from_file_name (const char *file_name)
{
  const char *mime_type;

  xdg_mime_init ();

  mime_type = _xdg_glob_hash_lookup_file_name (global_hash, file_name);
  if (mime_type)
    return mime_type;
  else
    return XDG_MIME_TYPE_UNKNOWN;
}

int
xdg_mime_is_valid_mime_type (const char *mime_type)
{
  /* FIXME: We should make this a better test
   */
  return _xdg_utf8_validate (mime_type);
}

void
xdg_mime_shutdown (void)
{
  /* FIXME: Need to make this (and the whole library) thread safe */
  if (initted)
    {
      _xdg_glob_hash_free (global_hash);
      global_hash = NULL;

      _xdg_mime_magic_free (global_magic);
      global_magic = NULL;

      initted = 0;
    }
}