]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/mime.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / mime.c
index 903f66f2f2942560a8a3067076e6dd463bc7aa79..052b42922e9b77f9892451a70cf650e5c5c3ac47 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: mime.c 4970 2006-01-24 14:05:45Z mike $"
+ * "$Id: mime.c 5605 2006-05-30 19:38:02Z mike $"
  *
  *   MIME database file routines for the Common UNIX Printing System (CUPS).
  *
@@ -28,7 +28,6 @@
  *   mimeDeleteType()   - Delete a type from the MIME database.
  *   mimeFirstFilter()  - Get the first filter in the MIME database.
  *   mimeFirstType()    - Get the first type in the MIME database.
- *   mimeNextType()     - Get the next type in the MIME database.
  *   mimeLoad()         - Create a new MIME database from disk.
  *   mimeMerge()        - Merge a MIME database from disk with the current one.
  *   mimeNew()          - Create a new, empty MIME database.
  *   mimeNextType()     - Get the next type in the MIME database.
  *   mimeNumFilters()   - Get the number of filters in a MIME database.
  *   mimeNumTypes()     - Get the number of types in a MIME database.
- *   load_types()       - Load a xyz.types file...
+ *   add_fcache()       - Add a filter to the filter cache.
+ *   compare_fcache()   - Compare two filter cache entries.
+ *   delete_fcache()    - Free all memory used by the filter cache.
  *   delete_rules()     - Free all memory for the given rule tree.
  *   load_convs()       - Load a xyz.convs file...
+ *   load_types()       - Load a xyz.types file...
  */
 
 /*
 #include <stdlib.h>
 #include <ctype.h>
 
+#include <cups/debug.h>
 #include <cups/dir.h>
 #include <cups/string.h>
 #include "mime.h"
 
 
+/*
+ * Local types...
+ */
+
+typedef struct _mime_fcache_s          /**** Filter cache structure ****/
+{
+  char *name,                          /* Filter name */
+       *path;                          /* Full path to filter if available */
+} _mime_fcache_t;
+
+
 /*
  * Local functions...
  */
 
-static void    load_types(mime_t *mime, const char *filename);
-static void    load_convs(mime_t *mime, const char *filename,
-                          const char *filterpath);
+static const char *add_fcache(cups_array_t *filtercache, const char *name,
+                             const char *filterpath);
+static int     compare_fcache(_mime_fcache_t *a, _mime_fcache_t *b);
+static void    delete_fcache(cups_array_t *filtercache);
 static void    delete_rules(mime_magic_t *rules);
+static void    load_convs(mime_t *mime, const char *filename,
+                          const char *filterpath,
+                          cups_array_t *filtercache);
+static void    load_types(mime_t *mime, const char *filename);
 
 
 /*
@@ -78,15 +97,6 @@ mimeDelete(mime_t *mime)             /* I - MIME database */
   if (!mime)
     return;
 
- /*
-  * Loop through the file types and delete any rules...
-  */
-
-  for (type = (mime_type_t *)cupsArrayFirst(mime->types);
-       type;
-       type = (mime_type_t *)cupsArrayNext(mime->types))
-    mimeDeleteType(mime, type);
-
  /*
   * Loop through filters and free them...
   */
@@ -96,12 +106,22 @@ mimeDelete(mime_t *mime)           /* I - MIME database */
        filter = (mime_filter_t *)cupsArrayNext(mime->filters))
     mimeDeleteFilter(mime, filter);
 
+ /*
+  * Loop through the file types and delete any rules...
+  */
+
+  for (type = (mime_type_t *)cupsArrayFirst(mime->types);
+       type;
+       type = (mime_type_t *)cupsArrayNext(mime->types))
+    mimeDeleteType(mime, type);
+
  /*
   * Free the types and filters arrays, and then the MIME database structure.
   */
 
   cupsArrayDelete(mime->types);
   cupsArrayDelete(mime->filters);
+  cupsArrayDelete(mime->srcs);
   free(mime);
 }
 
@@ -119,6 +139,17 @@ mimeDeleteFilter(mime_t        *mime,      /* I - MIME database */
 
   cupsArrayRemove(mime->filters, filter);
   free(filter);
+
+ /*
+  * Deleting a filter invalidates the source lookup cache used by
+  * mimeFilter()...
+  */
+
+  if (mime->srcs)
+  {
+    cupsArrayDelete(mime->srcs);
+    mime->srcs = NULL;
+  }
 }
 
 
