Do not use stat() and struct stat directly

This commit is contained in:
Yevgen Muntyan 2015-12-25 19:16:35 -08:00
parent 6a48dacf78
commit 577ddebb83
31 changed files with 210 additions and 189 deletions

View File

@ -253,7 +253,6 @@ SET(source_files
../moo/mooutils/mooprefspage.h
../moo/mooutils/moospawn.c
../moo/mooutils/moospawn.h
../moo/mooutils/moostat.h
../moo/mooutils/moostock.c
../moo/mooutils/moostock.h
../moo/mooutils/moo-test-macros.h

View File

@ -223,7 +223,6 @@ source_files = \
../moo/mooutils/mooprefspage.h\
../moo/mooutils/moospawn.c\
../moo/mooutils/moospawn.h\
../moo/mooutils/moostat.h\
../moo/mooutils/moostock.c\
../moo/mooutils/moostock.h\
../moo/mooutils/moo-test-macros.h\

View File

@ -20,6 +20,12 @@ moo_builddir = .
moo_sources += moo-config.h
moo_sources += \
mooglib/moo-glib.h \
mooglib/moo-stat.h \
mooglib/moo-time.h \
mooglib/moo-glib.c
EXTRA_DIST += marshals.list
built_moo_sources += marshals.h
marshals.h: marshals.list

View File

@ -34,7 +34,6 @@
#include "mooutils/moocompat.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>

View File

@ -17,7 +17,6 @@
#define MOO_FILE_PRIVATE_H
#include "moofileview/moofile.h"
#include <sys/stat.h>
#include <sys/types.h>
G_BEGIN_DECLS
@ -39,17 +38,17 @@ typedef struct MooCollationKey MooCollationKey;
struct _MooFile
{
char *name;
char *link_target;
char *display_name; /* normalized */
char *case_display_name;
MooCollationKey *collation_key;
MooFileInfo info;
MooFileFlags flags;
guint8 icon;
const char *mime_type;
int ref_count;
struct stat *statbuf;
char *name;
char *link_target;
char *display_name; /* normalized */
char *case_display_name;
MooCollationKey *collation_key;
MooFileInfo info;
MooFileFlags flags;
guint8 icon;
const char *mime_type;
int ref_count;
struct MgwStatBuf *statbuf;
};

View File

@ -28,13 +28,13 @@
#include "mooutils/mooutils-misc.h"
#include "mooutils/mootype-macros.h"
#include "mooutils/mooutils-debug.h"
#include "mooutils/moostat.h"
#include "marshals.h"
#include "mooutils/moo-mime.h"
#include <mooglib/moo-glib.h>
#include <mooglib/moo-stat.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@ -309,7 +309,7 @@ _moo_file_unref (MooFile *file)
g_free (file->case_display_name);
g_free (file->collation_key);
g_free (file->link_target);
g_slice_free (struct stat, file->statbuf);
g_slice_free (MgwStatBuf, file->statbuf);
g_slice_free (MooFile, file);
}
}
@ -334,9 +334,9 @@ _moo_file_stat (MooFile *file,
errno = 0;
if (!file->statbuf)
file->statbuf = g_slice_new (struct stat);
file->statbuf = g_slice_new (MgwStatBuf);
if (moo_lstat (fullname, file->statbuf) != 0)
if (mgw_lstat (fullname, file->statbuf) != 0)
{
if (errno == ENOENT)
{
@ -362,8 +362,7 @@ _moo_file_stat (MooFile *file,
}
else
{
#ifdef S_ISLNK
if (S_ISLNK (file->statbuf->st_mode))
if (file->statbuf->islnk)
{
static char buf[1024];
gssize len;
@ -371,7 +370,7 @@ _moo_file_stat (MooFile *file,
file->info |= MOO_FILE_INFO_IS_LINK;
errno = 0;
if (moo_stat (fullname, file->statbuf) != 0)
if (mgw_stat (fullname, file->statbuf) != 0)
{
if (errno == ENOENT)
{
@ -413,30 +412,21 @@ _moo_file_stat (MooFile *file,
file->link_target = g_strndup (buf, len);
}
}
#endif
}
if ((file->info & MOO_FILE_INFO_EXISTS) &&
!(file->info & MOO_FILE_INFO_IS_LOCKED))
{
if (S_ISDIR (file->statbuf->st_mode))
if (file->statbuf->isdir)
file->info |= MOO_FILE_INFO_IS_DIR;
#ifdef S_ISBLK
else if (S_ISBLK (file->statbuf->st_mode))
else if (file->statbuf->isblk)
file->info |= MOO_FILE_INFO_IS_BLOCK_DEV;
#endif
#ifdef S_ISCHR
else if (S_ISCHR (file->statbuf->st_mode))
else if (file->statbuf->ischr)
file->info |= MOO_FILE_INFO_IS_CHAR_DEV;
#endif
#ifdef S_ISFIFO
else if (S_ISFIFO (file->statbuf->st_mode))
else if (file->statbuf->isfifo)
file->info |= MOO_FILE_INFO_IS_FIFO;
#endif
#ifdef S_ISSOCK
else if (S_ISSOCK (file->statbuf->st_mode))
else if (file->statbuf->issock)
file->info |= MOO_FILE_INFO_IS_SOCKET;
#endif
}
if (file->info & MOO_FILE_INFO_IS_DIR)
@ -457,7 +447,7 @@ void
_moo_file_free_statbuf (MooFile *file)
{
g_return_if_fail (file != NULL);
g_slice_free (struct stat, file->statbuf);
g_slice_free (MgwStatBuf, file->statbuf);
file->statbuf = NULL;
file->flags &= ~MOO_FILE_HAS_STAT;
}

View File

@ -23,9 +23,9 @@
#include "mooutils/mooutils-misc.h"
#include "marshals.h"
#include <mooglib/moo-glib.h>
#include <mooglib/moo-stat.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@ -1036,10 +1036,10 @@ moo_file_get_type_string (MooFile *file)
/* XXX */
static char *
get_size_string (struct stat *statbuf)
get_size_string (MgwStatBuf *statbuf)
{
g_return_val_if_fail (statbuf != NULL, NULL);
return g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) statbuf->st_size);
return g_strdup_printf ("%" G_GUINT64_FORMAT, statbuf->size);
}
@ -1059,7 +1059,7 @@ moo_file_get_mtime_string (MooFile *file)
g_return_val_if_fail (file->statbuf != NULL, NULL);
if (strftime (buf, 1024, "%x %X", localtime (&file->statbuf->st_mtime)))
if (strftime (buf, 1024, "%x %X", mgw_localtime (&file->statbuf->mtime)))
return g_strdup (buf);
else
return NULL;

57
moo/mooglib/moo-glib.c Normal file
View File

@ -0,0 +1,57 @@
#define MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS
#include <mooglib/moo-glib.h>
#include <mooglib/moo-time.h>
#include <mooglib/moo-stat.h>
#include <sys/stat.h>
#include <time.h>
static mgw_time_t convert_time_t (time_t t)
{
mgw_time_t result = { t };
return result;
}
static void convert_g_stat_buf (const GStatBuf* gbuf, MgwStatBuf* mbuf)
{
mbuf->atime = convert_time_t (gbuf->st_atime);
mbuf->mtime = convert_time_t (gbuf->st_mtime);
mbuf->ctime = convert_time_t (gbuf->st_ctime);
mbuf->size = gbuf->st_size;
mbuf->isreg = S_ISREG (gbuf->st_mode);
mbuf->isdir = S_ISDIR (gbuf->st_mode);
mbuf->islnk = S_ISLNK (gbuf->st_mode);
mbuf->issock = S_ISSOCK (gbuf->st_mode);
mbuf->isfifo = S_ISFIFO (gbuf->st_mode);
mbuf->ischr = S_ISCHR (gbuf->st_mode);
mbuf->isblk = S_ISBLK (gbuf->st_mode);
}
int mgw_stat (const gchar *filename, MgwStatBuf *buf)
{
GStatBuf gbuf = { 0 };
int result = g_stat (filename, &gbuf);
convert_g_stat_buf (&gbuf, buf);
return result;
}
int mgw_lstat (const gchar *filename, MgwStatBuf *buf)
{
GStatBuf gbuf = { 0 };
int result = g_lstat (filename, &gbuf);
convert_g_stat_buf (&gbuf, buf);
return result;
}
const struct tm *mgw_localtime(const mgw_time_t *timep)
{
time_t t = timep->value;
return localtime(&t);
}

View File

@ -3,6 +3,14 @@
#include <glib.h>
#include <glib/gstdio.h>
// Fun redefining and undefining things goes on here
#ifndef MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS
#undef g_stat
#undef g_lstat
#define g_stat DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD
#define g_lstat DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD
#endif // MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS
//#include <mooglib/moo-glib-wrappers.h>

30
moo/mooglib/moo-stat.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <mooglib/moo-glib.h>
#include <mooglib/moo-time.h>
G_BEGIN_DECLS
typedef struct MgwStatBuf MgwStatBuf;
struct MgwStatBuf
{
mgw_time_t atime;
mgw_time_t mtime;
mgw_time_t ctime;
guint64 size;
guint isreg : 1, // S_ISREG
isdir : 1, // S_ISDIR
islnk : 1, // S_ISLNK
issock : 1, // S_ISSOCK
isfifo : 1, // S_ISFIFO
ischr : 1, // S_ISCHR
isblk : 1; // S_ISBLK
};
int mgw_stat (const gchar *filename, MgwStatBuf *buf);
int mgw_lstat (const gchar *filename, MgwStatBuf *buf);
G_END_DECLS

16
moo/mooglib/moo-time.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <mooglib/moo-glib.h>
G_BEGIN_DECLS
typedef struct mgw_time_t mgw_time_t;
struct mgw_time_t
{
gint64 value;
};
const struct tm *mgw_localtime(const mgw_time_t *timep);
G_END_DECLS

View File

@ -100,7 +100,6 @@ moo_sources += \
mooutils/mooprefspage.h \
mooutils/moospawn.c \
mooutils/moospawn.h \
mooutils/moostat.h \
mooutils/moostock.c \
mooutils/moostock.h \
mooutils/mootype-macros.h \

View File

@ -16,6 +16,7 @@
#include "mooutils/moo-mime.h"
#include "xdgmime/xdgmime.h"
#include "mooutils/mooutils-fs.h"
#include <mooglib/moo-stat.h>
#include <string.h>
G_LOCK_DEFINE (moo_mime);
@ -49,17 +50,18 @@ mime_type_intern (const char *mime)
}
const char *
moo_get_mime_type_for_file (const char *filename,
struct stat *statbuf)
moo_get_mime_type_for_file (const char *filename,
MgwStatBuf *statbuf)
{
const char *mime;
char *filename_utf8 = NULL;
gboolean is_regular = statbuf ? statbuf->isreg : FALSE;
if (filename)
filename_utf8 = g_filename_display_name (filename);
G_LOCK (moo_mime);
mime = mime_type_intern (xdg_mime_get_mime_type_for_file (filename_utf8, statbuf));
mime = mime_type_intern (xdg_mime_get_mime_type_for_file (filename_utf8, statbuf ? &is_regular : NULL));
G_UNLOCK (moo_mime);
g_free (filename_utf8);

View File

@ -20,16 +20,16 @@
G_BEGIN_DECLS
/* All public functions here are thread-safe */
typedef struct MgwStatBuf MgwStatBuf;
#define MOO_MIME_TYPE_UNKNOWN (moo_mime_type_unknown ())
const char *moo_mime_type_unknown (void) G_GNUC_CONST;
struct stat;
const char *moo_get_mime_type_for_file (const char *filename,
struct stat *statbuf);
MgwStatBuf *statbuf);
const char *moo_get_mime_type_for_filename (const char *filename);
gboolean moo_mime_type_is_subclass (const char *mime_type,
const char *base);

View File

@ -33,7 +33,6 @@
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <fcntl.h>

View File

@ -22,7 +22,6 @@
#include "moo-pixbufs.h"
#include <mooglib/moo-glib.h>
#include <gtk/gtk.h>
#include <sys/stat.h>
#include <string.h>
void

View File

@ -36,15 +36,12 @@
#endif
#include <mooglib/moo-glib.h>
#include <sys/stat.h>
#include <mooglib/moo-stat.h>
#include <sys/types.h>
#include <errno.h>
/* sys/stat.h macros */
#include "mooutils/mooutils-fs.h"
#include "mooutils/mooutils-misc.h"
#include "mooutils/mooutils-mem.h"
#include "mooutils/moofilewatch.h"
#include "mooutils/moostat.h"
#include "mooutils/mootype-macros.h"
#include "marshals.h"
#include "mooutils/mooutils-thread.h"
@ -73,7 +70,7 @@ typedef struct {
GDestroyNotify notify;
gpointer data;
struct stat statbuf;
MgwStatBuf statbuf;
guint isdir : 1;
guint alive : 1;
@ -505,7 +502,7 @@ watch_fam_start (MooFileWatch *watch,
FAMNoExists (&watch->fam_connection);
#endif
fam_socket = g_io_channel_unix_new (watch->fam_connection.fd);
fam_socket = moo_g_io_channel_unix_new (watch->fam_connection.fd);
watch->fam_connection_watch =
_moo_io_add_watch_full (fam_socket, MOO_FAM_SOCKET_WATCH_PRIORITY,
G_IO_IN | G_IO_PRI | G_IO_HUP,
@ -804,14 +801,14 @@ watch_stat_start_monitor (MooFileWatch *watch,
Monitor *monitor,
GError **error)
{
struct stat buf;
MgwStatBuf buf;
g_return_val_if_fail (watch != NULL, FALSE);
g_return_val_if_fail (monitor->filename != NULL, FALSE);
errno = 0;
if (moo_stat (monitor->filename, &buf) != 0)
if (mgw_stat (monitor->filename, &buf) != 0)
{
int saved_errno = errno;
g_set_error (error, MOO_FILE_WATCH_ERROR,
@ -820,7 +817,7 @@ watch_stat_start_monitor (MooFileWatch *watch,
return FALSE;
}
monitor->isdir = S_ISDIR (buf.st_mode) != 0;
monitor->isdir = buf.isdir;
#ifdef __WIN32__
if (monitor->isdir) /* it's fatal on windows */
@ -868,14 +865,14 @@ do_stat (MooFileWatch *watch)
gboolean do_emit = FALSE;
MooFileEvent event;
Monitor *monitor;
time_t old;
mgw_time_t old;
monitor = (Monitor*) g_hash_table_lookup (watch->requests, lid->data);
if (!monitor || !monitor->alive)
continue;
old = monitor->statbuf.st_mtime;
old = monitor->statbuf.mtime;
errno = 0;
@ -883,7 +880,7 @@ do_stat (MooFileWatch *watch)
event.filename = monitor->filename;
event.error = NULL;
if (moo_stat (monitor->filename, &monitor->statbuf) != 0)
if (mgw_stat (monitor->filename, &monitor->statbuf) != 0)
{
if (errno == ENOENT)
{
@ -903,7 +900,7 @@ do_stat (MooFileWatch *watch)
do_emit = TRUE;
}
else if (monitor->statbuf.st_mtime > old)
else if (monitor->statbuf.mtime.value > old.value)
{
event.code = MOO_FILE_EVENT_CHANGED;
do_emit = TRUE;
@ -1122,11 +1119,11 @@ static void
fam_thread_check_dir (FAMThread *thr,
guint idx)
{
struct stat buf;
MgwStatBuf buf;
errno = 0;
if (moo_stat (thr->watches[idx].path, &buf) != 0 &&
if (mgw_stat (thr->watches[idx].path, &buf) != 0 &&
errno == ENOENT)
{
fam_thread_event (MOO_FILE_EVENT_DELETED,
@ -1444,14 +1441,14 @@ watch_win32_start_monitor (MooFileWatch *watch,
Monitor *monitor,
GError **error)
{
struct stat buf;
MgwStatBuf buf;
g_return_val_if_fail (watch != NULL, FALSE);
g_return_val_if_fail (monitor->filename != NULL, FALSE);
errno = 0;
if (moo_stat (monitor->filename, &buf) != 0)
if (mgw_stat (monitor->filename, &buf) != 0)
{
int saved_errno = errno;
g_set_error (error, MOO_FILE_WATCH_ERROR,

View File

@ -1,52 +0,0 @@
#ifndef MOO_STAT_H
#define MOO_STAT_H
#include <mooglib/moo-glib.h>
#undef MOO_G_STAT_BROKEN
#if defined(G_OS_WIN32) && GLIB_CHECK_VERSION(2,22,0) && !GLIB_CHECK_VERSION(2,26,0)
#define MOO_G_STAT_BROKEN 1
#endif
#ifdef MOO_G_STAT_BROKEN
inline static void _moo_check_stat_struct(void)
{
struct _g_stat_struct statbuf;
// check that _g_stat_struct is the same as plain struct stat
_off_t *pofft = &statbuf.st_size;
time_t *ptimet = &statbuf.st_atime;
(void) pofft;
(void) ptimet;
}
#endif
G_BEGIN_DECLS
inline static int moo_stat (const char *filename, struct stat *buf)
{
#ifdef MOO_G_STAT_BROKEN
/* _moo_check_stat_struct above checks that struct stat is okay,
cast to void* is to avoid using glib's internal _g_stat_struct */
return g_stat (filename, (struct _g_stat_struct*) buf);
#else
return g_stat (filename, buf);
#endif
}
inline static int moo_lstat (const char *filename, struct stat *buf)
{
#ifdef MOO_G_STAT_BROKEN
/* _moo_check_stat_struct above checks that struct stat is okay,
cast to void* is to avoid using glib's internal _g_stat_struct */
return g_lstat (filename, (struct _g_stat_struct*) buf);
#else
return g_lstat (filename, buf);
#endif
}
G_END_DECLS
#endif /* MOO_STAT_H */

View File

@ -18,20 +18,18 @@
#endif
#include "mooutils/mooutils-fs.h"
#include "mooutils/moostat.h"
#include "mooutils/mooutils-debug.h"
#include "mooutils/mooutils-mem.h"
#include "mooutils/mootype-macros.h"
#include "mooutils/mooi18n.h"
#include <mooutils/mooutils-tests.h>
#include <mooglib/moo-stat.h>
#include <mooglib/moo-glib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <mooglib/moo-glib.h>
#ifdef __WIN32__
#include <windows.h>
@ -368,14 +366,14 @@ gboolean
_moo_create_dir (const char *path,
GError **error)
{
struct stat buf;
MgwStatBuf buf;
char *utf8_path;
g_return_val_if_fail (path != NULL, FALSE);
errno = 0;
if (moo_stat (path, &buf) != 0 && errno != ENOENT)
if (mgw_stat (path, &buf) != 0 && errno != ENOENT)
{
int err_code = errno;
utf8_path = g_filename_display_name (path);
@ -412,7 +410,7 @@ _moo_create_dir (const char *path,
return TRUE;
}
if (S_ISDIR (buf.st_mode))
if (buf.isdir)
return TRUE;
utf8_path = g_filename_display_name (path);

View File

@ -19,16 +19,6 @@
#include <mooglib/moo-glib.h>
#include <gtk/gtk.h>
/* MSVC */
#if defined(_WIN32) && !defined(__GNUC__)
#include <sys/stat.h>
#include <fcntl.h>
#ifndef S_ISREG
#define S_ISREG(m) (((m) & (_S_IFMT)) == (_S_IFREG))
#define S_ISDIR(m) (((m) & (_S_IFMT)) == (_S_IFDIR))
#endif
#endif
#ifndef O_BINARY
#define O_BINARY 0x0
#endif

View File

@ -35,7 +35,6 @@
#endif
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>

View File

@ -41,12 +41,13 @@
#include "mooutils/moowin32/mingw/fnmatch.h"
#include "mooutils/moowin32/mingw/sys/mman.h"
#include <mooutils/mooutils-tests.h>
#include <mooglib/moo-stat.h>
#include <gdk/gdkwin32.h>
#include <windows.h>
#include <shellapi.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <io.h>
#include <stdarg.h>
@ -507,7 +508,7 @@ _moo_win32_mmap (gpointer start,
int fd,
guint64 offset)
{
struct stat st;
MgwStatBuf st;
HANDLE mapping;
char *buffer;
@ -517,7 +518,7 @@ _moo_win32_mmap (gpointer start,
g_return_val_if_fail (offset == 0, NULL);
errno = 0;
if (fstat (fd, &st) != 0)
if (mgw_fstat (fd, &st) != 0)
return MAP_FAILED;
if ((guint64) st.st_size != length)

View File

@ -102,7 +102,6 @@ SET(mooutils_sources
mooutils/mooprefspage.h
mooutils/moospawn.c
mooutils/moospawn.h
mooutils/moostat.h
mooutils/moostock.c
mooutils/moostock.h
mooutils/mootype-macros.h

View File

@ -3,11 +3,6 @@
#include <io.h>
#ifndef S_ISREG
#define S_ISREG(m) (((m) & (_S_IFMT)) == (_S_IFREG))
#define S_ISDIR(m) (((m) & (_S_IFMT)) == (_S_IFDIR))
#endif
#define F_OK 0x0
#define R_OK 0x4
#define W_OK 0x2

View File

@ -35,20 +35,19 @@
#include "mooutils/mooi18n.h"
#include "mooutils/moo-mime.h"
#include "mooutils/moomenu.h"
#include "mooutils/moostat.h"
#include "plugins/moofileselector-gxml.h"
#include "mooutils/moohelp.h"
#include "mooutils/mooatom.h"
#include <mooglib/moo-glib.h>
#include <mooglib/moo-stat.h>
#ifdef MOO_ENABLE_HELP
#include "moo-help-sections.h"
#endif
#include <gmodule.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <mooglib/moo-glib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#define PREFS_LAST_DIR MOO_PLUGIN_PREFS_ROOT "/" MOO_FILE_SELECTOR_PLUGIN_ID "/last_dir"
#define PREFS_HIDDEN_FILES MOO_PLUGIN_PREFS_ROOT "/" MOO_FILE_SELECTOR_PLUGIN_ID "/show_hidden_files"
@ -270,7 +269,7 @@ static void
moo_file_selector_activate (MooFileView *fileview,
const char *path)
{
struct stat statbuf;
MgwStatBuf statbuf;
MooFileSelector *filesel = MOO_FILE_SELECTOR (fileview);
gboolean is_text = TRUE, is_exe = FALSE;
@ -278,7 +277,7 @@ moo_file_selector_activate (MooFileView *fileview,
errno = 0;
if (moo_stat (path, &statbuf) != 0)
if (mgw_stat (path, &statbuf) != 0)
{
int err = errno;
g_warning ("error in stat(%s): %s", path, g_strerror (err));

View File

@ -1,4 +1,4 @@
moo_sources += \
xdgmime_sources = \
xdgmime/xdgmimealias.c \
xdgmime/xdgmimealias.h \
xdgmime/xdgmime.c \
@ -14,3 +14,9 @@ moo_sources += \
xdgmime/xdgmimemagic.h \
xdgmime/xdgmimeparent.c \
xdgmime/xdgmimeparent.h
if MOO_OS_WIN32
EXTRA_DIST += $(xdgmime_sources)
else
moo_sources += $(xdgmime_sources)
endif

View File

@ -421,7 +421,7 @@ xdg_mime_get_mime_type_for_data (const void *data,
const char *
xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf)
int *is_regular)
{
const char *mime_type;
/* currently, only a few globs occur twice, and none
@ -432,7 +432,7 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
unsigned char *data;
int max_extent;
int bytes_read;
struct stat buf;
int is_regular_here;
const char *base_name;
int n;
@ -444,7 +444,7 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
xdg_mime_init ();
if (_xdg_mime_caches)
return _xdg_mime_cache_get_mime_type_for_file (file_name, statbuf);
return _xdg_mime_cache_get_mime_type_for_file (file_name, is_regular);
base_name = _xdg_get_base_name (file_name);
n = _xdg_glob_hash_lookup_file_name (global_hash, base_name, mime_types, 5);
@ -452,15 +452,18 @@ xdg_mime_get_mime_type_for_file (const char *file_name,
if (n == 1)
return mime_types[0];
if (!statbuf)
if (!is_regular)
{
if (XDG_MIME_STAT (file_name, &buf) != 0)
struct stat statbuf;
if (XDG_MIME_STAT (file_name, &statbuf) != 0)
return XDG_MIME_TYPE_UNKNOWN;
statbuf = &buf;
is_regular_here = S_ISREG (statbuf.st_mode);
is_regular = &is_regular_here;
}
if (!S_ISREG (statbuf->st_mode))
if (!*is_regular)
return XDG_MIME_TYPE_UNKNOWN;
/* FIXME: Need to make sure that max_extent isn't totally broken. This could

View File

@ -83,7 +83,7 @@ const char *xdg_mime_get_mime_type_for_data (const void *data,
size_t len,
int *result_prio);
const char *xdg_mime_get_mime_type_for_file (const char *file_name,
struct stat *statbuf);
int *is_regular);
const char *xdg_mime_get_mime_type_from_file_name (const char *file_name);
int xdg_mime_get_mime_types_from_file_name(const char *file_name,
const char *mime_types[],

View File

@ -662,7 +662,7 @@ _xdg_mime_cache_get_mime_type_for_data (const void *data,
const char *
_xdg_mime_cache_get_mime_type_for_file (const char *file_name,
struct stat *statbuf)
int *is_regular)
{
const char *mime_type;
const char *mime_types[10];
@ -670,7 +670,7 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
unsigned char *data;
int max_extent;
int bytes_read;
struct stat buf;
int is_regular_here;
const char *base_name;
int n;
@ -686,15 +686,18 @@ _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
if (n == 1)
return mime_types[0];
if (!statbuf)
if (!is_regular)
{
if (XDG_MIME_STAT (file_name, &buf) != 0)
struct stat statbuf;
if (XDG_MIME_STAT (file_name, &statbuf) != 0)
return XDG_MIME_TYPE_UNKNOWN;
statbuf = &buf;
is_regular_here = S_ISREG (statbuf.st_mode);
is_regular = &is_regular_here;
}
if (!S_ISREG (statbuf->st_mode))
if (!*is_regular)
return XDG_MIME_TYPE_UNKNOWN;
/* FIXME: Need to make sure that max_extent isn't totally broken. This could

View File

@ -60,7 +60,7 @@ const char *_xdg_mime_cache_get_mime_type_for_data (const void *data,
size_t len,
int *result_prio);
const char *_xdg_mime_cache_get_mime_type_for_file (const char *file_name,
struct stat *statbuf);
int *is_regular);
int _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
const char *mime_types[],
int n_mime_types);

View File

@ -186,26 +186,7 @@ _xdg_reverse_ucs4 (xdg_unichar_t *source, int len)
}
#ifdef __WIN32__
#include <mooutils/moostat.h>
inline static int
_xdg_mime_stat (const char *path,
struct stat *buf)
{
errno = 0;
return moo_stat (path, buf);
}
inline static int
_xdg_mime_fstat (int fd,
struct stat *buf)
{
errno = 0;
return fstat (fd, buf);
}
#define XDG_MIME_STAT _xdg_mime_stat
#define XDG_MIME_FSTAT _xdg_mime_fstat
#error "xdgmime can't be used on windows"
#else
#define XDG_MIME_STAT stat
#define XDG_MIME_FSTAT fstat