Pass statbufs down to xdg_mime_get_mime_type_for_file() where possible, to

2005-09-01  Matthias Clasen  <mclasen@redhat.com>

	* gtk/gtkfilesystemunix.c: Pass statbufs down to
	xdg_mime_get_mime_type_for_file() where possible, to avoid
	useless re-stating.
This commit is contained in:
Matthias Clasen 2005-09-01 14:42:02 +00:00 committed by Matthias Clasen
parent 9a4ec3526d
commit 6224a37708
7 changed files with 41 additions and 12 deletions

View File

@ -1,5 +1,9 @@
2005-09-01 Matthias Clasen <mclasen@redhat.com> 2005-09-01 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkfilesystemunix.c: Pass statbufs down to
xdg_mime_get_mime_type_for_file() where possible, to avoid
useless re-stating.
* gtk/gtkaction.c (gtk_action_get_accel_closure): Fix doc * gtk/gtkaction.c (gtk_action_get_accel_closure): Fix doc
typo. (#314921, Guillaume Cottenceau) typo. (#314921, Guillaume Cottenceau)

View File

@ -1,5 +1,9 @@
2005-09-01 Matthias Clasen <mclasen@redhat.com> 2005-09-01 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkfilesystemunix.c: Pass statbufs down to
xdg_mime_get_mime_type_for_file() where possible, to avoid
useless re-stating.
* gtk/gtkaction.c (gtk_action_get_accel_closure): Fix doc * gtk/gtkaction.c (gtk_action_get_accel_closure): Fix doc
typo. (#314921, Guillaume Cottenceau) typo. (#314921, Guillaume Cottenceau)

View File

@ -1224,7 +1224,7 @@ get_icon_type_from_path (GtkFileSystemUnix *system_unix,
icon_type = get_icon_type (filename, NULL); icon_type = get_icon_type (filename, NULL);
if (icon_type == ICON_REGULAR) if (icon_type == ICON_REGULAR)
*mime_type = xdg_mime_get_mime_type_for_file (filename); *mime_type = xdg_mime_get_mime_type_for_file (filename, NULL);
return icon_type; return icon_type;
} }
@ -1923,7 +1923,7 @@ create_stat_info_entry_and_emit_add (GtkFileFolderUnix *folder_unix,
entry->statbuf = *statbuf; entry->statbuf = *statbuf;
if ((folder_unix->types & GTK_FILE_INFO_MIME_TYPE) != 0) if ((folder_unix->types & GTK_FILE_INFO_MIME_TYPE) != 0)
entry->mime_type = g_strdup (xdg_mime_get_mime_type_for_file (filename)); entry->mime_type = g_strdup (xdg_mime_get_mime_type_for_file (filename, statbuf));
g_hash_table_insert (folder_unix->stat_info, g_hash_table_insert (folder_unix->stat_info,
g_strdup (basename), g_strdup (basename),
@ -2002,7 +2002,7 @@ gtk_file_folder_unix_get_info (GtkFileFolder *folder,
} }
if ((types & GTK_FILE_INFO_MIME_TYPE) != 0) if ((types & GTK_FILE_INFO_MIME_TYPE) != 0)
mime_type = xdg_mime_get_mime_type_for_file (filename); mime_type = xdg_mime_get_mime_type_for_file (filename, &statbuf);
else else
mime_type = NULL; mime_type = NULL;
@ -2150,9 +2150,12 @@ cb_fill_in_mime_type (gpointer key, gpointer value, gpointer user_data)
struct stat_info_entry *entry = value; struct stat_info_entry *entry = value;
GtkFileFolderUnix *folder_unix = user_data; GtkFileFolderUnix *folder_unix = user_data;
char *fullname = g_build_filename (folder_unix->filename, basename, NULL); char *fullname = g_build_filename (folder_unix->filename, basename, NULL);
struct stat *statbuf = NULL;
/* FIXME: Should not need to re-stat. */ if (folder_unix->have_stat)
const char *mime_type = xdg_mime_get_mime_type_for_file (fullname); statbuf = &entry->statbuf;
const char *mime_type = xdg_mime_get_mime_type_for_file (fullname, statbuf);
entry->mime_type = g_strdup (mime_type); entry->mime_type = g_strdup (mime_type);
g_free (fullname); g_free (fullname);

View File

@ -1,3 +1,11 @@
2005-09-01 Matthias Clasen <mclasen@redhat.com>
* xdgmime.h:
* xdgmime.c (xdg_mime_get_mime_type_for_file): Take
a struct statbuf * as argument.
* test-mime.c (main): Adjust.
2005-08-24 Matthias Clasen <mclasen@redhat.com> 2005-08-24 Matthias Clasen <mclasen@redhat.com>
* === Released 2.8.2 === * === Released 2.8.2 ===

View File

@ -108,7 +108,7 @@ main (int argc, char *argv[])
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
{ {
file_name = argv[i]; file_name = argv[i];
result = xdg_mime_get_mime_type_for_file (file_name); result = xdg_mime_get_mime_type_for_file (file_name, NULL);
printf ("File \"%s\" has a mime-type of %s\n", file_name, result); printf ("File \"%s\" has a mime-type of %s\n", file_name, result);
} }

View File

@ -447,14 +447,15 @@ xdg_mime_get_mime_type_for_data (const void *data,
} }
const char * const char *
xdg_mime_get_mime_type_for_file (const char *file_name) xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf)
{ {
const char *mime_type; const char *mime_type;
FILE *file; FILE *file;
unsigned char *data; unsigned char *data;
int max_extent; int max_extent;
int bytes_read; int bytes_read;
struct stat statbuf; struct stat buf;
const char *base_name; const char *base_name;
if (file_name == NULL) if (file_name == NULL)
@ -473,10 +474,17 @@ xdg_mime_get_mime_type_for_file (const char *file_name)
if (mime_type != XDG_MIME_TYPE_UNKNOWN) if (mime_type != XDG_MIME_TYPE_UNKNOWN)
return mime_type; return mime_type;
if (stat (file_name, &statbuf) != 0) if (!statbuf)
return XDG_MIME_TYPE_UNKNOWN; {
if (stat (file_name, &buf) != 0)
return XDG_MIME_TYPE_UNKNOWN;
if (!S_ISREG (statbuf.st_mode)) statbuf = &buf;
}
else
printf ("don't restat\n");
if (!S_ISREG (statbuf->st_mode))
return XDG_MIME_TYPE_UNKNOWN; return XDG_MIME_TYPE_UNKNOWN;
/* FIXME: Need to make sure that max_extent isn't totally broken. This could /* FIXME: Need to make sure that max_extent isn't totally broken. This could

View File

@ -30,6 +30,7 @@
#define __XDG_MIME_H__ #define __XDG_MIME_H__
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -69,7 +70,8 @@ extern const char *xdg_mime_type_unknown;
const char *xdg_mime_get_mime_type_for_data (const void *data, const char *xdg_mime_get_mime_type_for_data (const void *data,
size_t len); size_t len);
const char *xdg_mime_get_mime_type_for_file (const char *file_name); const char *xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf);
const char *xdg_mime_get_mime_type_from_file_name (const char *file_name); const char *xdg_mime_get_mime_type_from_file_name (const char *file_name);
int xdg_mime_is_valid_mime_type (const char *mime_type); int xdg_mime_is_valid_mime_type (const char *mime_type);
int xdg_mime_mime_type_equal (const char *mime_a, int xdg_mime_mime_type_equal (const char *mime_a,