@@ -192,6 +223,7 @@ mimeMerge(mime_t     *mime,         /* I - MIME database to add to */
   cups_dir_t   *dir;                   /* Directory */
   cups_dentry_t        *dent;                  /* Directory entry */
   char         filename[1024];         /* Full filename of types/converts file */
+  cups_array_t *filtercache;           /* Filter cache */
 
 
  /*
@@ -238,6 +270,8 @@ mimeMerge(mime_t     *mime,         /* I - MIME database to add to */
   * Read all the .convs files...
   */
 
+  filtercache = cupsArrayNew((cups_array_func_t)compare_fcache, NULL);
+
   while ((dent = cupsDirRead(dir)) != NULL)
   {
     if (strlen(dent->filename) > 6 &&
@@ -248,10 +282,12 @@ mimeMerge(mime_t     *mime,               /* I - MIME database to add to */
       */
 
       snprintf(filename, sizeof(filename), "%s/%s", pathname, dent->filename);
-      load_convs(mime, filename, filterpath);
+      load_convs(mime, filename, filterpath, filtercache);
     }
   }
 
+  delete_fcache(filtercache);
+
   cupsDirClose(dir);
 
   return (mime);
@@ -326,94 +362,99 @@ mimeNumTypes(mime_t *mime)                /* I - MIME database */
 
 
 /*
- * 'load_types()' - Load a xyz.types file...
+ * 'add_fcache()' - Add a filter to the filter cache.
  */
 
-static void
-load_types(mime_t     *mime,           /* I - MIME database */
-           const char *filename)       /* I - Types file to load */
+static const char *                    /* O - Full path to filter or NULL */
+add_fcache(cups_array_t *filtercache,  /* I - Filter cache */
+           const char   *name,         /* I - Filter name */
+          const char   *filterpath)    /* I - Filter path */
 {
-  cups_file_t  *fp;                    /* Types file */
-  int          linelen;                /* Length of line */
-  char         line[65536],            /* Input line from file */
-               *lineptr,               /* Current position in line */
-               super[MIME_MAX_SUPER],  /* Super-type name */
-               type[MIME_MAX_TYPE],    /* Type name */
-               *temp;                  /* Temporary pointer */
-  mime_type_t  *typeptr;               /* New MIME type */
+  _mime_fcache_t       key,            /* Search key */
+                       *temp;          /* New filter cache */
+  char                 path[1024];     /* Full path to filter */
 
 
- /*
-  * First try to open the file...
-  */
+  key.name = (char *)name;
+  if ((temp = (_mime_fcache_t *)cupsArrayFind(filtercache, &key)) != NULL)
+    return (temp->path);
 
-  if ((fp = cupsFileOpen(filename, "r")) == NULL)
-    return;
+  if ((temp = calloc(1, sizeof(_mime_fcache_t))) == NULL)
+    return (NULL);
 
- /*
-  * Then read each line from the file, skipping any comments in the file...
-  */
+  temp->name = strdup(name);
 
-  while (cupsFileGets(fp, line, sizeof(line)) != NULL)
-  {
-   /*
-    * Skip blank lines and lines starting with a #...
-    */
+  if (cupsFileFind(name, filterpath, 1, path, sizeof(path)))
+    temp->path = strdup(path);
 
-    if (!line[0] || line[0] == '#')
-      continue;
+  cupsArrayAdd(filtercache, temp);
 
-   /*
-    * While the last character in the line is a backslash, continue on to the
-    * next line (and the next, etc.)
-    */
+  return (temp->path);
+}
 
-    linelen = strlen(line);
 
-    while (line[linelen - 1] == '\\')
-    {
-      linelen --;
+/*
+ * 'compare_fcache()' - Compare two filter cache entries.
+ */
 
-      if (cupsFileGets(fp, line + linelen, sizeof(line) - linelen) == NULL)
-        line[linelen] = '\0';
-      else
-        linelen += strlen(line + linelen);
-    }
+static int                             /* O - Result of comparison */
+compare_fcache(_mime_fcache_t *a,      /* I - First entry */
+               _mime_fcache_t *b)      /* I - Second entry */
+{
+  return (strcmp(a->name, b->name));
+}
 
-   /*
-    * Extract the super-type and type names from the beginning of the line.
-    */
 
-    lineptr = line;
-    temp    = super;
+/*
+ * 'delete_fcache()' - Free all memory used by the filter cache.
+ */
 
-    while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
-           (temp - super + 1) < MIME_MAX_SUPER)
-      *temp++ = tolower(*lineptr++ & 255);
+static void
+delete_fcache(cups_array_t *filtercache)/* I - Filter cache */
+{
+  _mime_fcache_t       *current;       /* Current cache entry */
 
-    *temp = '\0';
 
-    if (*lineptr != '/')
-      continue;
+  for (current = (_mime_fcache_t *)cupsArrayFirst(filtercache);
+       current;
+       current = (_mime_fcache_t *)cupsArrayNext(filtercache))
+  {
+    free(current->name);
 
-    lineptr ++;
-    temp = type;
+    if (current->path)
+      free(current->path);
 
-    while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
-           *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
-      *temp++ = tolower(*lineptr++ & 255);
+    free(current);
+  }
 
-    *temp = '\0';
+  cupsArrayDelete(filtercache);
+}
 
-   /*
-    * Add the type and rules to the MIME database...
-    */
 
-    typeptr = mimeAddType(mime, super, type);
-    mimeAddTypeRule(typeptr, lineptr);
-  }
+/*
+ * 'delete_rules()' - Free all memory for the given rule tree.
+ */
 
-  cupsFileClose(fp);
+static void
+delete_rules(mime_magic_t *rules)      /* I - Rules to free */
+{
+  mime_magic_t *next;                  /* Next rule to free */
+
+
+ /*
+  * Free the rules list, descending recursively to free any child rules.
+  */
+
+  while (rules != NULL)
+  {
+    next = rules->next;
+
+    if (rules->child != NULL)
+      delete_rules(rules->child);
+
+    free(rules);
+    rules = next;
+  }
 }
 
 
@@ -422,9 +463,10 @@ load_types(mime_t     *mime,               /* I - MIME database */
  */
 
 static void
-load_convs(mime_t     *mime,           /* I - MIME database */
-           const char *filename,       /* I - Convs file to load */
-           const char *filterpath)     /* I - Path for filters */
+load_convs(mime_t       *mime,         /* I - MIME database */
+           const char   *filename,     /* I - Convs file to load */
+           const char   *filterpath,   /* I - Path for filters */
+          cups_array_t *filtercache)   /* I - Filter program cache */
 {
   cups_file_t  *fp;                    /* Convs file */
   char         line[1024],             /* Input line from file */
@@ -436,9 +478,6 @@ load_convs(mime_t     *mime,                /* I - MIME database */
   mime_type_t  *temptype,              /* MIME type looping var */
                *dsttype;               /* Destination MIME type */
   int          cost;                   /* Cost of filter */
-#ifndef WIN32
-  char         filterprog[1024];       /* Full path of filter... */
-#endif /* !WIN32 */
 
 
  /*
@@ -448,6 +487,8 @@ load_convs(mime_t     *mime,                /* I - MIME database */
   if ((fp = cupsFileOpen(filename, "r")) == NULL)
     return;
 
+  DEBUG_printf(("\"%s\":\n", filename));
+
  /*
   * Then read each line from the file, skipping any comments in the file...
   */
@@ -458,6 +499,8 @@ load_convs(mime_t     *mime,                /* I - MIME database */
     * Skip blank lines and lines starting with a #...
     */
 
+    DEBUG_puts(line);
+
     if (!line[0] || line[0] == '#')
       continue;
 
@@ -506,7 +549,10 @@ load_convs(mime_t     *mime,               /* I - MIME database */
       continue;
 
     if ((dsttype = mimeType(mime, super, type)) == NULL)
+    {
+      DEBUG_printf(("    Destination type %s/%s not found!\n", super, type));
       continue;
+    }
 
    /*
     * Then get the cost and filter program...
@@ -530,23 +576,18 @@ load_convs(mime_t     *mime,              /* I - MIME database */
 
     filter = lineptr;
 
-#ifndef WIN32
     if (strcmp(filter, "-"))
     {
      /*
       * Verify that the filter exists and is executable...
       */
 
-      if (filter[0] == '/')
-       strlcpy(filterprog, filter, sizeof(filterprog));
-      else if (!cupsFileFind(filter, filterpath, filterprog,
-                             sizeof(filterprog)))
+      if (!add_fcache(filtercache, filter, filterpath))
+      {
+        DEBUG_printf(("    Filter %s not found in %s!\n", filter, filterpath)); 
         continue;
-
-      if (access(filterprog, X_OK))
-       continue;
+      }
     }
-#endif /* !WIN32 */
 
    /*
     * Finally, get the source super-type and type names from the beginning of
@@ -601,32 +642,101 @@ load_convs(mime_t     *mime,             /* I - MIME database */
 
 
 /*
- * 'delete_rules()' - Free all memory for the given rule tree.
+ * 'load_types()' - Load a xyz.types file...
  */
 
 static void
-delete_rules(mime_magic_t *rules)      /* I - Rules to free */
+load_types(mime_t     *mime,           /* I - MIME database */
+           const char *filename)       /* I - Types file to load */
 {
-  mime_magic_t *next;                  /* Next rule to free */
+  cups_file_t  *fp;                    /* Types file */
+  int          linelen;                /* Length of line */
+  char         line[32768],            /* Input line from file */
+               *lineptr,               /* Current position in line */
+               super[MIME_MAX_SUPER],  /* Super-type name */
+               type[MIME_MAX_TYPE],    /* Type name */
+               *temp;                  /* Temporary pointer */
+  mime_type_t  *typeptr;               /* New MIME type */
 
 
  /*
-  * Free the rules list, descending recursively to free any child rules.
+  * First try to open the file...
   */
 
-  while (rules != NULL)
+  if ((fp = cupsFileOpen(filename, "r")) == NULL)
+    return;
+
+  DEBUG_printf(("\"%s\":\n", filename));
+
+ /*
+  * Then read each line from the file, skipping any comments in the file...
+  */
+
+  while (cupsFileGets(fp, line, sizeof(line)) != NULL)
   {
-    next = rules->next;
+   /*
+    * Skip blank lines and lines starting with a #...
+    */
 
-    if (rules->child != NULL)
-      delete_rules(rules->child);
+    DEBUG_puts(line);
 
-    free(rules);
-    rules = next;
+    if (!line[0] || line[0] == '#')
+      continue;
+
+   /*
+    * While the last character in the line is a backslash, continue on to the
+    * next line (and the next, etc.)
+    */
+
+    linelen = strlen(line);
+
+    while (line[linelen - 1] == '\\')
+    {
+      linelen --;
+
+      if (cupsFileGets(fp, line + linelen, sizeof(line) - linelen) == NULL)
+        line[linelen] = '\0';
+      else
+        linelen += strlen(line + linelen);
+    }
+
+   /*
+    * Extract the super-type and type names from the beginning of the line.
+    */
+
+    lineptr = line;
+    temp    = super;
+
+    while (*lineptr != '/' && *lineptr != '\n' && *lineptr != '\0' &&
+           (temp - super + 1) < MIME_MAX_SUPER)
+      *temp++ = tolower(*lineptr++ & 255);
+
+    *temp = '\0';
+
+    if (*lineptr != '/')
+      continue;
+
+    lineptr ++;
+    temp = type;
+
+    while (*lineptr != ' ' && *lineptr != '\t' && *lineptr != '\n' &&
+           *lineptr != '\0' && (temp - type + 1) < MIME_MAX_TYPE)
+      *temp++ = tolower(*lineptr++ & 255);
+
+    *temp = '\0';
+
+   /*
+    * Add the type and rules to the MIME database...
+    */
+
+    typeptr = mimeAddType(mime, super, type);
+    mimeAddTypeRule(typeptr, lineptr);
   }
+
+  cupsFileClose(fp);
 }
 
 
 /*
- * End of "$Id: mime.c 4970 2006-01-24 14:05:45Z mike $".
+ * End of "$Id: mime.c 5605 2006-05-30 19:38:02Z mike $".
  */