OLD | NEW |
(Empty) | |
| 1 /* -*- mode: C; c-file-style: "gnu" -*- */ |
| 2 /* xdgmimealias.c: Private file. mmappable caches for mime data |
| 3 * |
| 4 * More info can be found at http://www.freedesktop.org/standards/ |
| 5 * |
| 6 * Copyright (C) 2005 Matthias Clasen <mclasen@redhat.com> |
| 7 * |
| 8 * Licensed under the Academic Free License version 2.0 |
| 9 * Or under the following terms: |
| 10 * |
| 11 * This library is free software; you can redistribute it and/or |
| 12 * modify it under the terms of the GNU Lesser General Public |
| 13 * License as published by the Free Software Foundation; either |
| 14 * version 2 of the License, or (at your option) any later version. |
| 15 * |
| 16 * This library 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 GNU |
| 19 * Lesser General Public License for more details. |
| 20 * |
| 21 * You should have received a copy of the GNU Lesser General Public |
| 22 * License along with this library; if not, write to the |
| 23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 24 * Boston, MA 02111-1307, USA. |
| 25 */ |
| 26 |
| 27 #ifdef HAVE_CONFIG_H |
| 28 #include "config.h" |
| 29 #endif |
| 30 |
| 31 #include <stdio.h> |
| 32 #include <stdlib.h> |
| 33 #include <string.h> |
| 34 #include <ctype.h> |
| 35 |
| 36 #include <fcntl.h> |
| 37 #include <unistd.h> |
| 38 #include <fnmatch.h> |
| 39 #include <assert.h> |
| 40 |
| 41 #include <netinet/in.h> /* for ntohl/ntohs */ |
| 42 |
| 43 #define HAVE_MMAP 1 |
| 44 |
| 45 #ifdef HAVE_MMAP |
| 46 #include <sys/mman.h> |
| 47 #else |
| 48 #warning Building xdgmime without MMAP support. Binary "mime.info" cache files w
ill not be used. |
| 49 #endif |
| 50 |
| 51 #include <sys/stat.h> |
| 52 #include <sys/types.h> |
| 53 |
| 54 #include "xdgmimecache.h" |
| 55 #include "xdgmimeint.h" |
| 56 |
| 57 #ifndef MAX |
| 58 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 59 #endif |
| 60 |
| 61 #ifndef FALSE |
| 62 #define FALSE (0) |
| 63 #endif |
| 64 |
| 65 #ifndef TRUE |
| 66 #define TRUE (!FALSE) |
| 67 #endif |
| 68 |
| 69 #ifndef _O_BINARY |
| 70 #define _O_BINARY 0 |
| 71 #endif |
| 72 |
| 73 #ifndef MAP_FAILED |
| 74 #define MAP_FAILED ((void *) -1) |
| 75 #endif |
| 76 |
| 77 #define MAJOR_VERSION 1 |
| 78 #define MINOR_VERSION 1 |
| 79 |
| 80 struct _XdgMimeCache |
| 81 { |
| 82 int ref_count; |
| 83 |
| 84 size_t size; |
| 85 char *buffer; |
| 86 }; |
| 87 |
| 88 #define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset)))) |
| 89 #define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset)))) |
| 90 |
| 91 XdgMimeCache * |
| 92 _xdg_mime_cache_ref (XdgMimeCache *cache) |
| 93 { |
| 94 cache->ref_count++; |
| 95 return cache; |
| 96 } |
| 97 |
| 98 void |
| 99 _xdg_mime_cache_unref (XdgMimeCache *cache) |
| 100 { |
| 101 cache->ref_count--; |
| 102 |
| 103 if (cache->ref_count == 0) |
| 104 { |
| 105 #ifdef HAVE_MMAP |
| 106 munmap (cache->buffer, cache->size); |
| 107 #endif |
| 108 free (cache); |
| 109 } |
| 110 } |
| 111 |
| 112 XdgMimeCache * |
| 113 _xdg_mime_cache_new_from_file (const char *file_name) |
| 114 { |
| 115 XdgMimeCache *cache = NULL; |
| 116 |
| 117 #ifdef HAVE_MMAP |
| 118 int fd = -1; |
| 119 struct stat st; |
| 120 char *buffer = NULL; |
| 121 |
| 122 /* Open the file and map it into memory */ |
| 123 fd = open (file_name, O_RDONLY|_O_BINARY, 0); |
| 124 |
| 125 if (fd < 0) |
| 126 return NULL; |
| 127 |
| 128 if (fstat (fd, &st) < 0 || st.st_size < 4) |
| 129 goto done; |
| 130 |
| 131 buffer = (char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 132 |
| 133 if (buffer == MAP_FAILED) |
| 134 goto done; |
| 135 |
| 136 /* Verify version */ |
| 137 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION || |
| 138 GET_UINT16 (buffer, 2) != MINOR_VERSION) |
| 139 { |
| 140 munmap (buffer, st.st_size); |
| 141 |
| 142 goto done; |
| 143 } |
| 144 |
| 145 cache = (XdgMimeCache *) malloc (sizeof (XdgMimeCache)); |
| 146 cache->ref_count = 1; |
| 147 cache->buffer = buffer; |
| 148 cache->size = st.st_size; |
| 149 |
| 150 done: |
| 151 if (fd != -1) |
| 152 close (fd); |
| 153 |
| 154 #endif /* HAVE_MMAP */ |
| 155 |
| 156 return cache; |
| 157 } |
| 158 |
| 159 static int |
| 160 cache_magic_matchlet_compare_to_data (XdgMimeCache *cache, |
| 161 xdg_uint32_t offset, |
| 162 const void *data, |
| 163 size_t len) |
| 164 { |
| 165 xdg_uint32_t range_start = GET_UINT32 (cache->buffer, offset); |
| 166 xdg_uint32_t range_length = GET_UINT32 (cache->buffer, offset + 4); |
| 167 xdg_uint32_t data_length = GET_UINT32 (cache->buffer, offset + 12); |
| 168 xdg_uint32_t data_offset = GET_UINT32 (cache->buffer, offset + 16); |
| 169 xdg_uint32_t mask_offset = GET_UINT32 (cache->buffer, offset + 20); |
| 170 |
| 171 int i, j; |
| 172 |
| 173 for (i = range_start; i <= range_start + range_length; i++) |
| 174 { |
| 175 int valid_matchlet = TRUE; |
| 176 |
| 177 if (i + data_length > len) |
| 178 return FALSE; |
| 179 |
| 180 if (mask_offset) |
| 181 { |
| 182 for (j = 0; j < data_length; j++) |
| 183 { |
| 184 if ((((unsigned char *)cache->buffer)[data_offset + j] & ((unsigne
d char *)cache->buffer)[mask_offset + j]) != |
| 185 ((((unsigned char *) data)[j + i]) & ((unsigned char *)cache->
buffer)[mask_offset + j])) |
| 186 { |
| 187 valid_matchlet = FALSE; |
| 188 break; |
| 189 } |
| 190 } |
| 191 } |
| 192 else |
| 193 { |
| 194 for (j = 0; j < data_length; j++) |
| 195 { |
| 196 if (((unsigned char *)cache->buffer)[data_offset + j] != ((unsigne
d char *) data)[j + i]) |
| 197 { |
| 198 valid_matchlet = FALSE; |
| 199 break; |
| 200 } |
| 201 } |
| 202 } |
| 203 |
| 204 if (valid_matchlet) |
| 205 return TRUE; |
| 206 } |
| 207 |
| 208 return FALSE; |
| 209 } |
| 210 |
| 211 static int |
| 212 cache_magic_matchlet_compare (XdgMimeCache *cache, |
| 213 xdg_uint32_t offset, |
| 214 const void *data, |
| 215 size_t len) |
| 216 { |
| 217 xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24); |
| 218 xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28); |
| 219 |
| 220 int i; |
| 221 |
| 222 if (cache_magic_matchlet_compare_to_data (cache, offset, data, len)) |
| 223 { |
| 224 if (n_children == 0) |
| 225 return TRUE; |
| 226 |
| 227 for (i = 0; i < n_children; i++) |
| 228 { |
| 229 if (cache_magic_matchlet_compare (cache, child_offset + 32 * i, |
| 230 data, len)) |
| 231 return TRUE; |
| 232 } |
| 233 } |
| 234 |
| 235 return FALSE; |
| 236 } |
| 237 |
| 238 static const char * |
| 239 cache_magic_compare_to_data (XdgMimeCache *cache, |
| 240 xdg_uint32_t offset, |
| 241 const void *data, |
| 242 size_t len, |
| 243 int *prio) |
| 244 { |
| 245 xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset); |
| 246 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4); |
| 247 xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8); |
| 248 xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12); |
| 249 |
| 250 int i; |
| 251 |
| 252 for (i = 0; i < n_matchlets; i++) |
| 253 { |
| 254 if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32, |
| 255 data, len)) |
| 256 { |
| 257 *prio = priority; |
| 258 |
| 259 return cache->buffer + mimetype_offset; |
| 260 } |
| 261 } |
| 262 |
| 263 return NULL; |
| 264 } |
| 265 |
| 266 static const char * |
| 267 cache_magic_lookup_data (XdgMimeCache *cache, |
| 268 const void *data, |
| 269 size_t len, |
| 270 int *prio, |
| 271 const char *mime_types[], |
| 272 int n_mime_types) |
| 273 { |
| 274 xdg_uint32_t list_offset; |
| 275 xdg_uint32_t n_entries; |
| 276 xdg_uint32_t offset; |
| 277 |
| 278 int j, n; |
| 279 |
| 280 *prio = 0; |
| 281 |
| 282 list_offset = GET_UINT32 (cache->buffer, 24); |
| 283 n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 284 offset = GET_UINT32 (cache->buffer, list_offset + 8); |
| 285 |
| 286 for (j = 0; j < n_entries; j++) |
| 287 { |
| 288 const char *match; |
| 289 |
| 290 match = cache_magic_compare_to_data (cache, offset + 16 * j, |
| 291 data, len, prio); |
| 292 if (match) |
| 293 return match; |
| 294 else |
| 295 { |
| 296 xdg_uint32_t mimetype_offset; |
| 297 const char *non_match; |
| 298 |
| 299 mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * j + 4); |
| 300 non_match = cache->buffer + mimetype_offset; |
| 301 |
| 302 for (n = 0; n < n_mime_types; n++) |
| 303 { |
| 304 if (mime_types[n] && |
| 305 _xdg_mime_mime_type_equal (mime_types[n], non_match)) |
| 306 mime_types[n] = NULL; |
| 307 } |
| 308 } |
| 309 } |
| 310 |
| 311 return NULL; |
| 312 } |
| 313 |
| 314 static const char * |
| 315 cache_alias_lookup (const char *alias) |
| 316 { |
| 317 const char *ptr; |
| 318 int i, min, max, mid, cmp; |
| 319 |
| 320 for (i = 0; _caches[i]; i++) |
| 321 { |
| 322 XdgMimeCache *cache = _caches[i]; |
| 323 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4); |
| 324 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 325 xdg_uint32_t offset; |
| 326 |
| 327 min = 0; |
| 328 max = n_entries - 1; |
| 329 while (max >= min) |
| 330 { |
| 331 mid = (min + max) / 2; |
| 332 |
| 333 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid); |
| 334 ptr = cache->buffer + offset; |
| 335 cmp = strcmp (ptr, alias); |
| 336 |
| 337 if (cmp < 0) |
| 338 min = mid + 1; |
| 339 else if (cmp > 0) |
| 340 max = mid - 1; |
| 341 else |
| 342 { |
| 343 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4)
; |
| 344 return cache->buffer + offset; |
| 345 } |
| 346 } |
| 347 } |
| 348 |
| 349 return NULL; |
| 350 } |
| 351 |
| 352 typedef struct { |
| 353 const char *mime; |
| 354 int weight; |
| 355 } MimeWeight; |
| 356 |
| 357 static int |
| 358 cache_glob_lookup_literal (const char *file_name, |
| 359 const char *mime_types[], |
| 360 int n_mime_types) |
| 361 { |
| 362 const char *ptr; |
| 363 int i, min, max, mid, cmp; |
| 364 |
| 365 for (i = 0; _caches[i]; i++) |
| 366 { |
| 367 XdgMimeCache *cache = _caches[i]; |
| 368 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12); |
| 369 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 370 xdg_uint32_t offset; |
| 371 |
| 372 min = 0; |
| 373 max = n_entries - 1; |
| 374 while (max >= min) |
| 375 { |
| 376 mid = (min + max) / 2; |
| 377 |
| 378 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid); |
| 379 ptr = cache->buffer + offset; |
| 380 cmp = strcmp (ptr, file_name); |
| 381 |
| 382 if (cmp < 0) |
| 383 min = mid + 1; |
| 384 else if (cmp > 0) |
| 385 max = mid - 1; |
| 386 else |
| 387 { |
| 388 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 4
); |
| 389 mime_types[0] = (const char *)(cache->buffer + offset); |
| 390 |
| 391 return 1; |
| 392 } |
| 393 } |
| 394 } |
| 395 |
| 396 return 0; |
| 397 } |
| 398 |
| 399 static int |
| 400 cache_glob_lookup_fnmatch (const char *file_name, |
| 401 MimeWeight mime_types[], |
| 402 int n_mime_types) |
| 403 { |
| 404 const char *mime_type; |
| 405 const char *ptr; |
| 406 |
| 407 int i, j, n; |
| 408 |
| 409 n = 0; |
| 410 for (i = 0; _caches[i]; i++) |
| 411 { |
| 412 XdgMimeCache *cache = _caches[i]; |
| 413 |
| 414 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20); |
| 415 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 416 |
| 417 for (j = 0; j < n_entries && n < n_mime_types; j++) |
| 418 { |
| 419 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12
* j); |
| 420 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset
+ 4 + 12 * j + 4); |
| 421 int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 8); |
| 422 ptr = cache->buffer + offset; |
| 423 mime_type = cache->buffer + mimetype_offset; |
| 424 |
| 425 /* FIXME: Not UTF-8 safe */ |
| 426 if (fnmatch (ptr, file_name, 0) == 0) |
| 427 { |
| 428 mime_types[n].mime = mime_type; |
| 429 mime_types[n].weight = weight; |
| 430 n++; |
| 431 } |
| 432 } |
| 433 |
| 434 if (n > 0) |
| 435 return n; |
| 436 } |
| 437 |
| 438 return 0; |
| 439 } |
| 440 |
| 441 static int |
| 442 cache_glob_node_lookup_suffix (XdgMimeCache *cache, |
| 443 xdg_uint32_t n_entries, |
| 444 xdg_uint32_t offset, |
| 445 const char *file_name, |
| 446 int len, |
| 447 int ignore_case, |
| 448 MimeWeight mime_types[], |
| 449 int n_mime_types) |
| 450 { |
| 451 xdg_unichar_t character; |
| 452 xdg_unichar_t match_char; |
| 453 xdg_uint32_t mimetype_offset; |
| 454 xdg_uint32_t n_children; |
| 455 xdg_uint32_t child_offset; |
| 456 int weight; |
| 457 |
| 458 int min, max, mid, n, i; |
| 459 |
| 460 character = file_name[len - 1]; |
| 461 if (ignore_case) |
| 462 character = tolower (character); |
| 463 |
| 464 assert (character != 0); |
| 465 |
| 466 min = 0; |
| 467 max = n_entries - 1; |
| 468 while (max >= min) |
| 469 { |
| 470 mid = (min + max) / 2; |
| 471 match_char = GET_UINT32 (cache->buffer, offset + 12 * mid); |
| 472 if (match_char < character) |
| 473 min = mid + 1; |
| 474 else if (match_char > character) |
| 475 max = mid - 1; |
| 476 else |
| 477 { |
| 478 len--; |
| 479 n = 0; |
| 480 n_children = GET_UINT32 (cache->buffer, offset + 12 * mid + 4); |
| 481 child_offset = GET_UINT32 (cache->buffer, offset + 12 * mid + 8); |
| 482 |
| 483 if (len > 0) |
| 484 { |
| 485 n = cache_glob_node_lookup_suffix (cache, |
| 486 n_children, child_offset, |
| 487 file_name, len, |
| 488 ignore_case, |
| 489 mime_types, |
| 490 n_mime_types); |
| 491 } |
| 492 if (n == 0) |
| 493 { |
| 494 i = 0; |
| 495 while (n < n_mime_types && i < n_children) |
| 496 { |
| 497 match_char = GET_UINT32 (cache->buffer, child_offset + 12 * i)
; |
| 498 if (match_char != 0) |
| 499 break; |
| 500 |
| 501 mimetype_offset = GET_UINT32 (cache->buffer, child_offset + 12
* i + 4); |
| 502 weight = GET_UINT32 (cache->buffer, child_offset + 12 * i + 8)
; |
| 503 |
| 504 mime_types[n].mime = cache->buffer + mimetype_offset; |
| 505 mime_types[n].weight = weight; |
| 506 n++; |
| 507 i++; |
| 508 } |
| 509 } |
| 510 return n; |
| 511 } |
| 512 } |
| 513 return 0; |
| 514 } |
| 515 |
| 516 static int |
| 517 cache_glob_lookup_suffix (const char *file_name, |
| 518 int len, |
| 519 int ignore_case, |
| 520 MimeWeight mime_types[], |
| 521 int n_mime_types) |
| 522 { |
| 523 int i, n; |
| 524 |
| 525 for (i = 0; _caches[i]; i++) |
| 526 { |
| 527 XdgMimeCache *cache = _caches[i]; |
| 528 |
| 529 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16); |
| 530 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 531 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4); |
| 532 |
| 533 n = cache_glob_node_lookup_suffix (cache, |
| 534 n_entries, offset, |
| 535 file_name, len, |
| 536 ignore_case, |
| 537 mime_types, |
| 538 n_mime_types); |
| 539 if (n > 0) |
| 540 return n; |
| 541 } |
| 542 |
| 543 return 0; |
| 544 } |
| 545 |
| 546 static int compare_mime_weight (const void *a, const void *b) |
| 547 { |
| 548 const MimeWeight *aa = (const MimeWeight *)a; |
| 549 const MimeWeight *bb = (const MimeWeight *)b; |
| 550 |
| 551 return aa->weight - bb->weight; |
| 552 } |
| 553 |
| 554 static int |
| 555 cache_glob_lookup_file_name (const char *file_name, |
| 556 const char *mime_types[], |
| 557 int n_mime_types) |
| 558 { |
| 559 int n; |
| 560 MimeWeight mimes[10]; |
| 561 int n_mimes = 10; |
| 562 int i; |
| 563 int len; |
| 564 |
| 565 assert (file_name != NULL && n_mime_types > 0); |
| 566 |
| 567 /* First, check the literals */ |
| 568 n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types); |
| 569 if (n > 0) |
| 570 return n; |
| 571 |
| 572 len = strlen (file_name); |
| 573 n = cache_glob_lookup_suffix (file_name, len, FALSE, mimes, n_mimes); |
| 574 |
| 575 if (n == 0) |
| 576 n = cache_glob_lookup_suffix (file_name, len, TRUE, mimes, n_mimes); |
| 577 |
| 578 /* Last, try fnmatch */ |
| 579 if (n == 0) |
| 580 n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes); |
| 581 |
| 582 qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight); |
| 583 |
| 584 if (n_mime_types < n) |
| 585 n = n_mime_types; |
| 586 |
| 587 for (i = 0; i < n; i++) |
| 588 mime_types[i] = mimes[i].mime; |
| 589 |
| 590 return n; |
| 591 } |
| 592 |
| 593 int |
| 594 _xdg_mime_cache_get_max_buffer_extents (void) |
| 595 { |
| 596 xdg_uint32_t offset; |
| 597 xdg_uint32_t max_extent; |
| 598 int i; |
| 599 |
| 600 max_extent = 0; |
| 601 for (i = 0; _caches[i]; i++) |
| 602 { |
| 603 XdgMimeCache *cache = _caches[i]; |
| 604 |
| 605 offset = GET_UINT32 (cache->buffer, 24); |
| 606 max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4)); |
| 607 } |
| 608 |
| 609 return max_extent; |
| 610 } |
| 611 |
| 612 static const char * |
| 613 cache_get_mime_type_for_data (const void *data, |
| 614 size_t len, |
| 615 int *result_prio, |
| 616 const char *mime_types[], |
| 617 int n_mime_types) |
| 618 { |
| 619 const char *mime_type; |
| 620 int i, n, priority; |
| 621 |
| 622 priority = 0; |
| 623 mime_type = NULL; |
| 624 for (i = 0; _caches[i]; i++) |
| 625 { |
| 626 XdgMimeCache *cache = _caches[i]; |
| 627 |
| 628 int prio; |
| 629 const char *match; |
| 630 |
| 631 match = cache_magic_lookup_data (cache, data, len, &prio, |
| 632 mime_types, n_mime_types); |
| 633 if (prio > priority) |
| 634 { |
| 635 priority = prio; |
| 636 mime_type = match; |
| 637 } |
| 638 } |
| 639 |
| 640 if (result_prio) |
| 641 *result_prio = priority; |
| 642 |
| 643 if (priority > 0) |
| 644 return mime_type; |
| 645 |
| 646 for (n = 0; n < n_mime_types; n++) |
| 647 { |
| 648 |
| 649 if (mime_types[n]) |
| 650 return mime_types[n]; |
| 651 } |
| 652 |
| 653 return XDG_MIME_TYPE_UNKNOWN; |
| 654 } |
| 655 |
| 656 const char * |
| 657 _xdg_mime_cache_get_mime_type_for_data (const void *data, |
| 658 size_t len, |
| 659 int *result_prio) |
| 660 { |
| 661 return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0); |
| 662 } |
| 663 |
| 664 const char * |
| 665 _xdg_mime_cache_get_mime_type_for_file (const char *file_name, |
| 666 struct stat *statbuf) |
| 667 { |
| 668 const char *mime_type; |
| 669 const char *mime_types[10]; |
| 670 FILE *file; |
| 671 unsigned char *data; |
| 672 int max_extent; |
| 673 int bytes_read; |
| 674 struct stat buf; |
| 675 const char *base_name; |
| 676 int n; |
| 677 |
| 678 if (file_name == NULL) |
| 679 return NULL; |
| 680 |
| 681 if (! _xdg_utf8_validate (file_name)) |
| 682 return NULL; |
| 683 |
| 684 base_name = _xdg_get_base_name (file_name); |
| 685 n = cache_glob_lookup_file_name (base_name, mime_types, 10); |
| 686 |
| 687 if (n == 1) |
| 688 return mime_types[0]; |
| 689 |
| 690 if (!statbuf) |
| 691 { |
| 692 if (stat (file_name, &buf) != 0) |
| 693 return XDG_MIME_TYPE_UNKNOWN; |
| 694 |
| 695 statbuf = &buf; |
| 696 } |
| 697 |
| 698 if (!S_ISREG (statbuf->st_mode)) |
| 699 return XDG_MIME_TYPE_UNKNOWN; |
| 700 |
| 701 /* FIXME: Need to make sure that max_extent isn't totally broken. This could |
| 702 * be large and need getting from a stream instead of just reading it all |
| 703 * in. */ |
| 704 max_extent = _xdg_mime_cache_get_max_buffer_extents (); |
| 705 data = malloc (max_extent); |
| 706 if (data == NULL) |
| 707 return XDG_MIME_TYPE_UNKNOWN; |
| 708 |
| 709 file = fopen (file_name, "r"); |
| 710 if (file == NULL) |
| 711 { |
| 712 free (data); |
| 713 return XDG_MIME_TYPE_UNKNOWN; |
| 714 } |
| 715 |
| 716 bytes_read = fread (data, 1, max_extent, file); |
| 717 if (ferror (file)) |
| 718 { |
| 719 free (data); |
| 720 fclose (file); |
| 721 return XDG_MIME_TYPE_UNKNOWN; |
| 722 } |
| 723 |
| 724 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL, |
| 725 mime_types, n); |
| 726 |
| 727 free (data); |
| 728 fclose (file); |
| 729 |
| 730 return mime_type; |
| 731 } |
| 732 |
| 733 const char * |
| 734 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name) |
| 735 { |
| 736 const char *mime_type; |
| 737 |
| 738 if (cache_glob_lookup_file_name (file_name, &mime_type, 1)) |
| 739 return mime_type; |
| 740 else |
| 741 return XDG_MIME_TYPE_UNKNOWN; |
| 742 } |
| 743 |
| 744 int |
| 745 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name, |
| 746 const char *mime_types[], |
| 747 int n_mime_types) |
| 748 { |
| 749 return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types); |
| 750 } |
| 751 |
| 752 #if 1 |
| 753 static int |
| 754 is_super_type (const char *mime) |
| 755 { |
| 756 int length; |
| 757 const char *type; |
| 758 |
| 759 length = strlen (mime); |
| 760 type = &(mime[length - 2]); |
| 761 |
| 762 if (strcmp (type, "/*") == 0) |
| 763 return 1; |
| 764 |
| 765 return 0; |
| 766 } |
| 767 #endif |
| 768 |
| 769 int |
| 770 _xdg_mime_cache_mime_type_subclass (const char *mime, |
| 771 const char *base) |
| 772 { |
| 773 const char *umime, *ubase; |
| 774 |
| 775 int i, j, min, max, med, cmp; |
| 776 |
| 777 umime = _xdg_mime_cache_unalias_mime_type (mime); |
| 778 ubase = _xdg_mime_cache_unalias_mime_type (base); |
| 779 |
| 780 if (strcmp (umime, ubase) == 0) |
| 781 return 1; |
| 782 |
| 783 /* We really want to handle text/ * in GtkFileFilter, so we just |
| 784 * turn on the supertype matching |
| 785 */ |
| 786 #if 1 |
| 787 /* Handle supertypes */ |
| 788 if (is_super_type (ubase) && |
| 789 xdg_mime_media_type_equal (umime, ubase)) |
| 790 return 1; |
| 791 #endif |
| 792 |
| 793 /* Handle special cases text/plain and application/octet-stream */ |
| 794 if (strcmp (ubase, "text/plain") == 0 && |
| 795 strncmp (umime, "text/", 5) == 0) |
| 796 return 1; |
| 797 |
| 798 if (strcmp (ubase, "application/octet-stream") == 0) |
| 799 return 1; |
| 800 |
| 801 for (i = 0; _caches[i]; i++) |
| 802 { |
| 803 XdgMimeCache *cache = _caches[i]; |
| 804 |
| 805 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8); |
| 806 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 807 xdg_uint32_t offset, n_parents, parent_offset; |
| 808 |
| 809 min = 0; |
| 810 max = n_entries - 1; |
| 811 while (max >= min) |
| 812 { |
| 813 med = (min + max)/2; |
| 814 |
| 815 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med); |
| 816 cmp = strcmp (cache->buffer + offset, umime); |
| 817 if (cmp < 0) |
| 818 min = med + 1; |
| 819 else if (cmp > 0) |
| 820 max = med - 1; |
| 821 else |
| 822 { |
| 823 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med + 4)
; |
| 824 n_parents = GET_UINT32 (cache->buffer, offset); |
| 825 |
| 826 for (j = 0; j < n_parents; j++) |
| 827 { |
| 828 parent_offset = GET_UINT32 (cache->buffer, offset + 4 + 4 * j)
; |
| 829 if (_xdg_mime_cache_mime_type_subclass (cache->buffer + parent
_offset, ubase)) |
| 830 return 1; |
| 831 } |
| 832 |
| 833 break; |
| 834 } |
| 835 } |
| 836 } |
| 837 |
| 838 return 0; |
| 839 } |
| 840 |
| 841 const char * |
| 842 _xdg_mime_cache_unalias_mime_type (const char *mime) |
| 843 { |
| 844 const char *lookup; |
| 845 |
| 846 lookup = cache_alias_lookup (mime); |
| 847 |
| 848 if (lookup) |
| 849 return lookup; |
| 850 |
| 851 return mime; |
| 852 } |
| 853 |
| 854 char ** |
| 855 _xdg_mime_cache_list_mime_parents (const char *mime) |
| 856 { |
| 857 int i, j, k, l, p; |
| 858 char *all_parents[128]; /* we'll stop at 128 */ |
| 859 char **result; |
| 860 |
| 861 mime = xdg_mime_unalias_mime_type (mime); |
| 862 |
| 863 p = 0; |
| 864 for (i = 0; _caches[i]; i++) |
| 865 { |
| 866 XdgMimeCache *cache = _caches[i]; |
| 867 |
| 868 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8); |
| 869 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 870 |
| 871 for (j = 0; j < n_entries; j++) |
| 872 { |
| 873 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset
+ 4 + 8 * j); |
| 874 xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset +
4 + 8 * j + 4); |
| 875 |
| 876 if (strcmp (cache->buffer + mimetype_offset, mime) == 0) |
| 877 { |
| 878 xdg_uint32_t parent_mime_offset; |
| 879 xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset
); |
| 880 |
| 881 for (k = 0; k < n_parents && p < 127; k++) |
| 882 { |
| 883 parent_mime_offset = GET_UINT32 (cache->buffer, parents_offset
+ 4 + 4 * k); |
| 884 |
| 885 /* Don't add same parent multiple times. |
| 886 * This can happen for instance if the same type is listed in
multiple directories |
| 887 */ |
| 888 for (l = 0; l < p; l++) |
| 889 { |
| 890 if (strcmp (all_parents[l], cache->buffer + parent_mime_of
fset) == 0) |
| 891 break; |
| 892 } |
| 893 |
| 894 if (l == p) |
| 895 all_parents[p++] = cache->buffer + parent_mime_offset; |
| 896 } |
| 897 |
| 898 break; |
| 899 } |
| 900 } |
| 901 } |
| 902 all_parents[p++] = NULL; |
| 903 |
| 904 result = (char **) malloc (p * sizeof (char *)); |
| 905 memcpy (result, all_parents, p * sizeof (char *)); |
| 906 |
| 907 return result; |
| 908 } |
| 909 |
| 910 static const char * |
| 911 cache_lookup_icon (const char *mime, int header) |
| 912 { |
| 913 const char *ptr; |
| 914 int i, min, max, mid, cmp; |
| 915 |
| 916 for (i = 0; _caches[i]; i++) |
| 917 { |
| 918 XdgMimeCache *cache = _caches[i]; |
| 919 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, header); |
| 920 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 921 xdg_uint32_t offset; |
| 922 |
| 923 min = 0; |
| 924 max = n_entries - 1; |
| 925 while (max >= min) |
| 926 { |
| 927 mid = (min + max) / 2; |
| 928 |
| 929 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid); |
| 930 ptr = cache->buffer + offset; |
| 931 cmp = strcmp (ptr, mime); |
| 932 |
| 933 if (cmp < 0) |
| 934 min = mid + 1; |
| 935 else if (cmp > 0) |
| 936 max = mid - 1; |
| 937 else |
| 938 { |
| 939 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4)
; |
| 940 return cache->buffer + offset; |
| 941 } |
| 942 } |
| 943 } |
| 944 |
| 945 return NULL; |
| 946 } |
| 947 |
| 948 const char * |
| 949 _xdg_mime_cache_get_generic_icon (const char *mime) |
| 950 { |
| 951 return cache_lookup_icon (mime, 36); |
| 952 } |
| 953 |
| 954 const char * |
| 955 _xdg_mime_cache_get_icon (const char *mime) |
| 956 { |
| 957 return cache_lookup_icon (mime, 32); |
| 958 } |
| 959 |
| 960 static void |
| 961 dump_glob_node (XdgMimeCache *cache, |
| 962 xdg_uint32_t offset, |
| 963 int depth) |
| 964 { |
| 965 xdg_unichar_t character; |
| 966 xdg_uint32_t mime_offset; |
| 967 xdg_uint32_t n_children; |
| 968 xdg_uint32_t child_offset; |
| 969 int i; |
| 970 |
| 971 character = GET_UINT32 (cache->buffer, offset); |
| 972 mime_offset = GET_UINT32 (cache->buffer, offset + 4); |
| 973 n_children = GET_UINT32 (cache->buffer, offset + 8); |
| 974 child_offset = GET_UINT32 (cache->buffer, offset + 12); |
| 975 for (i = 0; i < depth; i++) |
| 976 printf (" "); |
| 977 printf ("%c", character); |
| 978 if (mime_offset) |
| 979 printf (" - %s", cache->buffer + mime_offset); |
| 980 printf ("\n"); |
| 981 if (child_offset) |
| 982 { |
| 983 for (i = 0; i < n_children; i++) |
| 984 dump_glob_node (cache, child_offset + 20 * i, depth + 1); |
| 985 } |
| 986 } |
| 987 |
| 988 void |
| 989 _xdg_mime_cache_glob_dump (void) |
| 990 { |
| 991 int i, j; |
| 992 for (i = 0; _caches[i]; i++) |
| 993 { |
| 994 XdgMimeCache *cache = _caches[i]; |
| 995 xdg_uint32_t list_offset; |
| 996 xdg_uint32_t n_entries; |
| 997 xdg_uint32_t offset; |
| 998 list_offset = GET_UINT32 (cache->buffer, 16); |
| 999 n_entries = GET_UINT32 (cache->buffer, list_offset); |
| 1000 offset = GET_UINT32 (cache->buffer, list_offset + 4); |
| 1001 for (j = 0; j < n_entries; j++) |
| 1002 dump_glob_node (cache, offset + 20 * j, 0); |
| 1003 } |
| 1004 } |
OLD | NEW |