]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/mime.c
85310a4c7a9ef7a5802178d1e6eb18b9647d13c2
2 * MIME database file routines for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/string-private.h>
15 #include <cups/debug-private.h>
17 #include "mime-private.h"
24 typedef struct _mime_fcache_s
/**** Filter cache structure ****/
26 char *name
, /* Filter name */
27 *path
; /* Full path to filter if available */
35 static const char *mime_add_fcache(cups_array_t
*filtercache
, const char *name
,
36 const char *filterpath
);
37 static int mime_compare_fcache(_mime_fcache_t
*a
, _mime_fcache_t
*b
);
38 static void mime_delete_fcache(cups_array_t
*filtercache
);
39 static void mime_delete_rules(mime_magic_t
*rules
);
40 static void mime_load_convs(mime_t
*mime
, const char *filename
,
41 const char *filterpath
,
42 cups_array_t
*filtercache
);
43 static void mime_load_types(mime_t
*mime
, const char *filename
);
47 * 'mimeDelete()' - Delete (free) a MIME database.
51 mimeDelete(mime_t
*mime
) /* I - MIME database */
53 mime_type_t
*type
; /* Current type */
54 mime_filter_t
*filter
; /* Current filter */
57 DEBUG_printf(("mimeDelete(mime=%p)", mime
));
63 * Loop through filters and free them...
66 for (filter
= (mime_filter_t
*)cupsArrayFirst(mime
->filters
);
68 filter
= (mime_filter_t
*)cupsArrayNext(mime
->filters
))
69 mimeDeleteFilter(mime
, filter
);
72 * Loop through the file types and delete any rules...
75 for (type
= (mime_type_t
*)cupsArrayFirst(mime
->types
);
77 type
= (mime_type_t
*)cupsArrayNext(mime
->types
))
78 mimeDeleteType(mime
, type
);
81 * Free the types and filters arrays, and then the MIME database structure.
84 cupsArrayDelete(mime
->types
);
85 cupsArrayDelete(mime
->filters
);
86 cupsArrayDelete(mime
->srcs
);
92 * 'mimeDeleteFilter()' - Delete a filter from the MIME database.
96 mimeDeleteFilter(mime_t
*mime
, /* I - MIME database */
97 mime_filter_t
*filter
) /* I - Filter */
99 DEBUG_printf(("mimeDeleteFilter(mime=%p, filter=%p(%s/%s->%s/%s, cost=%d, "
100 "maxsize=" CUPS_LLFMT
"))", mime
, filter
,
101 filter
? filter
->src
->super
: "???",
102 filter
? filter
->src
->type
: "???",
103 filter
? filter
->dst
->super
: "???",
104 filter
? filter
->dst
->super
: "???",
105 filter
? filter
->cost
: -1,
106 filter
? CUPS_LLCAST filter
->maxsize
: CUPS_LLCAST
-1));
108 if (!mime
|| !filter
)
112 if (!cupsArrayFind(mime
->filters
, filter
))
113 DEBUG_puts("1mimeDeleteFilter: Filter not in MIME database.");
116 cupsArrayRemove(mime
->filters
, filter
);
120 * Deleting a filter invalidates the source lookup cache used by
126 DEBUG_puts("1mimeDeleteFilter: Deleting source lookup cache.");
127 cupsArrayDelete(mime
->srcs
);
134 * 'mimeDeleteType()' - Delete a type from the MIME database.
138 mimeDeleteType(mime_t
*mime
, /* I - MIME database */
139 mime_type_t
*mt
) /* I - Type */
141 DEBUG_printf(("mimeDeleteType(mime=%p, mt=%p(%s/%s))", mime
, mt
,
142 mt
? mt
->super
: "???", mt
? mt
->type
: "???"));
148 if (!cupsArrayFind(mime
->types
, mt
))
149 DEBUG_puts("1mimeDeleteFilter: Type not in MIME database.");
152 cupsArrayRemove(mime
->types
, mt
);
154 mime_delete_rules(mt
->rules
);
160 * '_mimeError()' - Show an error message.
164 _mimeError(mime_t
*mime
, /* I - MIME database */
165 const char *message
, /* I - Printf-style message string */
166 ...) /* I - Additional arguments as needed */
168 va_list ap
; /* Argument pointer */
169 char buffer
[8192]; /* Message buffer */
174 va_start(ap
, message
);
175 vsnprintf(buffer
, sizeof(buffer
), message
, ap
);
178 (*mime
->error_cb
)(mime
->error_ctx
, buffer
);
184 * 'mimeFirstFilter()' - Get the first filter in the MIME database.
187 mime_filter_t
* /* O - Filter or NULL */
188 mimeFirstFilter(mime_t
*mime
) /* I - MIME database */
190 DEBUG_printf(("6mimeFirstFilter(mime=%p)", mime
));
194 DEBUG_puts("7mimeFirstFilter: Returning NULL.");
199 mime_filter_t
*first
= (mime_filter_t
*)cupsArrayFirst(mime
->filters
);
202 DEBUG_printf(("7mimeFirstFilter: Returning %p.", first
));
209 * 'mimeFirstType()' - Get the first type in the MIME database.
212 mime_type_t
* /* O - Type or NULL */
213 mimeFirstType(mime_t
*mime
) /* I - MIME database */
215 DEBUG_printf(("6mimeFirstType(mime=%p)", mime
));
219 DEBUG_puts("7mimeFirstType: Returning NULL.");
224 mime_type_t
*first
= (mime_type_t
*)cupsArrayFirst(mime
->types
);
227 DEBUG_printf(("7mimeFirstType: Returning %p.", first
));
234 * 'mimeLoad()' - Create a new MIME database from disk.
236 * This function uses @link mimeLoadFilters@ and @link mimeLoadTypes@ to
237 * create a MIME database from a single directory.
240 mime_t
* /* O - New MIME database */
241 mimeLoad(const char *pathname
, /* I - Directory to load */
242 const char *filterpath
) /* I - Directory to load */
244 mime_t
*mime
; /* New MIME database */
246 DEBUG_printf(("mimeLoad(pathname=\"%s\", filterpath=\"%s\")", pathname
,
249 mime
= mimeLoadFilters(mimeLoadTypes(NULL
, pathname
), pathname
, filterpath
);
250 DEBUG_printf(("1mimeLoad: Returning %p.", mime
));
257 * 'mimeLoadFilters()' - Load filter definitions from disk.
259 * This function loads all of the .convs files from the specified directory.
260 * Use @link mimeLoadTypes@ to load all types before you load the filters.
263 mime_t
* /* O - MIME database */
264 mimeLoadFilters(mime_t
*mime
, /* I - MIME database */
265 const char *pathname
, /* I - Directory to load from */
266 const char *filterpath
) /* I - Default filter program directory */
268 cups_dir_t
*dir
; /* Directory */
269 cups_dentry_t
*dent
; /* Directory entry */
270 char filename
[1024]; /* Full filename of .convs file */
271 cups_array_t
*filtercache
; /* Filter cache */
274 DEBUG_printf(("mimeLoadFilters(mime=%p, pathname=\"%s\", filterpath=\"%s\")",
275 mime
, pathname
, filterpath
));
278 * Range check input...
281 if (!mime
|| !pathname
|| !filterpath
)
283 DEBUG_puts("1mimeLoadFilters: Bad arguments.");
288 * Then open the directory specified by pathname...
291 if ((dir
= cupsDirOpen(pathname
)) == NULL
)
293 DEBUG_printf(("1mimeLoadFilters: Unable to open \"%s\": %s", pathname
,
295 _mimeError(mime
, "Unable to open \"%s\": %s", pathname
, strerror(errno
));
300 * Read all the .convs files...
303 filtercache
= cupsArrayNew((cups_array_func_t
)mime_compare_fcache
, NULL
);
305 while ((dent
= cupsDirRead(dir
)) != NULL
)
307 if (strlen(dent
->filename
) > 6 &&
308 !strcmp(dent
->filename
+ strlen(dent
->filename
) - 6, ".convs"))
311 * Load a mime.convs file...
314 snprintf(filename
, sizeof(filename
), "%s/%s", pathname
, dent
->filename
);
315 DEBUG_printf(("1mimeLoadFilters: Loading \"%s\".", filename
));
316 mime_load_convs(mime
, filename
, filterpath
, filtercache
);
320 mime_delete_fcache(filtercache
);
329 * 'mimeLoadTypes()' - Load type definitions from disk.
331 * This function loads all of the .types files from the specified directory.
332 * Use @link mimeLoadFilters@ to load all filters after you load the types.
335 mime_t
* /* O - MIME database */
336 mimeLoadTypes(mime_t
*mime
, /* I - MIME database or @code NULL@ to create a new one */
337 const char *pathname
) /* I - Directory to load from */
339 cups_dir_t
*dir
; /* Directory */
340 cups_dentry_t
*dent
; /* Directory entry */
341 char filename
[1024]; /* Full filename of .types file */
344 DEBUG_printf(("mimeLoadTypes(mime=%p, pathname=\"%s\")", mime
, pathname
));
347 * First open the directory specified by pathname...
350 if ((dir
= cupsDirOpen(pathname
)) == NULL
)
352 DEBUG_printf(("1mimeLoadTypes: Unable to open \"%s\": %s", pathname
,
354 DEBUG_printf(("1mimeLoadTypes: Returning %p.", mime
));
355 _mimeError(mime
, "Unable to open \"%s\": %s", pathname
, strerror(errno
));
360 * If "mime" is NULL, make a new, empty database...
369 DEBUG_puts("1mimeLoadTypes: Returning NULL.");
374 * Read all the .types files...
377 while ((dent
= cupsDirRead(dir
)) != NULL
)
379 if (strlen(dent
->filename
) > 6 &&
380 !strcmp(dent
->filename
+ strlen(dent
->filename
) - 6, ".types"))
383 * Load a mime.types file...
386 snprintf(filename
, sizeof(filename
), "%s/%s", pathname
, dent
->filename
);
387 DEBUG_printf(("1mimeLoadTypes: Loading \"%s\".", filename
));
388 mime_load_types(mime
, filename
);
394 DEBUG_printf(("1mimeLoadTypes: Returning %p.", mime
));
401 * 'mimeNew()' - Create a new, empty MIME database.
404 mime_t
* /* O - MIME database */
407 return ((mime_t
*)calloc(1, sizeof(mime_t
)));
412 * 'mimeNextFilter()' - Get the next filter in the MIME database.
415 mime_filter_t
* /* O - Filter or NULL */
416 mimeNextFilter(mime_t
*mime
) /* I - MIME database */
418 DEBUG_printf(("6mimeNextFilter(mime=%p)", mime
));
422 DEBUG_puts("7mimeNextFilter: Returning NULL.");
427 mime_filter_t
*next
= (mime_filter_t
*)cupsArrayNext(mime
->filters
);
430 DEBUG_printf(("7mimeNextFilter: Returning %p.", next
));
437 * 'mimeNextType()' - Get the next type in the MIME database.
440 mime_type_t
* /* O - Type or NULL */
441 mimeNextType(mime_t
*mime
) /* I - MIME database */
443 DEBUG_printf(("6mimeNextType(mime=%p)", mime
));
447 DEBUG_puts("7mimeNextType: Returning NULL.");
452 mime_type_t
*next
= (mime_type_t
*)cupsArrayNext(mime
->types
);
455 DEBUG_printf(("7mimeNextType: Returning %p.", next
));
462 * 'mimeNumFilters()' - Get the number of filters in a MIME database.
466 mimeNumFilters(mime_t
*mime
) /* I - MIME database */
468 DEBUG_printf(("mimeNumFilters(mime=%p)", mime
));
472 DEBUG_puts("1mimeNumFilters: Returning 0.");
477 DEBUG_printf(("1mimeNumFilters: Returning %d.",
478 cupsArrayCount(mime
->filters
)));
479 return (cupsArrayCount(mime
->filters
));
485 * 'mimeNumTypes()' - Get the number of types in a MIME database.
489 mimeNumTypes(mime_t
*mime
) /* I - MIME database */
491 DEBUG_printf(("mimeNumTypes(mime=%p)", mime
));
495 DEBUG_puts("1mimeNumTypes: Returning 0.");
500 DEBUG_printf(("1mimeNumTypes: Returning %d.",
501 cupsArrayCount(mime
->types
)));
502 return (cupsArrayCount(mime
->types
));
508 * 'mimeSetErrorCallback()' - Set the callback for error messages.
512 mimeSetErrorCallback(
513 mime_t
*mime
, /* I - MIME database */
514 mime_error_cb_t cb
, /* I - Callback function */
515 void *ctx
) /* I - Context pointer for callback */
520 mime
->error_ctx
= ctx
;
526 * 'mime_add_fcache()' - Add a filter to the filter cache.
529 static const char * /* O - Full path to filter or NULL */
531 cups_array_t
*filtercache
, /* I - Filter cache */
532 const char *name
, /* I - Filter name */
533 const char *filterpath
) /* I - Filter path */
535 _mime_fcache_t key
, /* Search key */
536 *temp
; /* New filter cache */
537 char path
[1024]; /* Full path to filter */
540 DEBUG_printf(("2mime_add_fcache(filtercache=%p, name=\"%s\", "
541 "filterpath=\"%s\")", filtercache
, name
, filterpath
));
543 key
.name
= (char *)name
;
544 if ((temp
= (_mime_fcache_t
*)cupsArrayFind(filtercache
, &key
)) != NULL
)
546 DEBUG_printf(("3mime_add_fcache: Returning \"%s\".", temp
->path
));
550 if ((temp
= calloc(1, sizeof(_mime_fcache_t
))) == NULL
)
552 DEBUG_puts("3mime_add_fcache: Returning NULL.");
556 temp
->name
= strdup(name
);
558 if (cupsFileFind(name
, filterpath
, 1, path
, sizeof(path
)))
559 temp
->path
= strdup(path
);
561 cupsArrayAdd(filtercache
, temp
);
563 DEBUG_printf(("3mime_add_fcache: Returning \"%s\".", temp
->path
));
569 * 'mime_compare_fcache()' - Compare two filter cache entries.
572 static int /* O - Result of comparison */
573 mime_compare_fcache(_mime_fcache_t
*a
, /* I - First entry */
574 _mime_fcache_t
*b
) /* I - Second entry */
576 return (strcmp(a
->name
, b
->name
));
581 * 'mime_delete_fcache()' - Free all memory used by the filter cache.
586 cups_array_t
*filtercache
) /* I - Filter cache */
588 _mime_fcache_t
*current
; /* Current cache entry */
591 DEBUG_printf(("2mime_delete_fcache(filtercache=%p)", filtercache
));
593 for (current
= (_mime_fcache_t
*)cupsArrayFirst(filtercache
);
595 current
= (_mime_fcache_t
*)cupsArrayNext(filtercache
))
605 cupsArrayDelete(filtercache
);
610 * 'mime_delete_rules()' - Free all memory for the given rule tree.
614 mime_delete_rules(mime_magic_t
*rules
) /* I - Rules to free */
616 mime_magic_t
*next
; /* Next rule to free */
619 DEBUG_printf(("2mime_delete_rules(rules=%p)", rules
));
622 * Free the rules list, descending recursively to free any child rules.
625 while (rules
!= NULL
)
629 if (rules
->child
!= NULL
)
630 mime_delete_rules(rules
->child
);
632 if (rules
->op
== MIME_MAGIC_REGEX
)
633 regfree(&(rules
->value
.rev
));
642 * 'mime_load_convs()' - Load a xyz.convs file.
647 mime_t
*mime
, /* I - MIME database */
648 const char *filename
, /* I - Convs file to load */
649 const char *filterpath
, /* I - Path for filters */
650 cups_array_t
*filtercache
) /* I - Filter program cache */
652 cups_file_t
*fp
; /* Convs file */
653 char line
[1024], /* Input line from file */
654 *lineptr
, /* Current position in line */
655 super
[MIME_MAX_SUPER
], /* Super-type name */
656 type
[MIME_MAX_TYPE
], /* Type name */
657 *temp
, /* Temporary pointer */
658 *filter
; /* Filter program */
659 mime_type_t
*temptype
, /* MIME type looping var */
660 *dsttype
; /* Destination MIME type */
661 int cost
; /* Cost of filter */
664 DEBUG_printf(("2mime_load_convs(mime=%p, filename=\"%s\", filterpath=\"%s\", "
665 "filtercache=%p)", mime
, filename
, filterpath
, filtercache
));
668 * First try to open the file...
671 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
673 DEBUG_printf(("3mime_load_convs: Unable to open \"%s\": %s", filename
,
675 _mimeError(mime
, "Unable to open \"%s\": %s", filename
, strerror(errno
));
680 * Then read each line from the file, skipping any comments in the file...
683 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
686 * Skip blank lines and lines starting with a #...
689 if (!line
[0] || line
[0] == '#')
693 * Strip trailing whitespace...
696 for (lineptr
= line
+ strlen(line
) - 1;
697 lineptr
>= line
&& isspace(*lineptr
& 255);
702 * Extract the destination super-type and type names from the middle of
707 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\0')
710 while (*lineptr
== ' ' || *lineptr
== '\t')
715 while (*lineptr
!= '/' && *lineptr
!= '\n' && *lineptr
!= '\0' &&
716 (temp
- super
+ 1) < MIME_MAX_SUPER
)
717 *temp
++ = (char)tolower(*lineptr
++ & 255);
727 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\n' &&
728 *lineptr
!= '\0' && (temp
- type
+ 1) < MIME_MAX_TYPE
)
729 *temp
++ = (char)tolower(*lineptr
++ & 255);
733 if (*lineptr
== '\0' || *lineptr
== '\n')
736 if ((dsttype
= mimeType(mime
, super
, type
)) == NULL
)
738 DEBUG_printf(("3mime_load_convs: Destination type %s/%s not found.",
744 * Then get the cost and filter program...
747 while (*lineptr
== ' ' || *lineptr
== '\t')
750 if (*lineptr
< '0' || *lineptr
> '9')
753 cost
= atoi(lineptr
);
755 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\0')
757 while (*lineptr
== ' ' || *lineptr
== '\t')
760 if (*lineptr
== '\0' || *lineptr
== '\n')
765 if (strcmp(filter
, "-"))
768 * Verify that the filter exists and is executable...
771 if (!mime_add_fcache(filtercache
, filter
, filterpath
))
773 DEBUG_printf(("mime_load_convs: Filter %s not found in %s.", filter
,
775 _mimeError(mime
, "Filter \"%s\" not found.", filter
);
781 * Finally, get the source super-type and type names from the beginning of
782 * the line. We do it here so we can support wildcards...
788 while (*lineptr
!= '/' && *lineptr
!= '\n' && *lineptr
!= '\0' &&
789 (temp
- super
+ 1) < MIME_MAX_SUPER
)
790 *temp
++ = (char)tolower(*lineptr
++ & 255);
800 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\n' &&
801 *lineptr
!= '\0' && (temp
- type
+ 1) < MIME_MAX_TYPE
)
802 *temp
++ = (char)tolower(*lineptr
++ & 255);
806 if (!strcmp(super
, "*") && !strcmp(type
, "*"))
809 * Force * / * to be "application/octet-stream"...
812 strlcpy(super
, "application", sizeof(super
));
813 strlcpy(type
, "octet-stream", sizeof(type
));
817 * Add the filter to the MIME database, supporting wildcards as needed...
820 for (temptype
= (mime_type_t
*)cupsArrayFirst(mime
->types
);
822 temptype
= (mime_type_t
*)cupsArrayNext(mime
->types
))
823 if ((super
[0] == '*' || !strcmp(temptype
->super
, super
)) &&
824 (type
[0] == '*' || !strcmp(temptype
->type
, type
)))
825 mimeAddFilter(mime
, temptype
, dsttype
, cost
, filter
);
833 * 'mime_load_types()' - Load a xyz.types file.
837 mime_load_types(mime_t
*mime
, /* I - MIME database */
838 const char *filename
) /* I - Types file to load */
840 cups_file_t
*fp
; /* Types file */
841 size_t linelen
; /* Length of line */
842 char line
[32768], /* Input line from file */
843 *lineptr
, /* Current position in line */
844 super
[MIME_MAX_SUPER
], /* Super-type name */
845 type
[MIME_MAX_TYPE
], /* Type name */
846 *temp
; /* Temporary pointer */
847 mime_type_t
*typeptr
; /* New MIME type */
850 DEBUG_printf(("2mime_load_types(mime=%p, filename=\"%s\")", mime
, filename
));
853 * First try to open the file...
856 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
858 DEBUG_printf(("3mime_load_types: Unable to open \"%s\": %s", filename
,
860 _mimeError(mime
, "Unable to open \"%s\": %s", filename
, strerror(errno
));
865 * Then read each line from the file, skipping any comments in the file...
868 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
871 * Skip blank lines and lines starting with a #...
874 if (!line
[0] || line
[0] == '#')
878 * While the last character in the line is a backslash, continue on to the
879 * next line (and the next, etc.)
882 linelen
= strlen(line
);
884 while (line
[linelen
- 1] == '\\')
888 if (cupsFileGets(fp
, line
+ linelen
, sizeof(line
) - linelen
) == NULL
)
889 line
[linelen
] = '\0';
891 linelen
+= strlen(line
+ linelen
);
895 * Extract the super-type and type names from the beginning of the line.
901 while (*lineptr
!= '/' && *lineptr
!= '\n' && *lineptr
!= '\0' &&
902 (temp
- super
+ 1) < MIME_MAX_SUPER
)
903 *temp
++ = (char)tolower(*lineptr
++ & 255);
913 while (*lineptr
!= ' ' && *lineptr
!= '\t' && *lineptr
!= '\n' &&
914 *lineptr
!= '\0' && (temp
- type
+ 1) < MIME_MAX_TYPE
)
915 *temp
++ = (char)tolower(*lineptr
++ & 255);
920 * Add the type and rules to the MIME database...
923 typeptr
= mimeAddType(mime
, super
, type
);
924 mimeAddTypeRule(typeptr
, lineptr
);